diff --git a/res/values/strings.xml b/res/values/strings.xml index 977e3c24..cf6a56e9 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -36,7 +36,7 @@ Synchronizing changes Error with server certificate: Synchronizing, please wait… - Synchronization failed, try again? + Synchronization failed MobileOrg Synchronization This plugin allows you to trigger a MobileOrg Sync from Locale or Tasker. If you are okay with this click \'Accept\' otherwise click \'Deny\' Synchronize diff --git a/src/com/matburt/mobileorg/Gui/SynchronizerNotification.java b/src/com/matburt/mobileorg/Gui/SynchronizerNotification.java index b8317ec6..5f837fca 100644 --- a/src/com/matburt/mobileorg/Gui/SynchronizerNotification.java +++ b/src/com/matburt/mobileorg/Gui/SynchronizerNotification.java @@ -1,27 +1,29 @@ package com.matburt.mobileorg.Gui; +import android.annotation.TargetApi; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; -import android.support.v4.app.NotificationCompat; -import android.support.v4.app.NotificationCompat.Builder; -import android.widget.RemoteViews; +import android.os.Build; import com.matburt.mobileorg.R; import com.matburt.mobileorg.Gui.Outline.OutlineActivity; -public class SynchronizerNotification { +@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) +public class SynchronizerNotification extends SynchronizerNotificationCompat { private NotificationManager notificationManager; private Notification notification; private int notifyRef = 1; private Context context; public SynchronizerNotification(Context context) { + super(context); this.context = context; } + @Override public void errorNotification(String errorMsg) { this.notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); @@ -31,24 +33,20 @@ public void errorNotification(String errorMsg) { PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notifyIntent, 0); - - Builder builder = new NotificationCompat.Builder(context); + + Notification.Builder builder = new Notification.Builder(context); builder.setContentIntent(contentIntent); builder.setSmallIcon(R.drawable.icon); - builder.setContentTitle("Synchronization failed"); - + builder.setContentTitle(context.getString(R.string.sync_failed)); + builder.setContentText(errorMsg); notification = builder.getNotification(); - notification.contentView = notification.contentView = new RemoteViews( - context.getPackageName(), R.layout.sync_notification); - notification.contentView.setImageViewResource(R.id.status_icon, - R.drawable.icon); - notification.contentView.setTextViewText(R.id.status_text, errorMsg); - notification.contentView.setProgressBar(R.id.status_progress, 100, 100, - false); notificationManager.notify(notifyRef, notification); } + + @Override + @SuppressWarnings("deprecation") public void setupNotification() { this.notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); @@ -59,53 +57,44 @@ public void setupNotification() { PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notifyIntent, 0); - Builder builder = new NotificationCompat.Builder(context); + Notification.Builder builder = new Notification.Builder(context); builder.setContentIntent(contentIntent); builder.setSmallIcon(R.drawable.icon); builder.setOngoing(true); - builder.setContentTitle("Started synchronization"); - builder.setContentText("Started synchronization"); + builder.setContentTitle(context.getString(R.string.sync_synchronizing_changes)); + builder.setProgress(100, 0, true); notification = builder.getNotification(); - notification.contentView = new RemoteViews(context.getPackageName(), - R.layout.sync_notification); - - notification.contentView.setImageViewResource(R.id.status_icon, - R.drawable.icon); - notification.contentView.setTextViewText(R.id.status_text, - context.getString(R.string.sync_synchronizing_changes)); - notification.contentView.setProgressBar(R.id.status_progress, 100, 0, - true); - notificationManager.notify(notifyRef, notification); } + @Override public void updateNotification(String message) { if(notification == null) return; if(message != null) { - notification.contentView.setTextViewText(R.id.status_text, message); notificationManager.notify(notifyRef, notification); } } + @Override public void updateNotification(int progress) { updateNotification(progress, null); } + @Override public void updateNotification(int progress, String message) { if(notification == null) return; - - if(message != null) - notification.contentView.setTextViewText(R.id.status_text, message); - notification.contentView.setProgressBar(R.id.status_progress, 100, + notification.contentView.setProgressBar(android.R.id.progress, 100, progress, false); + notificationManager.notify(notifyRef, notification); } + @Override public void finalizeNotification() { notificationManager.cancel(notifyRef); } diff --git a/src/com/matburt/mobileorg/Gui/SynchronizerNotificationCompat.java b/src/com/matburt/mobileorg/Gui/SynchronizerNotificationCompat.java new file mode 100644 index 00000000..b02f053f --- /dev/null +++ b/src/com/matburt/mobileorg/Gui/SynchronizerNotificationCompat.java @@ -0,0 +1,113 @@ +package com.matburt.mobileorg.Gui; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationCompat.Builder; +import android.widget.RemoteViews; + +import com.matburt.mobileorg.R; +import com.matburt.mobileorg.Gui.Outline.OutlineActivity; + +public class SynchronizerNotificationCompat { + private NotificationManager notificationManager; + private Notification notification; + private int notifyRef = 1; + private Context context; + + public SynchronizerNotificationCompat(Context context) { + this.context = context; + } + + public void errorNotification(String errorMsg) { + this.notificationManager = (NotificationManager) context + .getSystemService(Context.NOTIFICATION_SERVICE); + Intent notifyIntent = new Intent(context, OutlineActivity.class); + notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP + | Intent.FLAG_ACTIVITY_SINGLE_TOP); + + PendingIntent contentIntent = PendingIntent.getActivity(context, 0, + notifyIntent, 0); + + Builder builder = new NotificationCompat.Builder(context); + builder.setContentIntent(contentIntent); + builder.setSmallIcon(R.drawable.icon); + builder.setContentTitle("Synchronization failed"); + + notification = builder.getNotification(); + notification.contentView = notification.contentView = new RemoteViews( + context.getPackageName(), R.layout.sync_notification); + + notification.contentView.setImageViewResource(R.id.status_icon, + R.drawable.icon); + notification.contentView.setTextViewText(R.id.status_text, errorMsg); + notification.contentView.setProgressBar(R.id.status_progress, 100, 100, + false); + notificationManager.notify(notifyRef, notification); + } + + public void setupNotification() { + this.notificationManager = (NotificationManager) context + .getSystemService(Context.NOTIFICATION_SERVICE); + Intent notifyIntent = new Intent(context, OutlineActivity.class); + notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP + | Intent.FLAG_ACTIVITY_SINGLE_TOP); + + PendingIntent contentIntent = PendingIntent.getActivity(context, 0, + notifyIntent, 0); + + Builder builder = new NotificationCompat.Builder(context); + builder.setContentIntent(contentIntent); + builder.setSmallIcon(R.drawable.icon); + builder.setOngoing(true); + builder.setContentTitle("Started synchronization"); + builder.setContentText("Started synchronization"); + notification = builder.getNotification(); + + notification.contentView = new RemoteViews(context.getPackageName(), + R.layout.sync_notification); + + notification.contentView.setImageViewResource(R.id.status_icon, + R.drawable.icon); + notification.contentView.setTextViewText(R.id.status_text, + context.getString(R.string.sync_synchronizing_changes)); + notification.contentView.setProgressBar(R.id.status_progress, 100, 0, + true); + + notificationManager.notify(notifyRef, notification); + } + + public void updateNotification(String message) { + if(notification == null) + return; + + if(message != null) { + notification.contentView.setTextViewText(R.id.status_text, message); + notificationManager.notify(notifyRef, notification); + } + } + + public void updateNotification(int progress) { + updateNotification(progress, null); + } + + public void updateNotification(int progress, String message) { + if(notification == null) + return; + + if(message != null) + notification.contentView.setTextViewText(R.id.status_text, message); + + notification.contentView.setProgressBar(R.id.status_progress, 100, + progress, false); + notificationManager.notify(notifyRef, notification); + } + + public void finalizeNotification() { + notificationManager.cancel(notifyRef); + } + +} diff --git a/src/com/matburt/mobileorg/Services/SyncService.java b/src/com/matburt/mobileorg/Services/SyncService.java index 5d3b674a..3534a0a4 100644 --- a/src/com/matburt/mobileorg/Services/SyncService.java +++ b/src/com/matburt/mobileorg/Services/SyncService.java @@ -12,6 +12,7 @@ import android.preference.PreferenceManager; import com.matburt.mobileorg.Gui.SynchronizerNotification; +import com.matburt.mobileorg.Gui.SynchronizerNotificationCompat; import com.matburt.mobileorg.OrgData.MobileOrgApplication; import com.matburt.mobileorg.OrgData.OrgDatabase; import com.matburt.mobileorg.OrgData.OrgFileParser; @@ -104,8 +105,13 @@ else if (syncSource.equals("null")) else synchronizer = null; - return new Synchronizer(c, synchronizer, - new SynchronizerNotification(c)); + SynchronizerNotificationCompat notification; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) + notification = new SynchronizerNotification(this); + else + notification = new SynchronizerNotificationCompat(this); + + return new Synchronizer(c, synchronizer, notification); } private void runSynchronizer() { diff --git a/src/com/matburt/mobileorg/Synchronizers/Synchronizer.java b/src/com/matburt/mobileorg/Synchronizers/Synchronizer.java index ea94bbae..bb7568b6 100644 --- a/src/com/matburt/mobileorg/Synchronizers/Synchronizer.java +++ b/src/com/matburt/mobileorg/Synchronizers/Synchronizer.java @@ -17,7 +17,7 @@ import com.matburt.mobileorg.R; import com.matburt.mobileorg.Gui.FileDecryptionActivity; -import com.matburt.mobileorg.Gui.SynchronizerNotification; +import com.matburt.mobileorg.Gui.SynchronizerNotificationCompat; import com.matburt.mobileorg.OrgData.OrgContract.Edits; import com.matburt.mobileorg.OrgData.OrgContract.Files; import com.matburt.mobileorg.OrgData.OrgEdit; @@ -49,9 +49,9 @@ public class Synchronizer { private Context context; private ContentResolver resolver; private SynchronizerInterface syncher; - private SynchronizerNotification notify; + private SynchronizerNotificationCompat notify; - public Synchronizer(Context context, SynchronizerInterface syncher, SynchronizerNotification notify) { + public Synchronizer(Context context, SynchronizerInterface syncher, SynchronizerNotificationCompat notify) { this.context = context; this.resolver = context.getContentResolver(); this.syncher = syncher; diff --git a/tests/src/com/matburt/mobileorg/test/Synchronizers/SynchronizerNotificationStub.java b/tests/src/com/matburt/mobileorg/test/Synchronizers/SynchronizerNotificationStub.java index 75bb5a8d..08f3aebc 100644 --- a/tests/src/com/matburt/mobileorg/test/Synchronizers/SynchronizerNotificationStub.java +++ b/tests/src/com/matburt/mobileorg/test/Synchronizers/SynchronizerNotificationStub.java @@ -2,9 +2,9 @@ import android.content.Context; -import com.matburt.mobileorg.Gui.SynchronizerNotification; +import com.matburt.mobileorg.Gui.SynchronizerNotificationCompat; -public class SynchronizerNotificationStub extends SynchronizerNotification { +public class SynchronizerNotificationStub extends SynchronizerNotificationCompat { int errorNotificationNum = 0;