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

Commit 9b1318ee authored by Hemant Gupta's avatar Hemant Gupta Committed by Andre Eisenbach
Browse files

Settings: Convert notifications to use NotificationChannel (1/2)

Test Setup:
===========
HW DUT: Pixel O
Remote: MecApp (SAP Client)

Usecase:
1) Connect from MecApp.
2) Accept SAP connection on DUT.

Expected Behaviour:
Notification popup for SAP connected should be displayed on UI
which provide option for user to disconnect SAP connection.

Observed Behaviour:
Notification popup for SAP connected is not displayed on UI so user
is not able to initiate disconnection from SAP server.

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

Fix:
Add notification channel before building notification from SAP.

Test: Checked if now notification for SAP connect can be seen in
notification bar when SAP is connected allowing user to disconnect.

Bug: 38331825
Change-Id: I67b0a3fd7f1df857c6288150edbef80d4a953490
(cherry picked from commit c5c434b205948c7bf6bea50fc55521904c896ce6)
parent af307b2f
Loading
Loading
Loading
Loading
+43 −34
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package com.android.bluetooth.sap;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothSap;
@@ -84,6 +85,7 @@ public class SapServer extends Thread implements Callback {
    public static final String SAP_DISCONNECT_TYPE_EXTRA =
            "com.android.bluetooth.sap.extra.DISCONNECT_TYPE";
    public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
    private static final String SAP_NOTIFICATION_CHANNEL = "sap_notification_channel";
    public static final int ISAP_GET_SERVICE_DELAY_MILLIS = 3 * 1000;
    private static final int DISCONNECT_TIMEOUT_IMMEDIATE = 5000; /* ms */
    private static final int DISCONNECT_TIMEOUT_RFCOMM = 2000; /* ms */
@@ -208,6 +210,12 @@ public class SapServer extends Thread implements Callback {
    {
        String title, text, button, ticker;
        Notification notification;
        NotificationManager notificationManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel notificationChannel = new NotificationChannel(SAP_NOTIFICATION_CHANNEL,
                mContext.getString(R.string.bluetooth_sap_notif_title),
                NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(notificationChannel);
        if(VERBOSE) Log.i(TAG, "setNotification type: " + type);
        /* For PTS TC_SERVER_DCN_BV_03_I we need to expose the option to send immediate disconnect
         * without first sending a graceful disconnect.
@@ -229,13 +237,14 @@ public class SapServer extends Thread implements Callback {
            text = mContext.getString(R.string.bluetooth_sap_notif_disconnecting);
            ticker = mContext.getString(R.string.bluetooth_sap_notif_ticker);
        }
        if(!pts_test)
        {
        if (!pts_test) {
            sapDisconnectIntent.putExtra(SapServer.SAP_DISCONNECT_TYPE_EXTRA, type);
            PendingIntent pIntentDisconnect = PendingIntent.getBroadcast(mContext, type,
                    sapDisconnectIntent,flags);
            notification = new Notification.Builder(mContext).setOngoing(true)
                .addAction(android.R.drawable.stat_sys_data_bluetooth, button, pIntentDisconnect)
            notification = new Notification.Builder(mContext, SAP_NOTIFICATION_CHANNEL)
                                   .setOngoing(true)
                                   .addAction(android.R.drawable.stat_sys_data_bluetooth, button,
                                           pIntentDisconnect)
                                   .setContentTitle(title)
                                   .setTicker(ticker)
                                   .setContentText(text)
@@ -245,7 +254,6 @@ public class SapServer extends Thread implements Callback {
                                   .setOnlyAlertOnce(true)
                                   .build();
        } else {

            sapDisconnectIntent.putExtra(SapServer.SAP_DISCONNECT_TYPE_EXTRA,
                    SapMessage.DISC_GRACEFULL);
            Intent sapForceDisconnectIntent = new Intent(SapServer.SAP_DISCONNECT_ACTION);
@@ -255,12 +263,16 @@ public class SapServer extends Thread implements Callback {
                    SapMessage.DISC_GRACEFULL, sapDisconnectIntent,flags);
            PendingIntent pIntentForceDisconnect = PendingIntent.getBroadcast(mContext,
                    SapMessage.DISC_IMMEDIATE, sapForceDisconnectIntent,flags);
            notification = new Notification.Builder(mContext).setOngoing(true)
            notification =
                    new Notification.Builder(mContext, SAP_NOTIFICATION_CHANNEL)
                            .setOngoing(true)
                            .addAction(android.R.drawable.stat_sys_data_bluetooth,
                            mContext.getString(R.string.bluetooth_sap_notif_disconnect_button),
                                    mContext.getString(
                                            R.string.bluetooth_sap_notif_disconnect_button),
                                    pIntentDisconnect)
                            .addAction(android.R.drawable.stat_sys_data_bluetooth,
                            mContext.getString(R.string.bluetooth_sap_notif_force_disconnect_button),
                                    mContext.getString(
                                            R.string.bluetooth_sap_notif_force_disconnect_button),
                                    pIntentForceDisconnect)
                            .setContentTitle(title)
                            .setTicker(ticker)
@@ -275,9 +287,6 @@ public class SapServer extends Thread implements Callback {
        // cannot be set with the builder
        notification.flags |= Notification.FLAG_NO_CLEAR |Notification.FLAG_ONLY_ALERT_ONCE;

        NotificationManager notificationManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(NOTIFICATION_ID, notification);
    }