Migrate Dreams default settings to DatabaseHelper.

Also switch Dreams on for all charging devices by default.

Change-Id: Iccbcc2906d55214320b2f467e88ba9cb188f4886
diff --git a/core/java/android/service/dreams/DreamManagerService.java b/core/java/android/service/dreams/DreamManagerService.java
index d6b38a1..dd177cb 100644
--- a/core/java/android/service/dreams/DreamManagerService.java
+++ b/core/java/android/service/dreams/DreamManagerService.java
@@ -5,8 +5,6 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 
-import com.android.internal.view.IInputMethod;
-
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -16,34 +14,32 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.ServiceManager;
-import android.os.SystemClock;
 import android.provider.Settings;
-import android.util.Log;
 import android.util.Slog;
 import android.view.IWindowManager;
 import android.view.WindowManager;
 
 /**
- * 
+ *
  * @hide
  *
  */
 
-public class DreamManagerService 
-        extends IDreamManager.Stub 
+public class DreamManagerService
+        extends IDreamManager.Stub
         implements ServiceConnection
 {
     private static final boolean DEBUG = true;
     private static final String TAG = "DreamManagerService";
-    
+
     final Object mLock = new Object[0];
 
     private Context mContext;
     private IWindowManager mIWindowManager;
-    
+
     private ComponentName mCurrentDreamComponent;
     private IDreamService mCurrentDream;
-    private Binder mCurrentDreamToken; 
+    private Binder mCurrentDreamToken;
 
     public DreamManagerService(Context context) {
         if (DEBUG) Slog.v(TAG, "DreamManagerService startup");
@@ -60,6 +56,7 @@
     }
 
     // IDreamManager method
+    @Override
     public void dream() {
         ComponentName name = getDreamComponent();
         if (name != null) {
@@ -75,26 +72,26 @@
     }
 
     // IDreamManager method
+    @Override
     public void setDreamComponent(ComponentName name) {
         Settings.Secure.putString(mContext.getContentResolver(), SCREENSAVER_COMPONENT, name.flattenToString());
     }
-    
+
     // IDreamManager method
+    @Override
     public ComponentName getDreamComponent() {
-        // TODO(dsandler) don't load this every time, watch the value  
+        // TODO(dsandler) don't load this every time, watch the value
         String component = Settings.Secure.getString(mContext.getContentResolver(), SCREENSAVER_COMPONENT);
-        if (component == null) {
-            component = mContext.getResources().getString(
-                com.android.internal.R.string.config_defaultDreamComponent);
-        }
         if (component != null) {
             return ComponentName.unflattenFromString(component);
         } else {
+            // We rely on DatabaseHelper to set a sane default for us when the settings DB is upgraded
             return null;
         }
     }
-    
+
     // IDreamManager method
+    @Override
     public void testDream(ComponentName name) {
         if (DEBUG) Slog.v(TAG, "startDream name=" + name
                 + " pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
@@ -110,6 +107,7 @@
     }
 
     // IDreamManager method
+    @Override
     public void awaken() {
         if (DEBUG) Slog.v(TAG, "awaken()");
         synchronized (mLock) {
@@ -123,6 +121,7 @@
     }
 
     // IDreamManager method
+    @Override
     public boolean isDreaming() {
         return mCurrentDream != null;
     }
@@ -141,14 +140,14 @@
         mCurrentDreamComponent = componentName;
         mCurrentDreamToken = new Binder();
         try {
-            if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurrentDreamToken 
+            if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurrentDreamToken
                     + " for window type: " + WindowManager.LayoutParams.TYPE_DREAM);
             mIWindowManager.addWindowToken(mCurrentDreamToken,
                     WindowManager.LayoutParams.TYPE_DREAM);
         } catch (RemoteException e) {
             Slog.w(TAG, "Unable to add window token. Proceed at your own risk.");
         }
-        
+
         if (!mContext.bindService(intent, this, Context.BIND_AUTO_CREATE)) {
             Slog.w(TAG, "unable to bind service: " + componentName);
         }
@@ -172,7 +171,7 @@
         if (DEBUG) Slog.v(TAG, "disconnected: " + name + " service: " + mCurrentDream);
         // Only happens in exceptional circumstances
     }
-    
+
     @Override
     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index b752471..a4bf43f 100755
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -853,8 +853,6 @@
 
     <!-- enable screen saver feature -->
     <bool name="config_enableDreams">true</bool>
-    <!-- Name of screensaver components to look for if none has been chosen by the user -->
-    <string name="config_defaultDreamComponent" translatable="false">com.google.android.deskclock/com.android.deskclock.Screensaver</string>
 
     <!-- Base "touch slop" value used by ViewConfiguration as a
          movement threshold where scrolling should begin. -->
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index 4881a94..36fd24f 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -1554,7 +1554,6 @@
   <java-symbol type="style" name="Theme.Toast" />
   <java-symbol type="xml" name="storage_list" />
   <java-symbol type="bool" name="config_enableDreams" />
-  <java-symbol type="string" name="config_defaultDreamComponent" />
   <java-symbol type="string" name="enable_explore_by_touch_warning_title" />
   <java-symbol type="string" name="enable_explore_by_touch_warning_message" />
 
diff --git a/packages/SettingsProvider/res/values/defaults.xml b/packages/SettingsProvider/res/values/defaults.xml
index cd0da5a..a9de51f 100644
--- a/packages/SettingsProvider/res/values/defaults.xml
+++ b/packages/SettingsProvider/res/values/defaults.xml
@@ -151,4 +151,12 @@
     <!-- Number of retries for connecting to DHCP.
          Value here is the same as WifiStateMachine.DEFAULT_MAX_DHCP_RETRIES -->
     <integer name="def_max_dhcp_retries">9</integer>
+
+    <!-- Dreams (screen saver) default settings -->
+    <!-- Whether the feature is enabled when charging (Settings.Secure.SCREENSAVER_ENABLED) -->
+    <bool name="def_screensaver_enabled">true</bool>
+    <!-- Whether the feature activates when docked (SCREENSAVER_ACTIVATE_ON_DOCK) -->
+    <bool name="def_screensaver_activate_on_dock">true</bool>
+    <!-- ComponentName of the default screen saver (Settings.Secure.SCREENSAVER_COMPONENT) -->
+    <string name="def_screensaver_component">com.google.android.deskclock/com.android.deskclock.Screensaver</string>
 </resources>
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
index 71c8c83..7c6ce73 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -65,7 +65,7 @@
     // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion'
     // is properly propagated through your change.  Not doing so will result in a loss of user
     // settings.
-    private static final int DATABASE_VERSION = 79;
+    private static final int DATABASE_VERSION = 80;
 
     private Context mContext;
 
@@ -1073,6 +1073,30 @@
             upgradeVersion = 79;
         }
 
+        // vvv Jelly Bean MR1 changes begin here vvv
+
+        if (upgradeVersion == 79) {
+            // update screensaver settings
+            db.beginTransaction();
+            SQLiteStatement stmt = null;
+            try {
+                stmt = db.compileStatement("INSERT OR REPLACE INTO secure(name,value)"
+                        + " VALUES(?,?);");
+                loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ENABLED,
+                        R.bool.def_screensaver_enabled);
+                loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
+                        R.bool.def_screensaver_activate_on_dock);
+                loadStringSetting(stmt, Settings.Secure.SCREENSAVER_COMPONENT,
+                        R.string.def_screensaver_component);
+                db.setTransactionSuccessful();
+            } finally {
+                db.endTransaction();
+                if (stmt != null) stmt.close();
+            }
+            upgradeVersion = 80;
+        }
+
+
         // *** Remember to update DATABASE_VERSION above!
 
         if (upgradeVersion != currentVersion) {