San Mehat | 64e6a45 | 2010-02-04 20:53:48 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. |
| 3 | * |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.Notification; |
| 21 | import android.app.NotificationManager; |
| 22 | import android.app.PendingIntent; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.res.Resources; |
| 30 | import android.os.Bundle; |
| 31 | import android.os.Environment; |
| 32 | import android.os.Handler; |
| 33 | import android.os.IMountService; |
| 34 | import android.os.Message; |
| 35 | import android.os.MountServiceResultCode; |
| 36 | import android.os.ServiceManager; |
| 37 | import android.storage.StorageEventListener; |
| 38 | import android.storage.StorageManager; |
| 39 | import android.util.Log; |
| 40 | import android.view.View; |
| 41 | import android.widget.Button; |
| 42 | import android.widget.ImageView; |
| 43 | import android.widget.TextView; |
| 44 | import android.widget.Toast; |
| 45 | |
| 46 | public class StorageNotification implements StorageEventListener { |
| 47 | private static final String TAG = "StorageNotification"; |
| 48 | |
| 49 | /** |
| 50 | * Binder context for this service |
| 51 | */ |
| 52 | private Context mContext; |
| 53 | |
| 54 | /** |
| 55 | * The notification that is shown when a USB mass storage host |
| 56 | * is connected. |
| 57 | * <p> |
| 58 | * This is lazily created, so use {@link #setUsbStorageNotification()}. |
| 59 | */ |
| 60 | private Notification mUsbStorageNotification; |
| 61 | |
| 62 | /** |
| 63 | * The notification that is shown when the following media events occur: |
| 64 | * - Media is being checked |
| 65 | * - Media is blank (or unknown filesystem) |
| 66 | * - Media is corrupt |
| 67 | * - Media is safe to unmount |
| 68 | * - Media is missing |
| 69 | * <p> |
| 70 | * This is lazily created, so use {@link #setMediaStorageNotification()}. |
| 71 | */ |
| 72 | private Notification mMediaStorageNotification; |
| 73 | |
| 74 | private boolean mShowSafeUnmountNotificationWhenUnmounted; |
| 75 | private boolean mUmsAvailable; |
| 76 | private IMountService mMountService; // XXX: This should go away soon |
| 77 | |
| 78 | public StorageNotification(Context context) { |
| 79 | mContext = context; |
| 80 | |
| 81 | /* |
| 82 | * XXX: This needs to be exposed via StorageManager |
| 83 | */ |
| 84 | mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); |
| 85 | try { |
| 86 | mUmsAvailable = mMountService.getShareMethodAvailable("ums"); |
| 87 | } catch (Exception e) { |
| 88 | Log.e(TAG, "Failed to get ums availability", e); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public void onShareAvailabilityChanged(String method, boolean available) { |
| 93 | if (method.equals("ums")) { |
| 94 | mUmsAvailable = available; |
| 95 | /* |
| 96 | * Even though we may have a UMS host connected, we the SD card |
| 97 | * may not be in a state for export. |
| 98 | */ |
| 99 | String st = Environment.getExternalStorageState(); |
| 100 | if (available && (st.equals( |
| 101 | Environment.MEDIA_REMOVED) || st.equals(Environment.MEDIA_CHECKING))) { |
| 102 | /* |
| 103 | * No card or card being checked = don't display |
| 104 | */ |
| 105 | available = false; |
| 106 | } |
| 107 | |
| 108 | updateUsbMassStorageNotification(available); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public void onMediaInserted(String label, String path, int major, int minor) { |
| 113 | } |
| 114 | |
| 115 | public void onMediaRemoved(String label, String path, int major, int minor, boolean clean) { |
| 116 | /* |
| 117 | * Media removed - first clear the USB storage notification (if any) |
| 118 | */ |
| 119 | updateUsbMassStorageNotification(false); |
| 120 | |
| 121 | if (clean) { |
| 122 | setMediaStorageNotification( |
| 123 | com.android.internal.R.string.ext_media_nomedia_notification_title, |
| 124 | com.android.internal.R.string.ext_media_nomedia_notification_message, |
| 125 | com.android.internal.R.drawable.stat_notify_sdcard_usb, |
| 126 | true, false, null); |
| 127 | } else { |
| 128 | setMediaStorageNotification( |
| 129 | com.android.internal.R.string.ext_media_badremoval_notification_title, |
| 130 | com.android.internal.R.string.ext_media_badremoval_notification_message, |
| 131 | com.android.internal.R.drawable.stat_sys_warning, |
| 132 | true, true, null); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public void onVolumeStateChanged(String label, String path, String oldState, String newState) { |
| 137 | if (newState.equals(Environment.MEDIA_SHARED)) { |
| 138 | Intent intent = new Intent(); |
| 139 | intent.setClass(mContext, com.android.internal.app.UsbStorageActivity.class); |
| 140 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 141 | setUsbStorageNotification( |
| 142 | com.android.internal.R.string.usb_storage_stop_notification_title, |
| 143 | com.android.internal.R.string.usb_storage_stop_notification_message, |
| 144 | com.android.internal.R.drawable.stat_sys_warning, false, true, pi); |
| 145 | } else if (newState.equals(Environment.MEDIA_CHECKING)) { |
| 146 | setMediaStorageNotification( |
| 147 | com.android.internal.R.string.ext_media_checking_notification_title, |
| 148 | com.android.internal.R.string.ext_media_checking_notification_message, |
| 149 | com.android.internal.R.drawable.stat_notify_sdcard_prepare, true, false, null); |
| 150 | updateUsbMassStorageNotification(false); |
| 151 | } else if (newState.equals(Environment.MEDIA_MOUNTED)) { |
| 152 | setMediaStorageNotification(0, 0, 0, false, false, null); |
| 153 | updateUsbMassStorageNotification(mUmsAvailable); |
| 154 | } else if (newState.equals(Environment.MEDIA_UNMOUNTED)) { |
| 155 | if (mShowSafeUnmountNotificationWhenUnmounted) { |
| 156 | setMediaStorageNotification( |
| 157 | com.android.internal.R.string.ext_media_safe_unmount_notification_title, |
| 158 | com.android.internal.R.string.ext_media_safe_unmount_notification_message, |
| 159 | com.android.internal.R.drawable.stat_notify_sdcard, true, true, null); |
| 160 | mShowSafeUnmountNotificationWhenUnmounted = false; |
| 161 | } else { |
| 162 | setMediaStorageNotification(0, 0, 0, false, false, null); |
| 163 | } |
| 164 | updateUsbMassStorageNotification(mUmsAvailable); |
| 165 | } else if (newState.equals(Environment.MEDIA_NOFS)) { |
| 166 | Intent intent = new Intent(); |
| 167 | intent.setClass(mContext, com.android.internal.app.ExternalMediaFormatActivity.class); |
| 168 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 169 | |
| 170 | setMediaStorageNotification( |
| 171 | com.android.internal.R.string.ext_media_nofs_notification_title, |
| 172 | com.android.internal.R.string.ext_media_nofs_notification_message, |
| 173 | com.android.internal.R.drawable.stat_notify_sdcard_usb, true, false, pi); |
| 174 | updateUsbMassStorageNotification(mUmsAvailable); |
| 175 | } else if (newState.equals(Environment.MEDIA_UNMOUNTABLE)) { |
| 176 | Intent intent = new Intent(); |
| 177 | intent.setClass(mContext, com.android.internal.app.ExternalMediaFormatActivity.class); |
| 178 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 179 | |
| 180 | setMediaStorageNotification( |
| 181 | com.android.internal.R.string.ext_media_unmountable_notification_title, |
| 182 | com.android.internal.R.string.ext_media_unmountable_notification_message, |
| 183 | com.android.internal.R.drawable.stat_notify_sdcard_usb, true, false, pi); |
| 184 | updateUsbMassStorageNotification(mUmsAvailable); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Update the state of the USB mass storage notification |
| 190 | */ |
| 191 | void updateUsbMassStorageNotification(boolean available) { |
| 192 | |
| 193 | if (available) { |
| 194 | Intent intent = new Intent(); |
| 195 | intent.setClass(mContext, com.android.internal.app.UsbStorageActivity.class); |
| 196 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 197 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 198 | setUsbStorageNotification( |
| 199 | com.android.internal.R.string.usb_storage_notification_title, |
| 200 | com.android.internal.R.string.usb_storage_notification_message, |
| 201 | com.android.internal.R.drawable.stat_sys_data_usb, |
| 202 | false, true, pi); |
| 203 | } else { |
| 204 | setUsbStorageNotification(0, 0, 0, false, false, null); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Sets the USB storage notification. |
| 210 | */ |
| 211 | private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon, boolean sound, boolean visible, |
| 212 | PendingIntent pi) { |
| 213 | |
| 214 | if (!visible && mUsbStorageNotification == null) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | NotificationManager notificationManager = (NotificationManager) mContext |
| 219 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 220 | |
| 221 | if (notificationManager == null) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | if (visible) { |
| 226 | Resources r = Resources.getSystem(); |
| 227 | CharSequence title = r.getText(titleId); |
| 228 | CharSequence message = r.getText(messageId); |
| 229 | |
| 230 | if (mUsbStorageNotification == null) { |
| 231 | mUsbStorageNotification = new Notification(); |
| 232 | mUsbStorageNotification.icon = icon; |
| 233 | mUsbStorageNotification.when = 0; |
| 234 | } |
| 235 | |
| 236 | if (sound) { |
| 237 | mUsbStorageNotification.defaults |= Notification.DEFAULT_SOUND; |
| 238 | } else { |
| 239 | mUsbStorageNotification.defaults &= ~Notification.DEFAULT_SOUND; |
| 240 | } |
| 241 | |
| 242 | mUsbStorageNotification.flags = Notification.FLAG_ONGOING_EVENT; |
| 243 | |
| 244 | mUsbStorageNotification.tickerText = title; |
| 245 | if (pi == null) { |
| 246 | Intent intent = new Intent(); |
| 247 | pi = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| 248 | } |
| 249 | |
| 250 | mUsbStorageNotification.setLatestEventInfo(mContext, title, message, pi); |
| 251 | } |
| 252 | |
| 253 | final int notificationId = mUsbStorageNotification.icon; |
| 254 | if (visible) { |
| 255 | notificationManager.notify(notificationId, mUsbStorageNotification); |
| 256 | } else { |
| 257 | notificationManager.cancel(notificationId); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private synchronized boolean getMediaStorageNotificationDismissable() { |
| 262 | if ((mMediaStorageNotification != null) && |
| 263 | ((mMediaStorageNotification.flags & Notification.FLAG_AUTO_CANCEL) == |
| 264 | Notification.FLAG_AUTO_CANCEL)) |
| 265 | return true; |
| 266 | |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Sets the media storage notification. |
| 272 | */ |
| 273 | private synchronized void setMediaStorageNotification(int titleId, int messageId, int icon, boolean visible, |
| 274 | boolean dismissable, PendingIntent pi) { |
| 275 | |
| 276 | if (!visible && mMediaStorageNotification == null) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | NotificationManager notificationManager = (NotificationManager) mContext |
| 281 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 282 | |
| 283 | if (notificationManager == null) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | if (mMediaStorageNotification != null && visible) { |
| 288 | /* |
| 289 | * Dismiss the previous notification - we're about to |
| 290 | * re-use it. |
| 291 | */ |
| 292 | final int notificationId = mMediaStorageNotification.icon; |
| 293 | notificationManager.cancel(notificationId); |
| 294 | } |
| 295 | |
| 296 | if (visible) { |
| 297 | Resources r = Resources.getSystem(); |
| 298 | CharSequence title = r.getText(titleId); |
| 299 | CharSequence message = r.getText(messageId); |
| 300 | |
| 301 | if (mMediaStorageNotification == null) { |
| 302 | mMediaStorageNotification = new Notification(); |
| 303 | mMediaStorageNotification.when = 0; |
| 304 | } |
| 305 | |
| 306 | mMediaStorageNotification.defaults &= ~Notification.DEFAULT_SOUND; |
| 307 | |
| 308 | if (dismissable) { |
| 309 | mMediaStorageNotification.flags = Notification.FLAG_AUTO_CANCEL; |
| 310 | } else { |
| 311 | mMediaStorageNotification.flags = Notification.FLAG_ONGOING_EVENT; |
| 312 | } |
| 313 | |
| 314 | mMediaStorageNotification.tickerText = title; |
| 315 | if (pi == null) { |
| 316 | Intent intent = new Intent(); |
| 317 | pi = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| 318 | } |
| 319 | |
| 320 | mMediaStorageNotification.icon = icon; |
| 321 | mMediaStorageNotification.setLatestEventInfo(mContext, title, message, pi); |
| 322 | } |
| 323 | |
| 324 | final int notificationId = mMediaStorageNotification.icon; |
| 325 | if (visible) { |
| 326 | notificationManager.notify(notificationId, mMediaStorageNotification); |
| 327 | } else { |
| 328 | notificationManager.cancel(notificationId); |
| 329 | } |
| 330 | } |
| 331 | } |