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