Add some SDK checks.

This commit is contained in:
Timothy Allen 2014-03-26 16:43:24 +02:00
parent c32aef7c86
commit c2806f2231
4 changed files with 91 additions and 72 deletions

View File

@ -5,6 +5,7 @@ import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
@ -35,8 +36,9 @@ public class AlarmAlertActivity extends Activity {
Window window = getWindow();
// Set to use the full screen
int fullScreen = WindowManager.LayoutParams.FLAG_FULLSCREEN;
// TODO if KitKat, mimic the main alarm
if (Build.VERSION.SDK_INT >= 19) {
fullScreen = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
}
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON

View File

@ -9,6 +9,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
@ -44,6 +45,7 @@ public class AlarmNotify extends Service {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_grey);
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 11) {
final Notification.Builder notification = new Notification.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(String.format(getString(R.string.notificationText), MainActivity.MinutesToGracePeriodStr(gracePeriod)))
@ -90,20 +92,22 @@ public class AlarmNotify extends Service {
while (progress < max) {
// Stop the thread if cancelled elsewhere
Log.d("AlarmNotify", "notificationRunning is "+notificationRunning);
Log.d("AlarmNotify", "notificationRunning is " + notificationRunning);
if (!notificationRunning) {
return;
}
int minutesLeft = secondsLeft / 60;
if (Build.VERSION.SDK_INT >= 11) {
notification.setContentText(String.format(getString(R.string.notificationText), MainActivity.MinutesToGracePeriodStr(minutesLeft)));
notification.setProgress(max, progress, false);
// Update the notification
nm.notify(notifyID, notification.build());
}
// Prepare secondsLeft and progress for the next loop
secondsLeft = secondsLeft - (UPDATE_INTERVAL / 1000);
// Multiply each int by 1000 for greater progress resolution
progress = (((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000);
Log.d("AlarmNotify", "secondsLeft is "+secondsLeft+" and progress is "+progress+" (gracePeriodSeconds is "+gracePeriodSeconds+")");
Log.d("AlarmNotify", "secondsLeft is " + secondsLeft + " and progress is " + progress + " (gracePeriodSeconds is " + gracePeriodSeconds + ")");
// Sleeps the thread, simulating an operation
// that takes time
try {
@ -115,7 +119,7 @@ public class AlarmNotify extends Service {
stopSelf(); // stop notification service
}
}).start();
}
return super.onStartCommand(intent, flags, startId);
}
}

View File

@ -7,8 +7,10 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.WindowManager;
import java.util.Calendar;
@ -34,7 +36,11 @@ public class AlarmReceiver extends BroadcastReceiver {
Intent graceIntent = new Intent(context, GraceReceiver.class);
gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0);
graceManager.cancel(gracePendingIntent);
if (Build.VERSION.SDK_INT >= 19) {
graceManager.setExact(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent);
} else {
graceManager.set(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent);
}
Log.d("AlarmReceiver", "Setting grace alarm for " + MainActivity.debugDate(graceCal));
// Allow user to acknowledge alarm and cancel grace alarm
@ -51,8 +57,11 @@ public class AlarmReceiver extends BroadcastReceiver {
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmPendingIntent = PendingIntent.getBroadcast(context, MainActivity.ALARM_REQUEST, intent, 0);
alarmManager.cancel(alarmPendingIntent);
// TODO use set() for older APIs
if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent);
}
Log.d("AlarmReceiver", "Resetting alarm for " + MainActivity.debugDate(cal));
}
}

View File

@ -13,6 +13,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
@ -308,8 +309,11 @@ public class MainActivity extends ActionBarActivity {
// Cancel any existing alarms
alarmManager.cancel(alarmPendingIntent);
// Set or reset alarm
// TODO use set() for older APIs
if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent);
}
Log.d("MainActivity", "Setting alarm for " + MainActivity.debugDate(cal));
// Set boot receiver, so alarm restarts on boot