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 | |
| 17 | package com.android.server; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.IPackageDataObserver; |
| 26 | import android.content.pm.IPackageManager; |
| 27 | import android.os.Binder; |
Dianne Hackborn | f882efa | 2012-04-11 16:04:12 -0700 | [diff] [blame] | 28 | import android.os.Environment; |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame^] | 29 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
| 32 | import android.os.Process; |
| 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
| 35 | import android.os.StatFs; |
| 36 | import android.os.SystemClock; |
| 37 | import android.os.SystemProperties; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 38 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 40 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 43 | * This class implements a service to monitor the amount of disk |
| 44 | * storage space on the device. If the free storage on device is less |
| 45 | * than a tunable threshold value (a secure settings parameter; |
| 46 | * default 10%) a low memory notification is displayed to alert the |
| 47 | * user. If the user clicks on the low memory notification the |
| 48 | * Application Manager application gets launched to let the user free |
| 49 | * storage space. |
| 50 | * |
| 51 | * Event log events: A low memory event with the free storage on |
| 52 | * device in bytes is logged to the event log when the device goes low |
| 53 | * on storage space. The amount of free storage on the device is |
| 54 | * periodically logged to the event log. The log interval is a secure |
| 55 | * settings parameter with a default value of 12 hours. When the free |
| 56 | * storage differential goes below a threshold (again a secure |
| 57 | * settings parameter with a default value of 2MB), the free memory is |
| 58 | * logged to the event log. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | */ |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 60 | public class DeviceStorageMonitorService extends Binder { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | private static final String TAG = "DeviceStorageMonitorService"; |
| 62 | private static final boolean DEBUG = false; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 63 | private static final boolean localLOGV = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | private static final int DEVICE_MEMORY_WHAT = 1; |
| 65 | private static final int MONITOR_INTERVAL = 1; //in minutes |
| 66 | private static final int LOW_MEMORY_NOTIFICATION_ID = 1; |
| 67 | private static final int DEFAULT_THRESHOLD_PERCENTAGE = 10; |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 68 | private static final int DEFAULT_THRESHOLD_MAX_BYTES = 500*1024*1024; // 500MB |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | 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] | 70 | private static final long DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD = 2 * 1024 * 1024; // 2MB |
| 71 | private static final long DEFAULT_CHECK_INTERVAL = MONITOR_INTERVAL*60*1000; |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 72 | private static final int DEFAULT_FULL_THRESHOLD_BYTES = 1024*1024; // 1MB |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 73 | private long mFreeMem; // on /data |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | private long mLastReportedFreeMem; |
| 75 | private long mLastReportedFreeMemTime; |
| 76 | private boolean mLowMemFlag=false; |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 77 | private boolean mMemFullFlag=false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | private Context mContext; |
| 79 | private ContentResolver mContentResolver; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 80 | private long mTotalMemory; // on /data |
| 81 | private StatFs mDataFileStats; |
| 82 | private StatFs mSystemFileStats; |
| 83 | private StatFs mCacheFileStats; |
| 84 | private static final String DATA_PATH = "/data"; |
| 85 | private static final String SYSTEM_PATH = "/system"; |
| 86 | private static final String CACHE_PATH = "/cache"; |
| 87 | private long mThreadStartTime = -1; |
| 88 | private boolean mClearSucceeded = false; |
| 89 | private boolean mClearingCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | private Intent mStorageLowIntent; |
| 91 | private Intent mStorageOkIntent; |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 92 | private Intent mStorageFullIntent; |
| 93 | private Intent mStorageNotFullIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | private CachePackageDataObserver mClearCacheObserver; |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame^] | 95 | private final CacheFileDeletedObserver mCacheFileDeletedObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private static final int _TRUE = 1; |
| 97 | private static final int _FALSE = 0; |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 98 | private long mMemLowThreshold; |
| 99 | private int mMemFullThreshold; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | /** |
| 102 | * This string is used for ServiceManager access to this class. |
| 103 | */ |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 104 | public static final String SERVICE = "devicestoragemonitor"; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | /** |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 107 | * 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] | 108 | * notification if the device runs low on disk space |
| 109 | */ |
| 110 | Handler mHandler = new Handler() { |
| 111 | @Override |
| 112 | public void handleMessage(Message msg) { |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 113 | //don't handle an invalid message |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | if (msg.what != DEVICE_MEMORY_WHAT) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 115 | Slog.e(TAG, "Will not process invalid message"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | checkMemory(msg.arg1 == _TRUE); |
| 119 | } |
| 120 | }; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | class CachePackageDataObserver extends IPackageDataObserver.Stub { |
| 123 | public void onRemoveCompleted(String packageName, boolean succeeded) { |
| 124 | mClearSucceeded = succeeded; |
| 125 | mClearingCache = false; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 126 | if(localLOGV) Slog.i(TAG, " Clear succeeded:"+mClearSucceeded |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | +", mClearingCache:"+mClearingCache+" Forcing memory check"); |
| 128 | postCheckMemoryMsg(false, 0); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 129 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | private final void restatDataDir() { |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 133 | try { |
| 134 | mDataFileStats.restat(DATA_PATH); |
| 135 | mFreeMem = (long) mDataFileStats.getAvailableBlocks() * |
| 136 | mDataFileStats.getBlockSize(); |
| 137 | } catch (IllegalArgumentException e) { |
| 138 | // use the old value of mFreeMem |
| 139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | // Allow freemem to be overridden by debug.freemem for testing |
| 141 | String debugFreeMem = SystemProperties.get("debug.freemem"); |
| 142 | if (!"".equals(debugFreeMem)) { |
| 143 | mFreeMem = Long.parseLong(debugFreeMem); |
| 144 | } |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 145 | // Read the log interval from secure settings |
| 146 | long freeMemLogInterval = Settings.Secure.getLong(mContentResolver, |
| 147 | Settings.Secure.SYS_FREE_STORAGE_LOG_INTERVAL, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES)*60*1000; |
| 149 | //log the amount of free memory in event log |
| 150 | long currTime = SystemClock.elapsedRealtime(); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 151 | if((mLastReportedFreeMemTime == 0) || |
| 152 | (currTime-mLastReportedFreeMemTime) >= freeMemLogInterval) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | mLastReportedFreeMemTime = currTime; |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 154 | long mFreeSystem = -1, mFreeCache = -1; |
| 155 | try { |
| 156 | mSystemFileStats.restat(SYSTEM_PATH); |
| 157 | mFreeSystem = (long) mSystemFileStats.getAvailableBlocks() * |
| 158 | mSystemFileStats.getBlockSize(); |
| 159 | } catch (IllegalArgumentException e) { |
| 160 | // ignore; report -1 |
| 161 | } |
| 162 | try { |
| 163 | mCacheFileStats.restat(CACHE_PATH); |
| 164 | mFreeCache = (long) mCacheFileStats.getAvailableBlocks() * |
| 165 | mCacheFileStats.getBlockSize(); |
| 166 | } catch (IllegalArgumentException e) { |
| 167 | // ignore; report -1 |
| 168 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 169 | EventLog.writeEvent(EventLogTags.FREE_STORAGE_LEFT, |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 170 | mFreeMem, mFreeSystem, mFreeCache); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 172 | // Read the reporting threshold from secure settings |
| 173 | long threshold = Settings.Secure.getLong(mContentResolver, |
| 174 | Settings.Secure.DISK_FREE_CHANGE_REPORTING_THRESHOLD, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD); |
| 176 | // If mFree changed significantly log the new value |
| 177 | long delta = mFreeMem - mLastReportedFreeMem; |
| 178 | if (delta > threshold || delta < -threshold) { |
| 179 | mLastReportedFreeMem = mFreeMem; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 180 | EventLog.writeEvent(EventLogTags.FREE_STORAGE_CHANGED, mFreeMem); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | private final void clearCache() { |
| 185 | if (mClearCacheObserver == null) { |
| 186 | // Lazy instantiation |
| 187 | mClearCacheObserver = new CachePackageDataObserver(); |
| 188 | } |
| 189 | mClearingCache = true; |
| 190 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 191 | if (localLOGV) Slog.i(TAG, "Clearing cache"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | IPackageManager.Stub.asInterface(ServiceManager.getService("package")). |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 193 | freeStorageAndNotify(mMemLowThreshold, mClearCacheObserver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 195 | 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] | 196 | mClearingCache = false; |
| 197 | mClearSucceeded = false; |
| 198 | } |
| 199 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | private final void checkMemory(boolean checkCache) { |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 202 | //if the thread that was started to clear cache is still running do nothing till its |
| 203 | //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] | 204 | // and should be accessed via a lock but even if it does this test will fail now and |
| 205 | //hopefully the next time this flag will be set to the correct value. |
| 206 | if(mClearingCache) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 207 | 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] | 208 | //make sure the thread is not hung for too long |
| 209 | long diffTime = System.currentTimeMillis() - mThreadStartTime; |
| 210 | if(diffTime > (10*60*1000)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 211 | 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] | 212 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } else { |
| 214 | restatDataDir(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 215 | if (localLOGV) Slog.v(TAG, "freeMemory="+mFreeMem); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | //post intent to NotificationManager to display icon if necessary |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 218 | if (mFreeMem < mMemLowThreshold) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | if (!mLowMemFlag) { |
| 220 | if (checkCache) { |
| 221 | // See if clearing cache helps |
| 222 | // Note that clearing cache is asynchronous and so we do a |
| 223 | // memory check again once the cache has been cleared. |
| 224 | mThreadStartTime = System.currentTimeMillis(); |
| 225 | mClearSucceeded = false; |
| 226 | clearCache(); |
| 227 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 228 | Slog.i(TAG, "Running low on memory. Sending notification"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | sendNotification(); |
| 230 | mLowMemFlag = true; |
| 231 | } |
| 232 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 233 | if (localLOGV) Slog.v(TAG, "Running low on memory " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | "notification already sent. do nothing"); |
| 235 | } |
| 236 | } else { |
| 237 | if (mLowMemFlag) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 238 | Slog.i(TAG, "Memory available. Cancelling notification"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | cancelNotification(); |
| 240 | mLowMemFlag = false; |
| 241 | } |
| 242 | } |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 243 | if (mFreeMem < mMemFullThreshold) { |
| 244 | if (!mMemFullFlag) { |
| 245 | sendFullNotification(); |
| 246 | mMemFullFlag = true; |
| 247 | } |
| 248 | } else { |
| 249 | if (mMemFullFlag) { |
| 250 | cancelFullNotification(); |
| 251 | mMemFullFlag = false; |
| 252 | } |
| 253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 255 | if(localLOGV) Slog.i(TAG, "Posting Message again"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | //keep posting messages to itself periodically |
| 257 | postCheckMemoryMsg(true, DEFAULT_CHECK_INTERVAL); |
| 258 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | private void postCheckMemoryMsg(boolean clearCache, long delay) { |
| 261 | // Remove queued messages |
| 262 | mHandler.removeMessages(DEVICE_MEMORY_WHAT); |
| 263 | mHandler.sendMessageDelayed(mHandler.obtainMessage(DEVICE_MEMORY_WHAT, |
| 264 | clearCache ?_TRUE : _FALSE, 0), |
| 265 | delay); |
| 266 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 267 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | /* |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 269 | * just query settings to retrieve the memory threshold. |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 270 | * Preferred this over using a ContentObserver since Settings.Secure caches the value |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | * any way |
| 272 | */ |
| 273 | private long getMemThreshold() { |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 274 | long value = Settings.Secure.getInt( |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 275 | mContentResolver, |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 276 | Settings.Secure.SYS_STORAGE_THRESHOLD_PERCENTAGE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | DEFAULT_THRESHOLD_PERCENTAGE); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 278 | if(localLOGV) Slog.v(TAG, "Threshold Percentage="+value); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 279 | value *= mTotalMemory; |
| 280 | long maxValue = Settings.Secure.getInt( |
| 281 | mContentResolver, |
| 282 | Settings.Secure.SYS_STORAGE_THRESHOLD_MAX_BYTES, |
| 283 | DEFAULT_THRESHOLD_MAX_BYTES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | //evaluate threshold value |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 285 | return value < maxValue ? value : maxValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 288 | /* |
| 289 | * just query settings to retrieve the memory full threshold. |
| 290 | * Preferred this over using a ContentObserver since Settings.Secure caches the value |
| 291 | * any way |
| 292 | */ |
| 293 | private int getMemFullThreshold() { |
| 294 | int value = Settings.Secure.getInt( |
| 295 | mContentResolver, |
| 296 | Settings.Secure.SYS_STORAGE_FULL_THRESHOLD_BYTES, |
| 297 | DEFAULT_FULL_THRESHOLD_BYTES); |
| 298 | if(localLOGV) Slog.v(TAG, "Full Threshold Bytes="+value); |
| 299 | return value; |
| 300 | } |
| 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | /** |
| 303 | * Constructor to run service. initializes the disk space threshold value |
| 304 | * and posts an empty message to kickstart the process. |
| 305 | */ |
| 306 | public DeviceStorageMonitorService(Context context) { |
| 307 | mLastReportedFreeMemTime = 0; |
| 308 | mContext = context; |
| 309 | mContentResolver = mContext.getContentResolver(); |
| 310 | //create StatFs object |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 311 | mDataFileStats = new StatFs(DATA_PATH); |
| 312 | mSystemFileStats = new StatFs(SYSTEM_PATH); |
| 313 | mCacheFileStats = new StatFs(CACHE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | //initialize total storage on device |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 315 | mTotalMemory = ((long)mDataFileStats.getBlockCount() * |
| 316 | mDataFileStats.getBlockSize())/100L; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | mStorageLowIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_LOW); |
Jeff Hamilton | 4b33092 | 2010-06-04 15:16:06 -0500 | [diff] [blame] | 318 | mStorageLowIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | mStorageOkIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_OK); |
Jeff Hamilton | 4b33092 | 2010-06-04 15:16:06 -0500 | [diff] [blame] | 320 | mStorageOkIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 321 | mStorageFullIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_FULL); |
| 322 | mStorageFullIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 323 | mStorageNotFullIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_NOT_FULL); |
| 324 | mStorageNotFullIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 325 | // cache storage thresholds |
| 326 | mMemLowThreshold = getMemThreshold(); |
| 327 | mMemFullThreshold = getMemFullThreshold(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | checkMemory(true); |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame^] | 329 | |
| 330 | mCacheFileDeletedObserver = new CacheFileDeletedObserver(); |
| 331 | mCacheFileDeletedObserver.startWatching(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 333 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | * This method sends a notification to NotificationManager to display |
| 337 | * an error dialog indicating low disk space and launch the Installer |
| 338 | * application |
| 339 | */ |
| 340 | private final void sendNotification() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 341 | if(localLOGV) Slog.i(TAG, "Sending low memory notification"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | //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] | 343 | EventLog.writeEvent(EventLogTags.LOW_STORAGE, mFreeMem); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | // Pack up the values and broadcast them to everyone |
Dianne Hackborn | f882efa | 2012-04-11 16:04:12 -0700 | [diff] [blame] | 345 | Intent lowMemIntent = new Intent(Environment.isExternalStorageEmulated() |
| 346 | ? Settings.ACTION_INTERNAL_STORAGE_SETTINGS |
| 347 | : Intent.ACTION_MANAGE_PACKAGE_STORAGE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | lowMemIntent.putExtra("memory", mFreeMem); |
| 349 | lowMemIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 350 | NotificationManager mNotificationMgr = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | (NotificationManager)mContext.getSystemService( |
| 352 | Context.NOTIFICATION_SERVICE); |
| 353 | CharSequence title = mContext.getText( |
| 354 | com.android.internal.R.string.low_internal_storage_view_title); |
| 355 | CharSequence details = mContext.getText( |
| 356 | com.android.internal.R.string.low_internal_storage_view_text); |
| 357 | PendingIntent intent = PendingIntent.getActivity(mContext, 0, lowMemIntent, 0); |
| 358 | Notification notification = new Notification(); |
| 359 | notification.icon = com.android.internal.R.drawable.stat_notify_disk_full; |
| 360 | notification.tickerText = title; |
| 361 | notification.flags |= Notification.FLAG_NO_CLEAR; |
| 362 | notification.setLatestEventInfo(mContext, title, details, intent); |
| 363 | mNotificationMgr.notify(LOW_MEMORY_NOTIFICATION_ID, notification); |
| 364 | mContext.sendStickyBroadcast(mStorageLowIntent); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Cancels low storage notification and sends OK intent. |
| 369 | */ |
| 370 | private final void cancelNotification() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 371 | if(localLOGV) Slog.i(TAG, "Canceling low memory notification"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | NotificationManager mNotificationMgr = |
| 373 | (NotificationManager)mContext.getSystemService( |
| 374 | Context.NOTIFICATION_SERVICE); |
| 375 | //cancel notification since memory has been freed |
| 376 | mNotificationMgr.cancel(LOW_MEMORY_NOTIFICATION_ID); |
| 377 | |
| 378 | mContext.removeStickyBroadcast(mStorageLowIntent); |
| 379 | mContext.sendBroadcast(mStorageOkIntent); |
| 380 | } |
Doug Zongker | 3161795b | 2009-10-07 15:14:03 -0700 | [diff] [blame] | 381 | |
Jake Hamby | bb37163 | 2010-08-23 18:16:48 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Send a notification when storage is full. |
| 384 | */ |
| 385 | private final void sendFullNotification() { |
| 386 | if(localLOGV) Slog.i(TAG, "Sending memory full notification"); |
| 387 | mContext.sendStickyBroadcast(mStorageFullIntent); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Cancels memory full notification and sends "not full" intent. |
| 392 | */ |
| 393 | private final void cancelFullNotification() { |
| 394 | if(localLOGV) Slog.i(TAG, "Canceling memory full notification"); |
| 395 | mContext.removeStickyBroadcast(mStorageFullIntent); |
| 396 | mContext.sendBroadcast(mStorageNotFullIntent); |
| 397 | } |
| 398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | public void updateMemory() { |
| 400 | int callingUid = getCallingUid(); |
| 401 | if(callingUid != Process.SYSTEM_UID) { |
| 402 | return; |
| 403 | } |
| 404 | // force an early check |
| 405 | postCheckMemoryMsg(true, 0); |
| 406 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * Callable from other things in the system service to obtain the low memory |
| 410 | * threshold. |
| 411 | * |
| 412 | * @return low memory threshold in bytes |
| 413 | */ |
| 414 | public long getMemoryLowThreshold() { |
| 415 | return mMemLowThreshold; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Callable from other things in the system process to check whether memory |
| 420 | * is low. |
| 421 | * |
| 422 | * @return true is memory is low |
| 423 | */ |
| 424 | public boolean isMemoryLow() { |
| 425 | return mLowMemFlag; |
| 426 | } |
Jeff Sharkey | 4b49657 | 2012-04-19 14:17:03 -0700 | [diff] [blame^] | 427 | |
| 428 | public static class CacheFileDeletedObserver extends FileObserver { |
| 429 | public CacheFileDeletedObserver() { |
| 430 | super(Environment.getDownloadCacheDirectory().getAbsolutePath(), FileObserver.DELETE); |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void onEvent(int event, String path) { |
| 435 | EventLogTags.writeCacheFileDeleted(path); |
| 436 | } |
| 437 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | } |