use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method setCurrentRinger.
@TargetApi(android.os.Build.VERSION_CODES.M)
// Set the ringer mode. Returns true if mode changed.
boolean setCurrentRinger(AudioManager audio, int apiVersion, int mode, int current) {
if ((current == mode) || ((mode == PrefsManager.RINGER_MODE_NONE) && (current == PrefsManager.RINGER_MODE_NORMAL)) || ((mode == PrefsManager.RINGER_MODE_MUTED) && (current == PrefsManager.getMuteResult(this)))) {
// no change
return false;
}
PrefsManager.setLastRinger(this, mode);
if (apiVersion >= android.os.Build.VERSION_CODES.M) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
switch(mode) {
case PrefsManager.RINGER_MODE_SILENT:
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
if (nm.isNotificationPolicyAccessGranted()) {
nm.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
} else {
PermissionFail(mode);
}
break;
case PrefsManager.RINGER_MODE_ALARMS:
if (nm.isNotificationPolicyAccessGranted()) {
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
nm.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS);
break;
}
/*FALLTHRU if no permission, treat as muted */
case PrefsManager.RINGER_MODE_DO_NOT_DISTURB:
if (nm.isNotificationPolicyAccessGranted()) {
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
nm.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);
break;
}
PermissionFail(mode);
/*FALLTHRU if no permission, treat as muted */
case PrefsManager.RINGER_MODE_MUTED:
if (nm.isNotificationPolicyAccessGranted()) {
nm.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
break;
case PrefsManager.RINGER_MODE_VIBRATE:
if (nm.isNotificationPolicyAccessGranted()) {
nm.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
case PrefsManager.RINGER_MODE_NORMAL:
case PrefsManager.RINGER_MODE_NONE:
if (nm.isNotificationPolicyAccessGranted()) {
nm.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
default:
// unknown
return false;
}
} else {
switch(mode) {
case PrefsManager.RINGER_MODE_MUTED:
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
break;
case PrefsManager.RINGER_MODE_VIBRATE:
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
case PrefsManager.RINGER_MODE_NONE:
case PrefsManager.RINGER_MODE_NORMAL:
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
default:
// unknown
return false;
}
}
int gotmode = PrefsManager.getCurrentMode(this);
new MyLog(this, "Tried to set mode " + PrefsManager.getEnglishStateName(this, mode) + ", actually got " + PrefsManager.getEnglishStateName(this, gotmode));
// Some versions of Android give us a mode different from the one that
// we asked for, and some versions of Android take a while to do it.
// We use a Handler to delay getting the mode actually set.
boolean result = mHandler.sendMessageDelayed(mHandler.obtainMessage(what, this), 1000);
new MyLog(this, "mHandler.hasMessages() returns " + (mHandler.hasMessages(what) ? "true" : "false"));
lock();
return true;
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method updateState.
// Determine which event classes have become active
// and which event classes have become inactive
// and consequently what we need to do.
// Incidentally we compute the next alarm time.
@TargetApi(Build.VERSION_CODES.M)
public void updateState(Intent intent) {
// Timestamp used in all requests (so it remains consistent)
long currentTime = System.currentTimeMillis();
long nextTime = currentTime + FIVE_MINUTES;
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int wantedMode = PrefsManager.RINGER_MODE_NONE;
int user = PrefsManager.getUserRinger(this);
int last = PrefsManager.getLastRinger(this);
int current = PrefsManager.getCurrentMode(this);
new MyLog(this, "last mode is " + PrefsManager.getEnglishStateName(this, last) + ", current mode is " + PrefsManager.getEnglishStateName(this, current));
/* This will do the wrong thing if the user changes the mode during the
* one second that we are waiting for Android to set the new mode, but
* there seems to be no workaround because Android is a bit
* unpredictable in this area. Since Android can delay setting the
* mode that we asked for, or even set a different mode, but doesn't
* always do so, we can't tell if a change was done by Android or by the
* user.
*/
if ((last != current) && (last != PrefsManager.RINGER_MODE_NONE) && (!mHandler.hasMessages(what))) {
// user changed ringer mode
user = current;
PrefsManager.setUserRinger(this, user);
new MyLog(this, "Setting user ringer to " + PrefsManager.getEnglishStateName(this, user));
}
long nextAlarmTime = Long.MAX_VALUE;
nextAccelTime = Long.MAX_VALUE;
int classNum;
String startEvent = "";
String startClassName = "";
int startClassNum = 0;
String endEvent = "";
String endClassName = "";
int endClassNum = 0;
String alarmReason = "";
boolean startNotifyWanted = false;
boolean endNotifyWanted = false;
int phoneState = UpdatePhoneState(intent);
boolean anyStepCountActive = false;
boolean anyLocationActive = false;
PackageManager packageManager = getPackageManager();
final boolean haveStepCounter = currentApiVersion >= android.os.Build.VERSION_CODES.KITKAT && packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_COUNTER);
final boolean havelocation = PackageManager.PERMISSION_GRANTED == PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (PrefsManager.getStepCount(this) >= 0) {
new MyLog(this, "Step counter running");
}
int n = PrefsManager.getNumClasses(this);
CalendarProvider provider = new CalendarProvider(this);
for (classNum = 0; classNum < n; ++classNum) {
if (PrefsManager.isClassUsed(this, classNum)) {
String className = PrefsManager.getClassName(this, classNum);
int ringerAction = PrefsManager.getRingerAction(this, classNum);
CalendarProvider.startAndEnd result = provider.nextActionTimes(this, currentTime, classNum);
// true if user requested immediate event of this class
boolean triggered = PrefsManager.isClassTriggered(this, classNum);
// true if an event of this class should be currently active
boolean active = (result.startTime <= currentTime) && (result.endTime > currentTime);
if (triggered) {
long after = PrefsManager.getAfterMinutes(this, classNum) * 60000;
if (after > 0) {
long triggerEnd = currentTime + after;
if ((active && (triggerEnd > result.endTime)) || ((!active) && (triggerEnd < result.startTime))) {
result.endEventName = "<immediate>";
result.endTime = triggerEnd;
}
PrefsManager.setLastTriggerEnd(this, classNum, result.endTime);
} else if (!active) {
// We will start the event (possibly after a state
// delay) and end it immediately: this is probably not
// a useful case, but we have to handle it correctly.
result.endEventName = "<immediate>";
// We do need this because we may have found an event
// which starts and ends in the future.
result.endTime = currentTime;
}
if (!active) {
result.startEventName = "<immediate>";
result.startTime = currentTime;
}
if (after > 0) {
// need to do this after above test
active = true;
}
}
if (triggered || active) {
if (!PrefsManager.isClassActive(this, classNum)) {
// need to start this class
// True if not ready to start it yet.
boolean notNow = phoneState == PrefsManager.PHONE_CALL_ACTIVE;
if (!notNow) {
notNow = checkOrientationWait(classNum, true);
new MyLog(this, "checkOrientationWait(" + className + ", true) returns " + (notNow ? "true" : "false"));
if (notNow) {
if ((nextAccelTime < nextAlarmTime) && (nextAccelTime > currentTime)) {
nextAlarmTime = nextAccelTime;
alarmReason = " for next orientation check for event ".concat(result.startEventName).concat(" of class ").concat(className);
}
} else {
notNow = checkConnectionWait(classNum, true);
if (notNow) {
if (nextTime < nextAlarmTime) {
nextAlarmTime = nextTime;
alarmReason = (" for next connection check for " + "event ").concat(result.startEventName).concat(" of class ").concat(className);
}
}
}
}
if (notNow) {
// we can't start it yet
triggered = false;
active = false;
result.endTime = Long.MAX_VALUE;
} else {
if (ringerAction > wantedMode) {
if (PrefsManager.getNotifyStart(this, classNum)) {
startNotifyWanted = true;
}
startEvent = result.startEventName;
startClassName = className;
startClassNum = classNum;
PrefsManager.setTargetSteps(this, classNum, 0);
PrefsManager.setLatitude(this, classNum, 360.0);
PrefsManager.setClassActive(this, classNum, true);
}
}
}
}
if (// may have changed
active) {
// class should be currently active
if (ringerAction > wantedMode) {
wantedMode = ringerAction;
}
PrefsManager.setLastActive(this, classNum, result.endEventName);
}
if (!active) {
// class should not be currently active
// check if we're waiting for movement
boolean done = false;
boolean waiting = false;
int lastCounterSteps = PrefsManager.getStepCount(this);
int aftersteps = PrefsManager.getAfterSteps(this, classNum);
if (PrefsManager.isClassActive(this, classNum)) {
done = true;
if (haveStepCounter && (aftersteps > 0)) {
if (lastCounterSteps < 0) {
// need to start up the sensor
if (StartStepCounter(classNum)) {
PrefsManager.setTargetSteps(this, classNum, 0);
anyStepCountActive = true;
waiting = true;
done = false;
}
} else // sensor already running
{
PrefsManager.setTargetSteps(this, classNum, lastCounterSteps + aftersteps);
anyStepCountActive = true;
new MyLog(this, "Setting target steps for class " + className + " to " + String.valueOf(lastCounterSteps) + " + " + String.valueOf(aftersteps));
waiting = true;
done = false;
}
}
if (havelocation && (PrefsManager.getAfterMetres(this, classNum) > 0)) {
// keep it active while waiting for location
startLocationWait(classNum, intent);
anyLocationActive = true;
waiting = true;
done = false;
}
if (checkOrientationWait(classNum, false)) {
if ((nextAccelTime < nextAlarmTime) && (nextAccelTime > currentTime)) {
nextAlarmTime = nextAccelTime;
alarmReason = " for next orientation check for event ".concat(result.endEventName).concat(" of class ").concat(className);
}
waiting = true;
done = false;
} else if (checkConnectionWait(classNum, false)) {
if (nextTime < nextAlarmTime) {
nextAlarmTime = nextTime;
alarmReason = (" for next connection check for " + "event ").concat(result.endEventName).concat(" of class ").concat(className);
}
waiting = true;
done = false;
}
PrefsManager.setClassActive(this, classNum, false);
}
if (PrefsManager.isClassWaiting(this, classNum)) {
done = true;
if ((lastCounterSteps != PrefsManager.STEP_COUNTER_IDLE) && (aftersteps > 0)) {
int steps = PrefsManager.getTargetSteps(this, classNum);
if (steps == 0) {
if (lastCounterSteps >= 0) {
PrefsManager.setTargetSteps(this, classNum, lastCounterSteps + aftersteps);
}
anyStepCountActive = true;
new MyLog(this, "Setting target steps for class " + className + " to " + String.valueOf(lastCounterSteps) + " + " + String.valueOf(aftersteps));
waiting = true;
done = false;
} else if (lastCounterSteps < steps) {
anyStepCountActive = true;
waiting = true;
done = false;
}
}
double latitude = PrefsManager.getLatitude(this, classNum);
if ((latitude != 360.0) && checkLocationWait(classNum, latitude, intent)) {
anyLocationActive = true;
waiting = true;
done = false;
}
if (checkOrientationWait(classNum, false)) {
if ((nextAccelTime < nextAlarmTime) && (nextAccelTime > currentTime)) {
nextAlarmTime = nextAccelTime;
alarmReason = " for next orientation check for event ".concat(result.endEventName).concat(" of class ").concat(className);
}
waiting = true;
done = false;
} else if (checkConnectionWait(classNum, false)) {
if (nextTime < nextAlarmTime) {
nextAlarmTime = nextTime;
alarmReason = (" for next connection check for " + "event ").concat(result.endEventName).concat(" of class ").concat(className);
}
waiting = true;
done = false;
}
} else if (waiting) {
PrefsManager.setClassWaiting(this, classNum, true);
}
if (done) {
if ((PrefsManager.getRestoreRinger(this, classNum)) || (endEvent.isEmpty())) {
if (PrefsManager.getNotifyEnd(this, classNum)) {
endNotifyWanted = true;
endEvent = PrefsManager.getLastActive(this, classNum);
endClassName = className;
endClassNum = classNum;
}
}
PrefsManager.setClassActive(this, classNum, false);
PrefsManager.setClassWaiting(this, classNum, false);
} else if (waiting && (wantedMode < ringerAction)) {
wantedMode = ringerAction;
}
}
if ((result.endTime < nextAlarmTime) && (result.endTime > currentTime)) {
nextAlarmTime = result.endTime;
alarmReason = " for end of event ".concat(result.endEventName).concat(" of class ").concat(className);
}
if ((result.startTime < nextAlarmTime) && (result.startTime > currentTime)) {
nextAlarmTime = result.startTime;
alarmReason = " for start of event ".concat(result.startEventName).concat(" of class ").concat(className);
}
if (triggered) {
PrefsManager.setClassTriggered(this, classNum, false);
}
}
}
if ((PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)) && (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)) && (PrefsManager.getNextLocationMode(this))) {
CalendarProvider.StartAndLocation sl = provider.nextLocation(this, currentTime);
if (sl != null) {
ContactCreator cc = new ContactCreator(this);
cc.makeContact("!NextEventLocation", "", sl.location);
long slst = sl.startTime + 60000;
if ((slst < nextAlarmTime) && (slst > currentTime)) {
nextAlarmTime = slst;
alarmReason = " after start of event ".concat(sl.eventName);
}
}
}
String shortText;
String longText;
if (wantedMode < user) {
wantedMode = user;
} else if (wantedMode == PrefsManager.RINGER_MODE_NONE) {
wantedMode = PrefsManager.RINGER_MODE_NORMAL;
}
boolean changed = setCurrentRinger(audio, currentApiVersion, wantedMode, current);
if (changed) {
shortText = PrefsManager.getRingerStateName(this, wantedMode);
} else {
shortText = getString(R.string.ringerModeNone);
}
if (endNotifyWanted) {
longText = shortText + " " + getString(R.string.eventend) + " " + endEvent + " " + getString(R.string.ofclass) + " " + endClassName;
if (PrefsManager.getPlaysoundEnd(this, endClassNum)) {
String path = PrefsManager.getSoundFileEnd(this, endClassNum);
emitNotification(longText, path);
} else if (changed && (wantedMode < current)) {
emitNotification(longText, "");
}
}
if (startNotifyWanted) {
longText = shortText + " " + getString(R.string.eventstart) + " " + startEvent + " " + getString(R.string.ofclass) + " " + startClassName;
if (PrefsManager.getPlaysoundStart(this, startClassNum)) {
String path = PrefsManager.getSoundFileStart(this, startClassNum);
emitNotification(longText, path);
} else if (changed) {
emitNotification(longText, "");
}
}
if (startEvent != "") {
new MyLog(this, "Setting audio mode to " + PrefsManager.getEnglishStateName(this, wantedMode) + " for start of event " + startEvent + " of class " + startClassName);
} else if (endEvent != "") {
new MyLog(this, "Setting audio mode to " + PrefsManager.getEnglishStateName(this, wantedMode) + " for end of event " + endEvent + " of class " + endClassName);
} else if (resetting) {
new MyLog(this, "Setting audio mode to " + PrefsManager.getEnglishStateName(this, wantedMode) + " after reset");
}
resetting = false;
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, /*requestCode*/
new Intent("CalendarTrigger.Alarm", Uri.EMPTY, this, StartServiceReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// silently fail to do so if the alarm time is too soon
if (PrefsManager.getLastAlarmTime(this) != nextAlarmTime) {
if (nextAlarmTime == Long.MAX_VALUE) {
alarmManager.cancel(pIntent);
new MyLog(this, "Alarm cancelled");
} else {
if (currentApiVersion >= android.os.Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextAlarmTime, pIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextAlarmTime, pIntent);
}
DateFormat df = DateFormat.getDateTimeInstance();
new MyLog(this, "Alarm time set to ".concat(df.format(nextAlarmTime)).concat(alarmReason));
PrefsManager.setLastAlarmTime(this, nextAlarmTime);
}
}
PrefsManager.setLastInvocationTime(this, currentTime);
if (!anyStepCountActive) {
if (PrefsManager.getStepCount(this) != PrefsManager.STEP_COUNTER_IDLE) {
PrefsManager.setStepCount(this, PrefsManager.STEP_COUNTER_IDLE);
SensorManager sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
new MyLog(this, "Step counter deactivated");
}
}
if (!anyLocationActive) {
if (PrefsManager.getLocationState(this)) {
LocationUpdates(0, PrefsManager.LATITUDE_IDLE);
}
}
unlock();
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method UpdatePhoneState.
// Check if there is a current call (not a ringing call).
// If so we don't want to mute even if an event starts.
int UpdatePhoneState(Intent intent) {
// 0 idle
// 1 incoming call ringing (but no active call)
// 2 call active
int phoneState = PrefsManager.getPhoneState(this);
if (intent.getAction() == TelephonyManager.ACTION_PHONE_STATE_CHANGED) {
String event = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
switch(event) {
case "IDLE":
phoneState = PrefsManager.PHONE_IDLE;
PrefsManager.setPhoneState(this, phoneState);
break;
case "OFFHOOK":
phoneState = PrefsManager.PHONE_CALL_ACTIVE;
PrefsManager.setPhoneState(this, phoneState);
break;
case "RINGING":
// Tricky case - can be ringing when already in a call
if (phoneState == PrefsManager.PHONE_IDLE) {
phoneState = PrefsManager.PHONE_RINGING;
PrefsManager.setPhoneState(this, phoneState);
}
break;
}
PrefsManager.setNotifiedCannotReadPhoneState(this, false);
} else {
boolean canCheckCallState = PackageManager.PERMISSION_GRANTED == PermissionChecker.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (!canCheckCallState) {
if (!PrefsManager.getNotifiedCannotReadPhoneState(this)) {
Notification.Builder builder = new Notification.Builder(this).setSmallIcon(R.drawable.notif_icon).setContentText(getString(R.string.readphonefail)).setStyle(new Notification.BigTextStyle().bigText(getString(R.string.readphonefailbig)));
// Show notification
NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(notifyId++, builder.build());
new MyLog(this, "CalendarTrigger no longer has permission " + "READ_PHONE_STATE.");
PrefsManager.setNotifiedCannotReadPhoneState(this, true);
}
}
}
return phoneState;
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method checkLocationWait.
// return true if not left geofence yet
private boolean checkLocationWait(int classNum, double latitude, Intent intent) {
if (!PrefsManager.getLocationState(this)) {
// we got reset, pretend we left the geofence
PrefsManager.setLatitude(this, classNum, PrefsManager.LATITUDE_IDLE);
return false;
}
Location here = intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
if (here != null) {
if (latitude == PrefsManager.LATITUDE_FIRST) {
// waiting for current location, and got it
LocationUpdates(classNum, 0.0);
PrefsManager.setLatitude(this, classNum, here.getLatitude());
PrefsManager.setLongitude(this, classNum, here.getLongitude());
new MyLog(this, "Set up geofence for class ".concat(PrefsManager.getClassName(this, classNum)).concat(" at location ").concat(((Double) here.getLatitude()).toString()).concat(", ").concat(((Double) here.getLongitude()).toString()));
return true;
}
// waiting for geofence exit
{
float meters = (float) PrefsManager.getAfterMetres(this, classNum);
float[] results = new float[1];
double longitude = PrefsManager.getLongitude(this, classNum);
Location.distanceBetween(latitude, longitude, here.getLatitude(), here.getLongitude(), results);
if (results[0] < meters * 0.9) {
new MyLog(this, "Still within geofence for class ".concat(PrefsManager.getClassName(this, classNum)).concat(" at location ").concat(((Double) here.getLatitude()).toString()).concat(", ").concat(((Double) here.getLongitude()).toString()));
return true;
}
// else we've exited the geofence
PrefsManager.setLatitude(this, classNum, PrefsManager.LATITUDE_IDLE);
new MyLog(this, "Exited geofence for class ".concat(PrefsManager.getClassName(this, classNum)).concat(" at location ").concat(((Double) here.getLatitude()).toString()).concat(", ").concat(((Double) here.getLongitude()).toString()));
return false;
}
}
// location wait active, but no new location
return true;
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method lock.
private void lock() {
// get the wake lock if we don't already have it
if (wakelock == null) {
new MyLog(this, "Getting lock");
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakelock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CalendarTrigger");
wakelock.acquire();
}
}
Aggregations