Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 958284ad authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Convert OPP notifications to use NotificationChannel

In O, Bluetooth notifications are hidden by default. This must be
changed to use notification channels so that the priority of the
notifications can be updated.

Bug: 36974285
Test: Send files/batches over OPP
      Testtracker: 84338
Change-Id: Ie34123bbb559cc760c83a6bdc7094a3a72382c33
parent c3e8f590
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -204,6 +204,7 @@
    <string name="btopp_live_folder">Bluetooth received</string>

    <!-- Bluetooth OPP Transfer History -->
    <string name="opp_notification_group">Bluetooth Share</string>
    <string name="download_success"> <xliff:g id="file_size">%1$s</xliff:g> Received complete.</string>
    <string name="upload_success"> <xliff:g id="file_size">%1$s</xliff:g> Sent complete.</string>
    <string name="inbound_history_title">Inbound transfers</string>
+52 −55
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.content.Context;
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;
@@ -91,6 +92,9 @@ class BluetoothOppNotification {

    public NotificationManager mNotificationMgr;

    private NotificationChannel mNotificationChannel;
    private static final String OPP_NOTIFICATION_CHANNEL = "opp_notification_channel";

    private Context mContext;

    private HashMap<String, NotificationItem> mNotifications;
@@ -99,13 +103,13 @@ class BluetoothOppNotification {

    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;
    /**
@@ -139,6 +143,11 @@ class BluetoothOppNotification {
        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();
@@ -320,7 +329,8 @@ class BluetoothOppNotification {
            }
            // 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()));
@@ -351,9 +361,7 @@ class BluetoothOppNotification {
            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());
        }
    }

@@ -366,22 +374,6 @@ class BluetoothOppNotification {
        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");
@@ -422,21 +414,25 @@ class BluetoothOppNotification {
            Intent delete_intent = new Intent(Constants.ACTION_COMPLETE_HIDE)
                    .setClassName(Constants.THIS_PACKAGE_NAME,
                            BluetoothOppReceiver.class.getName());
            Notification outNoti = new Notification.Builder(mContext)
            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))
                            .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);
            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.");
            }
        }
@@ -478,21 +474,25 @@ class BluetoothOppNotification {
            Intent delete_intent = new Intent(Constants.ACTION_COMPLETE_HIDE)
                    .setClassName(Constants.THIS_PACKAGE_NAME,
                            BluetoothOppReceiver.class.getName());
            Notification inNoti = new Notification.Builder(mContext)
            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))
                            .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);
            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.");
            }
        }
@@ -530,13 +530,10 @@ class BluetoothOppNotification {
                                          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,
@@ -557,7 +554,7 @@ class BluetoothOppNotification {
                          .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();
    }
+5 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import javax.obex.HeaderSet;
import javax.obex.ObexTransport;
import javax.obex.ResponseCodes;

import android.app.NotificationManager;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
@@ -103,6 +104,10 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                if (V) Log.v(TAG, "Interrupted waiting for thread to join");
            }
        }
        NotificationManager nm =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(BluetoothOppNotification.NOTIFICATION_ID_PROGRESS);

        mCallback = null;
    }

+6 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import android.app.NotificationManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
@@ -107,6 +108,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
        mServerSocket = serverSocket;
        PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
        mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        mPartialWakeLock.setReferenceCounted(false);
    }

    public void unblock() {
@@ -596,6 +598,10 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
            mServerSocket.prepareForNewConnect();
        }

        NotificationManager nm =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(BluetoothOppNotification.NOTIFICATION_ID_PROGRESS);

        /* onClose could happen even before start() where mCallback is set */
        if (mCallback != null) {
            Message msg = Message.obtain(mCallback);
+2 −15
Original line number Diff line number Diff line
@@ -90,7 +90,6 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            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");
@@ -99,7 +98,7 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            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");
@@ -108,8 +107,6 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            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)) {
@@ -140,7 +137,6 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
                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.");

@@ -252,19 +248,10 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
        }
    }

    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");
    }