The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 The Android Open Source Project |
| 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 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 17 | package com.android.server.storage; |
| 18 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 19 | import android.annotation.WorkerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.app.Notification; |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 21 | import android.app.NotificationChannel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.NotificationManager; |
| 23 | import android.app.PendingIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 27 | import android.net.TrafficStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.os.Binder; |
Dianne Hackborn | f882efa | 2012-04-11 16:04:12 -0700 | [diff] [blame] | 29 | import android.os.Environment; |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame] | 30 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 33 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.ServiceManager; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 35 | import android.os.ShellCallback; |
| 36 | import android.os.ShellCommand; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 37 | import android.os.UserHandle; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 38 | import android.os.storage.StorageManager; |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 39 | import android.os.storage.VolumeInfo; |
| 40 | import android.text.format.DateUtils; |
| 41 | import android.util.ArrayMap; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 42 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 44 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
| 45 | import com.android.internal.notification.SystemNotificationChannels; |
| 46 | import com.android.internal.util.DumpUtils; |
| 47 | import com.android.internal.util.IndentingPrintWriter; |
| 48 | import com.android.server.EventLogTags; |
| 49 | import com.android.server.IoThread; |
| 50 | import com.android.server.SystemService; |
| 51 | import com.android.server.pm.InstructionSets; |
| 52 | import com.android.server.pm.PackageManagerService; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 53 | |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 54 | import dalvik.system.VMRuntime; |
| 55 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 56 | import java.io.File; |
| 57 | import java.io.FileDescriptor; |
| 58 | import java.io.IOException; |
| 59 | import java.io.PrintWriter; |
| 60 | import java.util.Objects; |
| 61 | import java.util.UUID; |
| 62 | import java.util.concurrent.atomic.AtomicInteger; |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 65 | * Service that monitors and maintains free space on storage volumes. |
| 66 | * <p> |
| 67 | * As the free space on a volume nears the threshold defined by |
| 68 | * {@link StorageManager#getStorageLowBytes(File)}, this service will clear out |
| 69 | * cached data to keep the disk from entering this low state. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 71 | public class DeviceStorageMonitorService extends SystemService { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 72 | private static final String TAG = "DeviceStorageMonitorService"; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 73 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}: |
| 76 | * Current int sequence number of the update. |
| 77 | */ |
| 78 | public static final String EXTRA_SEQUENCE = "seq"; |
| 79 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 80 | private static final int MSG_CHECK = 1; |
Jeff Sharkey | 529f91f | 2015-04-18 20:23:13 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 82 | private static final long DEFAULT_LOG_DELTA_BYTES = 64 * TrafficStats.MB_IN_BYTES; |
| 83 | private static final long DEFAULT_CHECK_INTERVAL = DateUtils.MINUTE_IN_MILLIS; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 84 | |
Richard Uhler | d42fe85 | 2016-08-12 13:51:51 -0700 | [diff] [blame] | 85 | // com.android.internal.R.string.low_internal_storage_view_text_no_boot |
| 86 | // hard codes 250MB in the message as the storage space required for the |
| 87 | // boot image. |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 88 | private static final long BOOT_IMAGE_STORAGE_REQUIREMENT = 250 * TrafficStats.MB_IN_BYTES; |
Richard Uhler | d42fe85 | 2016-08-12 13:51:51 -0700 | [diff] [blame] | 89 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 90 | private NotificationManager mNotifManager; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 91 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 92 | /** Sequence number used for testing */ |
| 93 | private final AtomicInteger mSeq = new AtomicInteger(1); |
| 94 | /** Forced level used for testing */ |
| 95 | private volatile int mForceLevel = State.LEVEL_UNKNOWN; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 96 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 97 | /** Map from storage volume UUID to internal state */ |
| 98 | private final ArrayMap<UUID, State> mStates = new ArrayMap<>(); |
| 99 | |
| 100 | /** |
| 101 | * State for a specific storage volume, including the current "level" that |
| 102 | * we've alerted the user and apps about. |
| 103 | */ |
| 104 | private static class State { |
| 105 | private static final int LEVEL_UNKNOWN = -1; |
| 106 | private static final int LEVEL_NORMAL = 0; |
| 107 | private static final int LEVEL_LOW = 1; |
| 108 | private static final int LEVEL_FULL = 2; |
| 109 | |
| 110 | /** Last "level" that we alerted about */ |
| 111 | public int level = LEVEL_NORMAL; |
| 112 | /** Last {@link File#getUsableSpace()} that we logged about */ |
| 113 | public long lastUsableBytes = Long.MAX_VALUE; |
| 114 | |
| 115 | /** |
| 116 | * Test if the given level transition is "entering" a specific level. |
| 117 | * <p> |
| 118 | * As an example, a transition from {@link #LEVEL_NORMAL} to |
| 119 | * {@link #LEVEL_FULL} is considered to "enter" both {@link #LEVEL_LOW} |
| 120 | * and {@link #LEVEL_FULL}. |
| 121 | */ |
| 122 | private static boolean isEntering(int level, int oldLevel, int newLevel) { |
| 123 | return newLevel >= level && (oldLevel < level || oldLevel == LEVEL_UNKNOWN); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Test if the given level transition is "leaving" a specific level. |
| 128 | * <p> |
| 129 | * As an example, a transition from {@link #LEVEL_FULL} to |
| 130 | * {@link #LEVEL_NORMAL} is considered to "leave" both |
| 131 | * {@link #LEVEL_FULL} and {@link #LEVEL_LOW}. |
| 132 | */ |
| 133 | private static boolean isLeaving(int level, int oldLevel, int newLevel) { |
| 134 | return newLevel < level && (oldLevel >= level || oldLevel == LEVEL_UNKNOWN); |
| 135 | } |
| 136 | |
| 137 | private static String levelToString(int level) { |
| 138 | switch (level) { |
| 139 | case State.LEVEL_UNKNOWN: return "UNKNOWN"; |
| 140 | case State.LEVEL_NORMAL: return "NORMAL"; |
| 141 | case State.LEVEL_LOW: return "LOW"; |
| 142 | case State.LEVEL_FULL: return "FULL"; |
| 143 | default: return Integer.toString(level); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 148 | private CacheFileDeletedObserver mCacheFileDeletedObserver; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | /** |
| 151 | * This string is used for ServiceManager access to this class. |
| 152 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 153 | static final String SERVICE = "devicestoragemonitor"; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 154 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 155 | private static final String TV_NOTIFICATION_CHANNEL_ID = "devicestoragemonitor.tv"; |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | /** |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 158 | * Handler that checks the amount of disk space on the device and sends a |
| 159 | * notification if the device runs low on disk space |
| 160 | */ |
| 161 | private final Handler mHandler = new Handler(IoThread.get().getLooper()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | @Override |
| 163 | public void handleMessage(Message msg) { |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 164 | switch (msg.what) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 165 | case MSG_CHECK: |
| 166 | check(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 167 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
| 170 | }; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 171 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 172 | private State findOrCreateState(UUID uuid) { |
| 173 | State state = mStates.get(uuid); |
| 174 | if (state == null) { |
| 175 | state = new State(); |
| 176 | mStates.put(uuid, state); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 177 | } |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 178 | return state; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 181 | /** |
| 182 | * Core logic that checks the storage state of every mounted private volume. |
| 183 | * Since this can do heavy I/O, callers should invoke indirectly using |
| 184 | * {@link #MSG_CHECK}. |
| 185 | */ |
| 186 | @WorkerThread |
| 187 | private void check() { |
| 188 | final StorageManager storage = getContext().getSystemService(StorageManager.class); |
| 189 | final int seq = mSeq.get(); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 190 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 191 | // Check every mounted private volume to see if they're low on space |
| 192 | for (VolumeInfo vol : storage.getWritablePrivateVolumes()) { |
| 193 | final File file = vol.getPath(); |
| 194 | final long fullBytes = storage.getStorageFullBytes(file); |
| 195 | final long lowBytes = storage.getStorageLowBytes(file); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 196 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 197 | // Automatically trim cached data when nearing the low threshold; |
| 198 | // when it's within 150% of the threshold, we try trimming usage |
| 199 | // back to 200% of the threshold. |
| 200 | if (file.getUsableSpace() < (lowBytes * 3) / 2) { |
| 201 | final PackageManagerService pms = (PackageManagerService) ServiceManager |
| 202 | .getService("package"); |
| 203 | try { |
| 204 | pms.freeStorage(vol.getFsUuid(), lowBytes * 2, 0); |
| 205 | } catch (IOException e) { |
| 206 | Slog.w(TAG, e); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 209 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 210 | // Send relevant broadcasts and show notifications based on any |
| 211 | // recently noticed state transitions. |
| 212 | final UUID uuid = StorageManager.convert(vol.getFsUuid()); |
| 213 | final State state = findOrCreateState(uuid); |
| 214 | final long totalBytes = file.getTotalSpace(); |
| 215 | final long usableBytes = file.getUsableSpace(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 216 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 217 | int oldLevel = state.level; |
| 218 | int newLevel; |
| 219 | if (mForceLevel != State.LEVEL_UNKNOWN) { |
| 220 | // When in testing mode, use unknown old level to force sending |
| 221 | // of any relevant broadcasts. |
| 222 | oldLevel = State.LEVEL_UNKNOWN; |
| 223 | newLevel = mForceLevel; |
| 224 | } else if (usableBytes <= fullBytes) { |
| 225 | newLevel = State.LEVEL_FULL; |
| 226 | } else if (usableBytes <= lowBytes) { |
| 227 | newLevel = State.LEVEL_LOW; |
| 228 | } else if (StorageManager.UUID_DEFAULT.equals(uuid) && !isBootImageOnDisk() |
| 229 | && usableBytes < BOOT_IMAGE_STORAGE_REQUIREMENT) { |
| 230 | newLevel = State.LEVEL_LOW; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } else { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 232 | newLevel = State.LEVEL_NORMAL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 234 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 235 | // Log whenever we notice drastic storage changes |
| 236 | if ((Math.abs(state.lastUsableBytes - usableBytes) > DEFAULT_LOG_DELTA_BYTES) |
| 237 | || oldLevel != newLevel) { |
| 238 | EventLogTags.writeStorageState(uuid.toString(), oldLevel, newLevel, |
| 239 | usableBytes, totalBytes); |
| 240 | state.lastUsableBytes = usableBytes; |
| 241 | } |
| 242 | |
| 243 | updateNotifications(vol, oldLevel, newLevel); |
| 244 | updateBroadcasts(vol, oldLevel, newLevel, seq); |
| 245 | |
| 246 | state.level = newLevel; |
| 247 | } |
| 248 | |
| 249 | // Loop around to check again in future; we don't remove messages since |
| 250 | // there might be an immediate request pending. |
| 251 | if (!mHandler.hasMessages(MSG_CHECK)) { |
| 252 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CHECK), |
| 253 | DEFAULT_CHECK_INTERVAL); |
| 254 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 256 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 257 | public DeviceStorageMonitorService(Context context) { |
| 258 | super(context); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 259 | } |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 260 | |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 261 | private static boolean isBootImageOnDisk() { |
Fyodor Kupolov | eeea67b | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 262 | for (String instructionSet : InstructionSets.getAllDexCodeInstructionSets()) { |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 263 | if (!VMRuntime.isBootClassPathOnDisk(instructionSet)) { |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 270 | @Override |
| 271 | public void onStart() { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 272 | final Context context = getContext(); |
| 273 | mNotifManager = context.getSystemService(NotificationManager.class); |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame] | 274 | |
| 275 | mCacheFileDeletedObserver = new CacheFileDeletedObserver(); |
| 276 | mCacheFileDeletedObserver.startWatching(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 277 | |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 278 | // Ensure that the notification channel is set up |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 279 | PackageManager packageManager = context.getPackageManager(); |
| 280 | boolean isTv = packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK); |
| 281 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 282 | if (isTv) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 283 | mNotifManager.createNotificationChannel(new NotificationChannel( |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 284 | TV_NOTIFICATION_CHANNEL_ID, |
| 285 | context.getString( |
| 286 | com.android.internal.R.string.device_storage_monitor_notification_channel), |
| 287 | NotificationManager.IMPORTANCE_HIGH)); |
| 288 | } |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 289 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 290 | publishBinderService(SERVICE, mRemoteService); |
| 291 | publishLocalService(DeviceStorageMonitorInternal.class, mLocalService); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 292 | |
| 293 | // Kick off pass to examine storage state |
| 294 | mHandler.removeMessages(MSG_CHECK); |
| 295 | mHandler.obtainMessage(MSG_CHECK).sendToTarget(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private final DeviceStorageMonitorInternal mLocalService = new DeviceStorageMonitorInternal() { |
| 299 | @Override |
| 300 | public void checkMemory() { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 301 | // Kick off pass to examine storage state |
| 302 | mHandler.removeMessages(MSG_CHECK); |
| 303 | mHandler.obtainMessage(MSG_CHECK).sendToTarget(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public boolean isMemoryLow() { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 308 | return Environment.getDataDirectory().getUsableSpace() < getMemoryLowThreshold(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public long getMemoryLowThreshold() { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 313 | return getContext().getSystemService(StorageManager.class) |
| 314 | .getStorageLowBytes(Environment.getDataDirectory()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 315 | } |
| 316 | }; |
| 317 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 318 | private final Binder mRemoteService = new Binder() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 319 | @Override |
| 320 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 321 | if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 322 | dumpImpl(fd, pw, args); |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 327 | FileDescriptor err, String[] args, ShellCallback callback, |
| 328 | ResultReceiver resultReceiver) { |
| 329 | (new Shell()).exec(this, in, out, err, args, callback, resultReceiver); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 330 | } |
| 331 | }; |
| 332 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 333 | class Shell extends ShellCommand { |
| 334 | @Override |
| 335 | public int onCommand(String cmd) { |
| 336 | return onShellCommand(this, cmd); |
| 337 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 338 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 339 | @Override |
| 340 | public void onHelp() { |
| 341 | PrintWriter pw = getOutPrintWriter(); |
| 342 | dumpHelp(pw); |
| 343 | } |
| 344 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 345 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 346 | static final int OPTION_FORCE_UPDATE = 1<<0; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 347 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 348 | int parseOptions(Shell shell) { |
| 349 | String opt; |
| 350 | int opts = 0; |
| 351 | while ((opt = shell.getNextOption()) != null) { |
| 352 | if ("-f".equals(opt)) { |
| 353 | opts |= OPTION_FORCE_UPDATE; |
| 354 | } |
| 355 | } |
| 356 | return opts; |
| 357 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 358 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 359 | int onShellCommand(Shell shell, String cmd) { |
| 360 | if (cmd == null) { |
| 361 | return shell.handleDefaultCommands(cmd); |
| 362 | } |
| 363 | PrintWriter pw = shell.getOutPrintWriter(); |
| 364 | switch (cmd) { |
| 365 | case "force-low": { |
| 366 | int opts = parseOptions(shell); |
| 367 | getContext().enforceCallingOrSelfPermission( |
| 368 | android.Manifest.permission.DEVICE_POWER, null); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 369 | mForceLevel = State.LEVEL_LOW; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 370 | int seq = mSeq.incrementAndGet(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 371 | if ((opts & OPTION_FORCE_UPDATE) != 0) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 372 | mHandler.removeMessages(MSG_CHECK); |
| 373 | mHandler.obtainMessage(MSG_CHECK).sendToTarget(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 374 | pw.println(seq); |
| 375 | } |
| 376 | } break; |
| 377 | case "force-not-low": { |
| 378 | int opts = parseOptions(shell); |
| 379 | getContext().enforceCallingOrSelfPermission( |
| 380 | android.Manifest.permission.DEVICE_POWER, null); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 381 | mForceLevel = State.LEVEL_NORMAL; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 382 | int seq = mSeq.incrementAndGet(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 383 | if ((opts & OPTION_FORCE_UPDATE) != 0) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 384 | mHandler.removeMessages(MSG_CHECK); |
| 385 | mHandler.obtainMessage(MSG_CHECK).sendToTarget(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 386 | pw.println(seq); |
| 387 | } |
| 388 | } break; |
| 389 | case "reset": { |
| 390 | int opts = parseOptions(shell); |
| 391 | getContext().enforceCallingOrSelfPermission( |
| 392 | android.Manifest.permission.DEVICE_POWER, null); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 393 | mForceLevel = State.LEVEL_UNKNOWN; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 394 | int seq = mSeq.incrementAndGet(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 395 | if ((opts & OPTION_FORCE_UPDATE) != 0) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 396 | mHandler.removeMessages(MSG_CHECK); |
| 397 | mHandler.obtainMessage(MSG_CHECK).sendToTarget(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 398 | pw.println(seq); |
| 399 | } |
| 400 | } break; |
| 401 | default: |
| 402 | return shell.handleDefaultCommands(cmd); |
| 403 | } |
| 404 | return 0; |
| 405 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 406 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 407 | static void dumpHelp(PrintWriter pw) { |
| 408 | pw.println("Device storage monitor service (devicestoragemonitor) commands:"); |
| 409 | pw.println(" help"); |
| 410 | pw.println(" Print this help text."); |
| 411 | pw.println(" force-low [-f]"); |
| 412 | pw.println(" Force storage to be low, freezing storage state."); |
| 413 | pw.println(" -f: force a storage change broadcast be sent, prints new sequence."); |
| 414 | pw.println(" force-not-low [-f]"); |
| 415 | pw.println(" Force storage to not be low, freezing storage state."); |
| 416 | pw.println(" -f: force a storage change broadcast be sent, prints new sequence."); |
| 417 | pw.println(" reset [-f]"); |
| 418 | pw.println(" Unfreeze storage state, returning to current real values."); |
| 419 | pw.println(" -f: force a storage change broadcast be sent, prints new sequence."); |
| 420 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 421 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 422 | void dumpImpl(FileDescriptor fd, PrintWriter _pw, String[] args) { |
| 423 | final IndentingPrintWriter pw = new IndentingPrintWriter(_pw, " "); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 424 | if (args == null || args.length == 0 || "-a".equals(args[0])) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 425 | pw.println("Known volumes:"); |
| 426 | pw.increaseIndent(); |
| 427 | for (int i = 0; i < mStates.size(); i++) { |
| 428 | final UUID uuid = mStates.keyAt(i); |
| 429 | final State state = mStates.valueAt(i); |
| 430 | if (StorageManager.UUID_DEFAULT.equals(uuid)) { |
| 431 | pw.println("Default:"); |
| 432 | } else { |
| 433 | pw.println(uuid + ":"); |
| 434 | } |
| 435 | pw.increaseIndent(); |
| 436 | pw.printPair("level", State.levelToString(state.level)); |
| 437 | pw.printPair("lastUsableBytes", state.lastUsableBytes); |
| 438 | pw.println(); |
| 439 | pw.decreaseIndent(); |
| 440 | } |
| 441 | pw.decreaseIndent(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 442 | pw.println(); |
| 443 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 444 | pw.printPair("mSeq", mSeq.get()); |
| 445 | pw.printPair("mForceState", State.levelToString(mForceLevel)); |
| 446 | pw.println(); |
| 447 | pw.println(); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 448 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 449 | } else { |
| 450 | Shell shell = new Shell(); |
| 451 | shell.exec(mRemoteService, null, fd, null, args, null, new ResultReceiver(null)); |
| 452 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 454 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 455 | private void updateNotifications(VolumeInfo vol, int oldLevel, int newLevel) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 456 | final Context context = getContext(); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 457 | final UUID uuid = StorageManager.convert(vol.getFsUuid()); |
| 458 | |
| 459 | if (State.isEntering(State.LEVEL_LOW, oldLevel, newLevel)) { |
| 460 | Intent lowMemIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE); |
| 461 | lowMemIntent.putExtra(StorageManager.EXTRA_UUID, uuid); |
| 462 | lowMemIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 463 | |
| 464 | final CharSequence title = context.getText( |
| 465 | com.android.internal.R.string.low_internal_storage_view_title); |
| 466 | |
| 467 | final CharSequence details; |
| 468 | if (StorageManager.UUID_DEFAULT.equals(uuid)) { |
| 469 | details = context.getText(isBootImageOnDisk() |
| 470 | ? com.android.internal.R.string.low_internal_storage_view_text |
| 471 | : com.android.internal.R.string.low_internal_storage_view_text_no_boot); |
| 472 | } else { |
| 473 | details = context.getText( |
| 474 | com.android.internal.R.string.low_internal_storage_view_text); |
| 475 | } |
| 476 | |
| 477 | PendingIntent intent = PendingIntent.getActivityAsUser(context, 0, lowMemIntent, 0, |
| 478 | null, UserHandle.CURRENT); |
| 479 | Notification notification = |
| 480 | new Notification.Builder(context, SystemNotificationChannels.ALERTS) |
| 481 | .setSmallIcon(com.android.internal.R.drawable.stat_notify_disk_full) |
| 482 | .setTicker(title) |
| 483 | .setColor(context.getColor( |
| 484 | com.android.internal.R.color.system_notification_accent_color)) |
| 485 | .setContentTitle(title) |
| 486 | .setContentText(details) |
| 487 | .setContentIntent(intent) |
| 488 | .setStyle(new Notification.BigTextStyle() |
| 489 | .bigText(details)) |
| 490 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 491 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 492 | .extend(new Notification.TvExtender() |
| 493 | .setChannelId(TV_NOTIFICATION_CHANNEL_ID)) |
| 494 | .build(); |
| 495 | notification.flags |= Notification.FLAG_NO_CLEAR; |
| 496 | mNotifManager.notifyAsUser(uuid.toString(), SystemMessage.NOTE_LOW_STORAGE, |
| 497 | notification, UserHandle.ALL); |
| 498 | } else if (State.isLeaving(State.LEVEL_LOW, oldLevel, newLevel)) { |
| 499 | mNotifManager.cancelAsUser(uuid.toString(), SystemMessage.NOTE_LOW_STORAGE, |
| 500 | UserHandle.ALL); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 501 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 504 | private void updateBroadcasts(VolumeInfo vol, int oldLevel, int newLevel, int seq) { |
| 505 | if (!Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, vol.getFsUuid())) { |
| 506 | // We don't currently send broadcasts for secondary volumes |
| 507 | return; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 508 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 509 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 510 | final Intent lowIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_LOW) |
| 511 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 512 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
| 513 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) |
| 514 | .putExtra(EXTRA_SEQUENCE, seq); |
| 515 | final Intent notLowIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_OK) |
| 516 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 517 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
| 518 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) |
| 519 | .putExtra(EXTRA_SEQUENCE, seq); |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 520 | |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 521 | if (State.isEntering(State.LEVEL_LOW, oldLevel, newLevel)) { |
| 522 | getContext().sendStickyBroadcastAsUser(lowIntent, UserHandle.ALL); |
| 523 | } else if (State.isLeaving(State.LEVEL_LOW, oldLevel, newLevel)) { |
| 524 | getContext().removeStickyBroadcastAsUser(lowIntent, UserHandle.ALL); |
| 525 | getContext().sendBroadcastAsUser(notLowIntent, UserHandle.ALL); |
| 526 | } |
| 527 | |
| 528 | final Intent fullIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_FULL) |
| 529 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) |
| 530 | .putExtra(EXTRA_SEQUENCE, seq); |
| 531 | final Intent notFullIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_NOT_FULL) |
| 532 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) |
| 533 | .putExtra(EXTRA_SEQUENCE, seq); |
| 534 | |
| 535 | if (State.isEntering(State.LEVEL_FULL, oldLevel, newLevel)) { |
| 536 | getContext().sendStickyBroadcastAsUser(fullIntent, UserHandle.ALL); |
| 537 | } else if (State.isLeaving(State.LEVEL_FULL, oldLevel, newLevel)) { |
| 538 | getContext().removeStickyBroadcastAsUser(fullIntent, UserHandle.ALL); |
| 539 | getContext().sendBroadcastAsUser(notFullIntent, UserHandle.ALL); |
| 540 | } |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 543 | private static class CacheFileDeletedObserver extends FileObserver { |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame] | 544 | public CacheFileDeletedObserver() { |
| 545 | super(Environment.getDownloadCacheDirectory().getAbsolutePath(), FileObserver.DELETE); |
| 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public void onEvent(int event, String path) { |
| 550 | EventLogTags.writeCacheFileDeleted(path); |
| 551 | } |
| 552 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |