diff options
author | 2017-04-03 12:20:42 +0900 | |
---|---|---|
committer | 2017-04-07 05:12:37 +0000 | |
commit | 98f48479e95b04479b5512c587da6bc9fbd73b7e (patch) | |
tree | 6089ce0eb1265fa4b1a2245f223b6412179b38ec /packages/MtpDocumentsProvider | |
parent | 8ce439a76861e9e90b5b8ef7fa757bf798ac76fa (diff) |
Use Context#startForegroundService in MTP provider
NotificationManager.startServiceInForeground() was moved to
Context#startForegroundService. MtpDocumentsProvider should use new one.
Bug: 36794559
Test: MtpDocumentsProviderTests
Change-Id: I84723ee8c3f0f8bfe4d5ea8ad035c01c42ce8bab
(cherry picked from commit 203be491ef479deea1f39f8b63f3e1916501a37a)
Diffstat (limited to 'packages/MtpDocumentsProvider')
4 files changed, 73 insertions, 48 deletions
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java index e60b5a9a373a..db88f2c038ec 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java @@ -509,7 +509,7 @@ public class MtpDocumentsProvider extends DocumentsProvider { final DeviceToolkit toolkit = new DeviceToolkit(mMtpManager, mResolver, mDatabase, device); mDeviceToolkits.put(deviceId, toolkit); - mIntentSender.sendUpdateNotificationIntent(device); + mIntentSender.sendUpdateNotificationIntent(getOpenedDeviceRecordsCache()); try { mRootScanner.resume().await(); } catch (InterruptedException error) { @@ -524,9 +524,9 @@ public class MtpDocumentsProvider extends DocumentsProvider { void closeDevice(int deviceId) throws IOException, InterruptedException { synchronized (mDeviceListLock) { closeDeviceInternal(deviceId); + mIntentSender.sendUpdateNotificationIntent(getOpenedDeviceRecordsCache()); } mRootScanner.resume(); - mIntentSender.sendUpdateNotificationIntent(null); } MtpDeviceRecord[] getOpenedDeviceRecordsCache() { diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsService.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsService.java index 664d3c99344b..fa1a12030240 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsService.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsService.java @@ -16,13 +16,17 @@ package com.android.mtp; +import android.annotation.NonNull; +import android.annotation.Nullable; import android.app.Notification; import android.app.Service; import android.app.NotificationManager; -import android.content.Context; import android.content.Intent; import android.os.IBinder; +import android.os.Parcelable; import android.service.notification.StatusBarNotification; +import android.util.Log; +import com.android.internal.util.Preconditions; import java.util.HashSet; import java.util.Set; @@ -33,7 +37,8 @@ import java.util.Set; */ public class MtpDocumentsService extends Service { static final String ACTION_UPDATE_NOTIFICATION = "com.android.mtp.UPDATE_NOTIFICATION"; - static final String EXTRA_DEVICE = "device"; + static final String EXTRA_DEVICE_IDS = "deviceIds"; + static final String EXTRA_DEVICE_NOTIFICATIONS = "deviceNotifications"; private NotificationManager mNotificationManager; @@ -53,7 +58,12 @@ public class MtpDocumentsService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { // If intent is null, the service was restarted. if (intent == null || ACTION_UPDATE_NOTIFICATION.equals(intent.getAction())) { - return updateForegroundState() ? START_STICKY : START_NOT_STICKY; + final int[] ids = intent.hasExtra(EXTRA_DEVICE_IDS) ? + intent.getExtras().getIntArray(EXTRA_DEVICE_IDS) : null; + final Notification[] notifications = intent.hasExtra(EXTRA_DEVICE_NOTIFICATIONS) ? + castToNotifications(intent.getExtras().getParcelableArray( + EXTRA_DEVICE_NOTIFICATIONS)) : null; + return updateForegroundState(ids, notifications) ? START_STICKY : START_NOT_STICKY; } return START_NOT_STICKY; } @@ -62,35 +72,38 @@ public class MtpDocumentsService extends Service { * Updates the foreground state of the service. * @return Whether the service is foreground or not. */ - private boolean updateForegroundState() { - final MtpDocumentsProvider provider = MtpDocumentsProvider.getInstance(); + private boolean updateForegroundState( + @Nullable int[] ids, @Nullable Notification[] notifications) { final Set<Integer> openedNotification = new HashSet<>(); - boolean hasForegroundNotification = false; - - final StatusBarNotification[] activeNotifications = - mNotificationManager.getActiveNotifications(); + final int size = ids != null ? ids.length : 0; + if (size != 0) { + Preconditions.checkArgument(ids != null); + Preconditions.checkArgument(notifications != null); + Preconditions.checkArgument(ids.length == notifications.length); + } - for (final MtpDeviceRecord record : provider.getOpenedDeviceRecordsCache()) { - openedNotification.add(record.deviceId); - if (!hasForegroundNotification) { - // Mark this service as foreground with the notification so that the process is not - // killed by the system while a MTP device is opened. - startForeground(record.deviceId, createNotification(this, record)); - hasForegroundNotification = true; + for (int i = 0; i < size; i++) { + if (i == 0) { + // Mark this service as foreground with the notification so that the process is + // not killed by the system while a MTP device is opened. + startForeground(ids[i], notifications[i]); } else { - // Only one notification can be shown as a foreground notification. We need to show - // the rest as normal notification. - mNotificationManager.notify(record.deviceId, createNotification(this, record)); + // Only one notification can be shown as a foreground notification. We need to + // show the rest as normal notification. + mNotificationManager.notify(ids[i], notifications[i]); } + openedNotification.add(ids[i]); } + final StatusBarNotification[] activeNotifications = + mNotificationManager.getActiveNotifications(); for (final StatusBarNotification notification : activeNotifications) { if (!openedNotification.contains(notification.getId())) { mNotificationManager.cancel(notification.getId()); } } - if (!hasForegroundNotification) { + if (size == 0) { // There is no opened device. stopForeground(true /* removeNotification */); stopSelf(); @@ -100,17 +113,12 @@ public class MtpDocumentsService extends Service { return true; } - public static Notification createNotification(Context context, MtpDeviceRecord device) { - final String title = context.getResources().getString( - R.string.accessing_notification_title, - device.name); - return new Notification.Builder(context) - .setLocalOnly(true) - .setContentTitle(title) - .setSmallIcon(com.android.internal.R.drawable.stat_sys_data_usb) - .setCategory(Notification.CATEGORY_SYSTEM) - .setPriority(Notification.PRIORITY_LOW) - .setFlag(Notification.FLAG_NO_CLEAR, true) - .build(); + private static @NonNull Notification[] castToNotifications(@NonNull Parcelable[] src) { + Preconditions.checkNotNull(src); + final Notification[] notifications = new Notification[src.length]; + for (int i = 0; i < src.length; i++) { + notifications[i] = (Notification) src[i]; + } + return notifications; } } diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/ServiceIntentSender.java b/packages/MtpDocumentsProvider/src/com/android/mtp/ServiceIntentSender.java index d8c3d357b563..c5292b878f37 100644 --- a/packages/MtpDocumentsProvider/src/com/android/mtp/ServiceIntentSender.java +++ b/packages/MtpDocumentsProvider/src/com/android/mtp/ServiceIntentSender.java @@ -16,11 +16,12 @@ package com.android.mtp; -import android.annotation.Nullable; -import android.app.NotificationManager; +import android.annotation.NonNull; +import android.app.Notification; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import com.android.internal.util.Preconditions; /** * Sends intent to MtpDocumentsService. @@ -34,20 +35,38 @@ class ServiceIntentSender { /** * Notify the change of opened device set. - * @param record If a new device is opened, pass the device record. If a device is closed, pass - * null. + * @param records List of opened devices. Can be empty. */ - void sendUpdateNotificationIntent(@Nullable MtpDeviceRecord record) { + void sendUpdateNotificationIntent(@NonNull MtpDeviceRecord[] records) { + Preconditions.checkNotNull(records); final Intent intent = new Intent(MtpDocumentsService.ACTION_UPDATE_NOTIFICATION); intent.setComponent(new ComponentName(mContext, MtpDocumentsService.class)); - final NotificationManager manager = mContext.getSystemService(NotificationManager.class); - if (record != null) { - manager.startServiceInForeground( - intent, - record.deviceId, - MtpDocumentsService.createNotification(mContext, record)); + if (records.length != 0) { + final int[] ids = new int[records.length]; + final Notification[] notifications = new Notification[records.length]; + for (int i = 0; i < records.length; i++) { + ids[i] = records[i].deviceId; + notifications[i] = createNotification(mContext, records[i]); + } + intent.putExtra(MtpDocumentsService.EXTRA_DEVICE_IDS, ids); + intent.putExtra(MtpDocumentsService.EXTRA_DEVICE_NOTIFICATIONS, notifications); + mContext.startForegroundService(intent); } else { mContext.startService(intent); } } + + private static Notification createNotification(Context context, MtpDeviceRecord device) { + final String title = context.getResources().getString( + R.string.accessing_notification_title, + device.name); + return new Notification.Builder(context) + .setLocalOnly(true) + .setContentTitle(title) + .setSmallIcon(com.android.internal.R.drawable.stat_sys_data_usb) + .setCategory(Notification.CATEGORY_SYSTEM) + .setPriority(Notification.PRIORITY_LOW) + .setFlag(Notification.FLAG_NO_CLEAR, true) + .build(); + } } diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestServiceIntentSender.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestServiceIntentSender.java index 74dd429d8af5..ed2dc3826a67 100644 --- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestServiceIntentSender.java +++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestServiceIntentSender.java @@ -16,13 +16,11 @@ package com.android.mtp; -import android.annotation.Nullable; - class TestServiceIntentSender extends ServiceIntentSender { TestServiceIntentSender() { super(null); } @Override - void sendUpdateNotificationIntent(@Nullable MtpDeviceRecord record) {} + void sendUpdateNotificationIntent(MtpDeviceRecord[] record) {} } |