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 | |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 19 | import android.app.NotificationChannel; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 20 | |
| 21 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 22 | import com.android.internal.notification.SystemNotificationChannels; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame^] | 23 | import com.android.internal.util.DumpUtils; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 24 | import com.android.server.EventLogTags; |
| 25 | import com.android.server.SystemService; |
Fyodor Kupolov | eeea67b | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 26 | import com.android.server.pm.InstructionSets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.app.Notification; |
| 28 | import android.app.NotificationManager; |
| 29 | import android.app.PendingIntent; |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.pm.IPackageDataObserver; |
| 34 | import android.content.pm.IPackageManager; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.Binder; |
Dianne Hackborn | f882efa | 2012-04-11 16:04:12 -0700 | [diff] [blame] | 37 | import android.os.Environment; |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame] | 38 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.os.Handler; |
| 40 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.RemoteException; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 42 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.os.ServiceManager; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 44 | import android.os.ShellCallback; |
| 45 | import android.os.ShellCommand; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.StatFs; |
| 47 | import android.os.SystemClock; |
| 48 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 49 | import android.os.UserHandle; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 50 | import android.os.storage.StorageManager; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 51 | import android.provider.Settings; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 52 | import android.text.format.Formatter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 54 | import android.util.Slog; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 55 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 57 | import java.io.File; |
| 58 | import java.io.FileDescriptor; |
| 59 | import java.io.PrintWriter; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 60 | import java.util.concurrent.atomic.AtomicInteger; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 61 | |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 62 | import dalvik.system.VMRuntime; |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 65 | * This class implements a service to monitor the amount of disk |
| 66 | * storage space on the device. If the free storage on device is less |
| 67 | * than a tunable threshold value (a secure settings parameter; |
| 68 | * default 10%) a low memory notification is displayed to alert the |
| 69 | * user. If the user clicks on the low memory notification the |
| 70 | * Application Manager application gets launched to let the user free |
| 71 | * storage space. |
| 72 | * |
| 73 | * Event log events: A low memory event with the free storage on |
| 74 | * device in bytes is logged to the event log when the device goes low |
| 75 | * on storage space. The amount of free storage on the device is |
| 76 | * periodically logged to the event log. The log interval is a secure |
| 77 | * settings parameter with a default value of 12 hours. When the free |
| 78 | * storage differential goes below a threshold (again a secure |
| 79 | * settings parameter with a default value of 2MB), the free memory is |
| 80 | * logged to the event log. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 82 | public class DeviceStorageMonitorService extends SystemService { |
| 83 | static final String TAG = "DeviceStorageMonitorService"; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 84 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}: |
| 87 | * Current int sequence number of the update. |
| 88 | */ |
| 89 | public static final String EXTRA_SEQUENCE = "seq"; |
| 90 | |
Jeff Sharkey | 529f91f | 2015-04-18 20:23:13 -0700 | [diff] [blame] | 91 | // TODO: extend to watch and manage caches on all private volumes |
| 92 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 93 | static final boolean DEBUG = false; |
| 94 | static final boolean localLOGV = false; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 95 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 96 | static final int DEVICE_MEMORY_WHAT = 1; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 97 | static final int FORCE_MEMORY_WHAT = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | private static final int MONITOR_INTERVAL = 1; //in minutes |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | private static final int DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES = 12*60; //in minutes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | private static final long DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD = 2 * 1024 * 1024; // 2MB |
| 102 | private static final long DEFAULT_CHECK_INTERVAL = MONITOR_INTERVAL*60*1000; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 103 | |
Richard Uhler | d42fe85 | 2016-08-12 13:51:51 -0700 | [diff] [blame] | 104 | // com.android.internal.R.string.low_internal_storage_view_text_no_boot |
| 105 | // hard codes 250MB in the message as the storage space required for the |
| 106 | // boot image. |
| 107 | private static final long BOOT_IMAGE_STORAGE_REQUIREMENT = 250 * 1024 * 1024; |
| 108 | |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 109 | private long mFreeMem; // on /data |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 110 | private long mFreeMemAfterLastCacheClear; // on /data |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | private long mLastReportedFreeMem; |
| 112 | private long mLastReportedFreeMemTime; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 113 | boolean mLowMemFlag=false; |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 114 | private boolean mMemFullFlag=false; |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 115 | private final boolean mIsBootImageOnDisk; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 116 | private final ContentResolver mResolver; |
| 117 | private final long mTotalMemory; // on /data |
| 118 | private final StatFs mDataFileStats; |
| 119 | private final StatFs mSystemFileStats; |
| 120 | private final StatFs mCacheFileStats; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 121 | |
| 122 | private static final File DATA_PATH = Environment.getDataDirectory(); |
| 123 | private static final File SYSTEM_PATH = Environment.getRootDirectory(); |
| 124 | private static final File CACHE_PATH = Environment.getDownloadCacheDirectory(); |
| 125 | |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 126 | private long mThreadStartTime = -1; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 127 | boolean mUpdatesStopped; |
| 128 | AtomicInteger mSeq = new AtomicInteger(1); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 129 | boolean mClearSucceeded = false; |
| 130 | boolean mClearingCache; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 131 | private final Intent mStorageLowIntent; |
| 132 | private final Intent mStorageOkIntent; |
| 133 | private final Intent mStorageFullIntent; |
| 134 | private final Intent mStorageNotFullIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | private CachePackageDataObserver mClearCacheObserver; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 136 | private CacheFileDeletedObserver mCacheFileDeletedObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | private static final int _TRUE = 1; |
| 138 | private static final int _FALSE = 0; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 139 | // This is the raw threshold that has been set at which we consider |
| 140 | // storage to be low. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 141 | long mMemLowThreshold; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 142 | // This is the threshold at which we start trying to flush caches |
| 143 | // to get below the low threshold limit. It is less than the low |
| 144 | // threshold; we will allow storage to get a bit beyond the limit |
| 145 | // before flushing and checking if we are actually low. |
| 146 | private long mMemCacheStartTrimThreshold; |
| 147 | // This is the threshold that we try to get to when deleting cache |
| 148 | // files. This is greater than the low threshold so that we will flush |
| 149 | // more files than absolutely needed, to reduce the frequency that |
| 150 | // flushing takes place. |
| 151 | private long mMemCacheTrimToThreshold; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 152 | private long mMemFullThreshold; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | /** |
| 155 | * This string is used for ServiceManager access to this class. |
| 156 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 157 | static final String SERVICE = "devicestoragemonitor"; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 158 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 159 | private static final String TV_NOTIFICATION_CHANNEL_ID = "devicestoragemonitor.tv"; |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | /** |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 162 | * Handler that checks the amount of disk space on the device and sends a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | * notification if the device runs low on disk space |
| 164 | */ |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 165 | private final Handler mHandler = new Handler() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | @Override |
| 167 | public void handleMessage(Message msg) { |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 168 | //don't handle an invalid message |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 169 | switch (msg.what) { |
| 170 | case DEVICE_MEMORY_WHAT: |
| 171 | checkMemory(msg.arg1 == _TRUE); |
| 172 | return; |
| 173 | case FORCE_MEMORY_WHAT: |
| 174 | forceMemory(msg.arg1, msg.arg2); |
| 175 | return; |
| 176 | default: |
| 177 | Slog.w(TAG, "Will not process invalid message"); |
| 178 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | }; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 183 | private class CachePackageDataObserver extends IPackageDataObserver.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | public void onRemoveCompleted(String packageName, boolean succeeded) { |
| 185 | mClearSucceeded = succeeded; |
| 186 | mClearingCache = false; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 187 | if(localLOGV) Slog.i(TAG, " Clear succeeded:"+mClearSucceeded |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | +", mClearingCache:"+mClearingCache+" Forcing memory check"); |
| 189 | postCheckMemoryMsg(false, 0); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 190 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 192 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 193 | private void restatDataDir() { |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 194 | try { |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 195 | mDataFileStats.restat(DATA_PATH.getAbsolutePath()); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 196 | mFreeMem = (long) mDataFileStats.getAvailableBlocks() * |
| 197 | mDataFileStats.getBlockSize(); |
| 198 | } catch (IllegalArgumentException e) { |
| 199 | // use the old value of mFreeMem |
| 200 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | // Allow freemem to be overridden by debug.freemem for testing |
| 202 | String debugFreeMem = SystemProperties.get("debug.freemem"); |
| 203 | if (!"".equals(debugFreeMem)) { |
| 204 | mFreeMem = Long.parseLong(debugFreeMem); |
| 205 | } |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 206 | // Read the log interval from secure settings |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 207 | long freeMemLogInterval = Settings.Global.getLong(mResolver, |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 208 | Settings.Global.SYS_FREE_STORAGE_LOG_INTERVAL, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES)*60*1000; |
| 210 | //log the amount of free memory in event log |
| 211 | long currTime = SystemClock.elapsedRealtime(); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 212 | if((mLastReportedFreeMemTime == 0) || |
| 213 | (currTime-mLastReportedFreeMemTime) >= freeMemLogInterval) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | mLastReportedFreeMemTime = currTime; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 215 | long mFreeSystem = -1, mFreeCache = -1; |
| 216 | try { |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 217 | mSystemFileStats.restat(SYSTEM_PATH.getAbsolutePath()); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 218 | mFreeSystem = (long) mSystemFileStats.getAvailableBlocks() * |
| 219 | mSystemFileStats.getBlockSize(); |
| 220 | } catch (IllegalArgumentException e) { |
| 221 | // ignore; report -1 |
| 222 | } |
| 223 | try { |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 224 | mCacheFileStats.restat(CACHE_PATH.getAbsolutePath()); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 225 | mFreeCache = (long) mCacheFileStats.getAvailableBlocks() * |
| 226 | mCacheFileStats.getBlockSize(); |
| 227 | } catch (IllegalArgumentException e) { |
| 228 | // ignore; report -1 |
| 229 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 230 | EventLog.writeEvent(EventLogTags.FREE_STORAGE_LEFT, |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 231 | mFreeMem, mFreeSystem, mFreeCache); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 233 | // Read the reporting threshold from secure settings |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 234 | long threshold = Settings.Global.getLong(mResolver, |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 235 | Settings.Global.DISK_FREE_CHANGE_REPORTING_THRESHOLD, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD); |
| 237 | // If mFree changed significantly log the new value |
| 238 | long delta = mFreeMem - mLastReportedFreeMem; |
| 239 | if (delta > threshold || delta < -threshold) { |
| 240 | mLastReportedFreeMem = mFreeMem; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 241 | EventLog.writeEvent(EventLogTags.FREE_STORAGE_CHANGED, mFreeMem); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 245 | private void clearCache() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | if (mClearCacheObserver == null) { |
| 247 | // Lazy instantiation |
| 248 | mClearCacheObserver = new CachePackageDataObserver(); |
| 249 | } |
| 250 | mClearingCache = true; |
| 251 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 252 | if (localLOGV) Slog.i(TAG, "Clearing cache"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | IPackageManager.Stub.asInterface(ServiceManager.getService("package")). |
Jeff Sharkey | 529f91f | 2015-04-18 20:23:13 -0700 | [diff] [blame] | 254 | freeStorageAndNotify(null, mMemCacheTrimToThreshold, mClearCacheObserver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 256 | Slog.w(TAG, "Failed to get handle for PackageManger Exception: "+e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | mClearingCache = false; |
| 258 | mClearSucceeded = false; |
| 259 | } |
| 260 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 261 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 262 | void forceMemory(int opts, int seq) { |
| 263 | if ((opts&OPTION_UPDATES_STOPPED) == 0) { |
| 264 | if (mUpdatesStopped) { |
| 265 | mUpdatesStopped = false; |
| 266 | checkMemory(true); |
| 267 | } |
| 268 | } else { |
| 269 | mUpdatesStopped = true; |
| 270 | final boolean forceLow = (opts&OPTION_STORAGE_LOW) != 0; |
| 271 | if (mLowMemFlag != forceLow || (opts&OPTION_FORCE_UPDATE) != 0) { |
| 272 | mLowMemFlag = forceLow; |
| 273 | if (forceLow) { |
| 274 | sendNotification(seq); |
| 275 | } else { |
| 276 | cancelNotification(seq); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 282 | void checkMemory(boolean checkCache) { |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 283 | if (mUpdatesStopped) { |
| 284 | return; |
| 285 | } |
| 286 | |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 287 | //if the thread that was started to clear cache is still running do nothing till its |
| 288 | //finished clearing cache. Ideally this flag could be modified by clearCache |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | // and should be accessed via a lock but even if it does this test will fail now and |
| 290 | //hopefully the next time this flag will be set to the correct value. |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 291 | if (mClearingCache) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 292 | if(localLOGV) Slog.i(TAG, "Thread already running just skip"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | //make sure the thread is not hung for too long |
| 294 | long diffTime = System.currentTimeMillis() - mThreadStartTime; |
| 295 | if(diffTime > (10*60*1000)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 296 | Slog.w(TAG, "Thread that clears cache file seems to run for ever"); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 297 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } else { |
| 299 | restatDataDir(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 300 | if (localLOGV) Slog.v(TAG, "freeMemory="+mFreeMem); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | //post intent to NotificationManager to display icon if necessary |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 303 | if (mFreeMem < mMemLowThreshold) { |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 304 | if (checkCache) { |
| 305 | // We are allowed to clear cache files at this point to |
| 306 | // try to get down below the limit, because this is not |
| 307 | // the initial call after a cache clear has been attempted. |
| 308 | // In this case we will try a cache clear if our free |
| 309 | // space has gone below the cache clear limit. |
| 310 | if (mFreeMem < mMemCacheStartTrimThreshold) { |
| 311 | // We only clear the cache if the free storage has changed |
| 312 | // a significant amount since the last time. |
| 313 | if ((mFreeMemAfterLastCacheClear-mFreeMem) |
| 314 | >= ((mMemLowThreshold-mMemCacheStartTrimThreshold)/4)) { |
| 315 | // See if clearing cache helps |
| 316 | // Note that clearing cache is asynchronous and so we do a |
| 317 | // memory check again once the cache has been cleared. |
| 318 | mThreadStartTime = System.currentTimeMillis(); |
| 319 | mClearSucceeded = false; |
| 320 | clearCache(); |
| 321 | } |
| 322 | } |
| 323 | } else { |
| 324 | // This is a call from after clearing the cache. Note |
| 325 | // the amount of free storage at this point. |
| 326 | mFreeMemAfterLastCacheClear = mFreeMem; |
| 327 | if (!mLowMemFlag) { |
| 328 | // We tried to clear the cache, but that didn't get us |
| 329 | // below the low storage limit. Tell the user. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 330 | Slog.i(TAG, "Running low on memory. Sending notification"); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 331 | sendNotification(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | mLowMemFlag = true; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 333 | } else { |
| 334 | if (localLOGV) Slog.v(TAG, "Running low on memory " + |
| 335 | "notification already sent. do nothing"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
| 338 | } else { |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 339 | mFreeMemAfterLastCacheClear = mFreeMem; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | if (mLowMemFlag) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 341 | Slog.i(TAG, "Memory available. Cancelling notification"); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 342 | cancelNotification(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | mLowMemFlag = false; |
| 344 | } |
| 345 | } |
Richard Uhler | d42fe85 | 2016-08-12 13:51:51 -0700 | [diff] [blame] | 346 | if (!mLowMemFlag && !mIsBootImageOnDisk && mFreeMem < BOOT_IMAGE_STORAGE_REQUIREMENT) { |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 347 | Slog.i(TAG, "No boot image on disk due to lack of space. Sending notification"); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 348 | sendNotification(0); |
Richard Uhler | d42fe85 | 2016-08-12 13:51:51 -0700 | [diff] [blame] | 349 | mLowMemFlag = true; |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 350 | } |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 351 | if (mFreeMem < mMemFullThreshold) { |
| 352 | if (!mMemFullFlag) { |
| 353 | sendFullNotification(); |
| 354 | mMemFullFlag = true; |
| 355 | } |
| 356 | } else { |
| 357 | if (mMemFullFlag) { |
| 358 | cancelFullNotification(); |
| 359 | mMemFullFlag = false; |
| 360 | } |
| 361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 363 | if(localLOGV) Slog.i(TAG, "Posting Message again"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | //keep posting messages to itself periodically |
| 365 | postCheckMemoryMsg(true, DEFAULT_CHECK_INTERVAL); |
| 366 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 367 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 368 | void postCheckMemoryMsg(boolean clearCache, long delay) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | // Remove queued messages |
| 370 | mHandler.removeMessages(DEVICE_MEMORY_WHAT); |
| 371 | mHandler.sendMessageDelayed(mHandler.obtainMessage(DEVICE_MEMORY_WHAT, |
| 372 | clearCache ?_TRUE : _FALSE, 0), |
| 373 | delay); |
| 374 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 375 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 376 | public DeviceStorageMonitorService(Context context) { |
| 377 | super(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | mLastReportedFreeMemTime = 0; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 379 | mResolver = context.getContentResolver(); |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 380 | mIsBootImageOnDisk = isBootImageOnDisk(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | //create StatFs object |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 382 | mDataFileStats = new StatFs(DATA_PATH.getAbsolutePath()); |
| 383 | mSystemFileStats = new StatFs(SYSTEM_PATH.getAbsolutePath()); |
| 384 | mCacheFileStats = new StatFs(CACHE_PATH.getAbsolutePath()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | //initialize total storage on device |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 386 | mTotalMemory = (long)mDataFileStats.getBlockCount() * |
| 387 | mDataFileStats.getBlockSize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | mStorageLowIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_LOW); |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 389 | mStorageLowIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
Chad Brubaker | 6e392ed | 2017-03-22 14:36:37 -0700 | [diff] [blame] | 390 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
| 391 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | mStorageOkIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_OK); |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 393 | mStorageOkIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
Chad Brubaker | 6e392ed | 2017-03-22 14:36:37 -0700 | [diff] [blame] | 394 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
| 395 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 396 | mStorageFullIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_FULL); |
| 397 | mStorageFullIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 398 | mStorageNotFullIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_NOT_FULL); |
| 399 | mStorageNotFullIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 400 | } |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 401 | |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 402 | private static boolean isBootImageOnDisk() { |
Fyodor Kupolov | eeea67b | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 403 | for (String instructionSet : InstructionSets.getAllDexCodeInstructionSets()) { |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 404 | if (!VMRuntime.isBootClassPathOnDisk(instructionSet)) { |
| 405 | return false; |
| 406 | } |
| 407 | } |
| 408 | return true; |
| 409 | } |
| 410 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 411 | /** |
| 412 | * Initializes the disk space threshold value and posts an empty message to |
| 413 | * kickstart the process. |
| 414 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 415 | @Override |
| 416 | public void onStart() { |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 417 | // cache storage thresholds |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 418 | Context context = getContext(); |
| 419 | final StorageManager sm = StorageManager.from(context); |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 420 | mMemLowThreshold = sm.getStorageLowBytes(DATA_PATH); |
| 421 | mMemFullThreshold = sm.getStorageFullBytes(DATA_PATH); |
| 422 | |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 423 | mMemCacheStartTrimThreshold = ((mMemLowThreshold*3)+mMemFullThreshold)/4; |
| 424 | mMemCacheTrimToThreshold = mMemLowThreshold |
| 425 | + ((mMemLowThreshold-mMemCacheStartTrimThreshold)*2); |
| 426 | mFreeMemAfterLastCacheClear = mTotalMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | checkMemory(true); |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame] | 428 | |
| 429 | mCacheFileDeletedObserver = new CacheFileDeletedObserver(); |
| 430 | mCacheFileDeletedObserver.startWatching(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 431 | |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 432 | // Ensure that the notification channel is set up |
| 433 | NotificationManager notificationMgr = |
| 434 | (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 435 | PackageManager packageManager = context.getPackageManager(); |
| 436 | boolean isTv = packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK); |
| 437 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 438 | if (isTv) { |
| 439 | notificationMgr.createNotificationChannel(new NotificationChannel( |
| 440 | TV_NOTIFICATION_CHANNEL_ID, |
| 441 | context.getString( |
| 442 | com.android.internal.R.string.device_storage_monitor_notification_channel), |
| 443 | NotificationManager.IMPORTANCE_HIGH)); |
| 444 | } |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 445 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 446 | publishBinderService(SERVICE, mRemoteService); |
| 447 | publishLocalService(DeviceStorageMonitorInternal.class, mLocalService); |
| 448 | } |
| 449 | |
| 450 | private final DeviceStorageMonitorInternal mLocalService = new DeviceStorageMonitorInternal() { |
| 451 | @Override |
| 452 | public void checkMemory() { |
| 453 | // force an early check |
| 454 | postCheckMemoryMsg(true, 0); |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public boolean isMemoryLow() { |
Richard Uhler | d42fe85 | 2016-08-12 13:51:51 -0700 | [diff] [blame] | 459 | return mLowMemFlag; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public long getMemoryLowThreshold() { |
| 464 | return mMemLowThreshold; |
| 465 | } |
| 466 | }; |
| 467 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 468 | private final Binder mRemoteService = new Binder() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 469 | @Override |
| 470 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame^] | 471 | if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 472 | dumpImpl(fd, pw, args); |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 477 | FileDescriptor err, String[] args, ShellCallback callback, |
| 478 | ResultReceiver resultReceiver) { |
| 479 | (new Shell()).exec(this, in, out, err, args, callback, resultReceiver); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 480 | } |
| 481 | }; |
| 482 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 483 | class Shell extends ShellCommand { |
| 484 | @Override |
| 485 | public int onCommand(String cmd) { |
| 486 | return onShellCommand(this, cmd); |
| 487 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 488 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 489 | @Override |
| 490 | public void onHelp() { |
| 491 | PrintWriter pw = getOutPrintWriter(); |
| 492 | dumpHelp(pw); |
| 493 | } |
| 494 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 495 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 496 | static final int OPTION_FORCE_UPDATE = 1<<0; |
| 497 | static final int OPTION_UPDATES_STOPPED = 1<<1; |
| 498 | static final int OPTION_STORAGE_LOW = 1<<2; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 499 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 500 | int parseOptions(Shell shell) { |
| 501 | String opt; |
| 502 | int opts = 0; |
| 503 | while ((opt = shell.getNextOption()) != null) { |
| 504 | if ("-f".equals(opt)) { |
| 505 | opts |= OPTION_FORCE_UPDATE; |
| 506 | } |
| 507 | } |
| 508 | return opts; |
| 509 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 510 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 511 | int onShellCommand(Shell shell, String cmd) { |
| 512 | if (cmd == null) { |
| 513 | return shell.handleDefaultCommands(cmd); |
| 514 | } |
| 515 | PrintWriter pw = shell.getOutPrintWriter(); |
| 516 | switch (cmd) { |
| 517 | case "force-low": { |
| 518 | int opts = parseOptions(shell); |
| 519 | getContext().enforceCallingOrSelfPermission( |
| 520 | android.Manifest.permission.DEVICE_POWER, null); |
| 521 | int seq = mSeq.incrementAndGet(); |
| 522 | mHandler.sendMessage(mHandler.obtainMessage(FORCE_MEMORY_WHAT, |
| 523 | opts | OPTION_UPDATES_STOPPED | OPTION_STORAGE_LOW, seq)); |
| 524 | if ((opts & OPTION_FORCE_UPDATE) != 0) { |
| 525 | pw.println(seq); |
| 526 | } |
| 527 | } break; |
| 528 | case "force-not-low": { |
| 529 | int opts = parseOptions(shell); |
| 530 | getContext().enforceCallingOrSelfPermission( |
| 531 | android.Manifest.permission.DEVICE_POWER, null); |
| 532 | int seq = mSeq.incrementAndGet(); |
| 533 | mHandler.sendMessage(mHandler.obtainMessage(FORCE_MEMORY_WHAT, |
| 534 | opts | OPTION_UPDATES_STOPPED, seq)); |
| 535 | if ((opts & OPTION_FORCE_UPDATE) != 0) { |
| 536 | pw.println(seq); |
| 537 | } |
| 538 | } break; |
| 539 | case "reset": { |
| 540 | int opts = parseOptions(shell); |
| 541 | getContext().enforceCallingOrSelfPermission( |
| 542 | android.Manifest.permission.DEVICE_POWER, null); |
| 543 | int seq = mSeq.incrementAndGet(); |
| 544 | mHandler.sendMessage(mHandler.obtainMessage(FORCE_MEMORY_WHAT, |
| 545 | opts, seq)); |
| 546 | if ((opts & OPTION_FORCE_UPDATE) != 0) { |
| 547 | pw.println(seq); |
| 548 | } |
| 549 | } break; |
| 550 | default: |
| 551 | return shell.handleDefaultCommands(cmd); |
| 552 | } |
| 553 | return 0; |
| 554 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 555 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 556 | static void dumpHelp(PrintWriter pw) { |
| 557 | pw.println("Device storage monitor service (devicestoragemonitor) commands:"); |
| 558 | pw.println(" help"); |
| 559 | pw.println(" Print this help text."); |
| 560 | pw.println(" force-low [-f]"); |
| 561 | pw.println(" Force storage to be low, freezing storage state."); |
| 562 | pw.println(" -f: force a storage change broadcast be sent, prints new sequence."); |
| 563 | pw.println(" force-not-low [-f]"); |
| 564 | pw.println(" Force storage to not be low, freezing storage state."); |
| 565 | pw.println(" -f: force a storage change broadcast be sent, prints new sequence."); |
| 566 | pw.println(" reset [-f]"); |
| 567 | pw.println(" Unfreeze storage state, returning to current real values."); |
| 568 | pw.println(" -f: force a storage change broadcast be sent, prints new sequence."); |
| 569 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 570 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 571 | void dumpImpl(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 572 | if (args == null || args.length == 0 || "-a".equals(args[0])) { |
| 573 | final Context context = getContext(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 574 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 575 | pw.println("Current DeviceStorageMonitor state:"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 576 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 577 | pw.print(" mFreeMem="); |
| 578 | pw.print(Formatter.formatFileSize(context, mFreeMem)); |
| 579 | pw.print(" mTotalMemory="); |
| 580 | pw.println(Formatter.formatFileSize(context, mTotalMemory)); |
| 581 | |
| 582 | pw.print(" mFreeMemAfterLastCacheClear="); |
| 583 | pw.println(Formatter.formatFileSize(context, mFreeMemAfterLastCacheClear)); |
| 584 | |
| 585 | pw.print(" mLastReportedFreeMem="); |
| 586 | pw.print(Formatter.formatFileSize(context, mLastReportedFreeMem)); |
| 587 | pw.print(" mLastReportedFreeMemTime="); |
| 588 | TimeUtils.formatDuration(mLastReportedFreeMemTime, SystemClock.elapsedRealtime(), pw); |
| 589 | pw.println(); |
| 590 | |
| 591 | if (mUpdatesStopped) { |
| 592 | pw.print(" mUpdatesStopped="); |
| 593 | pw.print(mUpdatesStopped); |
| 594 | pw.print(" mSeq="); |
| 595 | pw.println(mSeq.get()); |
| 596 | } else { |
| 597 | pw.print(" mClearSucceeded="); |
| 598 | pw.print(mClearSucceeded); |
| 599 | pw.print(" mClearingCache="); |
| 600 | pw.println(mClearingCache); |
| 601 | } |
| 602 | |
| 603 | pw.print(" mLowMemFlag="); |
| 604 | pw.print(mLowMemFlag); |
| 605 | pw.print(" mMemFullFlag="); |
| 606 | pw.println(mMemFullFlag); |
| 607 | |
| 608 | pw.print(" mMemLowThreshold="); |
| 609 | pw.print(Formatter.formatFileSize(context, mMemLowThreshold)); |
| 610 | pw.print(" mMemFullThreshold="); |
| 611 | pw.println(Formatter.formatFileSize(context, mMemFullThreshold)); |
| 612 | |
| 613 | pw.print(" mMemCacheStartTrimThreshold="); |
| 614 | pw.print(Formatter.formatFileSize(context, mMemCacheStartTrimThreshold)); |
| 615 | pw.print(" mMemCacheTrimToThreshold="); |
| 616 | pw.println(Formatter.formatFileSize(context, mMemCacheTrimToThreshold)); |
| 617 | |
| 618 | pw.print(" mIsBootImageOnDisk="); pw.println(mIsBootImageOnDisk); |
| 619 | } else { |
| 620 | Shell shell = new Shell(); |
| 621 | shell.exec(mRemoteService, null, fd, null, args, null, new ResultReceiver(null)); |
| 622 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | /** |
| 626 | * This method sends a notification to NotificationManager to display |
| 627 | * an error dialog indicating low disk space and launch the Installer |
| 628 | * application |
| 629 | */ |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 630 | private void sendNotification(int seq) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 631 | final Context context = getContext(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 632 | if(localLOGV) Slog.i(TAG, "Sending low memory notification"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | //log the event to event log with the amount of free storage(in bytes) left on the device |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 634 | EventLog.writeEvent(EventLogTags.LOW_STORAGE, mFreeMem); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | // Pack up the values and broadcast them to everyone |
Daniel Nishi | 690346b | 2016-06-17 10:21:48 -0700 | [diff] [blame] | 636 | Intent lowMemIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | lowMemIntent.putExtra("memory", mFreeMem); |
| 638 | lowMemIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Dmitri Plotnikov | d6bd6b9 | 2017-01-27 16:42:12 -0800 | [diff] [blame] | 639 | NotificationManager notificationMgr = |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 640 | (NotificationManager)context.getSystemService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | Context.NOTIFICATION_SERVICE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 642 | CharSequence title = context.getText( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | com.android.internal.R.string.low_internal_storage_view_title); |
Brian Carlstrom | a39871e | 2014-09-29 13:44:04 -0700 | [diff] [blame] | 644 | CharSequence details = context.getText(mIsBootImageOnDisk |
| 645 | ? com.android.internal.R.string.low_internal_storage_view_text |
| 646 | : com.android.internal.R.string.low_internal_storage_view_text_no_boot); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 647 | PendingIntent intent = PendingIntent.getActivityAsUser(context, 0, lowMemIntent, 0, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 648 | null, UserHandle.CURRENT); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 649 | Notification notification = |
| 650 | new Notification.Builder(context, SystemNotificationChannels.ALERTS) |
| 651 | .setSmallIcon(com.android.internal.R.drawable.stat_notify_disk_full) |
| 652 | .setTicker(title) |
| 653 | .setColor(context.getColor( |
| 654 | com.android.internal.R.color.system_notification_accent_color)) |
| 655 | .setContentTitle(title) |
| 656 | .setContentText(details) |
| 657 | .setContentIntent(intent) |
| 658 | .setStyle(new Notification.BigTextStyle() |
| 659 | .bigText(details)) |
| 660 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 661 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 662 | .extend(new Notification.TvExtender() |
| 663 | .setChannel(TV_NOTIFICATION_CHANNEL_ID)) |
| 664 | .build(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | notification.flags |= Notification.FLAG_NO_CLEAR; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 666 | notificationMgr.notifyAsUser(null, SystemMessage.NOTE_LOW_STORAGE, notification, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 667 | UserHandle.ALL); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 668 | Intent broadcast = new Intent(mStorageLowIntent); |
| 669 | if (seq != 0) { |
| 670 | broadcast.putExtra(EXTRA_SEQUENCE, seq); |
| 671 | } |
| 672 | context.sendStickyBroadcastAsUser(broadcast, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Cancels low storage notification and sends OK intent. |
| 677 | */ |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 678 | private void cancelNotification(int seq) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 679 | final Context context = getContext(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 680 | if(localLOGV) Slog.i(TAG, "Canceling low memory notification"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | NotificationManager mNotificationMgr = |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 682 | (NotificationManager)context.getSystemService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | Context.NOTIFICATION_SERVICE); |
| 684 | //cancel notification since memory has been freed |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 685 | mNotificationMgr.cancelAsUser(null, SystemMessage.NOTE_LOW_STORAGE, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 687 | context.removeStickyBroadcastAsUser(mStorageLowIntent, UserHandle.ALL); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 688 | Intent broadcast = new Intent(mStorageOkIntent); |
| 689 | if (seq != 0) { |
| 690 | broadcast.putExtra(EXTRA_SEQUENCE, seq); |
| 691 | } |
| 692 | context.sendBroadcastAsUser(broadcast, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 694 | |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 695 | /** |
| 696 | * Send a notification when storage is full. |
| 697 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 698 | private void sendFullNotification() { |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 699 | if(localLOGV) Slog.i(TAG, "Sending memory full notification"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 700 | getContext().sendStickyBroadcastAsUser(mStorageFullIntent, UserHandle.ALL); |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Cancels memory full notification and sends "not full" intent. |
| 705 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 706 | private void cancelFullNotification() { |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 707 | if(localLOGV) Slog.i(TAG, "Canceling memory full notification"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 708 | getContext().removeStickyBroadcastAsUser(mStorageFullIntent, UserHandle.ALL); |
| 709 | getContext().sendBroadcastAsUser(mStorageNotFullIntent, UserHandle.ALL); |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 712 | private static class CacheFileDeletedObserver extends FileObserver { |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame] | 713 | public CacheFileDeletedObserver() { |
| 714 | super(Environment.getDownloadCacheDirectory().getAbsolutePath(), FileObserver.DELETE); |
| 715 | } |
| 716 | |
| 717 | @Override |
| 718 | public void onEvent(int event, String path) { |
| 719 | EventLogTags.writeCacheFileDeleted(path); |
| 720 | } |
| 721 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | } |