Skip to content

Commit

Permalink
Added support for new notification style, fixing background/font issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdweiss committed Apr 6, 2013
1 parent 2102ddf commit 5c7daa5
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 41 deletions.
2 changes: 1 addition & 1 deletion res/values/strings.xml
Expand Up @@ -36,7 +36,7 @@
<string name="sync_synchronizing_changes">Synchronizing changes</string>
<string name="sync_error_server_certificate">Error with server certificate:</string>
<string name="sync_wait">Synchronizing, please wait…</string>
<string name="sync_failed">Synchronization failed, try again?</string>
<string name="sync_failed">Synchronization failed</string>
<string name="sync_plugin">MobileOrg Synchronization</string>
<string name="sync_plugin_info">This plugin allows you to trigger a MobileOrg Sync from Locale or Tasker. If you are okay with this click \'Accept\' otherwise click \'Deny\'</string>
<string name="sync_plugin_message">Synchronize</string>
Expand Down
55 changes: 22 additions & 33 deletions 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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
113 changes: 113 additions & 0 deletions 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);
}

}
10 changes: 8 additions & 2 deletions src/com/matburt/mobileorg/Services/SyncService.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/com/matburt/mobileorg/Synchronizers/Synchronizer.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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;

Expand Down

0 comments on commit 5c7daa5

Please sign in to comment.