Loading packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java +2 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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() { Loading packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsService.java +40 −32 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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; } Loading @@ -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(); Loading @@ -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; } } packages/MtpDocumentsProvider/src/com/android/mtp/ServiceIntentSender.java +30 −11 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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(); } } packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestServiceIntentSender.java +1 −3 Original line number Diff line number Diff line Loading @@ -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) {} } Loading
packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java +2 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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() { Loading
packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsService.java +40 −32 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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; } Loading @@ -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(); Loading @@ -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; } }
packages/MtpDocumentsProvider/src/com/android/mtp/ServiceIntentSender.java +30 −11 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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(); } }
packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestServiceIntentSender.java +1 −3 Original line number Diff line number Diff line Loading @@ -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) {} }