diff options
| author | 2014-06-13 19:00:36 -0700 | |
|---|---|---|
| committer | 2014-06-17 18:20:42 -0700 | |
| commit | 2139276ce8b54aba5faa858ca69ed5f01445c269 (patch) | |
| tree | b7016d3863d000b731f2ef9b15b4633157ab7d30 | |
| parent | 2c43c339de5aaf4fef58aa9b5ac3af48609263a8 (diff) | |
Refactor BatteryService to new pattern.
Apply SystemService pattern to BatteryService.
Change-Id: I4971b2da8d2aed4d14440fb65863a8b916bab03c
5 files changed, 154 insertions, 111 deletions
diff --git a/core/java/android/os/BatteryManagerInternal.java b/core/java/android/os/BatteryManagerInternal.java new file mode 100644 index 000000000000..f3a95b90d5c1 --- /dev/null +++ b/core/java/android/os/BatteryManagerInternal.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.os; + +/** + * Battery manager local system service interface. + * + * @hide Only for use within the system server. + */ +public abstract class BatteryManagerInternal { + /** + * Returns true if the device is plugged into any of the specified plug types. + */ + public abstract boolean isPowered(int plugTypeSet); + + /** + * Returns the current plug type. + */ + public abstract int getPlugType(); + + /** + * Returns battery level as a percentage. + */ + public abstract int getBatteryLevel(); + + /** + * Returns whether we currently consider the battery level to be low. + */ + public abstract boolean getBatteryLevelLow(); + + /** + * Returns a non-zero value if an unsupported charger is attached. + */ + public abstract int getInvalidCharger(); +} diff --git a/services/core/java/com/android/server/BatteryService.java b/services/core/java/com/android/server/BatteryService.java index aeb195fe945e..912a181ef944 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; + } + } + } } diff --git a/services/core/java/com/android/server/job/controllers/BatteryController.java b/services/core/java/com/android/server/job/controllers/BatteryController.java index 4aef2d3142cf..538a2520311b 100644 --- a/services/core/java/com/android/server/job/controllers/BatteryController.java +++ b/services/core/java/com/android/server/job/controllers/BatteryController.java @@ -19,19 +19,16 @@ package com.android.server.job.controllers; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; -import android.os.BatteryProperty; -import android.os.RemoteException; -import android.os.ServiceManager; +import android.os.BatteryManagerInternal; import android.os.SystemClock; import android.util.Slog; import com.android.internal.annotations.VisibleForTesting; -import com.android.server.BatteryService; +import com.android.server.LocalServices; import com.android.server.job.JobSchedulerService; import com.android.server.job.StateChangedListener; @@ -158,14 +155,10 @@ public class BatteryController extends StateController { mContext.registerReceiver(this, filter); // Initialise tracker state. - BatteryService batteryService = (BatteryService) ServiceManager.getService("battery"); - if (batteryService != null) { - mBatteryHealthy = !batteryService.getBatteryLevelLow(); - mCharging = batteryService.isPowered(BatteryManager.BATTERY_PLUGGED_ANY); - } else { - // Unavailable for some reason, we default to false and let ACTION_BATTERY_[OK,LOW] - // sort it out. - } + BatteryManagerInternal batteryManagerInternal = + LocalServices.getService(BatteryManagerInternal.class); + mBatteryHealthy = !batteryManagerInternal.getBatteryLevelLow(); + mCharging = batteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY); } boolean isOnStablePower() { diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java index b25012fe9b17..bd80b54f6c10 100644 --- a/services/core/java/com/android/server/power/PowerManagerService.java +++ b/services/core/java/com/android/server/power/PowerManagerService.java @@ -19,7 +19,6 @@ package com.android.server.power; import com.android.internal.app.IAppOpsService; import com.android.internal.app.IBatteryStats; import com.android.internal.os.BackgroundThread; -import com.android.server.BatteryService; import com.android.server.EventLogTags; import com.android.server.LocalServices; import com.android.server.ServiceThread; @@ -43,6 +42,7 @@ import android.hardware.display.DisplayManagerInternal; import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; import android.net.Uri; import android.os.BatteryManager; +import android.os.BatteryManagerInternal; import android.os.Binder; import android.os.Handler; import android.os.IBinder; @@ -168,7 +168,7 @@ public final class PowerManagerService extends com.android.server.SystemService private final PowerManagerHandler mHandler; private LightsManager mLightsManager; - private BatteryService mBatteryService; + private BatteryManagerInternal mBatteryManagerInternal; private DisplayManagerInternal mDisplayManagerInternal; private IBatteryStats mBatteryStats; private IAppOpsService mAppOps; @@ -463,14 +463,14 @@ public final class PowerManagerService extends com.android.server.SystemService Watchdog.getInstance().addThread(mHandler); } - public void systemReady(BatteryService batteryService, IAppOpsService appOps) { + public void systemReady(IAppOpsService appOps) { synchronized (mLock) { mSystemReady = true; - mBatteryService = batteryService; mAppOps = appOps; mDreamManager = getLocalService(DreamManagerInternal.class); mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class); mPolicy = getLocalService(WindowManagerPolicy.class); + mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class); PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mScreenBrightnessSettingMinimum = pm.getMinimumScreenBrightnessSetting(); @@ -1158,10 +1158,10 @@ public final class PowerManagerService extends com.android.server.SystemService final boolean wasPowered = mIsPowered; final int oldPlugType = mPlugType; final boolean oldLevelLow = mBatteryLevelLow; - mIsPowered = mBatteryService.isPowered(BatteryManager.BATTERY_PLUGGED_ANY); - mPlugType = mBatteryService.getPlugType(); - mBatteryLevel = mBatteryService.getBatteryLevel(); - mBatteryLevelLow = mBatteryService.getBatteryLevelLow(); + mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY); + mPlugType = mBatteryManagerInternal.getPlugType(); + mBatteryLevel = mBatteryManagerInternal.getBatteryLevel(); + mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow(); if (DEBUG_SPEW) { Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered @@ -1244,7 +1244,7 @@ public final class PowerManagerService extends com.android.server.SystemService final boolean wasStayOn = mStayOn; if (mStayOnWhilePluggedInSetting != 0 && !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) { - mStayOn = mBatteryService.isPowered(mStayOnWhilePluggedInSetting); + mStayOn = mBatteryManagerInternal.isPowered(mStayOnWhilePluggedInSetting); } else { mStayOn = false; } diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index 4b67155df550..d294cd26c04b 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -150,7 +150,6 @@ public final class SystemServer { private DisplayManagerService mDisplayManagerService; private PackageManagerService mPackageManagerService; private PackageManager mPackageManager; - private BatteryService mBatteryService; private ContentResolver mContentResolver; private boolean mOnlyCore; @@ -362,11 +361,8 @@ public final class SystemServer { // Manages LEDs and display backlight. mSystemServiceManager.startService(LightsService.class); - // Tracks the battery level. - Slog.i(TAG, "Battery Service"); - mBatteryService = new BatteryService(mSystemContext, - LocalServices.getService(LightsManager.class)); - ServiceManager.addService("battery", mBatteryService); + // Tracks the battery level. Requires LightService. + mSystemServiceManager.startService(BatteryService.class); } /** @@ -998,8 +994,7 @@ public final class SystemServer { try { // TODO: use boot phase - mPowerManagerService.systemReady(mBatteryService, - mActivityManagerService.getAppOpsService()); + mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService()); } catch (Throwable e) { reportWtf("making Power Manager Service ready", e); } @@ -1067,11 +1062,6 @@ public final class SystemServer { reportWtf("making Mount Service ready", e); } try { - mBatteryService.systemReady(); - } catch (Throwable e) { - reportWtf("making Battery Service ready", e); - } - try { if (networkScoreF != null) networkScoreF.systemReady(); } catch (Throwable e) { reportWtf("making Network Score Service ready", e); |