blob: e6c95b5dd1ea1adec7a45a609abdaea63db30b60 [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)
179 .setSmallIcon(R.drawable.stat_notify_sdcard)
180 .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 Sharkey56bd3122015-04-14 10:30:34 -0700316 .addAction(new Action(0, mContext.getString(R.string.ext_media_init_action),
Jeff Sharkey50a05452015-04-29 11:24:52 -0700317 initIntent))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700318 .addAction(new Action(0, mContext.getString(R.string.ext_media_unmount_action),
319 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700320 .setContentIntent(initIntent)
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700321 .setDeleteIntent(buildSnoozeIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700322 .setCategory(Notification.CATEGORY_SYSTEM)
323 .build();
John Spurlock209bede2013-07-17 12:23:27 -0400324
San Mehat64e6a452010-02-04 20:53:48 -0800325 } else {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700326 final CharSequence title = disk.getDescription();
327 final CharSequence text = mContext.getString(
328 R.string.ext_media_ready_notification_message, disk.getDescription());
329
Jeff Sharkey50a05452015-04-29 11:24:52 -0700330 final PendingIntent browseIntent = buildBrowsePendingIntent(vol);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700331 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700332 .addAction(new Action(0, mContext.getString(R.string.ext_media_browse_action),
Jeff Sharkey50a05452015-04-29 11:24:52 -0700333 browseIntent))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700334 .addAction(new Action(0, mContext.getString(R.string.ext_media_unmount_action),
335 buildUnmountPendingIntent(vol)))
Jeff Sharkey50a05452015-04-29 11:24:52 -0700336 .setContentIntent(browseIntent)
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700337 .setDeleteIntent(buildSnoozeIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700338 .setCategory(Notification.CATEGORY_SYSTEM)
339 .setPriority(Notification.PRIORITY_LOW)
340 .build();
San Mehat64e6a452010-02-04 20:53:48 -0800341 }
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700342 }
343
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700344 private Notification onVolumeFormatting(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700345 // Ignored
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700346 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700347 }
348
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700349 private Notification onVolumeEjecting(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700350 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700351 final CharSequence title = mContext.getString(
352 R.string.ext_media_unmounting_notification_title, disk.getDescription());
353 final CharSequence text = mContext.getString(
354 R.string.ext_media_unmounting_notification_message, disk.getDescription());
355
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700356 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700357 .setCategory(Notification.CATEGORY_PROGRESS)
358 .setPriority(Notification.PRIORITY_LOW)
359 .setOngoing(true)
360 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700361 }
362
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700363 private Notification onVolumeUnmountable(VolumeInfo vol) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700364 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700365 final CharSequence title = mContext.getString(
366 R.string.ext_media_unmountable_notification_title, disk.getDescription());
367 final CharSequence text = mContext.getString(
368 R.string.ext_media_unmountable_notification_message, disk.getDescription());
369
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700370 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700371 .setContentIntent(buildVolumeSettingsPendingIntent(vol))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700372 .setCategory(Notification.CATEGORY_ERROR)
373 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700374 }
375
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700376 private Notification onVolumeRemoved(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700377 if (!vol.isPrimary()) {
378 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700379 return null;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700380 }
381
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700382 final DiskInfo disk = vol.getDisk();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700383 final CharSequence title = mContext.getString(
384 R.string.ext_media_nomedia_notification_title, disk.getDescription());
385 final CharSequence text = mContext.getString(
386 R.string.ext_media_nomedia_notification_message, disk.getDescription());
387
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700388 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700389 .setCategory(Notification.CATEGORY_ERROR)
390 .build();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700391 }
392
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700393 private Notification onVolumeBadRemoval(VolumeInfo vol) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700394 if (!vol.isPrimary()) {
395 // Ignore non-primary media
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700396 return null;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700397 }
398
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700399 final DiskInfo disk = vol.getDisk();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700400 final CharSequence title = mContext.getString(
401 R.string.ext_media_badremoval_notification_title, disk.getDescription());
402 final CharSequence text = mContext.getString(
403 R.string.ext_media_badremoval_notification_message, disk.getDescription());
404
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700405 return buildNotificationBuilder(vol, title, text)
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700406 .setCategory(Notification.CATEGORY_ERROR)
407 .build();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700408 }
409
Jeff Sharkey50a05452015-04-29 11:24:52 -0700410 private void onMoveProgress(MoveInfo move, int status, long estMillis) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700411 final CharSequence title;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700412 if (!TextUtils.isEmpty(move.label)) {
413 title = mContext.getString(R.string.ext_media_move_specific_title, move.label);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700414 } else {
415 title = mContext.getString(R.string.ext_media_move_title);
416 }
417
418 final CharSequence text;
419 if (estMillis < 0) {
420 text = null;
421 } else {
422 text = DateUtils.formatDuration(estMillis);
423 }
424
Jeff Sharkey50a05452015-04-29 11:24:52 -0700425 final PendingIntent intent;
426 if (move.packageName != null) {
427 intent = buildWizardMovePendingIntent(move);
428 } else {
429 intent = buildWizardMigratePendingIntent(move);
430 }
431
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700432 final Notification notif = new Notification.Builder(mContext)
433 .setSmallIcon(R.drawable.stat_notify_sdcard)
434 .setColor(mContext.getColor(R.color.system_notification_accent_color))
435 .setContentTitle(title)
436 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700437 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700438 .setStyle(new Notification.BigTextStyle().bigText(text))
439 .setVisibility(Notification.VISIBILITY_PUBLIC)
440 .setLocalOnly(true)
441 .setCategory(Notification.CATEGORY_PROGRESS)
442 .setPriority(Notification.PRIORITY_LOW)
443 .setProgress(100, status, false)
444 .setOngoing(true)
445 .build();
446
Jeff Sharkey50a05452015-04-29 11:24:52 -0700447 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700448 }
449
Jeff Sharkey50a05452015-04-29 11:24:52 -0700450 private void onMoveFinished(MoveInfo move, int status) {
451 if (move.packageName != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700452 // We currently ignore finished app moves; just clear the last
453 // published progress
Jeff Sharkey50a05452015-04-29 11:24:52 -0700454 mNotificationManager.cancelAsUser(move.packageName, MOVE_ID, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700455 return;
456 }
457
Jeff Sharkey50a05452015-04-29 11:24:52 -0700458 final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
459 final String descrip = mStorageManager.getBestVolumeDescription(privateVol);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700460
461 final CharSequence title;
462 final CharSequence text;
463 if (status == PackageManager.MOVE_SUCCEEDED) {
464 title = mContext.getString(R.string.ext_media_move_success_title);
465 text = mContext.getString(R.string.ext_media_move_success_message, descrip);
466 } else {
467 title = mContext.getString(R.string.ext_media_move_failure_title);
468 text = mContext.getString(R.string.ext_media_move_failure_message);
469 }
470
Jeff Sharkey50a05452015-04-29 11:24:52 -0700471 // Jump back into the wizard flow if we moved to a real disk
472 final PendingIntent intent;
473 if (privateVol != null && privateVol.getDisk() != null) {
474 intent = buildWizardReadyPendingIntent(privateVol.getDisk());
475 } else {
476 intent = buildVolumeSettingsPendingIntent(privateVol);
477 }
478
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700479 final Notification notif = new Notification.Builder(mContext)
480 .setSmallIcon(R.drawable.stat_notify_sdcard)
481 .setColor(mContext.getColor(R.color.system_notification_accent_color))
482 .setContentTitle(title)
483 .setContentText(text)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700484 .setContentIntent(intent)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700485 .setStyle(new Notification.BigTextStyle().bigText(text))
486 .setVisibility(Notification.VISIBILITY_PUBLIC)
487 .setLocalOnly(true)
488 .setCategory(Notification.CATEGORY_SYSTEM)
489 .setPriority(Notification.PRIORITY_LOW)
Jeff Sharkey50a05452015-04-29 11:24:52 -0700490 .setAutoCancel(true)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700491 .build();
492
Jeff Sharkey50a05452015-04-29 11:24:52 -0700493 mNotificationManager.notifyAsUser(move.packageName, MOVE_ID, notif, UserHandle.ALL);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700494 }
495
496 private int getSmallIcon(DiskInfo disk, int state) {
497 if (disk.isSd()) {
498 switch (state) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700499 case VolumeInfo.STATE_CHECKING:
500 case VolumeInfo.STATE_EJECTING:
501 return R.drawable.stat_notify_sdcard_prepare;
502 default:
503 return R.drawable.stat_notify_sdcard;
504 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700505 } else if (disk.isUsb()) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700506 return R.drawable.stat_sys_data_usb;
507 } else {
508 return R.drawable.stat_notify_sdcard;
509 }
510 }
511
512 private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title,
513 CharSequence text) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700514 return new Notification.Builder(mContext)
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700515 .setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState()))
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700516 .setColor(mContext.getColor(R.color.system_notification_accent_color))
517 .setContentTitle(title)
518 .setContentText(text)
519 .setStyle(new Notification.BigTextStyle().bigText(text))
520 .setVisibility(Notification.VISIBILITY_PUBLIC)
521 .setLocalOnly(true);
522 }
523
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700524 private PendingIntent buildInitPendingIntent(DiskInfo disk) {
525 final Intent intent = new Intent();
526 intent.setClassName("com.android.settings",
527 "com.android.settings.deviceinfo.StorageWizardInit");
528 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
529
530 final int requestKey = disk.getId().hashCode();
531 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
532 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
533 }
534
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700535 private PendingIntent buildInitPendingIntent(VolumeInfo vol) {
536 final Intent intent = new Intent();
537 intent.setClassName("com.android.settings",
538 "com.android.settings.deviceinfo.StorageWizardInit");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700539 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
540
541 final int requestKey = vol.getId().hashCode();
542 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
543 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700544 }
545
546 private PendingIntent buildUnmountPendingIntent(VolumeInfo vol) {
547 final Intent intent = new Intent();
548 intent.setClassName("com.android.settings",
549 "com.android.settings.deviceinfo.StorageUnmountReceiver");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700550 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
551
552 final int requestKey = vol.getId().hashCode();
553 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
554 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700555 }
556
557 private PendingIntent buildBrowsePendingIntent(VolumeInfo vol) {
558 final Intent intent = vol.buildBrowseIntent();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700559
560 final int requestKey = vol.getId().hashCode();
561 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
562 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700563 }
564
Jeff Sharkey50a05452015-04-29 11:24:52 -0700565 private PendingIntent buildVolumeSettingsPendingIntent(VolumeInfo vol) {
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700566 final Intent intent = new Intent();
Jeff Sharkey50a05452015-04-29 11:24:52 -0700567 switch (vol.getType()) {
568 case VolumeInfo.TYPE_PRIVATE:
569 intent.setClassName("com.android.settings",
570 "com.android.settings.Settings$PrivateVolumeSettingsActivity");
571 break;
572 case VolumeInfo.TYPE_PUBLIC:
573 intent.setClassName("com.android.settings",
574 "com.android.settings.Settings$PublicVolumeSettingsActivity");
575 break;
576 default:
577 return null;
578 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700579 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
580
581 final int requestKey = vol.getId().hashCode();
582 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
583 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
584 }
585
586 private PendingIntent buildSnoozeIntent(VolumeInfo vol) {
587 final Intent intent = new Intent(ACTION_SNOOZE_VOLUME);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700588 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, vol.getFsUuid());
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700589
590 final int requestKey = vol.getId().hashCode();
591 return PendingIntent.getBroadcastAsUser(mContext, requestKey, intent,
592 PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.CURRENT);
San Mehat64e6a452010-02-04 20:53:48 -0800593 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700594
595 private PendingIntent buildForgetPendingIntent(VolumeRecord rec) {
596 final Intent intent = new Intent();
597 intent.setClassName("com.android.settings",
598 "com.android.settings.Settings$PrivateVolumeForgetActivity");
599 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, rec.getFsUuid());
600
601 final int requestKey = rec.getFsUuid().hashCode();
602 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
603 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
604 }
Jeff Sharkey50a05452015-04-29 11:24:52 -0700605
606 private PendingIntent buildWizardMigratePendingIntent(MoveInfo move) {
607 final Intent intent = new Intent();
608 intent.setClassName("com.android.settings",
609 "com.android.settings.deviceinfo.StorageWizardMigrateProgress");
610 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
611
612 final VolumeInfo vol = mStorageManager.findVolumeByQualifiedUuid(move.volumeUuid);
613 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.getId());
614
615 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
616 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
617 }
618
619 private PendingIntent buildWizardMovePendingIntent(MoveInfo move) {
620 final Intent intent = new Intent();
621 intent.setClassName("com.android.settings",
622 "com.android.settings.deviceinfo.StorageWizardMoveProgress");
623 intent.putExtra(PackageManager.EXTRA_MOVE_ID, move.moveId);
624
625 return PendingIntent.getActivityAsUser(mContext, move.moveId, intent,
626 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
627 }
628
629 private PendingIntent buildWizardReadyPendingIntent(DiskInfo disk) {
630 final Intent intent = new Intent();
631 intent.setClassName("com.android.settings",
632 "com.android.settings.deviceinfo.StorageWizardReady");
633 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.getId());
634
635 final int requestKey = disk.getId().hashCode();
636 return PendingIntent.getActivityAsUser(mContext, requestKey, intent,
637 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
638 }
San Mehat64e6a452010-02-04 20:53:48 -0800639}