San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 1 | /* |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 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 | |
Joe Onorato | fe4f3ae | 2010-06-04 11:25:26 -0700 | [diff] [blame] | 17 | package com.android.systemui.usb; |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 18 | |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 19 | import android.app.Notification; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 20 | import android.app.Notification.Action; |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 21 | import android.app.NotificationManager; |
| 22 | import android.app.PendingIntent; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 26 | import android.content.IntentFilter; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.PackageManager.MoveCallback; |
| 29 | import android.os.Handler; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 31 | import android.os.storage.DiskInfo; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 32 | import android.os.storage.StorageEventListener; |
| 33 | import android.os.storage.StorageManager; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 34 | import android.os.storage.VolumeInfo; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 35 | import android.os.storage.VolumeRecord; |
| 36 | import android.text.TextUtils; |
| 37 | import android.text.format.DateUtils; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 38 | import android.util.Log; |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 39 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 40 | import com.android.internal.R; |
John Spurlock | 3e309b2 | 2013-06-25 11:01:29 -0400 | [diff] [blame] | 41 | import com.android.systemui.SystemUI; |
| 42 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 43 | import java.util.List; |
| 44 | |
John Spurlock | 3e309b2 | 2013-06-25 11:01:29 -0400 | [diff] [blame] | 45 | public class StorageNotification extends SystemUI { |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 46 | private static final String TAG = "StorageNotification"; |
| 47 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 48 | private static final int PUBLIC_ID = 0x53505542; // SPUB |
| 49 | private static final int PRIVATE_ID = 0x53505256; // SPRV |
| 50 | private static final int DISK_ID = 0x5344534b; // SDSK |
| 51 | private static final int MOVE_ID = 0x534d4f56; // SMOV |
Daniel Sandler | c07907e | 2010-02-22 15:08:41 -0500 | [diff] [blame] | 52 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 53 | private static final String ACTION_SNOOZE_VOLUME = "com.android.systemui.action.SNOOZE_VOLUME"; |
| 54 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 55 | // TODO: delay some notifications to avoid bumpy fast operations |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 56 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 57 | private NotificationManager mNotificationManager; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 58 | private StorageManager mStorageManager; |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 59 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 60 | private final StorageEventListener mListener = new StorageEventListener() { |
| 61 | @Override |
| 62 | public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 63 | onVolumeStateChangedInternal(vol); |
John Spurlock | 3e309b2 | 2013-06-25 11:01:29 -0400 | [diff] [blame] | 64 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 65 | |
| 66 | @Override |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 67 | public void onVolumeMetadataChanged(String fsUuid) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 68 | // Avoid kicking notifications when getting early metadata before |
| 69 | // mounted. If already mounted, we're being kicked because of a |
| 70 | // nickname or init'ed change. |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 71 | final VolumeInfo vol = mStorageManager.findVolumeByUuid(fsUuid); |
| 72 | if (vol != null && vol.isMountedReadable()) { |
| 73 | onVolumeStateChangedInternal(vol); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 74 | } |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 75 | |
| 76 | final VolumeRecord rec = mStorageManager.findRecordByUuid(fsUuid); |
| 77 | if (rec == null) { |
| 78 | // Private volume was probably just forgotten |
| 79 | mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onDiskScanned(DiskInfo disk, int volumeCount) { |
| 85 | onDiskScannedInternal(disk, volumeCount); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
| 89 | private final BroadcastReceiver mSnoozeReceiver = new BroadcastReceiver() { |
| 90 | @Override |
| 91 | public void onReceive(Context context, Intent intent) { |
| 92 | // TODO: kick this onto background thread |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 93 | final String fsUuid = intent.getStringExtra(VolumeRecord.EXTRA_FS_UUID); |
| 94 | mStorageManager.setVolumeSnoozed(fsUuid, true); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | private final MoveCallback mMoveCallback = new MoveCallback() { |
| 99 | @Override |
| 100 | public void onStatusChanged(int moveId, String moveTitle, int status, long estMillis) { |
| 101 | if (PackageManager.isMoveStatusFinished(status)) { |
| 102 | onMoveFinished(moveId, moveTitle, status); |
| 103 | } else { |
| 104 | onMoveProgress(moveId, moveTitle, status, estMillis); |
| 105 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 106 | } |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 107 | }; |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 108 | |
John Spurlock | 3e309b2 | 2013-06-25 11:01:29 -0400 | [diff] [blame] | 109 | @Override |
| 110 | public void start() { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 111 | mNotificationManager = mContext.getSystemService(NotificationManager.class); |
John Spurlock | 3e309b2 | 2013-06-25 11:01:29 -0400 | [diff] [blame] | 112 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 113 | mStorageManager = mContext.getSystemService(StorageManager.class); |
| 114 | mStorageManager.registerListener(mListener); |
Daniel Sandler | 5b8743f | 2010-11-03 09:43:46 -0400 | [diff] [blame] | 115 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 116 | mContext.registerReceiver(mSnoozeReceiver, new IntentFilter(ACTION_SNOOZE_VOLUME), |
| 117 | android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null); |
| 118 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 119 | // Kick current state into place |
| 120 | final List<VolumeInfo> vols = mStorageManager.getVolumes(); |
| 121 | for (VolumeInfo vol : vols) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 122 | onVolumeStateChangedInternal(vol); |
| 123 | } |
| 124 | |
| 125 | mContext.getPackageManager().registerMoveCallback(mMoveCallback, new Handler()); |
| 126 | |
| 127 | updateMissingPrivateVolumes(); |
| 128 | } |
| 129 | |
| 130 | private void updateMissingPrivateVolumes() { |
| 131 | final List<VolumeRecord> recs = mStorageManager.getVolumeRecords(); |
| 132 | for (VolumeRecord rec : recs) { |
| 133 | if (rec.getType() != VolumeInfo.TYPE_PRIVATE) continue; |
| 134 | |
| 135 | final String fsUuid = rec.getFsUuid(); |
| 136 | final VolumeInfo info = mStorageManager.findVolumeByUuid(fsUuid); |
| 137 | if (info != null && info.isMountedWritable()) { |
| 138 | // Yay, private volume is here! |
| 139 | mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL); |
| 140 | |
| 141 | } else { |
| 142 | // Boo, annoy the user to reinsert the private volume |
| 143 | final CharSequence title = mContext.getString(R.string.ext_media_missing_title, |
| 144 | rec.getNickname()); |
| 145 | final CharSequence text = mContext.getString(R.string.ext_media_missing_message); |
| 146 | |
| 147 | final Notification notif = new Notification.Builder(mContext) |
| 148 | .setSmallIcon(R.drawable.stat_notify_sdcard) |
| 149 | .setColor(mContext.getColor(R.color.system_notification_accent_color)) |
| 150 | .setContentTitle(title) |
| 151 | .setContentText(text) |
| 152 | .setStyle(new Notification.BigTextStyle().bigText(text)) |
| 153 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 154 | .setLocalOnly(true) |
| 155 | .setContentIntent(buildForgetPendingIntent(rec)) |
| 156 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 157 | .setOngoing(true) |
| 158 | .build(); |
| 159 | |
| 160 | mNotificationManager.notifyAsUser(fsUuid, PRIVATE_ID, notif, UserHandle.ALL); |
| 161 | } |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 165 | private void onDiskScannedInternal(DiskInfo disk, int volumeCount) { |
| 166 | if (volumeCount == 0) { |
| 167 | // No supported volumes found, give user option to format |
| 168 | final CharSequence title = mContext.getString( |
| 169 | R.string.ext_media_unmountable_notification_title, disk.getDescription()); |
| 170 | final CharSequence text = mContext.getString( |
| 171 | R.string.ext_media_unmountable_notification_message, disk.getDescription()); |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 172 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 173 | final Notification notif = new Notification.Builder(mContext) |
| 174 | .setSmallIcon(getSmallIcon(disk, VolumeInfo.STATE_UNMOUNTABLE)) |
| 175 | .setColor(mContext.getColor(R.color.system_notification_accent_color)) |
| 176 | .setContentTitle(title) |
| 177 | .setContentText(text) |
| 178 | .setStyle(new Notification.BigTextStyle().bigText(text)) |
| 179 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 180 | .setLocalOnly(true) |
| 181 | .setContentIntent(buildInitPendingIntent(disk)) |
| 182 | .setCategory(Notification.CATEGORY_ERROR) |
| 183 | .build(); |
| 184 | |
| 185 | mNotificationManager.notifyAsUser(disk.getId(), DISK_ID, notif, UserHandle.ALL); |
| 186 | |
| 187 | } else { |
| 188 | // Yay, we have volumes! |
| 189 | mNotificationManager.cancelAsUser(disk.getId(), DISK_ID, UserHandle.ALL); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private void onVolumeStateChangedInternal(VolumeInfo vol) { |
| 194 | switch (vol.getType()) { |
| 195 | case VolumeInfo.TYPE_PRIVATE: |
| 196 | onPrivateVolumeStateChangedInternal(vol); |
| 197 | break; |
| 198 | case VolumeInfo.TYPE_PUBLIC: |
| 199 | onPublicVolumeStateChangedInternal(vol); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private void onPrivateVolumeStateChangedInternal(VolumeInfo vol) { |
| 205 | Log.d(TAG, "Notifying about private volume: " + vol.toString()); |
| 206 | |
| 207 | updateMissingPrivateVolumes(); |
| 208 | } |
| 209 | |
| 210 | private void onPublicVolumeStateChangedInternal(VolumeInfo vol) { |
| 211 | Log.d(TAG, "Notifying about public volume: " + vol.toString()); |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 212 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 213 | final Notification notif; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 214 | switch (vol.getState()) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 215 | case VolumeInfo.STATE_UNMOUNTED: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 216 | notif = onVolumeUnmounted(vol); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 217 | break; |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 218 | case VolumeInfo.STATE_CHECKING: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 219 | notif = onVolumeChecking(vol); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 220 | break; |
| 221 | case VolumeInfo.STATE_MOUNTED: |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 222 | case VolumeInfo.STATE_MOUNTED_READ_ONLY: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 223 | notif = onVolumeMounted(vol); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 224 | break; |
| 225 | case VolumeInfo.STATE_FORMATTING: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 226 | notif = onVolumeFormatting(vol); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 227 | break; |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 228 | case VolumeInfo.STATE_EJECTING: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 229 | notif = onVolumeEjecting(vol); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 230 | break; |
| 231 | case VolumeInfo.STATE_UNMOUNTABLE: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 232 | notif = onVolumeUnmountable(vol); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 233 | break; |
| 234 | case VolumeInfo.STATE_REMOVED: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 235 | notif = onVolumeRemoved(vol); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 236 | break; |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 237 | case VolumeInfo.STATE_BAD_REMOVAL: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 238 | notif = onVolumeBadRemoval(vol); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 239 | break; |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 240 | default: |
| 241 | notif = null; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | if (notif != null) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 246 | mNotificationManager.notifyAsUser(vol.getId(), PUBLIC_ID, notif, UserHandle.ALL); |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 247 | } else { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 248 | mNotificationManager.cancelAsUser(vol.getId(), PUBLIC_ID, UserHandle.ALL); |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 252 | private Notification onVolumeUnmounted(VolumeInfo vol) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 253 | // Ignored |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 254 | return null; |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 257 | private Notification onVolumeChecking(VolumeInfo vol) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 258 | final DiskInfo disk = vol.getDisk(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 259 | final CharSequence title = mContext.getString( |
| 260 | R.string.ext_media_checking_notification_title, disk.getDescription()); |
| 261 | final CharSequence text = mContext.getString( |
| 262 | R.string.ext_media_checking_notification_message, disk.getDescription()); |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 263 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 264 | return buildNotificationBuilder(vol, title, text) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 265 | .setCategory(Notification.CATEGORY_PROGRESS) |
| 266 | .setPriority(Notification.PRIORITY_LOW) |
| 267 | .setOngoing(true) |
| 268 | .build(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 269 | } |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 270 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 271 | private Notification onVolumeMounted(VolumeInfo vol) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 272 | final VolumeRecord rec = mStorageManager.findRecordByUuid(vol.getFsUuid()); |
| 273 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 274 | // Don't annoy when user dismissed in past |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 275 | if (rec.isSnoozed()) return null; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 276 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 277 | final DiskInfo disk = vol.getDisk(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 278 | if (disk.isAdoptable() && !rec.isInited()) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 279 | final CharSequence title = disk.getDescription(); |
| 280 | final CharSequence text = mContext.getString( |
| 281 | R.string.ext_media_new_notification_message, disk.getDescription()); |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 282 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 283 | final PendingIntent initAction = buildInitPendingIntent(vol); |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 284 | return buildNotificationBuilder(vol, title, text) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 285 | .addAction(new Action(0, mContext.getString(R.string.ext_media_init_action), |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 286 | initAction)) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 287 | .addAction(new Action(0, mContext.getString(R.string.ext_media_unmount_action), |
| 288 | buildUnmountPendingIntent(vol))) |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 289 | .setContentIntent(initAction) |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 290 | .setDeleteIntent(buildSnoozeIntent(vol)) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 291 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 292 | .build(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 293 | |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 294 | } else { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 295 | final CharSequence title = disk.getDescription(); |
| 296 | final CharSequence text = mContext.getString( |
| 297 | R.string.ext_media_ready_notification_message, disk.getDescription()); |
| 298 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 299 | final PendingIntent browseAction = buildBrowsePendingIntent(vol); |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 300 | return buildNotificationBuilder(vol, title, text) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 301 | .addAction(new Action(0, mContext.getString(R.string.ext_media_browse_action), |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 302 | browseAction)) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 303 | .addAction(new Action(0, mContext.getString(R.string.ext_media_unmount_action), |
| 304 | buildUnmountPendingIntent(vol))) |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 305 | .setContentIntent(browseAction) |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 306 | .setDeleteIntent(buildSnoozeIntent(vol)) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 307 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 308 | .setPriority(Notification.PRIORITY_LOW) |
| 309 | .build(); |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 310 | } |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 313 | private Notification onVolumeFormatting(VolumeInfo vol) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 314 | // Ignored |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 315 | return null; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 318 | private Notification onVolumeEjecting(VolumeInfo vol) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 319 | final DiskInfo disk = vol.getDisk(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 320 | final CharSequence title = mContext.getString( |
| 321 | R.string.ext_media_unmounting_notification_title, disk.getDescription()); |
| 322 | final CharSequence text = mContext.getString( |
| 323 | R.string.ext_media_unmounting_notification_message, disk.getDescription()); |
| 324 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 325 | return buildNotificationBuilder(vol, title, text) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 326 | .setCategory(Notification.CATEGORY_PROGRESS) |
| 327 | .setPriority(Notification.PRIORITY_LOW) |
| 328 | .setOngoing(true) |
| 329 | .build(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 332 | private Notification onVolumeUnmountable(VolumeInfo vol) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 333 | final DiskInfo disk = vol.getDisk(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 334 | final CharSequence title = mContext.getString( |
| 335 | R.string.ext_media_unmountable_notification_title, disk.getDescription()); |
| 336 | final CharSequence text = mContext.getString( |
| 337 | R.string.ext_media_unmountable_notification_message, disk.getDescription()); |
| 338 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 339 | return buildNotificationBuilder(vol, title, text) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 340 | .setContentIntent(buildDetailsPendingIntent(vol)) |
| 341 | .setCategory(Notification.CATEGORY_ERROR) |
| 342 | .build(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 345 | private Notification onVolumeRemoved(VolumeInfo vol) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 346 | if (!vol.isPrimary()) { |
| 347 | // Ignore non-primary media |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 348 | return null; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 351 | final DiskInfo disk = vol.getDisk(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 352 | final CharSequence title = mContext.getString( |
| 353 | R.string.ext_media_nomedia_notification_title, disk.getDescription()); |
| 354 | final CharSequence text = mContext.getString( |
| 355 | R.string.ext_media_nomedia_notification_message, disk.getDescription()); |
| 356 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 357 | return buildNotificationBuilder(vol, title, text) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 358 | .setCategory(Notification.CATEGORY_ERROR) |
| 359 | .build(); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 362 | private Notification onVolumeBadRemoval(VolumeInfo vol) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 363 | if (!vol.isPrimary()) { |
| 364 | // Ignore non-primary media |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 365 | return null; |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 368 | final DiskInfo disk = vol.getDisk(); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 369 | final CharSequence title = mContext.getString( |
| 370 | R.string.ext_media_badremoval_notification_title, disk.getDescription()); |
| 371 | final CharSequence text = mContext.getString( |
| 372 | R.string.ext_media_badremoval_notification_message, disk.getDescription()); |
| 373 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 374 | return buildNotificationBuilder(vol, title, text) |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 375 | .setCategory(Notification.CATEGORY_ERROR) |
| 376 | .build(); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 379 | private void onMoveProgress(int moveId, String moveTitle, int status, long estMillis) { |
| 380 | final CharSequence title; |
| 381 | if (!TextUtils.isEmpty(moveTitle)) { |
| 382 | title = mContext.getString(R.string.ext_media_move_specific_title, moveTitle); |
| 383 | } else { |
| 384 | title = mContext.getString(R.string.ext_media_move_title); |
| 385 | } |
| 386 | |
| 387 | final CharSequence text; |
| 388 | if (estMillis < 0) { |
| 389 | text = null; |
| 390 | } else { |
| 391 | text = DateUtils.formatDuration(estMillis); |
| 392 | } |
| 393 | |
| 394 | final Notification notif = new Notification.Builder(mContext) |
| 395 | .setSmallIcon(R.drawable.stat_notify_sdcard) |
| 396 | .setColor(mContext.getColor(R.color.system_notification_accent_color)) |
| 397 | .setContentTitle(title) |
| 398 | .setContentText(text) |
| 399 | .setStyle(new Notification.BigTextStyle().bigText(text)) |
| 400 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 401 | .setLocalOnly(true) |
| 402 | .setCategory(Notification.CATEGORY_PROGRESS) |
| 403 | .setPriority(Notification.PRIORITY_LOW) |
| 404 | .setProgress(100, status, false) |
| 405 | .setOngoing(true) |
| 406 | .build(); |
| 407 | |
| 408 | mNotificationManager.notifyAsUser(moveTitle, MOVE_ID, notif, UserHandle.ALL); |
| 409 | } |
| 410 | |
| 411 | private void onMoveFinished(int moveId, String moveTitle, int status) { |
| 412 | if (!TextUtils.isEmpty(moveTitle)) { |
| 413 | // We currently ignore finished app moves; just clear the last |
| 414 | // published progress |
| 415 | mNotificationManager.cancelAsUser(moveTitle, MOVE_ID, UserHandle.ALL); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | final VolumeInfo vol = mContext.getPackageManager().getPrimaryStorageCurrentVolume(); |
| 420 | final String descrip = mStorageManager.getBestVolumeDescription(vol); |
| 421 | |
| 422 | final CharSequence title; |
| 423 | final CharSequence text; |
| 424 | if (status == PackageManager.MOVE_SUCCEEDED) { |
| 425 | title = mContext.getString(R.string.ext_media_move_success_title); |
| 426 | text = mContext.getString(R.string.ext_media_move_success_message, descrip); |
| 427 | } else { |
| 428 | title = mContext.getString(R.string.ext_media_move_failure_title); |
| 429 | text = mContext.getString(R.string.ext_media_move_failure_message); |
| 430 | } |
| 431 | |
| 432 | final Notification notif = new Notification.Builder(mContext) |
| 433 | .setSmallIcon(R.drawable.stat_notify_sdcard) |
| 434 | .setColor(mContext.getColor(R.color.system_notification_accent_color)) |
| 435 | .setContentTitle(title) |
| 436 | .setContentText(text) |
| 437 | .setStyle(new Notification.BigTextStyle().bigText(text)) |
| 438 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 439 | .setLocalOnly(true) |
| 440 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 441 | .setPriority(Notification.PRIORITY_LOW) |
| 442 | .build(); |
| 443 | |
| 444 | mNotificationManager.notifyAsUser(moveTitle, MOVE_ID, notif, UserHandle.ALL); |
| 445 | } |
| 446 | |
| 447 | private int getSmallIcon(DiskInfo disk, int state) { |
| 448 | if (disk.isSd()) { |
| 449 | switch (state) { |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 450 | case VolumeInfo.STATE_CHECKING: |
| 451 | case VolumeInfo.STATE_EJECTING: |
| 452 | return R.drawable.stat_notify_sdcard_prepare; |
| 453 | default: |
| 454 | return R.drawable.stat_notify_sdcard; |
| 455 | } |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 456 | } else if (disk.isUsb()) { |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 457 | return R.drawable.stat_sys_data_usb; |
| 458 | } else { |
| 459 | return R.drawable.stat_notify_sdcard; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title, |
| 464 | CharSequence text) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 465 | return new Notification.Builder(mContext) |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 466 | .setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState())) |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 467 | .setColor(mContext.getColor(R.color.system_notification_accent_color)) |
| 468 | .setContentTitle(title) |
| 469 | .setContentText(text) |
| 470 | .setStyle(new Notification.BigTextStyle().bigText(text)) |
| 471 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 472 | .setLocalOnly(true); |
| 473 | } |
| 474 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 475 | private PendingIntent buildInitPendingIntent(DiskInfo disk) { |
| 476 | final Intent intent = new Intent(); |
| 477 | intent.setClassName("com.android.settings", |
| 478 | "com.android.settings.deviceinfo.StorageWizardInit"); |
| 479 | intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId()); |
| 480 | |
| 481 | final int requestKey = disk.getId().hashCode(); |
| 482 | return PendingIntent.getActivityAsUser(mContext, requestKey, intent, |
| 483 | PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 484 | } |
| 485 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 486 | private PendingIntent buildInitPendingIntent(VolumeInfo vol) { |
| 487 | final Intent intent = new Intent(); |
| 488 | intent.setClassName("com.android.settings", |
| 489 | "com.android.settings.deviceinfo.StorageWizardInit"); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 490 | intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId()); |
| 491 | |
| 492 | final int requestKey = vol.getId().hashCode(); |
| 493 | return PendingIntent.getActivityAsUser(mContext, requestKey, intent, |
| 494 | PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | private PendingIntent buildUnmountPendingIntent(VolumeInfo vol) { |
| 498 | final Intent intent = new Intent(); |
| 499 | intent.setClassName("com.android.settings", |
| 500 | "com.android.settings.deviceinfo.StorageUnmountReceiver"); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 501 | intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId()); |
| 502 | |
| 503 | final int requestKey = vol.getId().hashCode(); |
| 504 | return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent, |
| 505 | PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | private PendingIntent buildBrowsePendingIntent(VolumeInfo vol) { |
| 509 | final Intent intent = vol.buildBrowseIntent(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 510 | |
| 511 | final int requestKey = vol.getId().hashCode(); |
| 512 | return PendingIntent.getActivityAsUser(mContext, requestKey, intent, |
| 513 | PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | private PendingIntent buildDetailsPendingIntent(VolumeInfo vol) { |
| 517 | final Intent intent = new Intent(); |
| 518 | intent.setClassName("com.android.settings", |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 519 | "com.android.settings.Settings$PublicVolumeSettingsActivity"); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 520 | intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId()); |
| 521 | |
| 522 | final int requestKey = vol.getId().hashCode(); |
| 523 | return PendingIntent.getActivityAsUser(mContext, requestKey, intent, |
| 524 | PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 525 | } |
| 526 | |
| 527 | private PendingIntent buildSnoozeIntent(VolumeInfo vol) { |
| 528 | final Intent intent = new Intent(ACTION_SNOOZE_VOLUME); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 529 | intent.putExtra(VolumeRecord.EXTRA_FS_UUID, vol.getFsUuid()); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 530 | |
| 531 | final int requestKey = vol.getId().hashCode(); |
| 532 | return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent, |
| 533 | PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT); |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 534 | } |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame^] | 535 | |
| 536 | private PendingIntent buildForgetPendingIntent(VolumeRecord rec) { |
| 537 | final Intent intent = new Intent(); |
| 538 | intent.setClassName("com.android.settings", |
| 539 | "com.android.settings.Settings$PrivateVolumeForgetActivity"); |
| 540 | intent.putExtra(VolumeRecord.EXTRA_FS_UUID, rec.getFsUuid()); |
| 541 | |
| 542 | final int requestKey = rec.getFsUuid().hashCode(); |
| 543 | return PendingIntent.getActivityAsUser(mContext, requestKey, intent, |
| 544 | PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 545 | } |
San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame] | 546 | } |