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