Gello: auto-restart on power mode change

Change-Id: I80ed368b4ab274ed87ef181c191abdd1d11b6130
Signed-off-by: jrizzoli <joey@cyanogenmoditalia.it>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1dc2c08..228c5b2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -371,8 +371,8 @@
     <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">Power save mode enabled.\nPlease restart browser.</string>
-    <string name="powersave_dialog_off">Power save mode disabled.\nPlease restart browser.</string>
+    <string name="powersave_dialog_on">Power save mode enabled. Browser will be restarted.</string>
+    <string name="powersave_dialog_off">Power save mode disabled. Browser will be restarted.</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>
diff --git a/src/com/android/browser/preferences/GeneralPreferencesFragment.java b/src/com/android/browser/preferences/GeneralPreferencesFragment.java
index df4cf19..e9d9611 100644
--- a/src/com/android/browser/preferences/GeneralPreferencesFragment.java
+++ b/src/com/android/browser/preferences/GeneralPreferencesFragment.java
@@ -17,17 +17,21 @@
 package com.android.browser.preferences;
 
 import android.app.ActionBar;
+import android.app.AlarmManager;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.DialogFragment;
 import android.app.Fragment;
 import android.app.FragmentManager;
 import android.app.FragmentTransaction;
+import android.app.PendingIntent;
+import android.content.Context;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnClickListener;
 import android.content.Intent;
 import android.content.res.Resources;
 import android.os.Bundle;
+import android.os.Handler;
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
@@ -177,8 +181,8 @@
                 settings.setPowerSaveModeEnabled((Boolean)objValue);
                 PermissionsServiceFactory.setDefaultPermissions(
                         PermissionsServiceFactory.PermissionType.WEBREFINER, !(Boolean) objValue);
-                showPowerSaveInfo((Boolean) objValue);
                 BrowserPreferencesPage.sResultExtra = PreferenceKeys.ACTION_RELOAD_PAGE;
+                restartGello(getActivity(), (Boolean) objValue);
         }
 
         if (pref.getKey().equals(PreferenceKeys.PREF_NIGHTMODE_ENABLED)) {
@@ -269,16 +273,26 @@
         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);
+    private void restartGello(final Context context, boolean toggle) {
+            String toastInfo;
+            toastInfo = toggle ?
+                context.getResources().getString(R.string.powersave_dialog_on) :
+                context.getResources().getString(R.string.powersave_dialog_off);
+            Toast.makeText(context, toastInfo, Toast.LENGTH_SHORT).show();
 
-        Toast toast = Toast.makeText(getActivity(), toastInfo, Toast.LENGTH_SHORT);
-        toast.setGravity(Gravity.CENTER, 0, 0);
-        toast.show();
+        new Handler().postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                Log.d("Gello", "Power save mode changed, restarting...");
+                Intent restartIntent = context.getPackageManager()
+                        .getLaunchIntentForPackage(context.getPackageName());
+                PendingIntent intent = PendingIntent.getActivity(
+                        context, 0, restartIntent, PendingIntent.FLAG_CANCEL_CURRENT);
+                AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+                manager.set(AlarmManager.RTC, System.currentTimeMillis() +  1, intent);
+                System.exit(2);
+            }
+        }, 1500);
     }
 
     /*