blob: d360875bce0f0bb6f7ff4b363c128088fa65e531 [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";
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -070056 private static final String ACTION_FINISH_WIZARD = "com.android.systemui.action.FINISH_WIZARD";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070057
Jeff Sharkey56bd3122015-04-14 10:30:34 -070058 // TODO: delay some notifications to avoid bumpy fast operations
San Mehat64e6a452010-02-04 20:53:48 -080059
Jeff Sharkey56bd3122015-04-14 10:30:34 -070060 private NotificationManager mNotificationManager;
San Mehatb1043402010-02-05 08:26:50 -080061 private StorageManager mStorageManager;
San Mehat64e6a452010-02-04 20:53:48 -080062
Jeff Sharkey50a05452015-04-29 11:24:52 -070063 private static class MoveInfo {
64 public int moveId;
65 public Bundle extras;
66 public String packageName;
67 public String label;
68 public String volumeUuid;
69 }
70
71 private final SparseArray<MoveInfo> mMoves = new SparseArray<>();
72
Jeff Sharkey56bd3122015-04-14 10:30:34 -070073 private final StorageEventListener mListener = new StorageEventListener() {
74 @Override
75 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070076 onVolumeStateChangedInternal(vol);
John Spurlock3e309b22013-06-25 11:01:29 -040077 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070078
79 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -070080 public void onVolumeRecordChanged(VolumeRecord rec) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070081 // Avoid kicking notifications when getting early metadata before
82 // mounted. If already mounted, we're being kicked because of a
83 // nickname or init'ed change.
Jeff Sharkey50a05452015-04-29 11:24:52 -070084 final VolumeInfo vol = mStorageManager.findVolumeByUuid(rec.getFsUuid());
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070085 if (vol != null && vol.isMountedReadable()) {
86 onVolumeStateChangedInternal(vol);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070087 }
Jeff Sharkey50a05452015-04-29 11:24:52 -070088 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070089
Jeff Sharkey50a05452015-04-29 11:24:52 -070090 @Override
91 public void onVolumeForgotten(String fsUuid) {
92 // Stop annoying the user
93 mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070094 }
95
96 @Override
97 public void onDiskScanned(DiskInfo disk, int volumeCount) {
98 onDiskScannedInternal(disk, volumeCount);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070099 }
100 };
101
102 private final BroadcastReceiver mSnoozeReceiver = new BroadcastReceiver() {
103 @Override
104 public void onReceive(Context context, Intent intent) {
105 // TODO: kick this onto background thread
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700106 final String fsUuid = intent.getStringExtra(VolumeRecord.EXTRA_FS_UUID);
107 mStorageManager.setVolumeSnoozed(fsUuid, true);
108 }
109 };
110
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -0700111 private final BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
112 @Override
113 public void onReceive(Context context, Intent intent) {
114 // When finishing the adoption wizard, clean up any notifications
115 // for moving primary storage
116 mNotificationManager.cancelAsUser(null, MOVE_ID, UserHandle.ALL);
117 }
118 };
119
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700120 private final MoveCallback mMoveCallback = new MoveCallback() {
121 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -0700122 public void onCreated(int moveId, Bundle extras) {
123 final MoveInfo move = new MoveInfo();
124 move.moveId = moveId;
125 move.extras = extras;
126 if (extras != null) {
127 move.packageName = extras.getString(Intent.EXTRA_PACKAGE_NAME);
128 move.label = extras.getString(Intent.EXTRA_TITLE);
129 move.volumeUuid = extras.getString(VolumeRecord.EXTRA_FS_UUID);
130 }
131 mMoves.put(moveId, move);
132 }
133
134 @Override
135 public void onStatusChanged(int moveId, int status, long estMillis) {
136 final MoveInfo move = mMoves.get(moveId);
137 if (move == null) {
138 Log.w(TAG, "Ignoring unknown move " + moveId);
139 return;
140 }
141
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700142 if (PackageManager.isMoveStatusFinished(status)) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700143 onMoveFinished(move, status);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700144 } else {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700145 onMoveProgress(move, status, estMillis);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700146 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700147 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700148 };
San Mehat64e6a452010-02-04 20:53:48 -0800149
John Spurlock3e309b22013-06-25 11:01:29 -0400150 @Override
151 public void start() {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700152 mNotificationManager = mContext.getSystemService(NotificationManager.class);
John Spurlock3e309b22013-06-25 11:01:29 -0400153
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700154 mStorageManager = mContext.getSystemService(StorageManager.class);
155 mStorageManager.registerListener(mListener);
Daniel Sandler5b8743f2010-11-03 09:43:46 -0400156
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700157 mContext.registerReceiver(mSnoozeReceiver, new IntentFilter(ACTION_SNOOZE_VOLUME),
158 android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null);
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -0700159 mContext.registerReceiver(mFinishReceiver, new IntentFilter(ACTION_FINISH_WIZARD),
160 android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700161
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700162 // Kick current state into place
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -0700163 final List<DiskInfo> disks = mStorageManager.getDisks();
164 for (DiskInfo disk : disks) {
165 onDiskScannedInternal(disk, disk.volumeCount);
166 }
167
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700168 final List<VolumeInfo> vols = mStorageManager.getVolumes();
169 for (VolumeInfo vol : vols) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700170 onVolumeStateChangedInternal(vol);
171 }
172
173 mContext.getPackageManager().registerMoveCallback(mMoveCallback, new Handler());
174
175 updateMissingPrivateVolumes();
176 }
177
178 private void updateMissingPrivateVolumes() {
179 final List<VolumeRecord> recs = mStorageManager.getVolumeRecords();
180 for (VolumeRecord rec : recs) {
181 if (rec.getType() != VolumeInfo.TYPE_PRIVATE) continue;
182
183 final String fsUuid = rec.getFsUuid();
184 final VolumeInfo info = mStorageManager.findVolumeByUuid(fsUuid);
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700185 if ((info != null && info.isMountedWritable()) || rec.isSnoozed()) {
186 // Yay, private volume is here, or user snoozed
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700187 mNotificationManager.cancelAsUser(fsUuid, PRIVATE_ID, UserHandle.ALL);
188
189 } else {
190 // Boo, annoy the user to reinsert the private volume
191 final CharSequence title = mContext.getString(R.string.ext_media_missing_title,
192 rec.getNickname());
193 final CharSequence text = mContext.getString(R.string.ext_media_missing_message);
194
195 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700196 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700197 .setColor(mContext.getColor(R.color.system_notification_accent_color))
198 .setContentTitle(title)
199 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700200 .setContentIntent(buildForgetPendingIntent(rec))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700201 .setStyle(new Notification.BigTextStyle().bigText(text))
202 .setVisibility(Notification.VISIBILITY_PUBLIC)
203 .setLocalOnly(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700204 .setCategory(Notification.CATEGORY_SYSTEM)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700205 .setDeleteIntent(buildSnoozeIntent(fsUuid))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700206 .build();
207
208 mNotificationManager.notifyAsUser(fsUuid, PRIVATE_ID, notif, UserHandle.ALL);
209 }
San Mehat64e6a452010-02-04 20:53:48 -0800210 }
211 }
212
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700213 private void onDiskScannedInternal(DiskInfo disk, int volumeCount) {
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -0700214 if (volumeCount == 0 && disk.size > 0) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700215 // No supported volumes found, give user option to format
216 final CharSequence title = mContext.getString(
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700217 R.string.ext_media_unsupported_notification_title, disk.getDescription());
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700218 final CharSequence text = mContext.getString(
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700219 R.string.ext_media_unsupported_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800220
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700221 final Notification notif = new Notification.Builder(mContext)
222 .setSmallIcon(getSmallIcon(disk, VolumeInfo.STATE_UNMOUNTABLE))
223 .setColor(mContext.getColor(R.color.system_notification_accent_color))
224 .setContentTitle(title)
225 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700226 .setContentIntent(buildInitPendingIntent(disk))
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700227 .setStyle(new Notification.BigTextStyle().bigText(text))
228 .setVisibility(Notification.VISIBILITY_PUBLIC)
229 .setLocalOnly(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700230 .setCategory(Notification.CATEGORY_ERROR)
231 .build();
232
233 mNotificationManager.notifyAsUser(disk.getId(), DISK_ID, notif, UserHandle.ALL);
234
235 } else {
236 // Yay, we have volumes!
237 mNotificationManager.cancelAsUser(disk.getId(), DISK_ID, UserHandle.ALL);
238 }
239 }
240
241 private void onVolumeStateChangedInternal(VolumeInfo vol) {
242 switch (vol.getType()) {
243 case VolumeInfo.TYPE_PRIVATE:
244 onPrivateVolumeStateChangedInternal(vol);
245 break;
246 case VolumeInfo.TYPE_PUBLIC:
247 onPublicVolumeStateChangedInternal(vol);
248 break;
249 }
250 }
251
252 private void onPrivateVolumeStateChangedInternal(VolumeInfo vol) {
253 Log.d(TAG, "Notifying about private volume: " + vol.toString());
254
255 updateMissingPrivateVolumes();
256 }
257
258 private void onPublicVolumeStateChangedInternal(VolumeInfo vol) {
259 Log.d(TAG, "Notifying about public volume: " + vol.toString());
San Mehat64e6a452010-02-04 20:53:48 -0800260
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700261 final Notification notif;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700262 switch (vol.getState()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700263 case VolumeInfo.STATE_UNMOUNTED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700264 notif = onVolumeUnmounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700265 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700266 case VolumeInfo.STATE_CHECKING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700267 notif = onVolumeChecking(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700268 break;
269 case VolumeInfo.STATE_MOUNTED:
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700270 case VolumeInfo.STATE_MOUNTED_READ_ONLY:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700271 notif = onVolumeMounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700272 break;
273 case VolumeInfo.STATE_FORMATTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700274 notif = onVolumeFormatting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700275 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700276 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700277 notif = onVolumeEjecting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700278 break;
279 case VolumeInfo.STATE_UNMOUNTABLE:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700280 notif = onVolumeUnmountable(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700281 break;
282 case VolumeInfo.STATE_REMOVED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700283 notif = onVolumeRemoved(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700284 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700285 case VolumeInfo.STATE_BAD_REMOVAL:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700286 notif = onVolumeBadRemoval(vol);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700287 break;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700288 default:
289 notif = null;
290 break;
291 }
292
293 if (notif != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700294 mNotificationManager.notifyAsUser(vol.getId(), PUBLIC_ID, notif, UserHandle.ALL);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700295 } else {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700296 mNotificationManager.cancelAsUser(vol.getId(), PUBLIC_ID, UserHandle.ALL);
San Mehat64e6a452010-02-04 20:53:48 -0800297 }
298 }
299
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700300 private Notification onVolumeUnmounted(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700301 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700302 return null;
San Mehat64e6a452010-02-04 20:53:48 -0800303 }
304
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700305 private Notification onVolumeChecking(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700306 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700307 final CharSequence title = mContext.getString(
308 R.string.ext_media_checking_notification_title, disk.getDescription());
309 final CharSequence text = mContext.getString(
310 R.string.ext_media_checking_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800311
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700312 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700313 .setCategory(Notification.CATEGORY_PROGRESS)
314 .setPriority(Notification.PRIORITY_LOW)
315 .setOngoing(true)
316 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700317 }
San Mehat64e6a452010-02-04 20:53:48 -0800318
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700319 private Notification onVolumeMounted(VolumeInfo vol) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700320 final VolumeRecord rec = mStorageManager.findRecordByUuid(vol.getFsUuid());
321
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700322 // Don't annoy when user dismissed in past
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700323 if (rec.isSnoozed()) return null;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700324
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700325 final DiskInfo disk = vol.getDisk();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700326 if (disk.isAdoptable() && !rec.isInited()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700327 final CharSequence title = disk.getDescription();
328 final CharSequence text = mContext.getString(
329 R.string.ext_media_new_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800330
Jeff Sharkey50a05452015-04-29 11:24:52 -0700331 final PendingIntent initIntent = buildInitPendingIntent(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_settings_24dp,
334 mContext.getString(R.string.ext_media_init_action), initIntent))
335 .addAction(new Action(R.drawable.ic_eject_24dp,
336 mContext.getString(R.string.ext_media_unmount_action),
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700337 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700338 .setContentIntent(initIntent)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700339 .setDeleteIntent(buildSnoozeIntent(vol.getFsUuid()))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700340 .setCategory(Notification.CATEGORY_SYSTEM)
341 .build();
John Spurlock209bede2013-07-17 12:23:27 -0400342
San Mehat64e6a452010-02-04 20:53:48 -0800343 } else {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700344 final CharSequence title = disk.getDescription();
345 final CharSequence text = mContext.getString(
346 R.string.ext_media_ready_notification_message, disk.getDescription());
347
Jeff Sharkey50a05452015-04-29 11:24:52 -0700348 final PendingIntent browseIntent = buildBrowsePendingIntent(vol);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700349 return buildNotificationBuilder(vol, title, text)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700350 .addAction(new Action(R.drawable.ic_folder_24dp,
351 mContext.getString(R.string.ext_media_browse_action),
Jeff Sharkey50a05452015-04-29 11:24:52 -0700352 browseIntent))
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700353 .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(browseIntent)
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 .setPriority(Notification.PRIORITY_LOW)
360 .build();
San Mehat64e6a452010-02-04 20:53:48 -0800361 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700362 }
363
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700364 private Notification onVolumeFormatting(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700365 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700366 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700367 }
368
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700369 private Notification onVolumeEjecting(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700370 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700371 final CharSequence title = mContext.getString(
372 R.string.ext_media_unmounting_notification_title, disk.getDescription());
373 final CharSequence text = mContext.getString(
374 R.string.ext_media_unmounting_notification_message, disk.getDescription());
375
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700376 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700377 .setCategory(Notification.CATEGORY_PROGRESS)
378 .setPriority(Notification.PRIORITY_LOW)
379 .setOngoing(true)
380 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700381 }
382
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700383 private Notification onVolumeUnmountable(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700384 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700385 final CharSequence title = mContext.getString(
386 R.string.ext_media_unmountable_notification_title, disk.getDescription());
387 final CharSequence text = mContext.getString(
388 R.string.ext_media_unmountable_notification_message, disk.getDescription());
389
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700390 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700391 .setContentIntent(buildInitPendingIntent(vol))
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 onVolumeRemoved(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700397 if (!vol.isPrimary()) {
398 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700399 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700400 }
401
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700402 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700403 final CharSequence title = mContext.getString(
404 R.string.ext_media_nomedia_notification_title, disk.getDescription());
405 final CharSequence text = mContext.getString(
406 R.string.ext_media_nomedia_notification_message, disk.getDescription());
407
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700408 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700409 .setCategory(Notification.CATEGORY_ERROR)
410 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700411 }
412
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700413 private Notification onVolumeBadRemoval(VolumeInfo vol) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700414 if (!vol.isPrimary()) {
415 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700416 return null;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700417 }
418
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700419 final DiskInfo disk = vol.getDisk();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700420 final CharSequence title = mContext.getString(
421 R.string.ext_media_badremoval_notification_title, disk.getDescription());
422 final CharSequence text = mContext.getString(
423 R.string.ext_media_badremoval_notification_message, disk.getDescription());
424
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700425 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700426 .setCategory(Notification.CATEGORY_ERROR)
427 .build();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700428 }
429
Jeff Sharkey50a05452015-04-29 11:24:52 -0700430 private void onMoveProgress(MoveInfo move, int status, long estMillis) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700431 final CharSequence title;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700432 if (!TextUtils.isEmpty(move.label)) {
433 title = mContext.getString(R.string.ext_media_move_specific_title, move.label);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700434 } else {
435 title = mContext.getString(R.string.ext_media_move_title);
436 }
437
438 final CharSequence text;
439 if (estMillis < 0) {
440 text = null;
441 } else {
442 text = DateUtils.formatDuration(estMillis);
443 }
444
Jeff Sharkey50a05452015-04-29 11:24:52 -0700445 final PendingIntent intent;
446 if (move.packageName != null) {
447 intent = buildWizardMovePendingIntent(move);
448 } else {
449 intent = buildWizardMigratePendingIntent(move);
450 }
451
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700452 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700453 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700454 .setColor(mContext.getColor(R.color.system_notification_accent_color))
455 .setContentTitle(title)
456 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700457 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700458 .setStyle(new Notification.BigTextStyle().bigText(text))
459 .setVisibility(Notification.VISIBILITY_PUBLIC)
460 .setLocalOnly(true)
461 .setCategory(Notification.CATEGORY_PROGRESS)
462 .setPriority(Notification.PRIORITY_LOW)
463 .setProgress(100, status, false)
464 .setOngoing(true)
465 .build();
466
Jeff Sharkey50a05452015-04-29 11:24:52 -0700467 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700468 }
469
Jeff Sharkey50a05452015-04-29 11:24:52 -0700470 private void onMoveFinished(MoveInfo move, int status) {
471 if (move.packageName != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700472 // We currently ignore finished app moves; just clear the last
473 // published progress
Jeff Sharkey50a05452015-04-29 11:24:52 -0700474 mNotificationManager.cancelAsUser(move.packageName, MOVE_ID, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700475 return;
476 }
477
Jeff Sharkey50a05452015-04-29 11:24:52 -0700478 final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
479 final String descrip = mStorageManager.getBestVolumeDescription(privateVol);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700480
481 final CharSequence title;
482 final CharSequence text;
483 if (status == PackageManager.MOVE_SUCCEEDED) {
484 title = mContext.getString(R.string.ext_media_move_success_title);
485 text = mContext.getString(R.string.ext_media_move_success_message, descrip);
486 } else {
487 title = mContext.getString(R.string.ext_media_move_failure_title);
488 text = mContext.getString(R.string.ext_media_move_failure_message);
489 }
490
Jeff Sharkey50a05452015-04-29 11:24:52 -0700491 // Jump back into the wizard flow if we moved to a real disk
492 final PendingIntent intent;
493 if (privateVol != null && privateVol.getDisk() != null) {
494 intent = buildWizardReadyPendingIntent(privateVol.getDisk());
495 } else {
496 intent = buildVolumeSettingsPendingIntent(privateVol);
497 }
498
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700499 final Notification notif = new Notification.Builder(mContext)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700500 .setSmallIcon(R.drawable.ic_sd_card_48dp)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700501 .setColor(mContext.getColor(R.color.system_notification_accent_color))
502 .setContentTitle(title)
503 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700504 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700505 .setStyle(new Notification.BigTextStyle().bigText(text))
506 .setVisibility(Notification.VISIBILITY_PUBLIC)
507 .setLocalOnly(true)
508 .setCategory(Notification.CATEGORY_SYSTEM)
509 .setPriority(Notification.PRIORITY_LOW)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700510 .setAutoCancel(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700511 .build();
512
Jeff Sharkey50a05452015-04-29 11:24:52 -0700513 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700514 }
515
516 private int getSmallIcon(DiskInfo disk, int state) {
517 if (disk.isSd()) {
518 switch (state) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700519 case VolumeInfo.STATE_CHECKING:
520 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700521 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700522 default:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700523 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700524 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700525 } else if (disk.isUsb()) {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700526 return R.drawable.ic_usb_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700527 } else {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700528 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700529 }
530 }
531
532 private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title,
533 CharSequence text) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700534 return new Notification.Builder(mContext)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700535 .setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState()))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700536 .setColor(mContext.getColor(R.color.system_notification_accent_color))
537 .setContentTitle(title)
538 .setContentText(text)
539 .setStyle(new Notification.BigTextStyle().bigText(text))
540 .setVisibility(Notification.VISIBILITY_PUBLIC)
541 .setLocalOnly(true);
542 }
543
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700544 private PendingIntent buildInitPendingIntent(DiskInfo disk) {
545 final Intent intent = new Intent();
546 intent.setClassName("com.android.settings",
547 "com.android.settings.deviceinfo.StorageWizardInit");
548 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
549
550 final int requestKey = disk.getId().hashCode();
551 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
552 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
553 }
554
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700555 private PendingIntent buildInitPendingIntent(VolumeInfo vol) {
556 final Intent intent = new Intent();
557 intent.setClassName("com.android.settings",
558 "com.android.settings.deviceinfo.StorageWizardInit");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700559 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
560
561 final int requestKey = vol.getId().hashCode();
562 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
563 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700564 }
565
566 private PendingIntent buildUnmountPendingIntent(VolumeInfo vol) {
567 final Intent intent = new Intent();
568 intent.setClassName("com.android.settings",
569 "com.android.settings.deviceinfo.StorageUnmountReceiver");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700570 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
571
572 final int requestKey = vol.getId().hashCode();
573 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
574 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700575 }
576
577 private PendingIntent buildBrowsePendingIntent(VolumeInfo vol) {
578 final Intent intent = vol.buildBrowseIntent();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700579
580 final int requestKey = vol.getId().hashCode();
581 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
582 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700583 }
584
Jeff Sharkey50a05452015-04-29 11:24:52 -0700585 private PendingIntent buildVolumeSettingsPendingIntent(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700586 final Intent intent = new Intent();
Jeff Sharkey50a05452015-04-29 11:24:52 -0700587 switch (vol.getType()) {
588 case VolumeInfo.TYPE_PRIVATE:
589 intent.setClassName("com.android.settings",
590 "com.android.settings.Settings$PrivateVolumeSettingsActivity");
591 break;
592 case VolumeInfo.TYPE_PUBLIC:
593 intent.setClassName("com.android.settings",
594 "com.android.settings.Settings$PublicVolumeSettingsActivity");
595 break;
596 default:
597 return null;
598 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700599 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
600
601 final int requestKey = vol.getId().hashCode();
602 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
603 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
604 }
605
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700606 private PendingIntent buildSnoozeIntent(String fsUuid) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700607 final Intent intent = new Intent(ACTION_SNOOZE_VOLUME);
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700608 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, fsUuid);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700609
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700610 final int requestKey = fsUuid.hashCode();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700611 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
612 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
San Mehat64e6a452010-02-04 20:53:48 -0800613 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700614
615 private PendingIntent buildForgetPendingIntent(VolumeRecord rec) {
616 final Intent intent = new Intent();
617 intent.setClassName("com.android.settings",
618 "com.android.settings.Settings$PrivateVolumeForgetActivity");
619 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, rec.getFsUuid());
620
621 final int requestKey = rec.getFsUuid().hashCode();
622 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
623 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
624 }
Jeff Sharkey50a05452015-04-29 11:24:52 -0700625
626 private PendingIntent buildWizardMigratePendingIntent(MoveInfo move) {
627 final Intent intent = new Intent();
628 intent.setClassName("com.android.settings",
629 "com.android.settings.deviceinfo.StorageWizardMigrateProgress");
630 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
631
632 final VolumeInfo vol = mStorageManager.findVolumeByQualifiedUuid(move.volumeUuid);
633 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
634
635 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
636 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
637 }
638
639 private PendingIntent buildWizardMovePendingIntent(MoveInfo move) {
640 final Intent intent = new Intent();
641 intent.setClassName("com.android.settings",
642 "com.android.settings.deviceinfo.StorageWizardMoveProgress");
643 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
644
645 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
646 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
647 }
648
649 private PendingIntent buildWizardReadyPendingIntent(DiskInfo disk) {
650 final Intent intent = new Intent();
651 intent.setClassName("com.android.settings",
652 "com.android.settings.deviceinfo.StorageWizardReady");
653 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
654
655 final int requestKey = disk.getId().hashCode();
656 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
657 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
658 }
San Mehat64e6a452010-02-04 20:53:48 -0800659}