From c2806f22313308474d495517e682d2a8883f1e30 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 26 Mar 2014 16:43:24 +0200 Subject: [PATCH] Add some SDK checks. --- .../hypoalarm/AlarmAlertActivity.java | 6 +- .../org/treehouse/hypoalarm/AlarmNotify.java | 134 +++++++++--------- .../treehouse/hypoalarm/AlarmReceiver.java | 15 +- .../org/treehouse/hypoalarm/MainActivity.java | 8 +- 4 files changed, 91 insertions(+), 72 deletions(-) diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java index 30ac659..d9f1390 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java @@ -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 - fullScreen = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; + 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 diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java index 5f5c976..3460bf9 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java @@ -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,78 +45,81 @@ public class AlarmNotify extends Service { Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_grey); final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - final Notification.Builder notification = new Notification.Builder(this) - .setContentTitle(getString(R.string.app_name)) - .setContentText(String.format(getString(R.string.notificationText), MainActivity.MinutesToGracePeriodStr(gracePeriod))) - .setSmallIcon(R.drawable.alarm_notification) - .setLargeIcon(bm) - .setOnlyAlertOnce(true) - .setAutoCancel(false) - .setPriority(Notification.PRIORITY_HIGH); + 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))) + .setSmallIcon(R.drawable.alarm_notification) + .setLargeIcon(bm) + .setOnlyAlertOnce(true) + .setAutoCancel(false) + .setPriority(Notification.PRIORITY_HIGH); - // Set up dismiss action - Intent cancellerIntent = new Intent(getBaseContext(), CancelGraceReceiver.class); - PendingIntent cancellerPendingIntent = PendingIntent.getBroadcast(getBaseContext(), MainActivity.CANCEL_GRACE_REQUEST, cancellerIntent, PendingIntent.FLAG_CANCEL_CURRENT); + // Set up dismiss action + Intent cancellerIntent = new Intent(getBaseContext(), CancelGraceReceiver.class); + PendingIntent cancellerPendingIntent = PendingIntent.getBroadcast(getBaseContext(), MainActivity.CANCEL_GRACE_REQUEST, cancellerIntent, PendingIntent.FLAG_CANCEL_CURRENT); - // Cancel the grace period if the user clears the notification - notification.setDeleteIntent(cancellerPendingIntent); - // Allow the user to cancel by clicking a "Cancel" button - notification.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.notificationCancellation), cancellerPendingIntent); - // Allow the user to cancel by selecting the ContentText or ContentTitle - notification.setContentIntent(cancellerPendingIntent); + // Cancel the grace period if the user clears the notification + notification.setDeleteIntent(cancellerPendingIntent); + // Allow the user to cancel by clicking a "Cancel" button + notification.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.notificationCancellation), cancellerPendingIntent); + // Allow the user to cancel by selecting the ContentText or ContentTitle + notification.setContentIntent(cancellerPendingIntent); - /** - * TODO load alert activity (without sound or vibration) on select? - * TODO This would allow the user to test competence - Intent alertActivityIntent = new Intent(this, AlarmAlertActivity.class); - alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | - Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); - notification.setContentIntent(alertActivityIntent); - */ + /** + * TODO load alert activity (without sound or vibration) on select? + * TODO This would allow the user to test competence + Intent alertActivityIntent = new Intent(this, AlarmAlertActivity.class); + alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | + Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + notification.setContentIntent(alertActivityIntent); + */ - nm.cancel(notifyID); - nm.notify(notifyID, notification.build()); + nm.cancel(notifyID); + nm.notify(notifyID, notification.build()); - new Thread(new Runnable() { - @Override - public void run() { - notificationRunning = true; - Log.d("AlarmNotify", "2: Setting notificationRunning to true"); - int max = 1000; - // Convert endTime from milliseconds to seconds, and translate to time remaining - int secondsLeft = (int) ((endTime - System.currentTimeMillis())) / (1000); - int gracePeriodSeconds = gracePeriod * 60; - // Multiply each int by 1000 for greater progress resolution - int progress = (((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000); - - while (progress < max) { - // Stop the thread if cancelled elsewhere - Log.d("AlarmNotify", "notificationRunning is "+notificationRunning); - if (!notificationRunning) { - return; - } - int minutesLeft = secondsLeft / 60; - 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); + new Thread(new Runnable() { + @Override + public void run() { + notificationRunning = true; + Log.d("AlarmNotify", "2: Setting notificationRunning to true"); + int max = 1000; + // Convert endTime from milliseconds to seconds, and translate to time remaining + int secondsLeft = (int) ((endTime - System.currentTimeMillis())) / (1000); + int gracePeriodSeconds = gracePeriod * 60; // 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+")"); - // Sleeps the thread, simulating an operation - // that takes time - try { - Thread.sleep(UPDATE_INTERVAL); - } catch (InterruptedException e) { - Log.d("AlarmNotify", "sleep failure"); - } - } - stopSelf(); // stop notification service - } - }).start(); + int progress = (((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000); + while (progress < max) { + // Stop the thread if cancelled elsewhere + 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 + ")"); + // Sleeps the thread, simulating an operation + // that takes time + try { + Thread.sleep(UPDATE_INTERVAL); + } catch (InterruptedException e) { + Log.d("AlarmNotify", "sleep failure"); + } + } + stopSelf(); // stop notification service + } + }).start(); + } return super.onStartCommand(intent, flags, startId); } } diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java index 9718223..50af2eb 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java @@ -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); - graceManager.setExact(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), 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 - alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent); + 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)); } } diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java index cefa5b1..3bdeb58 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java @@ -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 - alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent); + 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