Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created October 30, 2015 08:48
Show Gist options
  • Save arturaz/c4e5204b47170989520b to your computer and use it in GitHub Desktop.
Save arturaz/c4e5204b47170989520b to your computer and use it in GitHub Desktop.
package com.tinylabproductions.tbsgame.notifications;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.Toast;
import com.tinylabproductions.tbsgame.Prefs;
import com.tinylabproductions.tbsgame.data.IntDate;
import com.tinylabproductions.tbsgame.data.IntHHMM;
import java.util.Calendar;
public class LaunchMuteForDialogActivity extends FragmentActivity {
// Max 23 chars.
final static String TAG = "TBSGameLaunchMuteDialog";
public final static String ACTION_UNMUTE =
"com.tinylabproductions.tbsgame.notifications.LaunchMuteForDialogActivity.UNMUTE";
@Override
protected void onStart() {
super.onStart();
String action = getIntent().getAction();
if (Log.isLoggable(TAG, Log.DEBUG))
Log.d(TAG, "onCreate, action=" + action);
if (ACTION_UNMUTE.equals(action)) {
Prefs.clearNotificationMutedUntil(this);
Toast.makeText(this, "Notification sounds unmuted.", Toast.LENGTH_SHORT).show();
finish();
Notifications.notInQueueShowOtherIsSearching(this);
}
else {
new MuteForDialog().show(getSupportFragmentManager(), "mute-for-dialog");
}
}
public void muteUntil(long timestamp) {
Prefs.setNotificationMutedUntil(this, timestamp);
Toast.makeText(this, "Notification sounds muted.", Toast.LENGTH_SHORT).show();
finish();
Notifications.notInQueueHideOtherIsSearching(this);
}
IntDate setDate;
public void onDateSet(IntDate date) {
this.setDate = date;
new TimePickerFragment().show(getSupportFragmentManager(), "mute-for-dialog-time-picker");
}
public void onTimeSet(IntHHMM intHHMM) {
final Calendar c = Calendar.getInstance();
setDate.set(c);
intHHMM.set(c);
muteUntil(c.getTimeInMillis());
}
}
package com.tinylabproductions.tbsgame.notifications;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class LaunchMuteForDialogReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startActivity(
new Intent(context, LaunchMuteForDialogActivity.class)
.setAction(intent.getAction())
.setFlags(
Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |
Intent.FLAG_ACTIVITY_SINGLE_TOP
)
);
}
}
/* Called when your game is neither in foreground or background and someone else is
* searching for a match. */
public static void notInQueueShowOtherIsSearching(Context context) {
Log.d(TAG, "notInQueueShowOtherIsSearching");
NotificationCompat.Builder builder =
builder(context)
.setContentText("A challenger has appeared!")
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setLights(Color.RED, 2000, 2000);
boolean notificationMuted =
Prefs.getNotificationMutedUntil(context) >= System.currentTimeMillis();
if (Log.isLoggable(TAG, Log.DEBUG))
Log.d(TAG, "notificationMuted="+notificationMuted);
if (notificationMuted) {
builder.addAction(
android.R.drawable.ic_lock_silent_mode_off,
"Unmute sounds",
unmuteForDialogIntent(context)
);
}
else {
builder
.addAction(
android.R.drawable.ic_lock_silent_mode,
"Mute sounds",
muteForDialogIntent(context)
)
.setSound(audioUri(context, "opponent_found"));
}
manager(context).notify(ID_OTHER_SEARCHING_FOR_OPPONENT, builder.build());
}
static PendingIntent muteForDialogIntent(Context context) {
Intent intent = new Intent(context, LaunchMuteForDialogReceiver.class);
return PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
);
}
static PendingIntent unmuteForDialogIntent(Context context) {
Intent intent = new Intent(context, LaunchMuteForDialogReceiver.class)
.setAction(LaunchMuteForDialogActivity.ACTION_UNMUTE);
return PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment