1 /* 2 * Copyright (C) 2015 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.annotation.Nullable; 21 import android.app.Notification; 22 import android.app.Service; 23 import android.app.NotificationManager; 24 import android.content.Intent; 25 import android.os.IBinder; 26 import android.os.Parcelable; 27 import android.service.notification.StatusBarNotification; 28 import android.util.Log; 29 import com.android.internal.util.Preconditions; 30 import java.util.HashSet; 31 import java.util.Set; 32 33 /** 34 * Service to manage lifetime of DocumentsProvider's process. 35 * The service prevents the system from killing the process that holds USB connections. The service 36 * starts to run when the first MTP device is opened, and stops when the last MTP device is closed. 37 */ 38 public class MtpDocumentsService extends Service { 39 static final String ACTION_UPDATE_NOTIFICATION = "com.android.mtp.UPDATE_NOTIFICATION"; 40 static final String EXTRA_DEVICE_IDS = "deviceIds"; 41 static final String EXTRA_DEVICE_NOTIFICATIONS = "deviceNotifications"; 42 43 private NotificationManager mNotificationManager; 44 45 @Override onBind(Intent intent)46 public IBinder onBind(Intent intent) { 47 // The service is used via intents. 48 return null; 49 } 50 51 @Override onCreate()52 public void onCreate() { 53 super.onCreate(); 54 mNotificationManager = getSystemService(NotificationManager.class); 55 } 56 57 @Override onStartCommand(Intent intent, int flags, int startId)58 public int onStartCommand(Intent intent, int flags, int startId) { 59 // If intent is null, the service was restarted. 60 if (intent == null) { 61 updateForegroundState(null, null); 62 } else if (ACTION_UPDATE_NOTIFICATION.equals(intent.getAction())) { 63 final int[] ids = intent.hasExtra(EXTRA_DEVICE_IDS) ? 64 intent.getExtras().getIntArray(EXTRA_DEVICE_IDS) : null; 65 final Notification[] notifications = intent.hasExtra(EXTRA_DEVICE_NOTIFICATIONS) ? 66 castToNotifications(intent.getExtras().getParcelableArray( 67 EXTRA_DEVICE_NOTIFICATIONS)) : null; 68 return updateForegroundState(ids, notifications) ? START_STICKY : START_NOT_STICKY; 69 } 70 return START_NOT_STICKY; 71 } 72 73 /** 74 * Updates the foreground state of the service. 75 * @return Whether the service is foreground or not. 76 */ updateForegroundState( @ullable int[] ids, @Nullable Notification[] notifications)77 private boolean updateForegroundState( 78 @Nullable int[] ids, @Nullable Notification[] notifications) { 79 final Set<Integer> openedNotification = new HashSet<>(); 80 final int size = ids != null ? ids.length : 0; 81 if (size != 0) { 82 Preconditions.checkArgument(ids != null); 83 Preconditions.checkArgument(notifications != null); 84 Preconditions.checkArgument(ids.length == notifications.length); 85 } 86 87 for (int i = 0; i < size; i++) { 88 if (i == 0) { 89 // Mark this service as foreground with the notification so that the process is 90 // not killed by the system while a MTP device is opened. 91 startForeground(ids[i], notifications[i]); 92 } else { 93 // Only one notification can be shown as a foreground notification. We need to 94 // show the rest as normal notification. 95 mNotificationManager.notify(ids[i], notifications[i]); 96 } 97 openedNotification.add(ids[i]); 98 } 99 100 final StatusBarNotification[] activeNotifications = 101 mNotificationManager.getActiveNotifications(); 102 for (final StatusBarNotification notification : activeNotifications) { 103 if (!openedNotification.contains(notification.getId())) { 104 mNotificationManager.cancel(notification.getId()); 105 } 106 } 107 108 if (size == 0) { 109 // There is no opened device. 110 stopForeground(true /* removeNotification */); 111 stopSelf(); 112 return false; 113 } 114 115 return true; 116 } 117 castToNotifications(@onNull Parcelable[] src)118 private static @NonNull Notification[] castToNotifications(@NonNull Parcelable[] src) { 119 Preconditions.checkNotNull(src); 120 final Notification[] notifications = new Notification[src.length]; 121 for (int i = 0; i < src.length; i++) { 122 notifications[i] = (Notification) src[i]; 123 } 124 return notifications; 125 } 126 } 127