Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 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 | |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 21 | import android.app.Notification; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 22 | import android.app.Service; |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 23 | import android.app.NotificationManager; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 24 | import android.content.Intent; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 25 | import android.os.IBinder; |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 26 | import android.os.Parcelable; |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 27 | import android.service.notification.StatusBarNotification; |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import com.android.internal.util.Preconditions; |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 30 | import java.util.HashSet; |
| 31 | import java.util.Set; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 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 { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 39 | static final String ACTION_UPDATE_NOTIFICATION = "com.android.mtp.UPDATE_NOTIFICATION"; |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 40 | static final String EXTRA_DEVICE_IDS = "deviceIds"; |
| 41 | static final String EXTRA_DEVICE_NOTIFICATIONS = "deviceNotifications"; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 42 | |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 43 | private NotificationManager mNotificationManager; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 44 | |
| 45 | @Override |
| 46 | public IBinder onBind(Intent intent) { |
| 47 | // The service is used via intents. |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void onCreate() { |
| 53 | super.onCreate(); |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 54 | mNotificationManager = getSystemService(NotificationManager.class); |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public int onStartCommand(Intent intent, int flags, int startId) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 59 | // If intent is null, the service was restarted. |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 60 | if (intent == null || ACTION_UPDATE_NOTIFICATION.equals(intent.getAction())) { |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 61 | final int[] ids = intent.hasExtra(EXTRA_DEVICE_IDS) ? |
| 62 | intent.getExtras().getIntArray(EXTRA_DEVICE_IDS) : null; |
| 63 | final Notification[] notifications = intent.hasExtra(EXTRA_DEVICE_NOTIFICATIONS) ? |
| 64 | castToNotifications(intent.getExtras().getParcelableArray( |
| 65 | EXTRA_DEVICE_NOTIFICATIONS)) : null; |
| 66 | return updateForegroundState(ids, notifications) ? START_STICKY : START_NOT_STICKY; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 67 | } |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 68 | return START_NOT_STICKY; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 69 | } |
| 70 | |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 71 | /** |
| 72 | * Updates the foreground state of the service. |
| 73 | * @return Whether the service is foreground or not. |
| 74 | */ |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 75 | private boolean updateForegroundState( |
| 76 | @Nullable int[] ids, @Nullable Notification[] notifications) { |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 77 | final Set<Integer> openedNotification = new HashSet<>(); |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 78 | final int size = ids != null ? ids.length : 0; |
| 79 | if (size != 0) { |
| 80 | Preconditions.checkArgument(ids != null); |
| 81 | Preconditions.checkArgument(notifications != null); |
| 82 | Preconditions.checkArgument(ids.length == notifications.length); |
| 83 | } |
| 84 | |
| 85 | for (int i = 0; i < size; i++) { |
| 86 | if (i == 0) { |
| 87 | // Mark this service as foreground with the notification so that the process is |
| 88 | // not killed by the system while a MTP device is opened. |
| 89 | startForeground(ids[i], notifications[i]); |
| 90 | } else { |
| 91 | // Only one notification can be shown as a foreground notification. We need to |
| 92 | // show the rest as normal notification. |
| 93 | mNotificationManager.notify(ids[i], notifications[i]); |
| 94 | } |
| 95 | openedNotification.add(ids[i]); |
| 96 | } |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 97 | |
| 98 | final StatusBarNotification[] activeNotifications = |
| 99 | mNotificationManager.getActiveNotifications(); |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 100 | for (final StatusBarNotification notification : activeNotifications) { |
| 101 | if (!openedNotification.contains(notification.getId())) { |
| 102 | mNotificationManager.cancel(notification.getId()); |
| 103 | } |
| 104 | } |
| 105 | |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 106 | if (size == 0) { |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 107 | // There is no opened device. |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 108 | stopForeground(true /* removeNotification */); |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 109 | stopSelf(); |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 110 | return false; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 111 | } |
Daichi Hirono | d3c6dd1 | 2017-01-26 14:40:19 +0900 | [diff] [blame] | 112 | |
| 113 | return true; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 116 | private static @NonNull Notification[] castToNotifications(@NonNull Parcelable[] src) { |
| 117 | Preconditions.checkNotNull(src); |
| 118 | final Notification[] notifications = new Notification[src.length]; |
| 119 | for (int i = 0; i < src.length; i++) { |
| 120 | notifications[i] = (Notification) src[i]; |
| 121 | } |
| 122 | return notifications; |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 123 | } |
| 124 | } |