Refactor BatteryService to new pattern.

Apply SystemService pattern to BatteryService.

Change-Id: I4971b2da8d2aed4d14440fb65863a8b916bab03c
diff --git a/services/core/java/com/android/server/BatteryService.java b/services/core/java/com/android/server/BatteryService.java
index aeb195f..912a181 100644
--- a/services/core/java/com/android/server/BatteryService.java
+++ b/services/core/java/com/android/server/BatteryService.java
@@ -18,6 +18,7 @@
 
 import android.database.ContentObserver;
 import android.os.BatteryStats;
+
 import com.android.internal.app.IBatteryStats;
 import com.android.server.am.BatteryStatsService;
 import com.android.server.lights.Light;
@@ -29,6 +30,7 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.os.BatteryManager;
+import android.os.BatteryManagerInternal;
 import android.os.BatteryProperties;
 import android.os.Binder;
 import android.os.FileUtils;
@@ -83,7 +85,7 @@
  * service asynchronously itself.
  * </p>
  */
-public final class BatteryService extends Binder {
+public final class BatteryService extends SystemService {
     private static final String TAG = BatteryService.class.getSimpleName();
 
     private static final boolean DEBUG = false;
@@ -140,10 +142,12 @@
 
     private boolean mSentLowBatteryBroadcast = false;
 
-    public BatteryService(Context context, LightsManager lightsManager) {
+    public BatteryService(Context context) {
+        super(context);
+
         mContext = context;
         mHandler = new Handler(true /*async*/);
-        mLed = new Led(context, lightsManager);
+        mLed = new Led(context, getLocalService(LightsManager.class));
         mBatteryStats = BatteryStatsService.getService();
 
         mCriticalBatteryLevel = mContext.getResources().getInteger(
@@ -160,7 +164,10 @@
             mInvalidChargerObserver.startObserving(
                     "DEVPATH=/devices/virtual/switch/invalid_charger");
         }
+    }
 
+    @Override
+    public void onStart() {
         IBinder b = ServiceManager.getService("batteryproperties");
         final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
                 IBatteryPropertiesRegistrar.Stub.asInterface(b);
@@ -169,28 +176,34 @@
         } catch (RemoteException e) {
             // Should never happen.
         }
+
+        publishBinderService("battery", new BinderService());
+        publishLocalService(BatteryManagerInternal.class, new LocalService());
     }
 
-    void systemReady() {
-        // check our power situation now that it is safe to display the shutdown dialog.
-        synchronized (mLock) {
-            ContentObserver obs = new ContentObserver(mHandler) {
-                @Override
-                public void onChange(boolean selfChange) {
-                    synchronized (mLock) {
-                        updateBatteryWarningLevelLocked();
+    @Override
+    public void onBootPhase(int phase) {
+        if (phase == PHASE_ACTIVITY_MANAGER_READY) {
+            // check our power situation now that it is safe to display the shutdown dialog.
+            synchronized (mLock) {
+                ContentObserver obs = new ContentObserver(mHandler) {
+                    @Override
+                    public void onChange(boolean selfChange) {
+                        synchronized (mLock) {
+                            updateBatteryWarningLevelLocked();
+                        }
                     }
-                }
-            };
-            final ContentResolver resolver = mContext.getContentResolver();
-            resolver.registerContentObserver(Settings.Global.getUriFor(
-                    Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
-                    false, obs, UserHandle.USER_ALL);
-            updateBatteryWarningLevelLocked();
+                };
+                final ContentResolver resolver = mContext.getContentResolver();
+                resolver.registerContentObserver(Settings.Global.getUriFor(
+                        Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
+                        false, obs, UserHandle.USER_ALL);
+                updateBatteryWarningLevelLocked();
+            }
         }
     }
 
-    void updateBatteryWarningLevelLocked() {
+    private void updateBatteryWarningLevelLocked() {
         final ContentResolver resolver = mContext.getContentResolver();
         int defWarnLevel = mContext.getResources().getInteger(
                 com.android.internal.R.integer.config_lowBatteryWarningLevel);
@@ -207,15 +220,6 @@
         processValuesLocked(true);
     }
 
-    /**
-     * Returns true if the device is plugged into any of the specified plug types.
-     */
-    public boolean isPowered(int plugTypeSet) {
-        synchronized (mLock) {
-            return isPoweredLocked(plugTypeSet);
-        }
-    }
-
     private boolean isPoweredLocked(int plugTypeSet) {
         // assume we are powered if battery state is unknown so
         // the "stay on while plugged in" option will work.
@@ -234,34 +238,7 @@
         return false;
     }
 
-    /**
-     * Returns the current plug type.
-     */
-    public int getPlugType() {
-        synchronized (mLock) {
-            return mPlugType;
-        }
-    }
-
-    /**
-     * Returns battery level as a percentage.
-     */
-    public int getBatteryLevel() {
-        synchronized (mLock) {
-            return mBatteryProps.batteryLevel;
-        }
-    }
-
-    /**
-     * Returns whether we currently consider the battery level to be low.
-     */
-    public boolean getBatteryLevelLow() {
-        synchronized (mLock) {
-            return mBatteryLevelLow;
-        }
-    }
-
-    public boolean shouldSendBatteryLowLocked() {
+    private boolean shouldSendBatteryLowLocked() {
         final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
         final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
 
@@ -277,15 +254,6 @@
                 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
     }
 
-    /**
-     * Returns a non-zero value if an  unsupported charger is attached.
-     */
-    public int getInvalidCharger() {
-        synchronized (mLock) {
-            return mInvalidCharger;
-        }
-    }
-
     private void shutdownIfNoPowerLocked() {
         // shut down gracefully if our battery is critically low and we are not powered.
         // wait until the system has booted before attempting to display the shutdown dialog.
@@ -640,17 +608,7 @@
         }
     }
 
-    @Override
-    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
-        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
-                != PackageManager.PERMISSION_GRANTED) {
-
-            pw.println("Permission Denial: can't dump Battery service from from pid="
-                    + Binder.getCallingPid()
-                    + ", uid=" + Binder.getCallingUid());
-            return;
-        }
-
+    private void dumpInternal(PrintWriter pw, String[] args) {
         synchronized (mLock) {
             if (args == null || args.length == 0 || "-a".equals(args[0])) {
                 pw.println("Current Battery Service state:");
@@ -801,4 +759,57 @@
             }
        }
     }
+
+    private final class BinderService extends Binder {
+        @Override
+        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+            if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
+                    != PackageManager.PERMISSION_GRANTED) {
+
+                pw.println("Permission Denial: can't dump Battery service from from pid="
+                        + Binder.getCallingPid()
+                        + ", uid=" + Binder.getCallingUid());
+                return;
+            }
+
+            dumpInternal(pw, args);
+        }
+    }
+
+    private final class LocalService extends BatteryManagerInternal {
+        @Override
+        public boolean isPowered(int plugTypeSet) {
+            synchronized (mLock) {
+                return isPoweredLocked(plugTypeSet);
+            }
+        }
+
+        @Override
+        public int getPlugType() {
+            synchronized (mLock) {
+                return mPlugType;
+            }
+        }
+
+        @Override
+        public int getBatteryLevel() {
+            synchronized (mLock) {
+                return mBatteryProps.batteryLevel;
+            }
+        }
+
+        @Override
+        public boolean getBatteryLevelLow() {
+            synchronized (mLock) {
+                return mBatteryLevelLow;
+            }
+        }
+
+        @Override
+        public int getInvalidCharger() {
+            synchronized (mLock) {
+                return mInvalidCharger;
+            }
+        }
+    }
 }