blob: 36dd7279342890c180d3adb862afae48636e603e [file] [log] [blame]
San Mehat64e6a452010-02-04 20:53:48 -08001/*
Jeff Sharkey56bd3122015-04-14 10:30:34 -07002 * Copyright (C) 2015 The Android Open Source Project
San Mehat64e6a452010-02-04 20:53:48 -08003 *
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 Onoratofe4f3ae2010-06-04 11:25:26 -070017package com.android.systemui.usb;
San Mehat64e6a452010-02-04 20:53:48 -080018
Philip P. Moltmann75b230b2016-04-11 15:27:58 -070019import android.annotation.NonNull;
San Mehat64e6a452010-02-04 20:53:48 -080020import android.app.Notification;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070021import android.app.Notification.Action;
San Mehat64e6a452010-02-04 20:53:48 -080022import android.app.NotificationManager;
23import android.app.PendingIntent;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070024import android.content.BroadcastReceiver;
25import android.content.Context;
San Mehat64e6a452010-02-04 20:53:48 -080026import android.content.Intent;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070027import android.content.IntentFilter;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070028import android.content.pm.PackageManager;
29import android.content.pm.PackageManager.MoveCallback;
Jeff Sharkey50a05452015-04-29 11:24:52 -070030import android.os.Bundle;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070031import android.os.Handler;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070032import android.os.UserHandle;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070033import android.os.storage.DiskInfo;
San Mehatb1043402010-02-05 08:26:50 -080034import android.os.storage.StorageEventListener;
35import android.os.storage.StorageManager;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070036import android.os.storage.VolumeInfo;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070037import android.os.storage.VolumeRecord;
38import android.text.TextUtils;
39import android.text.format.DateUtils;
John Spurlockcd686b52013-06-05 10:13:46 -040040import android.util.Log;
Jeff Sharkey50a05452015-04-29 11:24:52 -070041import android.util.SparseArray;
San Mehat64e6a452010-02-04 20:53:48 -080042
Jeff Sharkey56bd3122015-04-14 10:30:34 -070043import com.android.internal.R;
John Spurlock3e309b22013-06-25 11:01:29 -040044import com.android.systemui.SystemUI;
45
Jeff Sharkey56bd3122015-04-14 10:30:34 -070046import java.util.List;
47
John Spurlock3e309b22013-06-25 11:01:29 -040048public class StorageNotification extends SystemUI {
San Mehat64e6a452010-02-04 20:53:48 -080049 private static final String TAG = "StorageNotification";
50
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070051 private static final int PUBLIC_ID = 0x53505542; // SPUB
52 private static final int PRIVATE_ID = 0x53505256; // SPRV
53 private static final int DISK_ID = 0x5344534b; // SDSK
54 private static final int MOVE_ID = 0x534d4f56; // SMOV
Daniel Sandlerc07907e2010-02-22 15:08:41 -050055
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070056 private static final String ACTION_SNOOZE_VOLUME = "com.android.systemui.action.SNOOZE_VOLUME";
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -070057 private static final String ACTION_FINISH_WIZARD = "com.android.systemui.action.FINISH_WIZARD";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070058
Jeff Sharkey56bd3122015-04-14 10:30:34 -070059 // TODO: delay some notifications to avoid bumpy fast operations
San Mehat64e6a452010-02-04 20:53:48 -080060
Jeff Sharkey56bd3122015-04-14 10:30:34 -070061 private NotificationManager mNotificationManager;
San Mehatb1043402010-02-05 08:26:50 -080062 private StorageManager mStorageManager;
San Mehat64e6a452010-02-04 20:53:48 -080063
Jeff Sharkey50a05452015-04-29 11:24:52 -070064 private static class MoveInfo {
65 public int moveId;
66 public Bundle extras;
67 public String packageName;
68 public String label;
69 public String volumeUuid;
70 }
71
72 private final SparseArray<MoveInfo> mMoves = new SparseArray<>();
73
Jeff Sharkey56bd3122015-04-14 10:30:34 -070074 private final StorageEventListener mListener = new StorageEventListener() {
75 @Override
76 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070077 onVolumeStateChangedInternal(vol);
John Spurlock3e309b22013-06-25 11:01:29 -040078 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070079
80 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -070081 public void onVolumeRecordChanged(VolumeRecord rec) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070082 // Avoid kicking notifications when getting early metadata before
83 // mounted. If already mounted, we're being kicked because of a
84 // nickname or init'ed change.
Jeff Sharkey50a05452015-04-29 11:24:52 -070085 final VolumeInfo vol = mStorageManager.findVolumeByUuid(rec.getFsUuid());
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070086 if (vol != null && vol.isMountedReadable()) {
87 onVolumeStateChangedInternal(vol);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070088 }
Jeff Sharkey50a05452015-04-29 11:24:52 -070089 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070090
Jeff Sharkey50a05452015-04-29 11:24:52 -070091 @Override
92 public void onVolumeForgotten(String fsUuid) {
93 // Stop annoying the user
94 mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070095 }
96
97 @Override
98 public void onDiskScanned(DiskInfo disk, int volumeCount) {
99 onDiskScannedInternal(disk, volumeCount);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700100 }
Philip P. Moltmann75b230b2016-04-11 15:27:58 -0700101
102 @Override
103 public void onDiskDestroyed(DiskInfo disk) {
104 onDiskDestroyedInternal(disk);
105 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700106 };
107
108 private final BroadcastReceiver mSnoozeReceiver = new BroadcastReceiver() {
109 @Override
110 public void onReceive(Context context, Intent intent) {
111 // TODO: kick this onto background thread
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700112 final String fsUuid = intent.getStringExtra(VolumeRecord.EXTRA_FS_UUID);
113 mStorageManager.setVolumeSnoozed(fsUuid, true);
114 }
115 };
116
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -0700117 private final BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
118 @Override
119 public void onReceive(Context context, Intent intent) {
120 // When finishing the adoption wizard, clean up any notifications
121 // for moving primary storage
122 mNotificationManager.cancelAsUser(null, MOVE_ID, UserHandle.ALL);
123 }
124 };
125
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700126 private final MoveCallback mMoveCallback = new MoveCallback() {
127 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -0700128 public void onCreated(int moveId, Bundle extras) {
129 final MoveInfo move = new MoveInfo();
130 move.moveId = moveId;
131 move.extras = extras;
132 if (extras != null) {
133 move.packageName = extras.getString(Intent.EXTRA_PACKAGE_NAME);
134 move.label = extras.getString(Intent.EXTRA_TITLE);
135 move.volumeUuid = extras.getString(VolumeRecord.EXTRA_FS_UUID);
136 }
137 mMoves.put(moveId, move);
138 }
139
140 @Override
141 public void onStatusChanged(int moveId, int status, long estMillis) {
142 final MoveInfo move = mMoves.get(moveId);
143 if (move == null) {
144 Log.w(TAG, "Ignoring unknown move " + moveId);
145 return;
146 }
147
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700148 if (PackageManager.isMoveStatusFinished(status)) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700149 onMoveFinished(move, status);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700150 } else {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700151 onMoveProgress(move, status, estMillis);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700152 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700153 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700154 };
San Mehat64e6a452010-02-04 20:53:48 -0800155
John Spurlock3e309b22013-06-25 11:01:29 -0400156 @Override
157 public void start() {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700158 mNotificationManager = mContext.getSystemService(NotificationManager.class);
John Spurlock3e309b22013-06-25 11:01:29 -0400159
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700160 mStorageManager = mContext.getSystemService(StorageManager.class);
161 mStorageManager.registerListener(mListener);
Daniel Sandler5b8743f2010-11-03 09:43:46 -0400162
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700163 mContext.registerReceiver(mSnoozeReceiver, new IntentFilter(ACTION_SNOOZE_VOLUME),
164 android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null);
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -0700165 mContext.registerReceiver(mFinishReceiver, new IntentFilter(ACTION_FINISH_WIZARD),
166 android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700167
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700168 // Kick current state into place
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -0700169 final List<DiskInfo> disks = mStorageManager.getDisks();
170 for (DiskInfo disk : disks) {
171 onDiskScannedInternal(disk, disk.volumeCount);
172 }
173
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700174 final List<VolumeInfo> vols = mStorageManager.getVolumes();
175 for (VolumeInfo vol : vols) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700176 onVolumeStateChangedInternal(vol);
177 }
178
179 mContext.getPackageManager().registerMoveCallback(mMoveCallback, new Handler());
180
181 updateMissingPrivateVolumes();
182 }
183
184 private void updateMissingPrivateVolumes() {
185 final List<VolumeRecord> recs = mStorageManager.getVolumeRecords();
186 for (VolumeRecord rec : recs) {
187 if (rec.getType() != VolumeInfo.TYPE_PRIVATE) continue;
188
189 final String fsUuid = rec.getFsUuid();
190 final VolumeInfo info = mStorageManager.findVolumeByUuid(fsUuid);
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700191 if ((info != null && info.isMountedWritable()) || rec.isSnoozed()) {
192 // Yay, private volume is here, or user snoozed
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700193 mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL);
194
195 } else {
196 // Boo, annoy the user to reinsert the private volume
197 final CharSequence title = mContext.getString(R.string.ext_media_missing_title,
198 rec.getNickname());
199 final CharSequence text = mContext.getString(R.string.ext_media_missing_message);
200
201 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700202 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700203 .setColor(mContext.getColor(R.color.system_notification_accent_color))
204 .setContentTitle(title)
205 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700206 .setContentIntent(buildForgetPendingIntent(rec))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700207 .setStyle(new Notification.BigTextStyle().bigText(text))
208 .setVisibility(Notification.VISIBILITY_PUBLIC)
209 .setLocalOnly(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700210 .setCategory(Notification.CATEGORY_SYSTEM)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700211 .setDeleteIntent(buildSnoozeIntent(fsUuid))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700212 .build();
213
214 mNotificationManager.notifyAsUser(fsUuid, PRIVATE_ID, notif, UserHandle.ALL);
215 }
San Mehat64e6a452010-02-04 20:53:48 -0800216 }
217 }
218
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700219 private void onDiskScannedInternal(DiskInfo disk, int volumeCount) {
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -0700220 if (volumeCount == 0 && disk.size > 0) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700221 // No supported volumes found, give user option to format
222 final CharSequence title = mContext.getString(
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700223 R.string.ext_media_unsupported_notification_title, disk.getDescription());
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700224 final CharSequence text = mContext.getString(
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700225 R.string.ext_media_unsupported_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800226
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700227 final Notification notif = new Notification.Builder(mContext)
228 .setSmallIcon(getSmallIcon(disk, VolumeInfo.STATE_UNMOUNTABLE))
229 .setColor(mContext.getColor(R.color.system_notification_accent_color))
230 .setContentTitle(title)
231 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700232 .setContentIntent(buildInitPendingIntent(disk))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700233 .setStyle(new Notification.BigTextStyle().bigText(text))
234 .setVisibility(Notification.VISIBILITY_PUBLIC)
235 .setLocalOnly(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700236 .setCategory(Notification.CATEGORY_ERROR)
237 .build();
238
239 mNotificationManager.notifyAsUser(disk.getId(), DISK_ID, notif, UserHandle.ALL);
240
241 } else {
242 // Yay, we have volumes!
243 mNotificationManager.cancelAsUser(disk.getId(), DISK_ID, UserHandle.ALL);
244 }
245 }
246
Philip P. Moltmann75b230b2016-04-11 15:27:58 -0700247 /**
248 * Remove all notifications for a disk when it goes away.
249 *
250 * @param disk The disk that went away.
251 */
252 private void onDiskDestroyedInternal(@NonNull DiskInfo disk) {
253 mNotificationManager.cancelAsUser(disk.getId(), DISK_ID, UserHandle.ALL);
254 }
255
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700256 private void onVolumeStateChangedInternal(VolumeInfo vol) {
257 switch (vol.getType()) {
258 case VolumeInfo.TYPE_PRIVATE:
259 onPrivateVolumeStateChangedInternal(vol);
260 break;
261 case VolumeInfo.TYPE_PUBLIC:
262 onPublicVolumeStateChangedInternal(vol);
263 break;
264 }
265 }
266
267 private void onPrivateVolumeStateChangedInternal(VolumeInfo vol) {
268 Log.d(TAG, "Notifying about private volume: " + vol.toString());
269
270 updateMissingPrivateVolumes();
271 }
272
273 private void onPublicVolumeStateChangedInternal(VolumeInfo vol) {
274 Log.d(TAG, "Notifying about public volume: " + vol.toString());
San Mehat64e6a452010-02-04 20:53:48 -0800275
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700276 final Notification notif;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700277 switch (vol.getState()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700278 case VolumeInfo.STATE_UNMOUNTED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700279 notif = onVolumeUnmounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700280 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700281 case VolumeInfo.STATE_CHECKING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700282 notif = onVolumeChecking(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700283 break;
284 case VolumeInfo.STATE_MOUNTED:
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700285 case VolumeInfo.STATE_MOUNTED_READ_ONLY:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700286 notif = onVolumeMounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700287 break;
288 case VolumeInfo.STATE_FORMATTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700289 notif = onVolumeFormatting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700290 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700291 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700292 notif = onVolumeEjecting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700293 break;
294 case VolumeInfo.STATE_UNMOUNTABLE:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700295 notif = onVolumeUnmountable(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700296 break;
297 case VolumeInfo.STATE_REMOVED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700298 notif = onVolumeRemoved(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700299 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700300 case VolumeInfo.STATE_BAD_REMOVAL:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700301 notif = onVolumeBadRemoval(vol);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700302 break;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700303 default:
304 notif = null;
305 break;
306 }
307
308 if (notif != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700309 mNotificationManager.notifyAsUser(vol.getId(), PUBLIC_ID, notif, UserHandle.ALL);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700310 } else {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700311 mNotificationManager.cancelAsUser(vol.getId(), PUBLIC_ID, UserHandle.ALL);
San Mehat64e6a452010-02-04 20:53:48 -0800312 }
313 }
314
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700315 private Notification onVolumeUnmounted(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700316 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700317 return null;
San Mehat64e6a452010-02-04 20:53:48 -0800318 }
319
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700320 private Notification onVolumeChecking(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700321 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700322 final CharSequence title = mContext.getString(
323 R.string.ext_media_checking_notification_title, disk.getDescription());
324 final CharSequence text = mContext.getString(
325 R.string.ext_media_checking_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800326
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700327 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700328 .setCategory(Notification.CATEGORY_PROGRESS)
329 .setPriority(Notification.PRIORITY_LOW)
330 .setOngoing(true)
331 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700332 }
San Mehat64e6a452010-02-04 20:53:48 -0800333
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700334 private Notification onVolumeMounted(VolumeInfo vol) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700335 final VolumeRecord rec = mStorageManager.findRecordByUuid(vol.getFsUuid());
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700336 final DiskInfo disk = vol.getDisk();
Makoto Onukib138cb22015-06-23 17:32:02 -0700337
338 // Don't annoy when user dismissed in past. (But make sure the disk is adoptable; we
339 // used to allow snoozing non-adoptable disks too.)
340 if (rec.isSnoozed() && disk.isAdoptable()) {
341 return null;
342 }
343
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700344 if (disk.isAdoptable() && !rec.isInited()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700345 final CharSequence title = disk.getDescription();
346 final CharSequence text = mContext.getString(
347 R.string.ext_media_new_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800348
Jeff Sharkey50a05452015-04-29 11:24:52 -0700349 final PendingIntent initIntent = buildInitPendingIntent(vol);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700350 return buildNotificationBuilder(vol, title, text)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700351 .addAction(new Action(R.drawable.ic_settings_24dp,
352 mContext.getString(R.string.ext_media_init_action), initIntent))
353 .addAction(new Action(R.drawable.ic_eject_24dp,
354 mContext.getString(R.string.ext_media_unmount_action),
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700355 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700356 .setContentIntent(initIntent)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700357 .setDeleteIntent(buildSnoozeIntent(vol.getFsUuid()))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700358 .setCategory(Notification.CATEGORY_SYSTEM)
359 .build();
John Spurlock209bede2013-07-17 12:23:27 -0400360
San Mehat64e6a452010-02-04 20:53:48 -0800361 } else {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700362 final CharSequence title = disk.getDescription();
363 final CharSequence text = mContext.getString(
364 R.string.ext_media_ready_notification_message, disk.getDescription());
365
Jeff Sharkey50a05452015-04-29 11:24:52 -0700366 final PendingIntent browseIntent = buildBrowsePendingIntent(vol);
Makoto Onukib138cb22015-06-23 17:32:02 -0700367 final Notification.Builder builder = buildNotificationBuilder(vol, title, text)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700368 .addAction(new Action(R.drawable.ic_folder_24dp,
369 mContext.getString(R.string.ext_media_browse_action),
Jeff Sharkey50a05452015-04-29 11:24:52 -0700370 browseIntent))
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700371 .addAction(new Action(R.drawable.ic_eject_24dp,
372 mContext.getString(R.string.ext_media_unmount_action),
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700373 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700374 .setContentIntent(browseIntent)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700375 .setCategory(Notification.CATEGORY_SYSTEM)
Makoto Onukib138cb22015-06-23 17:32:02 -0700376 .setPriority(Notification.PRIORITY_LOW);
377 // Non-adoptable disks can't be snoozed.
378 if (disk.isAdoptable()) {
379 builder.setDeleteIntent(buildSnoozeIntent(vol.getFsUuid()));
380 }
381
382 return builder.build();
San Mehat64e6a452010-02-04 20:53:48 -0800383 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700384 }
385
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700386 private Notification onVolumeFormatting(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700387 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700388 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700389 }
390
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700391 private Notification onVolumeEjecting(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700392 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700393 final CharSequence title = mContext.getString(
394 R.string.ext_media_unmounting_notification_title, disk.getDescription());
395 final CharSequence text = mContext.getString(
396 R.string.ext_media_unmounting_notification_message, disk.getDescription());
397
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700398 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700399 .setCategory(Notification.CATEGORY_PROGRESS)
400 .setPriority(Notification.PRIORITY_LOW)
401 .setOngoing(true)
402 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700403 }
404
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700405 private Notification onVolumeUnmountable(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700406 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700407 final CharSequence title = mContext.getString(
408 R.string.ext_media_unmountable_notification_title, disk.getDescription());
409 final CharSequence text = mContext.getString(
410 R.string.ext_media_unmountable_notification_message, disk.getDescription());
411
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700412 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700413 .setContentIntent(buildInitPendingIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700414 .setCategory(Notification.CATEGORY_ERROR)
415 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700416 }
417
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700418 private Notification onVolumeRemoved(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700419 if (!vol.isPrimary()) {
420 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700421 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700422 }
423
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700424 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700425 final CharSequence title = mContext.getString(
426 R.string.ext_media_nomedia_notification_title, disk.getDescription());
427 final CharSequence text = mContext.getString(
428 R.string.ext_media_nomedia_notification_message, disk.getDescription());
429
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700430 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700431 .setCategory(Notification.CATEGORY_ERROR)
432 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700433 }
434
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700435 private Notification onVolumeBadRemoval(VolumeInfo vol) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700436 if (!vol.isPrimary()) {
437 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700438 return null;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700439 }
440
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700441 final DiskInfo disk = vol.getDisk();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700442 final CharSequence title = mContext.getString(
443 R.string.ext_media_badremoval_notification_title, disk.getDescription());
444 final CharSequence text = mContext.getString(
445 R.string.ext_media_badremoval_notification_message, disk.getDescription());
446
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700447 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700448 .setCategory(Notification.CATEGORY_ERROR)
449 .build();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700450 }
451
Jeff Sharkey50a05452015-04-29 11:24:52 -0700452 private void onMoveProgress(MoveInfo move, int status, long estMillis) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700453 final CharSequence title;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700454 if (!TextUtils.isEmpty(move.label)) {
455 title = mContext.getString(R.string.ext_media_move_specific_title, move.label);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700456 } else {
457 title = mContext.getString(R.string.ext_media_move_title);
458 }
459
460 final CharSequence text;
461 if (estMillis < 0) {
462 text = null;
463 } else {
464 text = DateUtils.formatDuration(estMillis);
465 }
466
Jeff Sharkey50a05452015-04-29 11:24:52 -0700467 final PendingIntent intent;
468 if (move.packageName != null) {
469 intent = buildWizardMovePendingIntent(move);
470 } else {
471 intent = buildWizardMigratePendingIntent(move);
472 }
473
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700474 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700475 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700476 .setColor(mContext.getColor(R.color.system_notification_accent_color))
477 .setContentTitle(title)
478 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700479 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700480 .setStyle(new Notification.BigTextStyle().bigText(text))
481 .setVisibility(Notification.VISIBILITY_PUBLIC)
482 .setLocalOnly(true)
483 .setCategory(Notification.CATEGORY_PROGRESS)
484 .setPriority(Notification.PRIORITY_LOW)
485 .setProgress(100, status, false)
486 .setOngoing(true)
487 .build();
488
Jeff Sharkey50a05452015-04-29 11:24:52 -0700489 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700490 }
491
Jeff Sharkey50a05452015-04-29 11:24:52 -0700492 private void onMoveFinished(MoveInfo move, int status) {
493 if (move.packageName != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700494 // We currently ignore finished app moves; just clear the last
495 // published progress
Jeff Sharkey50a05452015-04-29 11:24:52 -0700496 mNotificationManager.cancelAsUser(move.packageName, MOVE_ID, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700497 return;
498 }
499
Jeff Sharkey50a05452015-04-29 11:24:52 -0700500 final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
501 final String descrip = mStorageManager.getBestVolumeDescription(privateVol);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700502
503 final CharSequence title;
504 final CharSequence text;
505 if (status == PackageManager.MOVE_SUCCEEDED) {
506 title = mContext.getString(R.string.ext_media_move_success_title);
507 text = mContext.getString(R.string.ext_media_move_success_message, descrip);
508 } else {
509 title = mContext.getString(R.string.ext_media_move_failure_title);
510 text = mContext.getString(R.string.ext_media_move_failure_message);
511 }
512
Jeff Sharkey50a05452015-04-29 11:24:52 -0700513 // Jump back into the wizard flow if we moved to a real disk
514 final PendingIntent intent;
515 if (privateVol != null && privateVol.getDisk() != null) {
516 intent = buildWizardReadyPendingIntent(privateVol.getDisk());
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700517 } else if (privateVol != null) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700518 intent = buildVolumeSettingsPendingIntent(privateVol);
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700519 } else {
520 intent = null;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700521 }
522
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700523 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700524 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700525 .setColor(mContext.getColor(R.color.system_notification_accent_color))
526 .setContentTitle(title)
527 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700528 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700529 .setStyle(new Notification.BigTextStyle().bigText(text))
530 .setVisibility(Notification.VISIBILITY_PUBLIC)
531 .setLocalOnly(true)
532 .setCategory(Notification.CATEGORY_SYSTEM)
533 .setPriority(Notification.PRIORITY_LOW)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700534 .setAutoCancel(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700535 .build();
536
Jeff Sharkey50a05452015-04-29 11:24:52 -0700537 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700538 }
539
540 private int getSmallIcon(DiskInfo disk, int state) {
541 if (disk.isSd()) {
542 switch (state) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700543 case VolumeInfo.STATE_CHECKING:
544 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700545 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700546 default:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700547 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700548 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700549 } else if (disk.isUsb()) {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700550 return R.drawable.ic_usb_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700551 } else {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700552 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700553 }
554 }
555
556 private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title,
557 CharSequence text) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700558 return new Notification.Builder(mContext)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700559 .setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState()))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700560 .setColor(mContext.getColor(R.color.system_notification_accent_color))
561 .setContentTitle(title)
562 .setContentText(text)
563 .setStyle(new Notification.BigTextStyle().bigText(text))
564 .setVisibility(Notification.VISIBILITY_PUBLIC)
565 .setLocalOnly(true);
566 }
567
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700568 private PendingIntent buildInitPendingIntent(DiskInfo disk) {
569 final Intent intent = new Intent();
570 intent.setClassName("com.android.settings",
571 "com.android.settings.deviceinfo.StorageWizardInit");
572 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
573
574 final int requestKey = disk.getId().hashCode();
575 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
576 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
577 }
578
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700579 private PendingIntent buildInitPendingIntent(VolumeInfo vol) {
580 final Intent intent = new Intent();
581 intent.setClassName("com.android.settings",
582 "com.android.settings.deviceinfo.StorageWizardInit");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700583 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
584
585 final int requestKey = vol.getId().hashCode();
586 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
587 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700588 }
589
590 private PendingIntent buildUnmountPendingIntent(VolumeInfo vol) {
591 final Intent intent = new Intent();
592 intent.setClassName("com.android.settings",
593 "com.android.settings.deviceinfo.StorageUnmountReceiver");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700594 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
595
596 final int requestKey = vol.getId().hashCode();
597 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
598 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700599 }
600
601 private PendingIntent buildBrowsePendingIntent(VolumeInfo vol) {
602 final Intent intent = vol.buildBrowseIntent();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700603
604 final int requestKey = vol.getId().hashCode();
605 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
606 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700607 }
608
Jeff Sharkey50a05452015-04-29 11:24:52 -0700609 private PendingIntent buildVolumeSettingsPendingIntent(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700610 final Intent intent = new Intent();
Jeff Sharkey50a05452015-04-29 11:24:52 -0700611 switch (vol.getType()) {
612 case VolumeInfo.TYPE_PRIVATE:
613 intent.setClassName("com.android.settings",
614 "com.android.settings.Settings$PrivateVolumeSettingsActivity");
615 break;
616 case VolumeInfo.TYPE_PUBLIC:
617 intent.setClassName("com.android.settings",
618 "com.android.settings.Settings$PublicVolumeSettingsActivity");
619 break;
620 default:
621 return null;
622 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700623 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
624
625 final int requestKey = vol.getId().hashCode();
626 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
627 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
628 }
629
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700630 private PendingIntent buildSnoozeIntent(String fsUuid) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700631 final Intent intent = new Intent(ACTION_SNOOZE_VOLUME);
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700632 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, fsUuid);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700633
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700634 final int requestKey = fsUuid.hashCode();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700635 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
636 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
San Mehat64e6a452010-02-04 20:53:48 -0800637 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700638
639 private PendingIntent buildForgetPendingIntent(VolumeRecord rec) {
640 final Intent intent = new Intent();
641 intent.setClassName("com.android.settings",
642 "com.android.settings.Settings$PrivateVolumeForgetActivity");
643 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, rec.getFsUuid());
644
645 final int requestKey = rec.getFsUuid().hashCode();
646 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
647 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
648 }
Jeff Sharkey50a05452015-04-29 11:24:52 -0700649
650 private PendingIntent buildWizardMigratePendingIntent(MoveInfo move) {
651 final Intent intent = new Intent();
652 intent.setClassName("com.android.settings",
653 "com.android.settings.deviceinfo.StorageWizardMigrateProgress");
654 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
655
656 final VolumeInfo vol = mStorageManager.findVolumeByQualifiedUuid(move.volumeUuid);
Suprabh Shuklaaf6c4192016-04-27 14:05:54 -0700657 if (vol != null) {
658 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
659 }
Jeff Sharkey50a05452015-04-29 11:24:52 -0700660 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
661 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
662 }
663
664 private PendingIntent buildWizardMovePendingIntent(MoveInfo move) {
665 final Intent intent = new Intent();
666 intent.setClassName("com.android.settings",
667 "com.android.settings.deviceinfo.StorageWizardMoveProgress");
668 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
669
670 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
671 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
672 }
673
674 private PendingIntent buildWizardReadyPendingIntent(DiskInfo disk) {
675 final Intent intent = new Intent();
676 intent.setClassName("com.android.settings",
677 "com.android.settings.deviceinfo.StorageWizardReady");
678 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
679
680 final int requestKey = disk.getId().hashCode();
681 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
682 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
683 }
San Mehat64e6a452010-02-04 20:53:48 -0800684}