move some system services from gservices to secure settings

Change-Id: Ie2dfb99a2b42b2cc9310b858c044d8684e3493fa
diff --git a/services/java/com/android/server/BatteryService.java b/services/java/com/android/server/BatteryService.java
index bdebc8d..f435ebc 100644
--- a/services/java/com/android/server/BatteryService.java
+++ b/services/java/com/android/server/BatteryService.java
@@ -413,10 +413,10 @@
 
     private final void logOutlier(long duration) {
         ContentResolver cr = mContext.getContentResolver();
-        String dischargeThresholdString = Settings.Gservices.getString(cr,
-                Settings.Gservices.BATTERY_DISCHARGE_THRESHOLD);
-        String durationThresholdString = Settings.Gservices.getString(cr,
-                Settings.Gservices.BATTERY_DISCHARGE_DURATION_THRESHOLD);
+        String dischargeThresholdString = Settings.Secure.getString(cr,
+                Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
+        String durationThresholdString = Settings.Secure.getString(cr,
+                Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
 
         if (dischargeThresholdString != null && durationThresholdString != null) {
             try {
diff --git a/services/java/com/android/server/DeviceStorageMonitorService.java b/services/java/com/android/server/DeviceStorageMonitorService.java
index 8e54c6e..e58d346 100644
--- a/services/java/com/android/server/DeviceStorageMonitorService.java
+++ b/services/java/com/android/server/DeviceStorageMonitorService.java
@@ -34,25 +34,29 @@
 import android.os.StatFs;
 import android.os.SystemClock;
 import android.os.SystemProperties;
-import android.provider.Settings.Gservices;
+import android.provider.Settings;
 import android.util.Config;
 import android.util.EventLog;
 import android.util.Log;
 import android.provider.Settings;
 
 /**
- * This class implements a service to monitor the amount of disk storage space
- * on the device. If the free storage on device is less than a tunable threshold value
- * (default is 10%. this value is a gservices parameter) a low memory notification is
- * displayed to alert the user. If the user clicks on the low memory notification the
- * Application Manager application gets launched to let the user free storage space.
- * Event log events:
- * A low memory event with the free storage on device in bytes  is logged to the event log
- * when the device goes low on storage space.
- * The amount of free storage on the device is periodically logged to the event log. The log
- * interval is a gservices parameter with a default value of 12 hours
- * When the free storage differential goes below a threshold(again a gservices parameter with
- * a default value of 2MB), the free memory is logged to the event log
+ * This class implements a service to monitor the amount of disk
+ * storage space on the device.  If the free storage on device is less
+ * than a tunable threshold value (a secure settings parameter;
+ * default 10%) a low memory notification is displayed to alert the
+ * user. If the user clicks on the low memory notification the
+ * Application Manager application gets launched to let the user free
+ * storage space.
+ *
+ * Event log events: A low memory event with the free storage on
+ * device in bytes is logged to the event log when the device goes low
+ * on storage space.  The amount of free storage on the device is
+ * periodically logged to the event log. The log interval is a secure
+ * settings parameter with a default value of 12 hours.  When the free
+ * storage differential goes below a threshold (again a secure
+ * settings parameter with a default value of 2MB), the free memory is
+ * logged to the event log.
  */
 class DeviceStorageMonitorService extends Binder {
     private static final String TAG = "DeviceStorageMonitorService";
@@ -131,9 +135,9 @@
         if (!"".equals(debugFreeMem)) {
             mFreeMem = Long.parseLong(debugFreeMem);
         }
-        // Read the log interval from Gservices
-        long freeMemLogInterval = Gservices.getLong(mContentResolver,
-                Gservices.SYS_FREE_STORAGE_LOG_INTERVAL,
+        // Read the log interval from secure settings
+        long freeMemLogInterval = Settings.Secure.getLong(mContentResolver,
+                Settings.Secure.SYS_FREE_STORAGE_LOG_INTERVAL,
                 DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES)*60*1000;
         //log the amount of free memory in event log
         long currTime = SystemClock.elapsedRealtime();
@@ -159,9 +163,9 @@
             EventLog.writeEvent(EventLogTags.FREE_STORAGE_LEFT,
                                 mFreeMem, mFreeSystem, mFreeCache);
         }
-        // Read the reporting threshold from Gservices
-        long threshold = Gservices.getLong(mContentResolver,
-                Gservices.DISK_FREE_CHANGE_REPORTING_THRESHOLD,
+        // Read the reporting threshold from secure settings
+        long threshold = Settings.Secure.getLong(mContentResolver,
+                Settings.Secure.DISK_FREE_CHANGE_REPORTING_THRESHOLD,
                 DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD);
         // If mFree changed significantly log the new value
         long delta = mFreeMem - mLastReportedFreeMem;
@@ -247,13 +251,13 @@
 
     /*
      * just query settings to retrieve the memory threshold.
-     * Preferred this over using a ContentObserver since Settings.Gservices caches the value
+     * Preferred this over using a ContentObserver since Settings.Secure caches the value
      * any way
      */
     private long getMemThreshold() {
-        int value = Settings.Gservices.getInt(
+        int value = Settings.Secure.getInt(
                               mContentResolver,
-                              Settings.Gservices.SYS_STORAGE_THRESHOLD_PERCENTAGE,
+                              Settings.Secure.SYS_STORAGE_THRESHOLD_PERCENTAGE,
                               DEFAULT_THRESHOLD_PERCENTAGE);
         if(localLOGV) Log.v(TAG, "Threshold Percentage="+value);
         //evaluate threshold value
diff --git a/services/java/com/android/server/DropBoxManagerService.java b/services/java/com/android/server/DropBoxManagerService.java
index e1db6b6..7a708f9 100644
--- a/services/java/com/android/server/DropBoxManagerService.java
+++ b/services/java/com/android/server/DropBoxManagerService.java
@@ -22,9 +22,11 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
+import android.database.ContentObserver;
 import android.net.Uri;
 import android.os.Debug;
 import android.os.DropBoxManager;
+import android.os.Handler;
 import android.os.ParcelFileDescriptor;
 import android.os.StatFs;
 import android.os.SystemClock;
@@ -113,14 +115,21 @@
      * @param context to use for receiving free space & gservices intents
      * @param path to store drop box entries in
      */
-    public DropBoxManagerService(Context context, File path) {
+    public DropBoxManagerService(final Context context, File path) {
         mDropBoxDir = path;
 
         // Set up intent receivers
         mContext = context;
         mContentResolver = context.getContentResolver();
         context.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW));
-        context.registerReceiver(mReceiver, new IntentFilter(Settings.Gservices.CHANGED_ACTION));
+
+        mContentResolver.registerContentObserver(
+            Settings.Secure.CONTENT_URI, true,
+            new ContentObserver(new Handler()) {
+                public void onChange(boolean selfChange) {
+                    mReceiver.onReceive(context, (Intent) null);
+                }
+            });
 
         // The real work gets done lazily in init() -- that way service creation always
         // succeeds, and things like disk problems cause individual method failures.
@@ -205,8 +214,8 @@
     }
 
     public boolean isTagEnabled(String tag) {
-        return !"disabled".equals(Settings.Gservices.getString(
-                mContentResolver, Settings.Gservices.DROPBOX_TAG_PREFIX + tag));
+        return !"disabled".equals(Settings.Secure.getString(
+                mContentResolver, Settings.Secure.DROPBOX_TAG_PREFIX + tag));
     }
 
     public synchronized DropBoxManager.Entry getNextEntry(String tag, long millis) {
@@ -611,8 +620,8 @@
     private synchronized long trimToFit() {
         // Expunge aged items (including tombstones marking deleted data).
 
-        int ageSeconds = Settings.Gservices.getInt(mContentResolver,
-                Settings.Gservices.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS);
+        int ageSeconds = Settings.Secure.getInt(mContentResolver,
+                Settings.Secure.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS);
         long cutoffMillis = System.currentTimeMillis() - ageSeconds * 1000;
         while (!mAllFiles.contents.isEmpty()) {
             EntryFile entry = mAllFiles.contents.first();
@@ -631,12 +640,12 @@
 
         long uptimeMillis = SystemClock.uptimeMillis();
         if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) {
-            int quotaPercent = Settings.Gservices.getInt(mContentResolver,
-                    Settings.Gservices.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT);
-            int reservePercent = Settings.Gservices.getInt(mContentResolver,
-                    Settings.Gservices.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT);
-            int quotaKb = Settings.Gservices.getInt(mContentResolver,
-                    Settings.Gservices.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB);
+            int quotaPercent = Settings.Secure.getInt(mContentResolver,
+                    Settings.Secure.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT);
+            int reservePercent = Settings.Secure.getInt(mContentResolver,
+                    Settings.Secure.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT);
+            int quotaKb = Settings.Secure.getInt(mContentResolver,
+                    Settings.Secure.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB);
 
             mStatFs.restat(mDropBoxDir.getPath());
             int available = mStatFs.getAvailableBlocks();
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index 89261a8..bf6996c 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -29,6 +29,7 @@
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
+import android.database.ContentObserver;
 import android.database.Cursor;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
@@ -36,9 +37,11 @@
 import android.hardware.SensorManager;
 import android.os.BatteryStats;
 import android.os.Binder;
+import android.os.Environment;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.IBinder;
+import android.os.IMountService;
 import android.os.IPowerManager;
 import android.os.LocalPowerManager;
 import android.os.Power;
@@ -46,8 +49,6 @@
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
-import android.os.Environment;
-import android.os.IMountService;
 import android.os.SystemClock;
 import android.provider.Settings.SettingNotFoundException;
 import android.provider.Settings;
@@ -88,7 +89,7 @@
                                         | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
 
     //                       time since last state:               time since last event:
-    // The short keylight delay comes from Gservices; this is the default.
+    // The short keylight delay comes from secure settings; this is the default.
     private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec
     private static final int MEDIUM_KEYLIGHT_DELAY = 15000;       // t+15 sec
     private static final int LONG_KEYLIGHT_DELAY = 6000;        // t+6 sec
@@ -103,7 +104,7 @@
     // trigger proximity if distance is less than 5 cm
     private static final float PROXIMITY_THRESHOLD = 5.0f;
 
-    // Cached Gservices settings; see updateGservicesValues()
+    // Cached secure settings; see updateSettingsValues()
     private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT;
 
     // flags for setPowerState
@@ -516,12 +517,15 @@
         filter.addAction(Intent.ACTION_BOOT_COMPLETED);
         mContext.registerReceiver(new BootCompletedReceiver(), filter);
 
-        // Listen for Gservices changes
-        IntentFilter gservicesChangedFilter =
-                new IntentFilter(Settings.Gservices.CHANGED_ACTION);
-        mContext.registerReceiver(new GservicesChangedReceiver(), gservicesChangedFilter);
-        // And explicitly do the initial update of our cached settings
-        updateGservicesValues();
+        // Listen for secure settings changes
+        mContext.getContentResolver().registerContentObserver(
+            Settings.Secure.CONTENT_URI, true,
+            new ContentObserver(new Handler()) {
+                public void onChange(boolean selfChange) {
+                    updateSettingsValues();
+                }
+            });
+        updateSettingsValues();
 
         if (mUseSoftwareAutoBrightness) {
             // turn the screen on
@@ -2273,7 +2277,7 @@
      * */
     private void setScreenOffTimeoutsLocked() {
         if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
-            mKeylightDelay = mShortKeylightDelay;  // Configurable via Gservices
+            mKeylightDelay = mShortKeylightDelay;  // Configurable via secure settings
             mDimDelay = -1;
             mScreenOffDelay = 0;
         } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
@@ -2308,28 +2312,15 @@
     }
 
     /**
-     * Refreshes cached Gservices settings.  Called once on startup, and
-     * on subsequent Settings.Gservices.CHANGED_ACTION broadcasts (see
-     * GservicesChangedReceiver).
+     * Refreshes cached secure settings.  Called once on startup, and
+     * on subsequent changes to secure settings.
      */
-    private void updateGservicesValues() {
-        mShortKeylightDelay = Settings.Gservices.getInt(
+    private void updateSettingsValues() {
+        mShortKeylightDelay = Settings.Secure.getInt(
                 mContext.getContentResolver(),
-                Settings.Gservices.SHORT_KEYLIGHT_DELAY_MS,
+                Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
                 SHORT_KEYLIGHT_DELAY_DEFAULT);
-        // Log.i(TAG, "updateGservicesValues(): mShortKeylightDelay now " + mShortKeylightDelay);
-    }
-
-    /**
-     * Receiver for the Gservices.CHANGED_ACTION broadcast intent,
-     * which tells us we need to refresh our cached Gservices settings.
-     */
-    private class GservicesChangedReceiver extends BroadcastReceiver {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            // Log.i(TAG, "GservicesChangedReceiver.onReceive(): " + intent);
-            updateGservicesValues();
-        }
+        // Log.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
     }
 
     private class LockList extends ArrayList<WakeLock>
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java
index 987a24e..6fab96b 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/WifiService.java
@@ -113,8 +113,8 @@
     private final IBatteryStats mBatteryStats;
 
     /**
-     * See {@link Settings.Gservices#WIFI_IDLE_MS}. This is the default value if a
-     * Settings.Gservices value is not present. This timeout value is chosen as
+     * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
+     * Settings.Secure value is not present. This timeout value is chosen as
      * the approximate point at which the battery drain caused by Wi-Fi
      * being enabled but not active exceeds the battery drain caused by
      * re-establishing a connection to the mobile data network.
@@ -1317,11 +1317,12 @@
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
 
-            long idleMillis = Settings.Gservices.getLong(mContext.getContentResolver(),
-                                                  Settings.Gservices.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS);
+            long idleMillis =
+                Settings.Secure.getLong(mContext.getContentResolver(),
+                                        Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS);
             int stayAwakeConditions =
-                    Settings.System.getInt(mContext.getContentResolver(),
-                                           Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
+                Settings.System.getInt(mContext.getContentResolver(),
+                                       Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
             if (action.equals(Intent.ACTION_SCREEN_ON)) {
                 Log.d(TAG, "ACTION_SCREEN_ON");
                 mAlarmManager.cancel(mIdleIntent);
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 7f12b6d..172bcdb 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -8579,9 +8579,9 @@
     }
 
     private ComponentName getErrorReportReceiver(ProcessRecord app) {
-        // check if error reporting is enabled in Gservices
-        int enabled = Settings.Gservices.getInt(mContext.getContentResolver(),
-                Settings.Gservices.SEND_ACTION_APP_ERROR, 0);
+        // check if error reporting is enabled in secure settings
+        int enabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.SEND_ACTION_APP_ERROR, 0);
         if (enabled == 0) {
             return null;
         }
@@ -8830,8 +8830,8 @@
 
         addExceptionToDropBox("wtf", r, tag, crashInfo);
 
-        if (Settings.Gservices.getInt(mContext.getContentResolver(),
-                Settings.Gservices.WTF_IS_FATAL, 0) != 0) {
+        if (Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.WTF_IS_FATAL, 0) != 0) {
             crashApplication(r, crashInfo);
             return true;
         } else {