Add a power saving mode setting

Create a setting that tunes performance optimizations and improves the
power usage.
Automatically tune mode with the Power-saving mode of Android Lollipop+.
Also, when the phone drops below threshold for low battery, prompt user
to turn browser's power save mode on.

Change-Id: I64089a371de562f9a18c297eb09555733f020515
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 353fea9..07f2d12 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -1,6 +1,8 @@
 /*
  * Copyright (C) 2010 The Android Open Source Project
  *
+ * Copyright (C) 2015 The Linux Foundation
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -30,6 +32,7 @@
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnCancelListener;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.res.Configuration;
@@ -255,6 +258,8 @@
     private float mLevel = 0.0f;
     private WebView.HitTestResult mResult;
     private static Bitmap mBookmarkBitmap;
+    private PowerConnectionReceiver mLowPowerReceiver;
+    private PowerConnectionReceiver mPowerChangeReceiver;
 
     public Controller(Activity browser) {
         mActivity = browser;
@@ -398,6 +403,19 @@
                 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
             bookmarksOrHistoryPicker(ComboViews.Bookmarks);
         }
+
+        mLowPowerReceiver = new PowerConnectionReceiver();
+        mPowerChangeReceiver = new PowerConnectionReceiver();
+
+        //always track the android framework's power save mode
+        IntentFilter filter = new IntentFilter();
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            // Power save mode only exists in Lollipop and above
+            filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
+        }
+        filter.addAction(Intent.ACTION_BATTERY_OKAY);
+        mActivity.registerReceiver(mPowerChangeReceiver, filter);
+
     }
 
     private static class PruneThumbnails implements Runnable {
@@ -748,6 +766,8 @@
 
         WebView.disablePlatformNotifications();
         NfcHandler.unregister(mActivity);
+
+        mActivity.unregisterReceiver(mLowPowerReceiver);
     }
 
     @Override
@@ -797,6 +817,8 @@
         if (current != null && current.hasCrashed) {
             current.replaceCrashView(current.getWebView(), current.getViewContainer());
         }
+
+        mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
     }
 
     private void releaseWakeLock() {
@@ -852,6 +874,8 @@
         mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
         // Destroy all the tabs
         mTabControl.destroy();
+        // Unregister receiver
+        mActivity.unregisterReceiver(mPowerChangeReceiver);
     }
 
     protected boolean isActivityPaused() {