Better API for low disk space warnings.

Provides uniform interface to ask about low disk thresholds; can
be mocked by other tests.  Opens door to adjust thresholds based on
disk type.

Switch monitor service to using new API, and use filesystem paths
from Environment instead of hard-coding.

Change-Id: Ifdb536e36a453f1b67bc65849037ec3cc0232cf2
diff --git a/services/java/com/android/server/DeviceStorageMonitorService.java b/services/java/com/android/server/DeviceStorageMonitorService.java
index 94a087a..016c561 100644
--- a/services/java/com/android/server/DeviceStorageMonitorService.java
+++ b/services/java/com/android/server/DeviceStorageMonitorService.java
@@ -16,9 +16,6 @@
 
 package com.android.server;
 
-import java.io.FileDescriptor;
-import java.io.PrintWriter;
-
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
@@ -40,12 +37,17 @@
 import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UserHandle;
+import android.os.storage.StorageManager;
 import android.provider.Settings;
 import android.text.format.Formatter;
 import android.util.EventLog;
 import android.util.Slog;
 import android.util.TimeUtils;
 
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+
 /**
  * This class implements a service to monitor the amount of disk
  * storage space on the device.  If the free storage on device is less
@@ -66,17 +68,18 @@
  */
 public class DeviceStorageMonitorService extends Binder {
     private static final String TAG = "DeviceStorageMonitorService";
+
     private static final boolean DEBUG = false;
     private static final boolean localLOGV = false;
+
     private static final int DEVICE_MEMORY_WHAT = 1;
     private static final int MONITOR_INTERVAL = 1; //in minutes
     private static final int LOW_MEMORY_NOTIFICATION_ID = 1;
-    private static final int DEFAULT_THRESHOLD_PERCENTAGE = 10;
-    private static final int DEFAULT_THRESHOLD_MAX_BYTES = 500*1024*1024; // 500MB
+
     private static final int DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES = 12*60; //in minutes
     private static final long DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD = 2 * 1024 * 1024; // 2MB
     private static final long DEFAULT_CHECK_INTERVAL = MONITOR_INTERVAL*60*1000;
-    private static final int DEFAULT_FULL_THRESHOLD_BYTES = 1024*1024; // 1MB
+
     private long mFreeMem;  // on /data
     private long mFreeMemAfterLastCacheClear;  // on /data
     private long mLastReportedFreeMem;
@@ -84,14 +87,16 @@
     private boolean mLowMemFlag=false;
     private boolean mMemFullFlag=false;
     private Context mContext;
-    private ContentResolver mContentResolver;
+    private ContentResolver mResolver;
     private long mTotalMemory;  // on /data
     private StatFs mDataFileStats;
     private StatFs mSystemFileStats;
     private StatFs mCacheFileStats;
-    private static final String DATA_PATH = "/data";
-    private static final String SYSTEM_PATH = "/system";
-    private static final String CACHE_PATH = "/cache";
+
+    private static final File DATA_PATH = Environment.getDataDirectory();
+    private static final File SYSTEM_PATH = Environment.getRootDirectory();
+    private static final File CACHE_PATH = Environment.getDownloadCacheDirectory();
+
     private long mThreadStartTime = -1;
     private boolean mClearSucceeded = false;
     private boolean mClearingCache;
@@ -116,7 +121,7 @@
     // more files than absolutely needed, to reduce the frequency that
     // flushing takes place.
     private long mMemCacheTrimToThreshold;
-    private int mMemFullThreshold;
+    private long mMemFullThreshold;
 
     /**
      * This string is used for ServiceManager access to this class.
@@ -151,7 +156,7 @@
 
     private final void restatDataDir() {
         try {
-            mDataFileStats.restat(DATA_PATH);
+            mDataFileStats.restat(DATA_PATH.getAbsolutePath());
             mFreeMem = (long) mDataFileStats.getAvailableBlocks() *
                 mDataFileStats.getBlockSize();
         } catch (IllegalArgumentException e) {
@@ -163,7 +168,7 @@
             mFreeMem = Long.parseLong(debugFreeMem);
         }
         // Read the log interval from secure settings
-        long freeMemLogInterval = Settings.Global.getLong(mContentResolver,
+        long freeMemLogInterval = Settings.Global.getLong(mResolver,
                 Settings.Global.SYS_FREE_STORAGE_LOG_INTERVAL,
                 DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES)*60*1000;
         //log the amount of free memory in event log
@@ -173,14 +178,14 @@
             mLastReportedFreeMemTime = currTime;
             long mFreeSystem = -1, mFreeCache = -1;
             try {
-                mSystemFileStats.restat(SYSTEM_PATH);
+                mSystemFileStats.restat(SYSTEM_PATH.getAbsolutePath());
                 mFreeSystem = (long) mSystemFileStats.getAvailableBlocks() *
                     mSystemFileStats.getBlockSize();
             } catch (IllegalArgumentException e) {
                 // ignore; report -1
             }
             try {
-                mCacheFileStats.restat(CACHE_PATH);
+                mCacheFileStats.restat(CACHE_PATH.getAbsolutePath());
                 mFreeCache = (long) mCacheFileStats.getAvailableBlocks() *
                     mCacheFileStats.getBlockSize();
             } catch (IllegalArgumentException e) {
@@ -190,7 +195,7 @@
                                 mFreeMem, mFreeSystem, mFreeCache);
         }
         // Read the reporting threshold from secure settings
-        long threshold = Settings.Global.getLong(mContentResolver,
+        long threshold = Settings.Global.getLong(mResolver,
                 Settings.Global.DISK_FREE_CHANGE_REPORTING_THRESHOLD,
                 DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD);
         // If mFree changed significantly log the new value
@@ -303,40 +308,6 @@
                 delay);
     }
 
-    /*
-     * just query settings to retrieve the memory threshold.
-     * Preferred this over using a ContentObserver since Settings.Secure caches the value
-     * any way
-     */
-    private long getMemThreshold() {
-        long value = Settings.Global.getInt(
-                              mContentResolver,
-                              Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE,
-                              DEFAULT_THRESHOLD_PERCENTAGE);
-        if(localLOGV) Slog.v(TAG, "Threshold Percentage="+value);
-        value = (value*mTotalMemory)/100;
-        long maxValue = Settings.Global.getInt(
-                mContentResolver,
-                Settings.Global.SYS_STORAGE_THRESHOLD_MAX_BYTES,
-                DEFAULT_THRESHOLD_MAX_BYTES);
-        //evaluate threshold value
-        return value < maxValue ? value : maxValue;
-    }
-
-    /*
-     * just query settings to retrieve the memory full threshold.
-     * Preferred this over using a ContentObserver since Settings.Secure caches the value
-     * any way
-     */
-    private int getMemFullThreshold() {
-        int value = Settings.Global.getInt(
-                              mContentResolver,
-                              Settings.Global.SYS_STORAGE_FULL_THRESHOLD_BYTES,
-                              DEFAULT_FULL_THRESHOLD_BYTES);
-        if(localLOGV) Slog.v(TAG, "Full Threshold Bytes="+value);
-        return value;
-    }
-
     /**
     * Constructor to run service. initializes the disk space threshold value
     * and posts an empty message to kickstart the process.
@@ -344,11 +315,11 @@
     public DeviceStorageMonitorService(Context context) {
         mLastReportedFreeMemTime = 0;
         mContext = context;
-        mContentResolver = mContext.getContentResolver();
+        mResolver = mContext.getContentResolver();
         //create StatFs object
-        mDataFileStats = new StatFs(DATA_PATH);
-        mSystemFileStats = new StatFs(SYSTEM_PATH);
-        mCacheFileStats = new StatFs(CACHE_PATH);
+        mDataFileStats = new StatFs(DATA_PATH.getAbsolutePath());
+        mSystemFileStats = new StatFs(SYSTEM_PATH.getAbsolutePath());
+        mCacheFileStats = new StatFs(CACHE_PATH.getAbsolutePath());
         //initialize total storage on device
         mTotalMemory = (long)mDataFileStats.getBlockCount() *
                         mDataFileStats.getBlockSize();
@@ -360,9 +331,12 @@
         mStorageFullIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
         mStorageNotFullIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_NOT_FULL);
         mStorageNotFullIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
+
         // cache storage thresholds
-        mMemLowThreshold = getMemThreshold();
-        mMemFullThreshold = getMemFullThreshold();
+        final StorageManager sm = StorageManager.from(context);
+        mMemLowThreshold = sm.getStorageLowBytes(DATA_PATH);
+        mMemFullThreshold = sm.getStorageFullBytes(DATA_PATH);
+
         mMemCacheStartTrimThreshold = ((mMemLowThreshold*3)+mMemFullThreshold)/4;
         mMemCacheTrimToThreshold = mMemLowThreshold
                 + ((mMemLowThreshold-mMemCacheStartTrimThreshold)*2);
@@ -373,7 +347,6 @@
         mCacheFileDeletedObserver.startWatching();
     }
 
-
     /**
     * This method sends a notification to NotificationManager to display
     * an error dialog indicating low disk space and launch the Installer