blob: 23813d11919c171c276bb89e25ec526a90f5ff14 [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
San Mehat64e6a452010-02-04 20:53:48 -080019import android.app.Notification;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070020import android.app.Notification.Action;
San Mehat64e6a452010-02-04 20:53:48 -080021import android.app.NotificationManager;
22import android.app.PendingIntent;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070023import android.content.BroadcastReceiver;
24import android.content.Context;
San Mehat64e6a452010-02-04 20:53:48 -080025import android.content.Intent;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070026import android.content.IntentFilter;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070027import android.content.pm.PackageManager;
28import android.content.pm.PackageManager.MoveCallback;
Jeff Sharkey50a05452015-04-29 11:24:52 -070029import android.os.Bundle;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070030import android.os.Handler;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070031import android.os.UserHandle;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070032import android.os.storage.DiskInfo;
San Mehatb1043402010-02-05 08:26:50 -080033import android.os.storage.StorageEventListener;
34import android.os.storage.StorageManager;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070035import android.os.storage.VolumeInfo;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070036import android.os.storage.VolumeRecord;
37import android.text.TextUtils;
38import android.text.format.DateUtils;
John Spurlockcd686b52013-06-05 10:13:46 -040039import android.util.Log;
Jeff Sharkey50a05452015-04-29 11:24:52 -070040import android.util.SparseArray;
San Mehat64e6a452010-02-04 20:53:48 -080041
Jeff Sharkey56bd3122015-04-14 10:30:34 -070042import com.android.internal.R;
John Spurlock3e309b22013-06-25 11:01:29 -040043import com.android.systemui.SystemUI;
44
Jeff Sharkey56bd3122015-04-14 10:30:34 -070045import java.util.List;
46
John Spurlock3e309b22013-06-25 11:01:29 -040047public class StorageNotification extends SystemUI {
San Mehat64e6a452010-02-04 20:53:48 -080048 private static final String TAG = "StorageNotification";
49
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070050 private static final int PUBLIC_ID = 0x53505542; // SPUB
51 private static final int PRIVATE_ID = 0x53505256; // SPRV
52 private static final int DISK_ID = 0x5344534b; // SDSK
53 private static final int MOVE_ID = 0x534d4f56; // SMOV
Daniel Sandlerc07907e2010-02-22 15:08:41 -050054
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070055 private static final String ACTION_SNOOZE_VOLUME = "com.android.systemui.action.SNOOZE_VOLUME";
56
Jeff Sharkey56bd3122015-04-14 10:30:34 -070057 // TODO: delay some notifications to avoid bumpy fast operations
San Mehat64e6a452010-02-04 20:53:48 -080058
Jeff Sharkey56bd3122015-04-14 10:30:34 -070059 private NotificationManager mNotificationManager;
San Mehatb1043402010-02-05 08:26:50 -080060 private StorageManager mStorageManager;
San Mehat64e6a452010-02-04 20:53:48 -080061
Jeff Sharkey50a05452015-04-29 11:24:52 -070062 private static class MoveInfo {
63 public int moveId;
64 public Bundle extras;
65 public String packageName;
66 public String label;
67 public String volumeUuid;
68 }
69
70 private final SparseArray<MoveInfo> mMoves = new SparseArray<>();
71
Jeff Sharkey56bd3122015-04-14 10:30:34 -070072 private final StorageEventListener mListener = new StorageEventListener() {
73 @Override
74 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070075 onVolumeStateChangedInternal(vol);
John Spurlock3e309b22013-06-25 11:01:29 -040076 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070077
78 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -070079 public void onVolumeRecordChanged(VolumeRecord rec) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070080 // Avoid kicking notifications when getting early metadata before
81 // mounted. If already mounted, we're being kicked because of a
82 // nickname or init'ed change.
Jeff Sharkey50a05452015-04-29 11:24:52 -070083 final VolumeInfo vol = mStorageManager.findVolumeByUuid(rec.getFsUuid());
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070084 if (vol != null && vol.isMountedReadable()) {
85 onVolumeStateChangedInternal(vol);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070086 }
Jeff Sharkey50a05452015-04-29 11:24:52 -070087 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070088
Jeff Sharkey50a05452015-04-29 11:24:52 -070089 @Override
90 public void onVolumeForgotten(String fsUuid) {
91 // Stop annoying the user
92 mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070093 }
94
95 @Override
96 public void onDiskScanned(DiskInfo disk, int volumeCount) {
97 onDiskScannedInternal(disk, volumeCount);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070098 }
99 };
100
101 private final BroadcastReceiver mSnoozeReceiver = new BroadcastReceiver() {
102 @Override
103 public void onReceive(Context context, Intent intent) {
104 // TODO: kick this onto background thread
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700105 final String fsUuid = intent.getStringExtra(VolumeRecord.EXTRA_FS_UUID);
106 mStorageManager.setVolumeSnoozed(fsUuid, true);
107 }
108 };
109
110 private final MoveCallback mMoveCallback = new MoveCallback() {
111 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -0700112 public void onCreated(int moveId, Bundle extras) {
113 final MoveInfo move = new MoveInfo();
114 move.moveId = moveId;
115 move.extras = extras;
116 if (extras != null) {
117 move.packageName = extras.getString(Intent.EXTRA_PACKAGE_NAME);
118 move.label = extras.getString(Intent.EXTRA_TITLE);
119 move.volumeUuid = extras.getString(VolumeRecord.EXTRA_FS_UUID);
120 }
121 mMoves.put(moveId, move);
122 }
123
124 @Override
125 public void onStatusChanged(int moveId, int status, long estMillis) {
126 final MoveInfo move = mMoves.get(moveId);
127 if (move == null) {
128 Log.w(TAG, "Ignoring unknown move " + moveId);
129 return;
130 }
131
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700132 if (PackageManager.isMoveStatusFinished(status)) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700133 onMoveFinished(move, status);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700134 } else {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700135 onMoveProgress(move, status, estMillis);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700136 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700137 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700138 };
San Mehat64e6a452010-02-04 20:53:48 -0800139
John Spurlock3e309b22013-06-25 11:01:29 -0400140 @Override
141 public void start() {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700142 mNotificationManager = mContext.getSystemService(NotificationManager.class);
John Spurlock3e309b22013-06-25 11:01:29 -0400143
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700144 mStorageManager = mContext.getSystemService(StorageManager.class);
145 mStorageManager.registerListener(mListener);
Daniel Sandler5b8743f2010-11-03 09:43:46 -0400146
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700147 mContext.registerReceiver(mSnoozeReceiver, new IntentFilter(ACTION_SNOOZE_VOLUME),
148 android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null);
149
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700150 // Kick current state into place
151 final List<VolumeInfo> vols = mStorageManager.getVolumes();
152 for (VolumeInfo vol : vols) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700153 onVolumeStateChangedInternal(vol);
154 }
155
156 mContext.getPackageManager().registerMoveCallback(mMoveCallback, new Handler());
157
158 updateMissingPrivateVolumes();
159 }
160
161 private void updateMissingPrivateVolumes() {
162 final List<VolumeRecord> recs = mStorageManager.getVolumeRecords();
163 for (VolumeRecord rec : recs) {
164 if (rec.getType() != VolumeInfo.TYPE_PRIVATE) continue;
165
166 final String fsUuid = rec.getFsUuid();
167 final VolumeInfo info = mStorageManager.findVolumeByUuid(fsUuid);
168 if (info != null && info.isMountedWritable()) {
169 // Yay, private volume is here!
170 mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL);
171
172 } else {
173 // Boo, annoy the user to reinsert the private volume
174 final CharSequence title = mContext.getString(R.string.ext_media_missing_title,
175 rec.getNickname());
176 final CharSequence text = mContext.getString(R.string.ext_media_missing_message);
177
178 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700179 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700180 .setColor(mContext.getColor(R.color.system_notification_accent_color))
181 .setContentTitle(title)
182 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700183 .setContentIntent(buildForgetPendingIntent(rec))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700184 .setStyle(new Notification.BigTextStyle().bigText(text))
185 .setVisibility(Notification.VISIBILITY_PUBLIC)
186 .setLocalOnly(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700187 .setCategory(Notification.CATEGORY_SYSTEM)
188 .setOngoing(true)
189 .build();
190
191 mNotificationManager.notifyAsUser(fsUuid, PRIVATE_ID, notif, UserHandle.ALL);
192 }
San Mehat64e6a452010-02-04 20:53:48 -0800193 }
194 }
195
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700196 private void onDiskScannedInternal(DiskInfo disk, int volumeCount) {
197 if (volumeCount == 0) {
198 // No supported volumes found, give user option to format
199 final CharSequence title = mContext.getString(
200 R.string.ext_media_unmountable_notification_title, disk.getDescription());
201 final CharSequence text = mContext.getString(
202 R.string.ext_media_unmountable_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800203
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700204 final Notification notif = new Notification.Builder(mContext)
205 .setSmallIcon(getSmallIcon(disk, VolumeInfo.STATE_UNMOUNTABLE))
206 .setColor(mContext.getColor(R.color.system_notification_accent_color))
207 .setContentTitle(title)
208 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700209 .setContentIntent(buildInitPendingIntent(disk))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700210 .setStyle(new Notification.BigTextStyle().bigText(text))
211 .setVisibility(Notification.VISIBILITY_PUBLIC)
212 .setLocalOnly(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700213 .setCategory(Notification.CATEGORY_ERROR)
214 .build();
215
216 mNotificationManager.notifyAsUser(disk.getId(), DISK_ID, notif, UserHandle.ALL);
217
218 } else {
219 // Yay, we have volumes!
220 mNotificationManager.cancelAsUser(disk.getId(), DISK_ID, UserHandle.ALL);
221 }
222 }
223
224 private void onVolumeStateChangedInternal(VolumeInfo vol) {
225 switch (vol.getType()) {
226 case VolumeInfo.TYPE_PRIVATE:
227 onPrivateVolumeStateChangedInternal(vol);
228 break;
229 case VolumeInfo.TYPE_PUBLIC:
230 onPublicVolumeStateChangedInternal(vol);
231 break;
232 }
233 }
234
235 private void onPrivateVolumeStateChangedInternal(VolumeInfo vol) {
236 Log.d(TAG, "Notifying about private volume: " + vol.toString());
237
238 updateMissingPrivateVolumes();
239 }
240
241 private void onPublicVolumeStateChangedInternal(VolumeInfo vol) {
242 Log.d(TAG, "Notifying about public volume: " + vol.toString());
San Mehat64e6a452010-02-04 20:53:48 -0800243
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700244 final Notification notif;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700245 switch (vol.getState()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700246 case VolumeInfo.STATE_UNMOUNTED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700247 notif = onVolumeUnmounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700248 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700249 case VolumeInfo.STATE_CHECKING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700250 notif = onVolumeChecking(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700251 break;
252 case VolumeInfo.STATE_MOUNTED:
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700253 case VolumeInfo.STATE_MOUNTED_READ_ONLY:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700254 notif = onVolumeMounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700255 break;
256 case VolumeInfo.STATE_FORMATTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700257 notif = onVolumeFormatting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700258 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700259 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700260 notif = onVolumeEjecting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700261 break;
262 case VolumeInfo.STATE_UNMOUNTABLE:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700263 notif = onVolumeUnmountable(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700264 break;
265 case VolumeInfo.STATE_REMOVED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700266 notif = onVolumeRemoved(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700267 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700268 case VolumeInfo.STATE_BAD_REMOVAL:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700269 notif = onVolumeBadRemoval(vol);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700270 break;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700271 default:
272 notif = null;
273 break;
274 }
275
276 if (notif != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700277 mNotificationManager.notifyAsUser(vol.getId(), PUBLIC_ID, notif, UserHandle.ALL);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700278 } else {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700279 mNotificationManager.cancelAsUser(vol.getId(), PUBLIC_ID, UserHandle.ALL);
San Mehat64e6a452010-02-04 20:53:48 -0800280 }
281 }
282
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700283 private Notification onVolumeUnmounted(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700284 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700285 return null;
San Mehat64e6a452010-02-04 20:53:48 -0800286 }
287
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700288 private Notification onVolumeChecking(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700289 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700290 final CharSequence title = mContext.getString(
291 R.string.ext_media_checking_notification_title, disk.getDescription());
292 final CharSequence text = mContext.getString(
293 R.string.ext_media_checking_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800294
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700295 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700296 .setCategory(Notification.CATEGORY_PROGRESS)
297 .setPriority(Notification.PRIORITY_LOW)
298 .setOngoing(true)
299 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700300 }
San Mehat64e6a452010-02-04 20:53:48 -0800301
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700302 private Notification onVolumeMounted(VolumeInfo vol) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700303 final VolumeRecord rec = mStorageManager.findRecordByUuid(vol.getFsUuid());
304
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700305 // Don't annoy when user dismissed in past
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700306 if (rec.isSnoozed()) return null;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700307
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700308 final DiskInfo disk = vol.getDisk();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700309 if (disk.isAdoptable() && !rec.isInited()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700310 final CharSequence title = disk.getDescription();
311 final CharSequence text = mContext.getString(
312 R.string.ext_media_new_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800313
Jeff Sharkey50a05452015-04-29 11:24:52 -0700314 final PendingIntent initIntent = buildInitPendingIntent(vol);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700315 return buildNotificationBuilder(vol, title, text)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700316 .addAction(new Action(R.drawable.ic_settings_24dp,
317 mContext.getString(R.string.ext_media_init_action), initIntent))
318 .addAction(new Action(R.drawable.ic_eject_24dp,
319 mContext.getString(R.string.ext_media_unmount_action),
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700320 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700321 .setContentIntent(initIntent)
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700322 .setDeleteIntent(buildSnoozeIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700323 .setCategory(Notification.CATEGORY_SYSTEM)
324 .build();
John Spurlock209bede2013-07-17 12:23:27 -0400325
San Mehat64e6a452010-02-04 20:53:48 -0800326 } else {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700327 final CharSequence title = disk.getDescription();
328 final CharSequence text = mContext.getString(
329 R.string.ext_media_ready_notification_message, disk.getDescription());
330
Jeff Sharkey50a05452015-04-29 11:24:52 -0700331 final PendingIntent browseIntent = buildBrowsePendingIntent(vol);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700332 return buildNotificationBuilder(vol, title, text)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700333 .addAction(new Action(R.drawable.ic_folder_24dp,
334 mContext.getString(R.string.ext_media_browse_action),
Jeff Sharkey50a05452015-04-29 11:24:52 -0700335 browseIntent))
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700336 .addAction(new Action(R.drawable.ic_eject_24dp,
337 mContext.getString(R.string.ext_media_unmount_action),
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700338 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700339 .setContentIntent(browseIntent)
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700340 .setDeleteIntent(buildSnoozeIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700341 .setCategory(Notification.CATEGORY_SYSTEM)
342 .setPriority(Notification.PRIORITY_LOW)
343 .build();
San Mehat64e6a452010-02-04 20:53:48 -0800344 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700345 }
346
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700347 private Notification onVolumeFormatting(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700348 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700349 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700350 }
351
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700352 private Notification onVolumeEjecting(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700353 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700354 final CharSequence title = mContext.getString(
355 R.string.ext_media_unmounting_notification_title, disk.getDescription());
356 final CharSequence text = mContext.getString(
357 R.string.ext_media_unmounting_notification_message, disk.getDescription());
358
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700359 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700360 .setCategory(Notification.CATEGORY_PROGRESS)
361 .setPriority(Notification.PRIORITY_LOW)
362 .setOngoing(true)
363 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700364 }
365
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700366 private Notification onVolumeUnmountable(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700367 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700368 final CharSequence title = mContext.getString(
369 R.string.ext_media_unmountable_notification_title, disk.getDescription());
370 final CharSequence text = mContext.getString(
371 R.string.ext_media_unmountable_notification_message, disk.getDescription());
372
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700373 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700374 .setContentIntent(buildVolumeSettingsPendingIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700375 .setCategory(Notification.CATEGORY_ERROR)
376 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700377 }
378
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700379 private Notification onVolumeRemoved(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700380 if (!vol.isPrimary()) {
381 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700382 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700383 }
384
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700385 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700386 final CharSequence title = mContext.getString(
387 R.string.ext_media_nomedia_notification_title, disk.getDescription());
388 final CharSequence text = mContext.getString(
389 R.string.ext_media_nomedia_notification_message, disk.getDescription());
390
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700391 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700392 .setCategory(Notification.CATEGORY_ERROR)
393 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700394 }
395
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700396 private Notification onVolumeBadRemoval(VolumeInfo vol) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700397 if (!vol.isPrimary()) {
398 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700399 return null;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700400 }
401
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700402 final DiskInfo disk = vol.getDisk();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700403 final CharSequence title = mContext.getString(
404 R.string.ext_media_badremoval_notification_title, disk.getDescription());
405 final CharSequence text = mContext.getString(
406 R.string.ext_media_badremoval_notification_message, disk.getDescription());
407
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700408 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700409 .setCategory(Notification.CATEGORY_ERROR)
410 .build();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700411 }
412
Jeff Sharkey50a05452015-04-29 11:24:52 -0700413 private void onMoveProgress(MoveInfo move, int status, long estMillis) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700414 final CharSequence title;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700415 if (!TextUtils.isEmpty(move.label)) {
416 title = mContext.getString(R.string.ext_media_move_specific_title, move.label);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700417 } else {
418 title = mContext.getString(R.string.ext_media_move_title);
419 }
420
421 final CharSequence text;
422 if (estMillis < 0) {
423 text = null;
424 } else {
425 text = DateUtils.formatDuration(estMillis);
426 }
427
Jeff Sharkey50a05452015-04-29 11:24:52 -0700428 final PendingIntent intent;
429 if (move.packageName != null) {
430 intent = buildWizardMovePendingIntent(move);
431 } else {
432 intent = buildWizardMigratePendingIntent(move);
433 }
434
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700435 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700436 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700437 .setColor(mContext.getColor(R.color.system_notification_accent_color))
438 .setContentTitle(title)
439 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700440 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700441 .setStyle(new Notification.BigTextStyle().bigText(text))
442 .setVisibility(Notification.VISIBILITY_PUBLIC)
443 .setLocalOnly(true)
444 .setCategory(Notification.CATEGORY_PROGRESS)
445 .setPriority(Notification.PRIORITY_LOW)
446 .setProgress(100, status, false)
447 .setOngoing(true)
448 .build();
449
Jeff Sharkey50a05452015-04-29 11:24:52 -0700450 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700451 }
452
Jeff Sharkey50a05452015-04-29 11:24:52 -0700453 private void onMoveFinished(MoveInfo move, int status) {
454 if (move.packageName != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700455 // We currently ignore finished app moves; just clear the last
456 // published progress
Jeff Sharkey50a05452015-04-29 11:24:52 -0700457 mNotificationManager.cancelAsUser(move.packageName, MOVE_ID, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700458 return;
459 }
460
Jeff Sharkey50a05452015-04-29 11:24:52 -0700461 final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
462 final String descrip = mStorageManager.getBestVolumeDescription(privateVol);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700463
464 final CharSequence title;
465 final CharSequence text;
466 if (status == PackageManager.MOVE_SUCCEEDED) {
467 title = mContext.getString(R.string.ext_media_move_success_title);
468 text = mContext.getString(R.string.ext_media_move_success_message, descrip);
469 } else {
470 title = mContext.getString(R.string.ext_media_move_failure_title);
471 text = mContext.getString(R.string.ext_media_move_failure_message);
472 }
473
Jeff Sharkey50a05452015-04-29 11:24:52 -0700474 // Jump back into the wizard flow if we moved to a real disk
475 final PendingIntent intent;
476 if (privateVol != null && privateVol.getDisk() != null) {
477 intent = buildWizardReadyPendingIntent(privateVol.getDisk());
478 } else {
479 intent = buildVolumeSettingsPendingIntent(privateVol);
480 }
481
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700482 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700483 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700484 .setColor(mContext.getColor(R.color.system_notification_accent_color))
485 .setContentTitle(title)
486 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700487 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700488 .setStyle(new Notification.BigTextStyle().bigText(text))
489 .setVisibility(Notification.VISIBILITY_PUBLIC)
490 .setLocalOnly(true)
491 .setCategory(Notification.CATEGORY_SYSTEM)
492 .setPriority(Notification.PRIORITY_LOW)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700493 .setAutoCancel(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700494 .build();
495
Jeff Sharkey50a05452015-04-29 11:24:52 -0700496 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700497 }
498
499 private int getSmallIcon(DiskInfo disk, int state) {
500 if (disk.isSd()) {
501 switch (state) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700502 case VolumeInfo.STATE_CHECKING:
503 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700504 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700505 default:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700506 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700507 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700508 } else if (disk.isUsb()) {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700509 return R.drawable.ic_usb_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700510 } else {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700511 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700512 }
513 }
514
515 private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title,
516 CharSequence text) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700517 return new Notification.Builder(mContext)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700518 .setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState()))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700519 .setColor(mContext.getColor(R.color.system_notification_accent_color))
520 .setContentTitle(title)
521 .setContentText(text)
522 .setStyle(new Notification.BigTextStyle().bigText(text))
523 .setVisibility(Notification.VISIBILITY_PUBLIC)
524 .setLocalOnly(true);
525 }
526
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700527 private PendingIntent buildInitPendingIntent(DiskInfo disk) {
528 final Intent intent = new Intent();
529 intent.setClassName("com.android.settings",
530 "com.android.settings.deviceinfo.StorageWizardInit");
531 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
532
533 final int requestKey = disk.getId().hashCode();
534 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
535 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
536 }
537
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700538 private PendingIntent buildInitPendingIntent(VolumeInfo vol) {
539 final Intent intent = new Intent();
540 intent.setClassName("com.android.settings",
541 "com.android.settings.deviceinfo.StorageWizardInit");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700542 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
543
544 final int requestKey = vol.getId().hashCode();
545 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
546 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700547 }
548
549 private PendingIntent buildUnmountPendingIntent(VolumeInfo vol) {
550 final Intent intent = new Intent();
551 intent.setClassName("com.android.settings",
552 "com.android.settings.deviceinfo.StorageUnmountReceiver");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700553 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
554
555 final int requestKey = vol.getId().hashCode();
556 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
557 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700558 }
559
560 private PendingIntent buildBrowsePendingIntent(VolumeInfo vol) {
561 final Intent intent = vol.buildBrowseIntent();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700562
563 final int requestKey = vol.getId().hashCode();
564 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
565 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700566 }
567
Jeff Sharkey50a05452015-04-29 11:24:52 -0700568 private PendingIntent buildVolumeSettingsPendingIntent(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700569 final Intent intent = new Intent();
Jeff Sharkey50a05452015-04-29 11:24:52 -0700570 switch (vol.getType()) {
571 case VolumeInfo.TYPE_PRIVATE:
572 intent.setClassName("com.android.settings",
573 "com.android.settings.Settings$PrivateVolumeSettingsActivity");
574 break;
575 case VolumeInfo.TYPE_PUBLIC:
576 intent.setClassName("com.android.settings",
577 "com.android.settings.Settings$PublicVolumeSettingsActivity");
578 break;
579 default:
580 return null;
581 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700582 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
583
584 final int requestKey = vol.getId().hashCode();
585 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
586 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
587 }
588
589 private PendingIntent buildSnoozeIntent(VolumeInfo vol) {
590 final Intent intent = new Intent(ACTION_SNOOZE_VOLUME);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700591 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, vol.getFsUuid());
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700592
593 final int requestKey = vol.getId().hashCode();
594 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
595 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
San Mehat64e6a452010-02-04 20:53:48 -0800596 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700597
598 private PendingIntent buildForgetPendingIntent(VolumeRecord rec) {
599 final Intent intent = new Intent();
600 intent.setClassName("com.android.settings",
601 "com.android.settings.Settings$PrivateVolumeForgetActivity");
602 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, rec.getFsUuid());
603
604 final int requestKey = rec.getFsUuid().hashCode();
605 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
606 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
607 }
Jeff Sharkey50a05452015-04-29 11:24:52 -0700608
609 private PendingIntent buildWizardMigratePendingIntent(MoveInfo move) {
610 final Intent intent = new Intent();
611 intent.setClassName("com.android.settings",
612 "com.android.settings.deviceinfo.StorageWizardMigrateProgress");
613 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
614
615 final VolumeInfo vol = mStorageManager.findVolumeByQualifiedUuid(move.volumeUuid);
616 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
617
618 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
619 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
620 }
621
622 private PendingIntent buildWizardMovePendingIntent(MoveInfo move) {
623 final Intent intent = new Intent();
624 intent.setClassName("com.android.settings",
625 "com.android.settings.deviceinfo.StorageWizardMoveProgress");
626 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
627
628 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
629 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
630 }
631
632 private PendingIntent buildWizardReadyPendingIntent(DiskInfo disk) {
633 final Intent intent = new Intent();
634 intent.setClassName("com.android.settings",
635 "com.android.settings.deviceinfo.StorageWizardReady");
636 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
637
638 final int requestKey = disk.getId().hashCode();
639 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
640 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
641 }
San Mehat64e6a452010-02-04 20:53:48 -0800642}