1 /* 2 * Copyright (C) 2016 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 com.android.mtp; 18 19 import android.annotation.NonNull; 20 import android.app.Notification; 21 import android.app.NotificationChannel; 22 import android.app.NotificationManager; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import com.android.internal.annotations.VisibleForTesting; 27 import com.android.internal.util.Preconditions; 28 29 /** 30 * Sends intent to MtpDocumentsService. 31 */ 32 class ServiceIntentSender { 33 private final static String CHANNEL_ID = "device_notification_channel"; 34 private final Context mContext; 35 ServiceIntentSender(Context context)36 ServiceIntentSender(Context context) { 37 mContext = context; 38 39 // Create notification channel. 40 final NotificationChannel mChannel = new NotificationChannel( 41 CHANNEL_ID, 42 context.getResources().getString( 43 com.android.internal.R.string.default_notification_channel_label), 44 NotificationManager.IMPORTANCE_LOW); 45 final NotificationManager notificationManager = 46 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 47 notificationManager.createNotificationChannel(mChannel); 48 } 49 50 @VisibleForTesting ServiceIntentSender()51 protected ServiceIntentSender() { 52 mContext = null; 53 } 54 55 /** 56 * Notify the change of opened device set. 57 * @param records List of opened devices. Can be empty. 58 */ sendUpdateNotificationIntent(@onNull MtpDeviceRecord[] records)59 void sendUpdateNotificationIntent(@NonNull MtpDeviceRecord[] records) { 60 Preconditions.checkNotNull(records); 61 final Intent intent = new Intent(MtpDocumentsService.ACTION_UPDATE_NOTIFICATION); 62 intent.setComponent(new ComponentName(mContext, MtpDocumentsService.class)); 63 if (records.length != 0) { 64 final int[] ids = new int[records.length]; 65 final Notification[] notifications = new Notification[records.length]; 66 for (int i = 0; i < records.length; i++) { 67 ids[i] = records[i].deviceId; 68 notifications[i] = createNotification(mContext, records[i]); 69 } 70 intent.putExtra(MtpDocumentsService.EXTRA_DEVICE_IDS, ids); 71 intent.putExtra(MtpDocumentsService.EXTRA_DEVICE_NOTIFICATIONS, notifications); 72 mContext.startForegroundService(intent); 73 } else { 74 mContext.startService(intent); 75 } 76 } 77 createNotification(Context context, MtpDeviceRecord device)78 private static Notification createNotification(Context context, MtpDeviceRecord device) { 79 final String title = context.getResources().getString( 80 R.string.accessing_notification_title, 81 device.name); 82 return new Notification.Builder(context, CHANNEL_ID) 83 .setLocalOnly(true) 84 .setContentTitle(title) 85 .setSmallIcon(com.android.internal.R.drawable.stat_sys_data_usb) 86 .setCategory(Notification.CATEGORY_SYSTEM) 87 .setFlag(Notification.FLAG_NO_CLEAR, true) 88 .build(); 89 } 90 } 91