OSDN Git Service

BatteryService: update battery status periodically
[android-x86/frameworks-base.git] / services / java / com / android / server / BatteryService.java
index dc41ff9..2257578 100644 (file)
@@ -34,7 +34,8 @@ import android.os.SystemClock;
 import android.os.UEventObserver;
 import android.provider.Settings;
 import android.util.EventLog;
-import android.util.Log;
+import android.util.Slog;
+import android.os.Handler;
 
 import java.io.File;
 import java.io.FileDescriptor;
@@ -42,6 +43,8 @@ import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Timer;
+import java.util.TimerTask;
 
 
 /**
@@ -119,6 +122,8 @@ class BatteryService extends Binder {
 
     private boolean mSentLowBatteryBroadcast = false;
 
+    private final Handler mHandler = new Handler();
+
     public BatteryService(Context context) {
         mContext = context;
         mBatteryStats = BatteryStatsService.getService();
@@ -132,6 +137,25 @@ class BatteryService extends Binder {
 
         // set initial status
         update();
+
+        // start auto refresh
+        autoRefresh();
+    }
+
+    private Runnable mUpdateResults = new Runnable() {
+        public void run() {
+            update();
+        }
+    };
+
+    private final void autoRefresh() {
+        // Schedule every minute
+        new Timer().scheduleAtFixedRate(new TimerTask() {
+            @Override // Override!!
+            public void run() {
+                mHandler.post(mUpdateResults);
+            }
+        }, 60000, 60000);
     }
 
     final boolean isPowered() {
@@ -177,6 +201,7 @@ class BatteryService extends Binder {
     void systemReady() {
         // check our power situation now that it is safe to display the shutdown dialog.
         shutdownIfNoPower();
+        shutdownIfOverTemp();
     }
 
     private final void shutdownIfNoPower() {
@@ -190,6 +215,17 @@ class BatteryService extends Binder {
         }
     }
 
+    private final void shutdownIfOverTemp() {
+        // shut down gracefully if temperature is too high (> 68.0C)
+        // wait until the system has booted before attempting to display the shutdown dialog.
+        if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
+            Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
+            intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
+            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            mContext.startActivity(intent);
+        }
+    }
+
     private native void native_update();
 
     private synchronized final void update() {
@@ -199,6 +235,7 @@ class BatteryService extends Binder {
         long dischargeDuration = 0;
 
         shutdownIfNoPower();
+        shutdownIfOverTemp();
 
         mBatteryLevelCritical = mBatteryLevel <= CRITICAL_BATTERY_LEVEL;
         if (mAcOnline) {
@@ -349,7 +386,7 @@ class BatteryService extends Binder {
         intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
 
         if (false) {
-            Log.d(TAG, "updateBattery level:" + mBatteryLevel +
+            Slog.d(TAG, "updateBattery level:" + mBatteryLevel +
                     " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
                     " health:" + mBatteryHealth +  " present:" + mBatteryPresent +
                     " voltage: " + mBatteryVoltage +
@@ -381,20 +418,20 @@ class BatteryService extends Binder {
             // add dump file to drop box
             db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
         } catch (RemoteException e) {
-            Log.e(TAG, "failed to dump battery service", e);
+            Slog.e(TAG, "failed to dump battery service", e);
         } catch (IOException e) {
-            Log.e(TAG, "failed to write dumpsys file", e);
+            Slog.e(TAG, "failed to write dumpsys file", e);
         } finally {
             // make sure we clean up
             if (dumpStream != null) {
                 try {
                     dumpStream.close();
                 } catch (IOException e) {
-                    Log.e(TAG, "failed to close dumpsys output stream");
+                    Slog.e(TAG, "failed to close dumpsys output stream");
                 }
             }
             if (dumpFile != null && !dumpFile.delete()) {
-                Log.e(TAG, "failed to delete temporary dumpsys file: "
+                Slog.e(TAG, "failed to delete temporary dumpsys file: "
                         + dumpFile.getAbsolutePath());
             }
         }
@@ -416,12 +453,12 @@ class BatteryService extends Binder {
                     // If the discharge cycle is bad enough we want to know about it.
                     logBatteryStats();
                 }
-                if (LOCAL_LOGV) Log.v(TAG, "duration threshold: " + durationThreshold +
+                if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
                         " discharge threshold: " + dischargeThreshold);
-                if (LOCAL_LOGV) Log.v(TAG, "duration: " + duration + " discharge: " +
+                if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
                         (mDischargeStartLevel - mBatteryLevel));
             } catch (NumberFormatException e) {
-                Log.e(TAG, "Invalid DischargeThresholds GService string: " +
+                Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
                         durationThresholdString + " or " + dischargeThresholdString);
                 return;
             }
@@ -436,7 +473,9 @@ class BatteryService extends Binder {
                 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
             return com.android.internal.R.drawable.stat_sys_battery;
         } else {
-            return com.android.internal.R.drawable.stat_sys_battery_unknown;
+            return mBatteryPresent ?
+                com.android.internal.R.drawable.stat_sys_battery_unknown :
+                com.android.internal.R.drawable.gpm_ac_adapter;
         }
     }