forked from syncthing/syncthing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all notification handling into common file
- Loading branch information
Showing
10 changed files
with
189 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.nutomic.syncthingandroid.service; | ||
|
||
import android.app.Notification; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.os.Build; | ||
import android.support.v4.app.NotificationCompat; | ||
|
||
import com.nutomic.syncthingandroid.R; | ||
import com.nutomic.syncthingandroid.SyncthingApp; | ||
import com.nutomic.syncthingandroid.activities.FirstStartActivity; | ||
import com.nutomic.syncthingandroid.activities.MainActivity; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class NotificationHandler { | ||
|
||
private static final int ID_PERSISTENT = 1; | ||
private static final int ID_RESTART = 2; | ||
private static final int ID_STOP_BACKGROUND_WARNING = 3; | ||
private static final int ID_CRASH = 9; | ||
|
||
private final Context mContext; | ||
@Inject SharedPreferences mPreferences; | ||
private final NotificationManager mNotificationManager; | ||
|
||
public NotificationHandler(Context context) { | ||
((SyncthingApp) context.getApplicationContext()).component().inject(this); | ||
mContext = context; | ||
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); | ||
} | ||
|
||
/** | ||
* Shows or hides the persistent notification based on running state and | ||
* {@link SyncthingService#PREF_NOTIFICATION_TYPE}. | ||
*/ | ||
public void updatePersistentNotification(SyncthingService service, SyncthingService.State currentState) { | ||
String type = mPreferences.getString(SyncthingService.PREF_NOTIFICATION_TYPE, "low_priority"); | ||
boolean foreground = mPreferences.getBoolean(SyncthingService.PREF_FOREGROUND_SERVICE, false); | ||
if ("none".equals(type) && foreground) { | ||
// foreground priority requires any notification | ||
// so this ensures that we either have a "default" or "low_priority" notification, | ||
// but not "none". | ||
type = "low_priority"; | ||
} | ||
if ((currentState == SyncthingService.State.ACTIVE || currentState == SyncthingService.State.STARTING) && | ||
!type.equals("none")) { | ||
// Launch FirstStartActivity instead of MainActivity so we can request permission if | ||
// necessary. | ||
PendingIntent pi = PendingIntent.getActivity(mContext, 0, | ||
new Intent(mContext, FirstStartActivity.class), 0); | ||
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext) | ||
.setContentTitle(mContext.getString(R.string.syncthing_active)) | ||
.setSmallIcon(R.drawable.ic_stat_notify) | ||
.setOngoing(true) | ||
.setContentIntent(pi); | ||
if (type.equals("low_priority")) | ||
builder.setPriority(NotificationCompat.PRIORITY_MIN); | ||
|
||
if (foreground) { | ||
builder.setContentText(mContext.getString(R.string.syncthing_active_foreground)); | ||
service.startForeground(ID_PERSISTENT, builder.build()); | ||
} else { | ||
service.stopForeground(false); // ensure no longer running with foreground priority | ||
mNotificationManager.notify(ID_PERSISTENT, builder.build()); | ||
} | ||
} else { | ||
// ensure no longer running with foreground priority | ||
cancelPersistentNotification(service); | ||
} | ||
} | ||
|
||
public void cancelPersistentNotification(SyncthingService service) { | ||
service.stopForeground(false); | ||
mNotificationManager.cancel(ID_PERSISTENT); | ||
} | ||
|
||
public void showCrashedNotification(Intent intent) { | ||
if (mPreferences.getBoolean("notify_crashes", false)) { | ||
Notification n = new NotificationCompat.Builder(mContext) | ||
.setContentTitle(mContext.getString(R.string.notification_crash_title)) | ||
.setContentText(mContext.getString(R.string.notification_crash_text)) | ||
.setSmallIcon(R.drawable.ic_stat_notify) | ||
.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0)) | ||
.setAutoCancel(true) | ||
.build(); | ||
mNotificationManager.notify(ID_CRASH, n); | ||
} | ||
} | ||
|
||
public void showEventNotification(String text, PendingIntent pi, int id) { | ||
Notification n = new NotificationCompat.Builder(mContext) | ||
.setContentTitle(mContext.getString(R.string.app_name)) | ||
.setContentText(text) | ||
.setStyle(new NotificationCompat.BigTextStyle() | ||
.bigText(text)) | ||
.setContentIntent(pi) | ||
.setSmallIcon(R.drawable.ic_stat_notify) | ||
.setAutoCancel(true) | ||
.build(); | ||
mNotificationManager.notify(id, n); | ||
} | ||
|
||
public void showRestartNotification() { | ||
Intent intent = new Intent(mContext, SyncthingService.class) | ||
.setAction(SyncthingService.ACTION_RESTART); | ||
PendingIntent pi = PendingIntent.getService(mContext, 0, intent, 0); | ||
|
||
Notification n = new NotificationCompat.Builder(mContext) | ||
.setContentTitle(mContext.getString(R.string.restart_title)) | ||
.setContentText(mContext.getString(R.string.restart_notification_text)) | ||
.setSmallIcon(R.drawable.ic_stat_notify) | ||
.setContentIntent(pi) | ||
.build(); | ||
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL; | ||
mNotificationManager.notify(ID_RESTART, n); | ||
} | ||
|
||
public void cancelRestartNotification() { | ||
mNotificationManager.cancel(ID_RESTART); | ||
} | ||
|
||
public void showStopSyncthingWarningNotification() { | ||
final String msg = mContext.getString(R.string.appconfig_receiver_background_enabled); | ||
NotificationCompat.Builder nb = new NotificationCompat.Builder(mContext) | ||
.setContentText(msg) | ||
.setTicker(msg) | ||
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) | ||
.setContentTitle(mContext.getText(mContext.getApplicationInfo().labelRes)) | ||
.setSmallIcon(R.drawable.ic_stat_notify) | ||
.setAutoCancel(true) | ||
.setContentIntent(PendingIntent.getActivity(mContext, 0, | ||
new Intent(mContext, MainActivity.class), | ||
PendingIntent.FLAG_UPDATE_CURRENT)); | ||
|
||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
nb.setCategory(Notification.CATEGORY_ERROR); | ||
} | ||
mNotificationManager.notify(ID_STOP_BACKGROUND_WARNING, nb.build()); | ||
} | ||
} |
Oops, something went wrong.