blob: 43727e07c7129d774ca91e64350bc20224989a8e [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;
Chris Wren5e6c0ff2017-01-05 12:57:06 -050044import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
John Spurlock3e309b22013-06-25 11:01:29 -040045import com.android.systemui.SystemUI;
Dan Sandler8e032e12017-01-25 13:41:38 -050046import com.android.systemui.util.NotificationChannels;
John Spurlock3e309b22013-06-25 11:01:29 -040047
Jeff Sharkey56bd3122015-04-14 10:30:34 -070048import java.util.List;
49
John Spurlock3e309b22013-06-25 11:01:29 -040050public class StorageNotification extends SystemUI {
San Mehat64e6a452010-02-04 20:53:48 -080051 private static final String TAG = "StorageNotification";
52
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070053 private static final String ACTION_SNOOZE_VOLUME = "com.android.systemui.action.SNOOZE_VOLUME";
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -070054 private static final String ACTION_FINISH_WIZARD = "com.android.systemui.action.FINISH_WIZARD";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070055
Jeff Sharkey56bd3122015-04-14 10:30:34 -070056 // TODO: delay some notifications to avoid bumpy fast operations
San Mehat64e6a452010-02-04 20:53:48 -080057
Jeff Sharkey56bd3122015-04-14 10:30:34 -070058 private NotificationManager mNotificationManager;
San Mehatb1043402010-02-05 08:26:50 -080059 private StorageManager mStorageManager;
San Mehat64e6a452010-02-04 20:53:48 -080060
Jeff Sharkey50a05452015-04-29 11:24:52 -070061 private static class MoveInfo {
62 public int moveId;
63 public Bundle extras;
64 public String packageName;
65 public String label;
66 public String volumeUuid;
67 }
68
69 private final SparseArray<MoveInfo> mMoves = new SparseArray<>();
70
Jeff Sharkey56bd3122015-04-14 10:30:34 -070071 private final StorageEventListener mListener = new StorageEventListener() {
72 @Override
73 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070074 onVolumeStateChangedInternal(vol);
John Spurlock3e309b22013-06-25 11:01:29 -040075 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070076
77 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -070078 public void onVolumeRecordChanged(VolumeRecord rec) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070079 // Avoid kicking notifications when getting early metadata before
80 // mounted. If already mounted, we're being kicked because of a
81 // nickname or init'ed change.
Jeff Sharkey50a05452015-04-29 11:24:52 -070082 final VolumeInfo vol = mStorageManager.findVolumeByUuid(rec.getFsUuid());
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070083 if (vol != null && vol.isMountedReadable()) {
84 onVolumeStateChangedInternal(vol);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070085 }
Jeff Sharkey50a05452015-04-29 11:24:52 -070086 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070087
Jeff Sharkey50a05452015-04-29 11:24:52 -070088 @Override
89 public void onVolumeForgotten(String fsUuid) {
90 // Stop annoying the user
Chris Wren5e6c0ff2017-01-05 12:57:06 -050091 mNotificationManager.cancelAsUser(fsUuid, SystemMessage.NOTE_STORAGE_PRIVATE,
92 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 }
Philip P. Moltmann75b230b2016-04-11 15:27:58 -070099
100 @Override
101 public void onDiskDestroyed(DiskInfo disk) {
102 onDiskDestroyedInternal(disk);
103 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700104 };
105
106 private final BroadcastReceiver mSnoozeReceiver = new BroadcastReceiver() {
107 @Override
108 public void onReceive(Context context, Intent intent) {
109 // TODO: kick this onto background thread
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700110 final String fsUuid = intent.getStringExtra(VolumeRecord.EXTRA_FS_UUID);
111 mStorageManager.setVolumeSnoozed(fsUuid, true);
112 }
113 };
114
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -0700115 private final BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
116 @Override
117 public void onReceive(Context context, Intent intent) {
118 // When finishing the adoption wizard, clean up any notifications
119 // for moving primary storage
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500120 mNotificationManager.cancelAsUser(null, SystemMessage.NOTE_STORAGE_MOVE,
121 UserHandle.ALL);
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -0700122 }
123 };
124
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700125 private final MoveCallback mMoveCallback = new MoveCallback() {
126 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -0700127 public void onCreated(int moveId, Bundle extras) {
128 final MoveInfo move = new MoveInfo();
129 move.moveId = moveId;
130 move.extras = extras;
131 if (extras != null) {
132 move.packageName = extras.getString(Intent.EXTRA_PACKAGE_NAME);
133 move.label = extras.getString(Intent.EXTRA_TITLE);
134 move.volumeUuid = extras.getString(VolumeRecord.EXTRA_FS_UUID);
135 }
136 mMoves.put(moveId, move);
137 }
138
139 @Override
140 public void onStatusChanged(int moveId, int status, long estMillis) {
141 final MoveInfo move = mMoves.get(moveId);
142 if (move == null) {
143 Log.w(TAG, "Ignoring unknown move " + moveId);
144 return;
145 }
146
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700147 if (PackageManager.isMoveStatusFinished(status)) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700148 onMoveFinished(move, status);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700149 } else {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700150 onMoveProgress(move, status, estMillis);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700151 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700152 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700153 };
San Mehat64e6a452010-02-04 20:53:48 -0800154
John Spurlock3e309b22013-06-25 11:01:29 -0400155 @Override
156 public void start() {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700157 mNotificationManager = mContext.getSystemService(NotificationManager.class);
John Spurlock3e309b22013-06-25 11:01:29 -0400158
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700159 mStorageManager = mContext.getSystemService(StorageManager.class);
160 mStorageManager.registerListener(mListener);
Daniel Sandler5b8743f2010-11-03 09:43:46 -0400161
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700162 mContext.registerReceiver(mSnoozeReceiver, new IntentFilter(ACTION_SNOOZE_VOLUME),
163 android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null);
Jeff Sharkeydf27d3b2015-06-16 13:51:48 -0700164 mContext.registerReceiver(mFinishReceiver, new IntentFilter(ACTION_FINISH_WIZARD),
165 android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, null);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700166
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700167 // Kick current state into place
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -0700168 final List<DiskInfo> disks = mStorageManager.getDisks();
169 for (DiskInfo disk : disks) {
170 onDiskScannedInternal(disk, disk.volumeCount);
171 }
172
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700173 final List<VolumeInfo> vols = mStorageManager.getVolumes();
174 for (VolumeInfo vol : vols) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700175 onVolumeStateChangedInternal(vol);
176 }
177
178 mContext.getPackageManager().registerMoveCallback(mMoveCallback, new Handler());
179
180 updateMissingPrivateVolumes();
181 }
182
183 private void updateMissingPrivateVolumes() {
184 final List<VolumeRecord> recs = mStorageManager.getVolumeRecords();
185 for (VolumeRecord rec : recs) {
186 if (rec.getType() != VolumeInfo.TYPE_PRIVATE) continue;
187
188 final String fsUuid = rec.getFsUuid();
189 final VolumeInfo info = mStorageManager.findVolumeByUuid(fsUuid);
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700190 if ((info != null && info.isMountedWritable()) || rec.isSnoozed()) {
191 // Yay, private volume is here, or user snoozed
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500192 mNotificationManager.cancelAsUser(fsUuid, SystemMessage.NOTE_STORAGE_PRIVATE,
193 UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700194
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
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -0500201 Notification.Builder builder =
202 new Notification.Builder(mContext, NotificationChannels.STORAGE)
203 .setSmallIcon(R.drawable.ic_sd_card_48dp)
204 .setColor(mContext.getColor(
205 R.color.system_notification_accent_color))
206 .setContentTitle(title)
207 .setContentText(text)
208 .setContentIntent(buildForgetPendingIntent(rec))
209 .setStyle(new Notification.BigTextStyle().bigText(text))
210 .setVisibility(Notification.VISIBILITY_PUBLIC)
211 .setLocalOnly(true)
212 .setCategory(Notification.CATEGORY_SYSTEM)
213 .setDeleteIntent(buildSnoozeIntent(fsUuid));
Adrian Roose25c18d2016-06-17 15:59:49 -0700214 SystemUI.overrideNotificationAppName(mContext, builder);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700215
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500216 mNotificationManager.notifyAsUser(fsUuid, SystemMessage.NOTE_STORAGE_PRIVATE,
217 builder.build(), UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700218 }
San Mehat64e6a452010-02-04 20:53:48 -0800219 }
220 }
221
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700222 private void onDiskScannedInternal(DiskInfo disk, int volumeCount) {
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -0700223 if (volumeCount == 0 && disk.size > 0) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700224 // No supported volumes found, give user option to format
225 final CharSequence title = mContext.getString(
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700226 R.string.ext_media_unsupported_notification_title, disk.getDescription());
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700227 final CharSequence text = mContext.getString(
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700228 R.string.ext_media_unsupported_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800229
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -0500230 Notification.Builder builder =
231 new Notification.Builder(mContext, NotificationChannels.STORAGE)
232 .setSmallIcon(getSmallIcon(disk, VolumeInfo.STATE_UNMOUNTABLE))
233 .setColor(mContext.getColor(R.color.system_notification_accent_color))
234 .setContentTitle(title)
235 .setContentText(text)
236 .setContentIntent(buildInitPendingIntent(disk))
237 .setStyle(new Notification.BigTextStyle().bigText(text))
238 .setVisibility(Notification.VISIBILITY_PUBLIC)
239 .setLocalOnly(true)
240 .setCategory(Notification.CATEGORY_ERROR);
Adrian Roose25c18d2016-06-17 15:59:49 -0700241 SystemUI.overrideNotificationAppName(mContext, builder);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700242
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500243 mNotificationManager.notifyAsUser(disk.getId(), SystemMessage.NOTE_STORAGE_DISK,
244 builder.build(), UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700245
246 } else {
247 // Yay, we have volumes!
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500248 mNotificationManager.cancelAsUser(disk.getId(), SystemMessage.NOTE_STORAGE_DISK,
249 UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700250 }
251 }
252
Philip P. Moltmann75b230b2016-04-11 15:27:58 -0700253 /**
254 * Remove all notifications for a disk when it goes away.
255 *
256 * @param disk The disk that went away.
257 */
258 private void onDiskDestroyedInternal(@NonNull DiskInfo disk) {
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500259 mNotificationManager.cancelAsUser(disk.getId(), SystemMessage.NOTE_STORAGE_DISK,
260 UserHandle.ALL);
Philip P. Moltmann75b230b2016-04-11 15:27:58 -0700261 }
262
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700263 private void onVolumeStateChangedInternal(VolumeInfo vol) {
264 switch (vol.getType()) {
265 case VolumeInfo.TYPE_PRIVATE:
266 onPrivateVolumeStateChangedInternal(vol);
267 break;
268 case VolumeInfo.TYPE_PUBLIC:
269 onPublicVolumeStateChangedInternal(vol);
270 break;
271 }
272 }
273
274 private void onPrivateVolumeStateChangedInternal(VolumeInfo vol) {
275 Log.d(TAG, "Notifying about private volume: " + vol.toString());
276
277 updateMissingPrivateVolumes();
278 }
279
280 private void onPublicVolumeStateChangedInternal(VolumeInfo vol) {
281 Log.d(TAG, "Notifying about public volume: " + vol.toString());
San Mehat64e6a452010-02-04 20:53:48 -0800282
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700283 final Notification notif;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700284 switch (vol.getState()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700285 case VolumeInfo.STATE_UNMOUNTED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700286 notif = onVolumeUnmounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700287 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700288 case VolumeInfo.STATE_CHECKING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700289 notif = onVolumeChecking(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700290 break;
291 case VolumeInfo.STATE_MOUNTED:
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700292 case VolumeInfo.STATE_MOUNTED_READ_ONLY:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700293 notif = onVolumeMounted(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700294 break;
295 case VolumeInfo.STATE_FORMATTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700296 notif = onVolumeFormatting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700297 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700298 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700299 notif = onVolumeEjecting(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700300 break;
301 case VolumeInfo.STATE_UNMOUNTABLE:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700302 notif = onVolumeUnmountable(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700303 break;
304 case VolumeInfo.STATE_REMOVED:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700305 notif = onVolumeRemoved(vol);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700306 break;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700307 case VolumeInfo.STATE_BAD_REMOVAL:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700308 notif = onVolumeBadRemoval(vol);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700309 break;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700310 default:
311 notif = null;
312 break;
313 }
314
315 if (notif != null) {
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500316 mNotificationManager.notifyAsUser(vol.getId(), SystemMessage.NOTE_STORAGE_PUBLIC,
317 notif, UserHandle.ALL);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700318 } else {
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500319 mNotificationManager.cancelAsUser(vol.getId(), SystemMessage.NOTE_STORAGE_PUBLIC,
320 UserHandle.ALL);
San Mehat64e6a452010-02-04 20:53:48 -0800321 }
322 }
323
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700324 private Notification onVolumeUnmounted(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700325 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700326 return null;
San Mehat64e6a452010-02-04 20:53:48 -0800327 }
328
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700329 private Notification onVolumeChecking(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700330 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700331 final CharSequence title = mContext.getString(
332 R.string.ext_media_checking_notification_title, disk.getDescription());
333 final CharSequence text = mContext.getString(
334 R.string.ext_media_checking_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800335
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700336 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700337 .setCategory(Notification.CATEGORY_PROGRESS)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700338 .setOngoing(true)
339 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700340 }
San Mehat64e6a452010-02-04 20:53:48 -0800341
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700342 private Notification onVolumeMounted(VolumeInfo vol) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700343 final VolumeRecord rec = mStorageManager.findRecordByUuid(vol.getFsUuid());
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700344 final DiskInfo disk = vol.getDisk();
Makoto Onukib138cb22015-06-23 17:32:02 -0700345
346 // Don't annoy when user dismissed in past. (But make sure the disk is adoptable; we
347 // used to allow snoozing non-adoptable disks too.)
348 if (rec.isSnoozed() && disk.isAdoptable()) {
349 return null;
350 }
351
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700352 if (disk.isAdoptable() && !rec.isInited()) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700353 final CharSequence title = disk.getDescription();
354 final CharSequence text = mContext.getString(
355 R.string.ext_media_new_notification_message, disk.getDescription());
San Mehat64e6a452010-02-04 20:53:48 -0800356
Jeff Sharkey50a05452015-04-29 11:24:52 -0700357 final PendingIntent initIntent = buildInitPendingIntent(vol);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700358 return buildNotificationBuilder(vol, title, text)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700359 .addAction(new Action(R.drawable.ic_settings_24dp,
360 mContext.getString(R.string.ext_media_init_action), initIntent))
361 .addAction(new Action(R.drawable.ic_eject_24dp,
362 mContext.getString(R.string.ext_media_unmount_action),
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700363 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700364 .setContentIntent(initIntent)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700365 .setDeleteIntent(buildSnoozeIntent(vol.getFsUuid()))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700366 .build();
John Spurlock209bede2013-07-17 12:23:27 -0400367
San Mehat64e6a452010-02-04 20:53:48 -0800368 } else {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700369 final CharSequence title = disk.getDescription();
370 final CharSequence text = mContext.getString(
371 R.string.ext_media_ready_notification_message, disk.getDescription());
372
Jeff Sharkey50a05452015-04-29 11:24:52 -0700373 final PendingIntent browseIntent = buildBrowsePendingIntent(vol);
Makoto Onukib138cb22015-06-23 17:32:02 -0700374 final Notification.Builder builder = buildNotificationBuilder(vol, title, text)
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700375 .addAction(new Action(R.drawable.ic_folder_24dp,
376 mContext.getString(R.string.ext_media_browse_action),
Jeff Sharkey50a05452015-04-29 11:24:52 -0700377 browseIntent))
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700378 .addAction(new Action(R.drawable.ic_eject_24dp,
379 mContext.getString(R.string.ext_media_unmount_action),
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700380 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700381 .setContentIntent(browseIntent)
Dan Sandler8e032e12017-01-25 13:41:38 -0500382 .setCategory(Notification.CATEGORY_SYSTEM);
Makoto Onukib138cb22015-06-23 17:32:02 -0700383 // Non-adoptable disks can't be snoozed.
384 if (disk.isAdoptable()) {
385 builder.setDeleteIntent(buildSnoozeIntent(vol.getFsUuid()));
386 }
387
388 return builder.build();
San Mehat64e6a452010-02-04 20:53:48 -0800389 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700390 }
391
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700392 private Notification onVolumeFormatting(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700393 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700394 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700395 }
396
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700397 private Notification onVolumeEjecting(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700398 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700399 final CharSequence title = mContext.getString(
400 R.string.ext_media_unmounting_notification_title, disk.getDescription());
401 final CharSequence text = mContext.getString(
402 R.string.ext_media_unmounting_notification_message, disk.getDescription());
403
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700404 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700405 .setCategory(Notification.CATEGORY_PROGRESS)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700406 .setOngoing(true)
407 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700408 }
409
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700410 private Notification onVolumeUnmountable(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700411 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700412 final CharSequence title = mContext.getString(
413 R.string.ext_media_unmountable_notification_title, disk.getDescription());
414 final CharSequence text = mContext.getString(
415 R.string.ext_media_unmountable_notification_message, disk.getDescription());
416
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700417 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700418 .setContentIntent(buildInitPendingIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700419 .setCategory(Notification.CATEGORY_ERROR)
420 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700421 }
422
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700423 private Notification onVolumeRemoved(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700424 if (!vol.isPrimary()) {
425 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700426 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700427 }
428
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700429 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700430 final CharSequence title = mContext.getString(
431 R.string.ext_media_nomedia_notification_title, disk.getDescription());
432 final CharSequence text = mContext.getString(
433 R.string.ext_media_nomedia_notification_message, disk.getDescription());
434
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700435 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700436 .setCategory(Notification.CATEGORY_ERROR)
437 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700438 }
439
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700440 private Notification onVolumeBadRemoval(VolumeInfo vol) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700441 if (!vol.isPrimary()) {
442 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700443 return null;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700444 }
445
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700446 final DiskInfo disk = vol.getDisk();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700447 final CharSequence title = mContext.getString(
448 R.string.ext_media_badremoval_notification_title, disk.getDescription());
449 final CharSequence text = mContext.getString(
450 R.string.ext_media_badremoval_notification_message, disk.getDescription());
451
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700452 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700453 .setCategory(Notification.CATEGORY_ERROR)
454 .build();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700455 }
456
Jeff Sharkey50a05452015-04-29 11:24:52 -0700457 private void onMoveProgress(MoveInfo move, int status, long estMillis) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700458 final CharSequence title;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700459 if (!TextUtils.isEmpty(move.label)) {
460 title = mContext.getString(R.string.ext_media_move_specific_title, move.label);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700461 } else {
462 title = mContext.getString(R.string.ext_media_move_title);
463 }
464
465 final CharSequence text;
466 if (estMillis < 0) {
467 text = null;
468 } else {
469 text = DateUtils.formatDuration(estMillis);
470 }
471
Jeff Sharkey50a05452015-04-29 11:24:52 -0700472 final PendingIntent intent;
473 if (move.packageName != null) {
474 intent = buildWizardMovePendingIntent(move);
475 } else {
476 intent = buildWizardMigratePendingIntent(move);
477 }
478
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -0500479 Notification.Builder builder =
480 new Notification.Builder(mContext, NotificationChannels.STORAGE)
481 .setSmallIcon(R.drawable.ic_sd_card_48dp)
482 .setColor(mContext.getColor(R.color.system_notification_accent_color))
483 .setContentTitle(title)
484 .setContentText(text)
485 .setContentIntent(intent)
486 .setStyle(new Notification.BigTextStyle().bigText(text))
487 .setVisibility(Notification.VISIBILITY_PUBLIC)
488 .setLocalOnly(true)
489 .setCategory(Notification.CATEGORY_PROGRESS)
490 .setProgress(100, status, false)
491 .setOngoing(true);
Adrian Roose25c18d2016-06-17 15:59:49 -0700492 SystemUI.overrideNotificationAppName(mContext, builder);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700493
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500494 mNotificationManager.notifyAsUser(move.packageName, SystemMessage.NOTE_STORAGE_MOVE,
Adrian Roose25c18d2016-06-17 15:59:49 -0700495 builder.build(), UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700496 }
497
Jeff Sharkey50a05452015-04-29 11:24:52 -0700498 private void onMoveFinished(MoveInfo move, int status) {
499 if (move.packageName != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700500 // We currently ignore finished app moves; just clear the last
501 // published progress
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500502 mNotificationManager.cancelAsUser(move.packageName, SystemMessage.NOTE_STORAGE_MOVE,
503 UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700504 return;
505 }
506
Jeff Sharkey50a05452015-04-29 11:24:52 -0700507 final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
508 final String descrip = mStorageManager.getBestVolumeDescription(privateVol);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700509
510 final CharSequence title;
511 final CharSequence text;
512 if (status == PackageManager.MOVE_SUCCEEDED) {
513 title = mContext.getString(R.string.ext_media_move_success_title);
514 text = mContext.getString(R.string.ext_media_move_success_message, descrip);
515 } else {
516 title = mContext.getString(R.string.ext_media_move_failure_title);
517 text = mContext.getString(R.string.ext_media_move_failure_message);
518 }
519
Jeff Sharkey50a05452015-04-29 11:24:52 -0700520 // Jump back into the wizard flow if we moved to a real disk
521 final PendingIntent intent;
522 if (privateVol != null && privateVol.getDisk() != null) {
523 intent = buildWizardReadyPendingIntent(privateVol.getDisk());
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700524 } else if (privateVol != null) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700525 intent = buildVolumeSettingsPendingIntent(privateVol);
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700526 } else {
527 intent = null;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700528 }
529
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -0500530 Notification.Builder builder =
531 new Notification.Builder(mContext, NotificationChannels.STORAGE)
532 .setSmallIcon(R.drawable.ic_sd_card_48dp)
533 .setColor(mContext.getColor(R.color.system_notification_accent_color))
534 .setContentTitle(title)
535 .setContentText(text)
536 .setContentIntent(intent)
537 .setStyle(new Notification.BigTextStyle().bigText(text))
538 .setVisibility(Notification.VISIBILITY_PUBLIC)
539 .setLocalOnly(true)
540 .setCategory(Notification.CATEGORY_SYSTEM)
541 .setAutoCancel(true);
Adrian Roose25c18d2016-06-17 15:59:49 -0700542 SystemUI.overrideNotificationAppName(mContext, builder);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700543
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500544 mNotificationManager.notifyAsUser(move.packageName, SystemMessage.NOTE_STORAGE_MOVE,
545 builder.build(), UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700546 }
547
548 private int getSmallIcon(DiskInfo disk, int state) {
549 if (disk.isSd()) {
550 switch (state) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700551 case VolumeInfo.STATE_CHECKING:
552 case VolumeInfo.STATE_EJECTING:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700553 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700554 default:
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700555 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700556 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700557 } else if (disk.isUsb()) {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700558 return R.drawable.ic_usb_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700559 } else {
Jeff Sharkeya49d5fc2015-05-13 11:40:30 -0700560 return R.drawable.ic_sd_card_48dp;
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700561 }
562 }
563
564 private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title,
565 CharSequence text) {
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -0500566 Notification.Builder builder =
567 new Notification.Builder(mContext, NotificationChannels.STORAGE)
568 .setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState()))
569 .setColor(mContext.getColor(R.color.system_notification_accent_color))
570 .setContentTitle(title)
571 .setContentText(text)
572 .setStyle(new Notification.BigTextStyle().bigText(text))
573 .setVisibility(Notification.VISIBILITY_PUBLIC)
574 .setLocalOnly(true);
Adrian Roose25c18d2016-06-17 15:59:49 -0700575 overrideNotificationAppName(mContext, builder);
576 return builder;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700577 }
578
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700579 private PendingIntent buildInitPendingIntent(DiskInfo disk) {
580 final Intent intent = new Intent();
581 intent.setClassName("com.android.settings",
582 "com.android.settings.deviceinfo.StorageWizardInit");
583 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
584
585 final int requestKey = disk.getId().hashCode();
586 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
587 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
588 }
589
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700590 private PendingIntent buildInitPendingIntent(VolumeInfo vol) {
591 final Intent intent = new Intent();
592 intent.setClassName("com.android.settings",
593 "com.android.settings.deviceinfo.StorageWizardInit");
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.getActivityAsUser(mContext, requestKey, intent,
598 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700599 }
600
601 private PendingIntent buildUnmountPendingIntent(VolumeInfo vol) {
602 final Intent intent = new Intent();
603 intent.setClassName("com.android.settings",
604 "com.android.settings.deviceinfo.StorageUnmountReceiver");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700605 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
606
607 final int requestKey = vol.getId().hashCode();
608 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
609 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700610 }
611
612 private PendingIntent buildBrowsePendingIntent(VolumeInfo vol) {
613 final Intent intent = vol.buildBrowseIntent();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700614
615 final int requestKey = vol.getId().hashCode();
616 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
617 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700618 }
619
Jeff Sharkey50a05452015-04-29 11:24:52 -0700620 private PendingIntent buildVolumeSettingsPendingIntent(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700621 final Intent intent = new Intent();
Jeff Sharkey50a05452015-04-29 11:24:52 -0700622 switch (vol.getType()) {
623 case VolumeInfo.TYPE_PRIVATE:
624 intent.setClassName("com.android.settings",
625 "com.android.settings.Settings$PrivateVolumeSettingsActivity");
626 break;
627 case VolumeInfo.TYPE_PUBLIC:
628 intent.setClassName("com.android.settings",
629 "com.android.settings.Settings$PublicVolumeSettingsActivity");
630 break;
631 default:
632 return null;
633 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700634 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
635
636 final int requestKey = vol.getId().hashCode();
637 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
638 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
639 }
640
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700641 private PendingIntent buildSnoozeIntent(String fsUuid) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700642 final Intent intent = new Intent(ACTION_SNOOZE_VOLUME);
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700643 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, fsUuid);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700644
Jeff Sharkey52fc3c0f2015-06-14 20:58:54 -0700645 final int requestKey = fsUuid.hashCode();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700646 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
647 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
San Mehat64e6a452010-02-04 20:53:48 -0800648 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700649
650 private PendingIntent buildForgetPendingIntent(VolumeRecord rec) {
651 final Intent intent = new Intent();
652 intent.setClassName("com.android.settings",
653 "com.android.settings.Settings$PrivateVolumeForgetActivity");
654 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, rec.getFsUuid());
655
656 final int requestKey = rec.getFsUuid().hashCode();
657 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
658 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
659 }
Jeff Sharkey50a05452015-04-29 11:24:52 -0700660
661 private PendingIntent buildWizardMigratePendingIntent(MoveInfo move) {
662 final Intent intent = new Intent();
663 intent.setClassName("com.android.settings",
664 "com.android.settings.deviceinfo.StorageWizardMigrateProgress");
665 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
666
667 final VolumeInfo vol = mStorageManager.findVolumeByQualifiedUuid(move.volumeUuid);
Suprabh Shuklaaf6c4192016-04-27 14:05:54 -0700668 if (vol != null) {
669 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
670 }
Jeff Sharkey50a05452015-04-29 11:24:52 -0700671 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
672 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
673 }
674
675 private PendingIntent buildWizardMovePendingIntent(MoveInfo move) {
676 final Intent intent = new Intent();
677 intent.setClassName("com.android.settings",
678 "com.android.settings.deviceinfo.StorageWizardMoveProgress");
679 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
680
681 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
682 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
683 }
684
685 private PendingIntent buildWizardReadyPendingIntent(DiskInfo disk) {
686 final Intent intent = new Intent();
687 intent.setClassName("com.android.settings",
688 "com.android.settings.deviceinfo.StorageWizardReady");
689 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
690
691 final int requestKey = disk.getId().hashCode();
692 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
693 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
694 }
San Mehat64e6a452010-02-04 20:53:48 -0800695}