• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.debug;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.Notification;
22 import android.app.PendingIntent;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.content.res.Resources;
28 import android.os.UserHandle;
29 import android.provider.Settings;
30 
31 import com.android.internal.notification.SystemNotificationChannels;
32 
33 /**
34  * Utility class for building adb notifications.
35  * @hide
36  */
37 public final class AdbNotifications {
38     /**
39      * Notification channel for tv types.
40      */
41     private static final String ADB_NOTIFICATION_CHANNEL_ID_TV = "usbdevicemanager.adb.tv";
42 
43     /**
44      * Builds a notification to show connected state for adb over a transport type.
45      * @param context the context
46      * @param transportType the adb transport type.
47      * @return a newly created Notification for the transport type, or null on error.
48      */
49     @Nullable
createNotification(@onNull Context context, byte transportType)50     public static Notification createNotification(@NonNull Context context,
51             byte transportType) {
52         Resources resources = context.getResources();
53         int titleId;
54         int messageId;
55 
56         if (transportType == AdbTransportType.USB) {
57             titleId = com.android.internal.R.string.adb_active_notification_title;
58             messageId = com.android.internal.R.string.adb_active_notification_message;
59         } else if (transportType == AdbTransportType.WIFI) {
60             titleId = com.android.internal.R.string.adbwifi_active_notification_title;
61             messageId = com.android.internal.R.string.adbwifi_active_notification_message;
62         } else {
63             throw new IllegalArgumentException(
64                     "createNotification called with unknown transport type=" + transportType);
65         }
66 
67         CharSequence title = resources.getText(titleId);
68         CharSequence message = resources.getText(messageId);
69 
70         Intent intent = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
71         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
72         ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
73                 PackageManager.MATCH_SYSTEM_ONLY);
74         // Settings app may not be available (e.g. device policy manager removes it)
75         PendingIntent pIntent = null;
76         if (resolveInfo != null) {
77             intent.setPackage(resolveInfo.activityInfo.packageName);
78             pIntent = PendingIntent.getActivityAsUser(context, 0, intent,
79                     PendingIntent.FLAG_IMMUTABLE, null, UserHandle.CURRENT);
80         }
81 
82 
83         return new Notification.Builder(context, SystemNotificationChannels.DEVELOPER_IMPORTANT)
84                 .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
85                 .setWhen(0)
86                 .setOngoing(true)
87                 .setTicker(title)
88                 .setDefaults(0)  // please be quiet
89                 .setColor(context.getColor(
90                             com.android.internal.R.color.system_notification_accent_color))
91                 .setContentTitle(title)
92                 .setContentText(message)
93                 .setContentIntent(pIntent)
94                 .setVisibility(Notification.VISIBILITY_PUBLIC)
95                 .extend(new Notification.TvExtender()
96                         .setChannelId(ADB_NOTIFICATION_CHANNEL_ID_TV))
97                 .build();
98     }
99 }
100