import android.app.Notification;
import android.app.Notification.Action;
import android.app.NotificationManager;
+import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
public NotificationManager mNotificationMgr;
+ private NotificationChannel mNotificationChannel;
+ private static final String OPP_NOTIFICATION_CHANNEL = "opp_notification_channel";
+
private Context mContext;
private HashMap<String, NotificationItem> mNotifications;
private int mPendingUpdate = 0;
- private static final int NOTIFICATION_ID_OUTBOUND = -1000005;
+ public static final int NOTIFICATION_ID_PROGRESS = -1000004;
- private static final int NOTIFICATION_ID_INBOUND = -1000006;
+ private static final int NOTIFICATION_ID_OUTBOUND_COMPLETE = -1000005;
- private boolean mUpdateCompleteNotification = true;
+ private static final int NOTIFICATION_ID_INBOUND_COMPLETE = -1000006;
- private int mActiveNotificationId = 0;
+ private boolean mUpdateCompleteNotification = true;
private ContentResolver mContentResolver = null;
/**
mContext = ctx;
mNotificationMgr = (NotificationManager)mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
+ mNotificationChannel = new NotificationChannel(OPP_NOTIFICATION_CHANNEL,
+ mContext.getString(R.string.opp_notification_group),
+ NotificationManager.IMPORTANCE_HIGH);
+
+ mNotificationMgr.createNotificationChannel(mNotificationChannel);
mNotifications = new HashMap<String, NotificationItem>();
// Get Content Resolver object one time
mContentResolver = mContext.getContentResolver();
}
// Build the notification object
// TODO: split description into two rows with filename in second row
- Notification.Builder b = new Notification.Builder(mContext);
+ Notification.Builder b = new Notification.Builder(mContext, OPP_NOTIFICATION_CHANNEL);
+ b.setOnlyAlertOnce(true);
b.setColor(mContext.getResources().getColor(
com.android.internal.R.color.system_notification_accent_color,
mContext.getTheme()));
intent.setDataAndNormalize(Uri.parse(BluetoothShare.CONTENT_URI + "/" + item.id));
b.setContentIntent(PendingIntent.getBroadcast(mContext, 0, intent, 0));
- mNotificationMgr.notify(item.id, b.build());
-
- mActiveNotificationId = item.id;
+ mNotificationMgr.notify(NOTIFICATION_ID_PROGRESS, b.build());
}
}
int inboundSuccNumber = 0;
int inboundFailNumber = 0;
- // If there is active transfer, no need to update complete transfer
- // notification
- if (!mUpdateCompleteNotification) {
- if (V) Log.v(TAG, "No need to update complete notification");
- return;
- }
-
- // After merge complete notifications to 2 notifications, there is no
- // chance to update the active notifications to complete notifications
- // as before. So need cancel the active notification after the active
- // transfer becomes complete.
- if (mNotificationMgr != null && mActiveNotificationId != 0) {
- mNotificationMgr.cancel(mActiveNotificationId);
- if (V) Log.v(TAG, "ongoing transfer notification was removed");
- }
-
// Creating outbound notification
Cursor cursor = mContentResolver.query(BluetoothShare.CONTENT_URI, null,
WHERE_COMPLETED_OUTBOUND, null, BluetoothShare.TIMESTAMP + " DESC");
Intent delete_intent = new Intent(Constants.ACTION_COMPLETE_HIDE)
.setClassName(Constants.THIS_PACKAGE_NAME,
BluetoothOppReceiver.class.getName());
- Notification outNoti = new Notification.Builder(mContext)
- .setContentTitle(mContext.getString(R.string.outbound_noti_title))
- .setContentText(caption)
- .setSmallIcon(android.R.drawable.stat_sys_upload_done)
- .setColor(mContext.getResources().getColor(
- com.android.internal.R.color.system_notification_accent_color,
- mContext.getTheme()))
- .setContentIntent(PendingIntent.getBroadcast(mContext, 0, content_intent, 0))
- .setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, delete_intent, 0))
- .setWhen(timeStamp)
- .build();
- mNotificationMgr.notify(NOTIFICATION_ID_OUTBOUND, outNoti);
+ Notification outNoti =
+ new Notification.Builder(mContext, OPP_NOTIFICATION_CHANNEL)
+ .setOnlyAlertOnce(true)
+ .setContentTitle(mContext.getString(R.string.outbound_noti_title))
+ .setContentText(caption)
+ .setSmallIcon(android.R.drawable.stat_sys_upload_done)
+ .setColor(mContext.getResources().getColor(
+ com.android.internal.R.color.system_notification_accent_color,
+ mContext.getTheme()))
+ .setContentIntent(
+ PendingIntent.getBroadcast(mContext, 0, content_intent, 0))
+ .setDeleteIntent(
+ PendingIntent.getBroadcast(mContext, 0, delete_intent, 0))
+ .setWhen(timeStamp)
+ .build();
+ mNotificationMgr.notify(NOTIFICATION_ID_OUTBOUND_COMPLETE, outNoti);
} else {
if (mNotificationMgr != null) {
- mNotificationMgr.cancel(NOTIFICATION_ID_OUTBOUND);
+ mNotificationMgr.cancel(NOTIFICATION_ID_OUTBOUND_COMPLETE);
if (V) Log.v(TAG, "outbound notification was removed.");
}
}
Intent delete_intent = new Intent(Constants.ACTION_COMPLETE_HIDE)
.setClassName(Constants.THIS_PACKAGE_NAME,
BluetoothOppReceiver.class.getName());
- Notification inNoti = new Notification.Builder(mContext)
- .setContentTitle(mContext.getString(R.string.inbound_noti_title))
- .setContentText(caption)
- .setSmallIcon(android.R.drawable.stat_sys_download_done)
- .setColor(mContext.getResources().getColor(
- com.android.internal.R.color.system_notification_accent_color,
- mContext.getTheme()))
- .setContentIntent(PendingIntent.getBroadcast(mContext, 0, content_intent, 0))
- .setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, delete_intent, 0))
- .setWhen(timeStamp)
- .build();
- mNotificationMgr.notify(NOTIFICATION_ID_INBOUND, inNoti);
+ Notification inNoti =
+ new Notification.Builder(mContext, OPP_NOTIFICATION_CHANNEL)
+ .setOnlyAlertOnce(true)
+ .setContentTitle(mContext.getString(R.string.inbound_noti_title))
+ .setContentText(caption)
+ .setSmallIcon(android.R.drawable.stat_sys_download_done)
+ .setColor(mContext.getResources().getColor(
+ com.android.internal.R.color.system_notification_accent_color,
+ mContext.getTheme()))
+ .setContentIntent(
+ PendingIntent.getBroadcast(mContext, 0, content_intent, 0))
+ .setDeleteIntent(
+ PendingIntent.getBroadcast(mContext, 0, delete_intent, 0))
+ .setWhen(timeStamp)
+ .build();
+ mNotificationMgr.notify(NOTIFICATION_ID_INBOUND_COMPLETE, inNoti);
} else {
if (mNotificationMgr != null) {
- mNotificationMgr.cancel(NOTIFICATION_ID_INBOUND);
+ mNotificationMgr.cancel(NOTIFICATION_ID_INBOUND_COMPLETE);
if (V) Log.v(TAG, "inbound notification was removed.");
}
}
0))
.build();
Notification n =
- new Notification.Builder(mContext)
+ new Notification.Builder(mContext, OPP_NOTIFICATION_CHANNEL)
.setOnlyAlertOnce(true)
.setOngoing(true)
- .setVibrate(new long[] {200})
.setWhen(info.mTimeStamp)
- .setDefaults(Notification.DEFAULT_SOUND)
- .setPriority(Notification.PRIORITY_HIGH)
.addAction(actionDecline)
.addAction(actionAccept)
.setContentIntent(PendingIntent.getBroadcast(mContext, 0,
.setContentInfo(Formatter.formatFileSize(mContext, info.mTotalBytes))
.setSmallIcon(R.drawable.bt_incomming_file_notification)
.build();
- mNotificationMgr.notify(info.mID, n);
+ mNotificationMgr.notify(NOTIFICATION_ID_PROGRESS, n);
}
cursor.close();
}
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.setDataAndNormalize(uri);
context.startActivity(in);
- cancelNotification(context, uri);
} else if (action.equals(Constants.ACTION_DECLINE)) {
if (V) Log.v(TAG, "Receiver ACTION_DECLINE");
ContentValues values = new ContentValues();
values.put(BluetoothShare.USER_CONFIRMATION, BluetoothShare.USER_CONFIRMATION_DENIED);
context.getContentResolver().update(uri, values, null, null);
- cancelNotification(context, uri);
+ cancelNotification(context, BluetoothOppNotification.NOTIFICATION_ID_PROGRESS);
} else if (action.equals(Constants.ACTION_ACCEPT)) {
if (V) Log.v(TAG, "Receiver ACTION_ACCEPT");
ContentValues values = new ContentValues();
values.put(BluetoothShare.USER_CONFIRMATION, BluetoothShare.USER_CONFIRMATION_CONFIRMED);
context.getContentResolver().update(uri, values, null, null);
- cancelNotification(context, uri);
-
} else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) {
if (V) {
if (action.equals(Constants.ACTION_OPEN)) {
context.startActivity(in);
}
- cancelNotification(context, uri);
} else if (action.equals(Constants.ACTION_OPEN_OUTBOUND_TRANSFER)) {
if (V) Log.v(TAG, "Received ACTION_OPEN_OUTBOUND_TRANSFER.");
}
}
- private void cancelNotification(Context context, Uri uri) {
+ private void cancelNotification(Context context, int id) {
NotificationManager notMgr = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (notMgr == null) return;
-
- int id = -1;
- try {
- id = (int) ContentUris.parseId(uri);
- } catch (NumberFormatException ex) {
- Log.v(TAG, "Can't parse notification ID from Uri!");
- return;
- }
-
notMgr.cancel(id);
if (V) Log.v(TAG, "notMgr.cancel called");
}