Add notification to power save mode

Add small notification when mode is toggled and also disable web refiner
and edge navigation.

Change-Id: I166897c0e396b53884e05dac8189f049f34851f7
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 17c2208..e135481 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -469,6 +469,10 @@
     <!-- Button text to delete all the AutoFill profile data [CHAR-LIMIT=20] -->
     <string name="autofill_profile_editor_delete_profile">Delete</string>
 
+    <!-- Text on toast shown to the user when power save mode is enabled or disabled -->
+    <string name="powersave_dialog_on">Enabled: Processor Core Control.\nDisabled: High Performance Features.\nPlease restart browser.</string>
+    <string name="powersave_dialog_off">Power save mode disabled.\nPlease restart browser.</string>
+
     <!-- Text on a dialog shown to the user when they are prompted to set up the autofill feature [CHAR-LIMIT=NONE] -->
     <string name="autofill_setup_dialog_message">The browser can automatically complete web forms like this one. Do you want to set up your auto-fill text?</string>
     <!-- Toast message displayed when the user decides to not set up autofill at this time. We want to remind them that they can configure
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index 3682d69..3a05724 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -446,7 +446,7 @@
     }
 
     public void refreshEdgeSwipeController(View container) {
-        if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
+        if (isUiLowPowerMode()) {
             return;
         }
 
@@ -696,6 +696,11 @@
         return false;
     }
 
+    public static boolean isUiLowPowerMode() {
+        return BrowserCommandLine.hasSwitch("ui-low-power-mode") ||
+            BrowserSettings.getInstance().isPowerSaveModeEnabled();
+    }
+
     // -------------------------------------------------------------------------
 
     protected void updateNavigationState(Tab tab) {
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 0a4bc34..a4cc47f 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -643,7 +643,7 @@
         @Override
         public void beforeNavigation(WebView view, String url) {
             mTouchIconUrl = null;
-            if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
+            if (BaseUi.isUiLowPowerMode()) {
                 return;
             }
 
@@ -677,7 +677,7 @@
 
         @Override
         public void onHistoryItemCommit(WebView view, int index) {
-            if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
+            if (BaseUi.isUiLowPowerMode()) {
                 return;
             }
 
@@ -1414,8 +1414,7 @@
             // save the WebView to call destroy() after detach it from the tab
             final WebView webView = mMainView;
             setWebView(null);
-            if (!mWebViewDestroyedByMemoryMonitor &&
-                    !BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
+            if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
                 // Tabs can be reused with new instance of WebView so delete the snapshots
                 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
                     @Override
diff --git a/src/com/android/browser/preferences/AdvancedPreferencesFragment.java b/src/com/android/browser/preferences/AdvancedPreferencesFragment.java
index fa73abc..0a5fa64 100644
--- a/src/com/android/browser/preferences/AdvancedPreferencesFragment.java
+++ b/src/com/android/browser/preferences/AdvancedPreferencesFragment.java
@@ -30,13 +30,13 @@
 import android.preference.PreferenceScreen;
 import android.util.Log;
 
+import com.android.browser.BaseUi;
 import com.android.browser.BrowserActivity;
 import com.android.browser.BrowserSettings;
 import com.android.browser.DownloadHandler;
 import com.android.browser.PreferenceKeys;
 import com.android.browser.R;
 
-import org.codeaurora.swe.BrowserCommandLine;
 import org.codeaurora.swe.PermissionsServiceFactory;
 
 public class AdvancedPreferencesFragment
@@ -77,7 +77,7 @@
                 (ListPreference) mFragment.findPreference("edge_swiping_action");
         edgeSwipePref.setOnPreferenceChangeListener(this);
 
-        if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
+        if (BaseUi.isUiLowPowerMode()) {
             edgeSwipePref.setEnabled(false);
         } else {
             String[] options = mFragment.getResources().getStringArray(
diff --git a/src/com/android/browser/preferences/GeneralPreferencesFragment.java b/src/com/android/browser/preferences/GeneralPreferencesFragment.java
index f3128e2..09355a5 100644
--- a/src/com/android/browser/preferences/GeneralPreferencesFragment.java
+++ b/src/com/android/browser/preferences/GeneralPreferencesFragment.java
@@ -31,15 +31,18 @@
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
+import android.preference.SwitchPreference;
 import android.text.InputType;
 import android.text.TextUtils;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.WindowManager;
 import android.view.inputmethod.EditorInfo;
 import android.widget.EditText;
 import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;
+import android.widget.Toast;
 
 import com.android.browser.AutoFillSettingsFragment;
 import com.android.browser.BrowserSettings;
@@ -50,6 +53,8 @@
 import com.android.browser.mdm.AutoFillRestriction;
 import com.android.browser.mdm.SearchEngineRestriction;
 
+import org.codeaurora.swe.PermissionsServiceFactory;
+
 public class GeneralPreferencesFragment extends SWEPreferenceFragment
         implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
 
@@ -65,6 +70,7 @@
     static final String OTHER = "other";
 
     static final String PREF_HOMEPAGE_PICKER = "homepage_picker";
+    static final String PREF_POWERSAVE = "powersave_enabled";
 
     String[] mChoices, mValues;
     String mCurrentPage;
@@ -93,6 +99,9 @@
                 PreferenceKeys.PREF_AUTOFILL_PROFILE);
         autofill.setOnPreferenceClickListener(this);
 
+        SwitchPreference powersave = (SwitchPreference) findPreference(PREF_POWERSAVE);
+        powersave.setOnPreferenceChangeListener(this);
+
         final Bundle arguments = getArguments();
         if (arguments != null && arguments.getBoolean("LowPower")) {
             LowPowerDialogFragment fragment = LowPowerDialogFragment.newInstance();
@@ -153,6 +162,14 @@
             return false;
         }
 
+        if (pref.getKey().equals(PREF_POWERSAVE)) {
+                BrowserSettings settings = BrowserSettings.getInstance();
+                settings.setPowerSaveModeEnabled((Boolean)objValue);
+                PermissionsServiceFactory.setDefaultPermissions(
+                    PermissionsServiceFactory.PermissionType.WEBREFINER, !(Boolean)objValue);
+                showPowerSaveInfo((Boolean) objValue);
+        }
+
         return true;
     }
 
@@ -244,6 +261,18 @@
         return false;
     }
 
+    void showPowerSaveInfo(boolean toggle) {
+        String toastInfo;
+        if (toggle)
+            toastInfo = getActivity().getResources().getString(R.string.powersave_dialog_on);
+        else
+            toastInfo = getActivity().getResources().getString(R.string.powersave_dialog_off);
+
+        Toast toast = Toast.makeText(getActivity(), toastInfo, Toast.LENGTH_SHORT);
+        toast.setGravity(Gravity.CENTER, 0, 0);
+        toast.show();
+    }
+
     /*
       Add this class to manage AlertDialog lifecycle.
     */