diff options
author | Jeff Brown <jeffbrown@google.com> | 2014-06-13 19:00:36 -0700 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2014-06-17 18:20:42 -0700 |
commit | 2139276ce8b54aba5faa858ca69ed5f01445c269 (patch) | |
tree | b7016d3863d000b731f2ef9b15b4633157ab7d30 /services/core/java/com/android/server/BatteryService.java | |
parent | 2c43c339de5aaf4fef58aa9b5ac3af48609263a8 (diff) | |
download | frameworks_base-2139276ce8b54aba5faa858ca69ed5f01445c269.zip frameworks_base-2139276ce8b54aba5faa858ca69ed5f01445c269.tar.gz frameworks_base-2139276ce8b54aba5faa858ca69ed5f01445c269.tar.bz2 |
Refactor BatteryService to new pattern.
Apply SystemService pattern to BatteryService.
Change-Id: I4971b2da8d2aed4d14440fb65863a8b916bab03c
Diffstat (limited to 'services/core/java/com/android/server/BatteryService.java')
-rw-r--r-- | services/core/java/com/android/server/BatteryService.java | 163 |
1 files changed, 87 insertions, 76 deletions
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 @@ package com.android.server; 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.Context; 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 @@ import java.io.PrintWriter; * 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 @@ public final class BatteryService extends Binder { 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 @@ public final class BatteryService extends Binder { 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 @@ public final class BatteryService extends Binder { } 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 @@ public final class BatteryService extends Binder { 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 @@ public final class BatteryService extends Binder { 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 @@ public final class BatteryService extends Binder { && (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 @@ public final class BatteryService 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; - } - + 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 @@ public final class BatteryService extends Binder { } } } + + 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; + } + } + } } |