OSDN Git Service

Add restore default settings for Camera/Camcorder app.
authorOwen Lin <owenlin@google.com>
Fri, 15 Jan 2010 21:24:14 +0000 (05:24 +0800)
committerOwen Lin <owenlin@google.com>
Fri, 22 Jan 2010 03:06:55 +0000 (19:06 -0800)
Bug: 2167010
Change-Id: I1b59ddd23ee9a3b9cc34f911c048e269fb872cf8

res/values/strings.xml
src/com/android/camera/Camera.java
src/com/android/camera/OnScreenSettings.java
src/com/android/camera/VideoCamera.java

index 5cd38e7..f7695d6 100644 (file)
     <!-- Confirmation dialog message after deleting a video -->
     <string name="confirm_delete_video_message">The video will be deleted.</string>
 
+    <!-- Confirmation dialog when restoring settings -->
+    <string name="confirm_restore_title">Restore Settings</string>
+    <string name="confirm_restore_message">Camera settings will be restored to default.</string>
+
     <!-- button in review mode indicating that the picture just taken should be deleted -->
     <string name="review_toss">DELETE</string>
 
     <!-- Scene mode optimized for taking images of fireworks. -->
     <string name="pref_camera_scenemode_entry_fireworks">Fireworks</string>
 
+    <!-- Restore settings item in preference -->
+    <string name="pref_restore_title">Restore settings</string>
+    <string name="pref_restore_detail">Restore camera settings to default</string>
+
     <!-- Message to show when there's no lat/lng information in the image -->
     <string name="no_location_image">No Location information contained in this image.</string>
     <!-- Title of Details dialog  -->
index a95c2b2..fb3ede8 100644 (file)
@@ -1073,11 +1073,35 @@ public class Camera extends NoSearchActivity implements View.OnClickListener,
         updateStorageHint(mPicturesRemaining);
     }
 
+    private void restorePreferences() {
+        // Unregister the listener since "upgrade preference" will change
+        // a bunch of preferences. We can handle them with one
+        // setCameraParameters().
+        mPreferences.unregisterOnSharedPreferenceChangeListener(this);
+        mSettings.clearSettings();
+        CameraSettings.upgradePreferences(mPreferences);
+        setCameraParameters();
+
+        // We reload the preference again to reload the new data
+        mSettings.setPreferenceScreen(
+                new CameraSettings(this, mParameters)
+                .getPreferenceScreen(R.xml.camera_preferences));
+        mPreferences.registerOnSharedPreferenceChangeListener(this);
+    }
 
     private void showOnScreenSettings() {
         if (mSettings == null) {
             mSettings = new OnScreenSettings(
                     findViewById(R.id.camera_preview));
+
+            Runnable resetPreferences = new Runnable() {
+                public void run() {
+                    restorePreferences();
+                }
+            };
+
+            mSettings.setRestoreRunner(resetPreferences);
+
             CameraSettings helper =
                     new CameraSettings(this, mInitialParams);
             mSettings.setPreferenceScreen(helper
index c5bbaf3..49f07af 100644 (file)
 package com.android.camera;
 
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.graphics.PixelFormat;
 import android.os.Handler;
 import android.os.Message;
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.PreferenceGroup;
+import android.preference.PreferenceManager;
 import android.preference.PreferenceScreen;
 import android.util.Log;
 import android.view.Display;
@@ -81,6 +83,7 @@ public class OnScreenSettings {
     private MainMenuAdapter mMainAdapter;
 
     private final LayoutInflater mInflater;
+    private Runnable mRestoreRunner;
 
     // We store the override values here. For a given preference,
     // if the mapping value of the preference key is not null, we will
@@ -114,6 +117,18 @@ public class OnScreenSettings {
         mContainer = createContainer();
     }
 
+    public void clearSettings() {
+        SharedPreferences.Editor editor = PreferenceManager
+                .getDefaultSharedPreferences(mContext).edit();
+        editor.clear();
+        editor.commit();
+        mOverride.clear();
+    }
+
+    public void setRestoreRunner(Runnable restorer) {
+        mRestoreRunner = restorer;
+    }
+
     public boolean isVisible() {
         return mIsVisible;
     }
@@ -302,15 +317,40 @@ public class OnScreenSettings {
 
         public void onItemClick(
                 AdapterView<?> parent, View view, int position, long id) {
-            Preference preference = mPreferences.get(position);
-            SubMenuAdapter adapter = new SubMenuAdapter(
-                    mContext, (ListPreference) preference);
-            mSubMenu.setAdapter(adapter);
-            mSubMenu.setOnItemClickListener(adapter);
-            showSubMenu();
+            // If not the last item (restore settings)
+            if (position < mPreferences.size()) {
+                Preference preference = mPreferences.get(position);
+                SubMenuAdapter adapter = new SubMenuAdapter(
+                        mContext, (ListPreference) preference);
+                mSubMenu.setAdapter(adapter);
+                mSubMenu.setOnItemClickListener(adapter);
+                showSubMenu();
+            } else {
+                MenuHelper.confirmAction(mContext,
+                        mContext.getString(R.string.confirm_restore_title),
+                        mContext.getString(R.string.confirm_restore_message),
+                        mRestoreRunner);
+            }
         }
 
         public View getView(int position, View convertView, ViewGroup parent) {
+            // The last item in the view (i.e., restore settings)
+            if (position == mPreferences.size()) {
+                convertView = inflateIfNeed(convertView,
+                        R.layout.on_screen_menu_list_item, parent, false);
+
+                ((TextView) convertView.findViewById(R.id.title)).setText(
+                        mContext.getString(R.string.pref_restore_title));
+                ((TextView) convertView.findViewById(R.id.summary)).setText(
+                        mContext.getString(R.string.pref_restore_detail));
+
+                // Make sure we didn't set focusable. Otherwise, it will
+                // consume all the events. See more detail in the comments
+                // below.
+                convertView.setFocusable(false);
+                return convertView;
+            }
+
             Preference preference = mPreferences.get(position);
 
             if (preference instanceof PreferenceGroup) {
@@ -351,12 +391,16 @@ public class OnScreenSettings {
 
         @Override
         public boolean isEnabled(int position) {
+            // The last item (restore default)
+            if (position == mPreferences.size()) return true;
+
             Preference preference = mPreferences.get(position);
             return !(preference instanceof PreferenceGroup);
         }
 
         public int getCount() {
-            return mPreferences.size();
+            // The last one is "restore default"
+            return mPreferences.size() + 1;
         }
 
         public Object getItem(int position) {
@@ -369,6 +413,7 @@ public class OnScreenSettings {
 
         @Override
         public int getItemViewType(int position) {
+            if (position == mPreferences.size()) return 1;
             Preference pref = mPreferences.get(position);
             if (pref instanceof PreferenceGroup) return 0;
             if (pref instanceof ListPreference) return 1;
index 4e34826..333b2a9 100644 (file)
@@ -1069,10 +1069,41 @@ public class VideoCamera extends NoSearchActivity
         item.setIcon(android.R.drawable.ic_menu_preferences);
     }
 
+    private void restorePreferences() {
+        // Unregister the listener since "upgrade preference" will change
+        // a bunch of preferences. We can handle them with one
+        // setCameraParameters().
+        mPreferences.unregisterOnSharedPreferenceChangeListener(this);
+        mSettings.clearSettings();
+        CameraSettings.upgradePreferences(mPreferences);
+        readVideoPreferences();
+        resetCameraParameters();
+
+        // If we start preview and reload the settings UI in a row. The
+        // preview will become a white screen. So, we reload the settings UI
+        // later.
+        mHandler.post(new Runnable() {
+            public void run() {
+                // We reload the preference again to reload the new data
+                mSettings.setPreferenceScreen(
+                        new CameraSettings(VideoCamera.this, mParameters)
+                        .getPreferenceScreen(R.xml.video_preferences));
+            }
+        });
+        mPreferences.registerOnSharedPreferenceChangeListener(this);
+    }
+
     private void showOnScreenSettings() {
         if (mSettings == null) {
             mSettings = new OnScreenSettings(
                     findViewById(R.id.camera_preview));
+            final Runnable resetPreferences = new Runnable() {
+                public void run() {
+                    restorePreferences();
+                }
+            };
+            mSettings.setRestoreRunner(resetPreferences);
+
             CameraSettings helper = new CameraSettings(this, mParameters);
             PreferenceScreen screen = helper
                     .getPreferenceScreen(R.xml.video_preferences);
@@ -1510,7 +1541,10 @@ public class VideoCamera extends NoSearchActivity
         // If mCameraDevice is not ready then we can set the parameter in
         // startPreview().
         if (mCameraDevice == null) return;
+        resetCameraParameters();
+    }
 
+    private void resetCameraParameters() {
         // We need to restart the preview if preview size is changed.
         Size size = mParameters.getPreviewSize();
         if (size.width != mProfile.mVideoWidth