Merge "Make SystemService constructor take a Context." into klp-modular-dev

This commit is contained in:
Jeff Brown
2014-02-11 04:12:02 +00:00
committed by Android (Google) Code Review
19 changed files with 177 additions and 197 deletions

View File

@ -23,24 +23,24 @@ import android.os.ServiceManager;
/**
* The base class for services running in the system process. Override and implement
* the lifecycle event callback methods as needed.
*
* <p>
* The lifecycle of a SystemService:
*
* {@link #onCreate(android.content.Context)} is called to initialize the
* service.
*
* {@link #onStart()} is called to get the service running. It is common
* for services to publish their Binder interface at this point. All required
* dependencies are also assumed to be ready to use.
*
* Then {@link #onBootPhase(int)} is called as many times as there are boot phases
* </p><ul>
* <li>The constructor is called and provided with the system {@link Context}
* to initialize the system service.
* <li>{@link #onStart()} is called to get the service running. The service should
* publish its binder interface at this point using
* {@link #publishBinderService(String, IBinder)}. It may also publish additional
* local interfaces that other services within the system server may use to access
* privileged internal functions.
* <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases
* until {@link #PHASE_BOOT_COMPLETE} is sent, which is the last boot phase. Each phase
* is an opportunity to do special work, like acquiring optional service dependencies,
* waiting to see if SafeMode is enabled, or registering with a service that gets
* started after this one.
*
* NOTE: All lifecycle methods are called from the same thread that created the
* SystemService.
* </ul><p>
* NOTE: All lifecycle methods are called from the system server's main looper thread.
* </p>
*
* {@hide}
*/
@ -54,17 +54,34 @@ public abstract class SystemService {
public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
public static final int PHASE_BOOT_COMPLETE = 1000;
private SystemServiceManager mManager;
private Context mContext;
private final Context mContext;
final void init(Context context, SystemServiceManager manager) {
/**
* Initializes the system service.
* <p>
* Subclasses must define a single argument constructor that accepts the context
* and passes it to super.
* </p>
*
* @param context The system server context.
*/
public SystemService(Context context) {
mContext = context;
mManager = manager;
onCreate(context);
}
/**
* Gets the system context.
*/
public final Context getContext() {
return mContext;
}
/**
* Returns true if the system is running in safe mode.
* TODO: we should define in which phase this becomes valid
*/
public final boolean isSafeMode() {
return mManager.isSafeMode();
return getManager().isSafeMode();
}
/**
@ -126,8 +143,8 @@ public abstract class SystemService {
return LocalServices.getService(type);
}
public final Context getContext() {
return mContext;
private SystemServiceManager getManager() {
return LocalServices.getService(SystemServiceManager.class);
}
// /**

View File

@ -17,14 +17,15 @@
package com.android.server;
import android.content.Context;
import android.util.Log;
import android.util.Slog;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
/**
* Manages creating, starting, and other lifecycle events of
* {@link com.android.server.SystemService}s.
* {@link com.android.server.SystemService system services}.
*
* {@hide}
*/
@ -68,24 +69,43 @@ public class SystemServiceManager {
*/
@SuppressWarnings("unchecked")
public <T extends SystemService> T startService(Class<T> serviceClass) {
final T serviceInstance = (T)createInstance(serviceClass);
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
Slog.i(TAG, "Creating " + serviceClass.getSimpleName());
serviceInstance.init(mContext, this);
} catch (Throwable e) {
throw new RuntimeException("Failed to create service " + serviceClass.getName(), e);
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
mServices.add(serviceInstance);
// Register it.
mServices.add(service);
// Start it.
try {
Slog.i(TAG, "Starting " + serviceClass.getSimpleName());
serviceInstance.onStart();
} catch (Throwable e) {
throw new RuntimeException("Failed to start service " + serviceClass.getName(), e);
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + name
+ ": onStart threw an exception", ex);
}
return serviceInstance;
return service;
}
/**
@ -107,9 +127,11 @@ public class SystemServiceManager {
final SystemService service = mServices.get(i);
try {
service.onBootPhase(mCurrentPhase);
} catch (Throwable e) {
reportWtf("Service " + service.getClass().getName() +
" threw an Exception processing boot phase " + mCurrentPhase, e);
} catch (Exception ex) {
throw new RuntimeException("Failed to boot service "
+ service.getClass().getName()
+ ": onBootPhase threw an exception during phase "
+ mCurrentPhase, ex);
}
}
}
@ -144,34 +166,4 @@ public class SystemServiceManager {
Slog.e(TAG, builder.toString());
}
private SystemService createInstance(Class<?> clazz) {
// Make sure it's a type we expect
if (!SystemService.class.isAssignableFrom(clazz)) {
reportWtf("Class " + clazz.getName() + " does not extend " +
SystemService.class.getName());
}
try {
return (SystemService) clazz.newInstance();
} catch (InstantiationException e) {
reportWtf("Class " + clazz.getName() + " is abstract", e);
} catch (IllegalAccessException e) {
reportWtf("Class " + clazz.getName() +
" must have a public no-arg constructor", e);
}
return null;
}
private static void reportWtf(String message) {
reportWtf(message, null);
}
private static void reportWtf(String message, Throwable e) {
Slog.i(TAG, "******************************");
Log.wtf(TAG, message, e);
// Make sure we die
throw new RuntimeException(message, e);
}
}

View File

@ -53,13 +53,13 @@ public class AppWidgetService extends SystemService {
static final String TAG = "AppWidgetService";
Context mContext;
Handler mSaveStateHandler;
final Context mContext;
final Handler mSaveStateHandler;
SparseArray<AppWidgetServiceImpl> mAppWidgetServices;
final SparseArray<AppWidgetServiceImpl> mAppWidgetServices;
@Override
public void onCreate(Context context) {
public AppWidgetService(Context context) {
super(context);
mContext = context;
mSaveStateHandler = BackgroundThread.getHandler();

View File

@ -274,6 +274,20 @@ public class BackupManagerService extends IBackupManager.Stub {
// Watch the device provisioning operation during setup
ContentObserver mProvisionedObserver;
public static final class Lifecycle extends SystemService {
private final BackupManagerService mService;
public Lifecycle(Context context) {
super(context);
mService = new BackupManagerService(context);
}
@Override
public void onStart() {
publishBinderService(Context.BACKUP_SERVICE, mService);
}
}
class ProvisionedObserver extends ContentObserver {
public ProvisionedObserver(Handler handler) {
super(handler);

View File

@ -1,36 +0,0 @@
/*
* Copyright (C) 2013 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 com.android.server.backup;
import android.content.Context;
import com.android.server.SystemService;
public class BackupManagerSystemService extends SystemService {
private BackupManagerService mBackupManagerImpl;
@Override
public void onCreate(Context context) {
mBackupManagerImpl = new BackupManagerService(context);
}
@Override
public void onStart() {
publishBinderService(Context.BACKUP_SERVICE, mBackupManagerImpl);
}
}

View File

@ -319,6 +319,10 @@ class AlarmManagerService extends SystemService {
static final BatchTimeOrder sBatchOrder = new BatchTimeOrder();
final ArrayList<Batch> mAlarmBatches = new ArrayList<Batch>();
public AlarmManagerService(Context context) {
super(context);
}
static long convertToElapsed(long when, int type) {
final boolean isRtc = (type == RTC || type == RTC_WAKEUP);
if (isRtc) {

View File

@ -88,6 +88,10 @@ final class UiModeManagerService extends SystemService {
private PowerManager.WakeLock mWakeLock;
public UiModeManagerService(Context context) {
super(context);
}
private static Intent buildHomeIntent(String category) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(category);

View File

@ -1866,11 +1866,11 @@ public final class ActivityManagerService extends ActivityManagerNative
}
}
public static class Lifecycle extends SystemService {
private ActivityManagerService mService;
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
@Override
public void onCreate(Context context) {
public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context);
}

View File

@ -124,7 +124,7 @@ public final class DisplayManagerService extends SystemService {
private static final int DISPLAY_BLANK_STATE_BLANKED = 1;
private static final int DISPLAY_BLANK_STATE_UNBLANKED = 2;
private Context mContext;
private final Context mContext;
private final DisplayManagerHandler mHandler;
private final Handler mUiHandler;
private final DisplayAdapterListener mDisplayAdapterListener;
@ -208,18 +208,15 @@ public final class DisplayManagerService extends SystemService {
private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
public DisplayManagerService() {
public DisplayManagerService(Context context) {
super(context);
mContext = context;
mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
mUiHandler = UiThread.getHandler();
mDisplayAdapterListener = new DisplayAdapterListener();
mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
}
@Override
public void onCreate(Context context) {
mContext = context;
}
@Override
public void onStart() {
mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);

View File

@ -156,8 +156,9 @@ public class LightsService extends SystemService {
}
};
@Override
public void onCreate(Context context) {
public LightsService(Context context) {
super(context);
mNativePointer = init_native();
for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) {
@ -182,6 +183,7 @@ public class LightsService extends SystemService {
}
};
@Override
protected void finalize() throws Throwable {
finalize_native(mNativePointer);
super.finalize();

View File

@ -1118,6 +1118,10 @@ public class NotificationManagerService extends SystemService {
return out;
}
public NotificationManagerService(Context context) {
super(context);
}
@Override
public void onStart() {
mAm = ActivityManagerNative.getDefault();

View File

@ -18,6 +18,7 @@ package com.android.server.pm;
import com.android.server.SystemService;
import android.content.Context;
import android.content.pm.PackageStats;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
@ -39,6 +40,10 @@ public final class Installer extends SystemService {
byte buf[] = new byte[1024];
int buflen = 0;
public Installer(Context context) {
super(context);
}
@Override
public void onStart() {
Slog.i(TAG, "Waiting for installd to be ready.");

View File

@ -168,7 +168,7 @@ public final class PowerManagerService extends com.android.server.SystemService
// effectively and terminate the dream.
private static final int DREAM_BATTERY_LEVEL_DRAIN_CUTOFF = 5;
private Context mContext;
private final Context mContext;
private LightsManager mLightsManager;
private BatteryService mBatteryService;
private DisplayManagerInternal mDisplayManagerInternal;
@ -374,7 +374,9 @@ public final class PowerManagerService extends com.android.server.SystemService
private static native void nativeSetInteractive(boolean enable);
private static native void nativeSetAutoSuspend(boolean enable);
public PowerManagerService() {
public PowerManagerService(Context context) {
super(context);
mContext = context;
synchronized (mLock) {
mWakeLockSuspendBlocker = createSuspendBlockerLocked("PowerManagerService.WakeLocks");
mDisplaySuspendBlocker = createSuspendBlockerLocked("PowerManagerService.Display");
@ -390,11 +392,6 @@ public final class PowerManagerService extends com.android.server.SystemService
nativeSetPowerState(true, true);
}
@Override
public void onCreate(Context context) {
mContext = context;
}
@Override
public void onStart() {
publishBinderService(Context.POWER_SERVICE, new BinderService());

View File

@ -89,11 +89,11 @@ public class DeviceStorageMonitorService extends SystemService {
private long mLastReportedFreeMemTime;
boolean mLowMemFlag=false;
private boolean mMemFullFlag=false;
private ContentResolver mResolver;
private long mTotalMemory; // on /data
private StatFs mDataFileStats;
private StatFs mSystemFileStats;
private StatFs mCacheFileStats;
private final ContentResolver mResolver;
private final long mTotalMemory; // on /data
private final StatFs mDataFileStats;
private final StatFs mSystemFileStats;
private final StatFs mCacheFileStats;
private static final File DATA_PATH = Environment.getDataDirectory();
private static final File SYSTEM_PATH = Environment.getRootDirectory();
@ -102,10 +102,10 @@ public class DeviceStorageMonitorService extends SystemService {
private long mThreadStartTime = -1;
boolean mClearSucceeded = false;
boolean mClearingCache;
private Intent mStorageLowIntent;
private Intent mStorageOkIntent;
private Intent mStorageFullIntent;
private Intent mStorageNotFullIntent;
private final Intent mStorageLowIntent;
private final Intent mStorageOkIntent;
private final Intent mStorageFullIntent;
private final Intent mStorageNotFullIntent;
private CachePackageDataObserver mClearCacheObserver;
private CacheFileDeletedObserver mCacheFileDeletedObserver;
private static final int _TRUE = 1;
@ -134,7 +134,7 @@ public class DeviceStorageMonitorService extends SystemService {
* Handler that checks the amount of disk space on the device and sends a
* notification if the device runs low on disk space
*/
private Handler mHandler = new Handler() {
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//don't handle an invalid message
@ -310,12 +310,8 @@ public class DeviceStorageMonitorService extends SystemService {
delay);
}
/**
* Constructor to run service. initializes the disk space threshold value
* and posts an empty message to kickstart the process.
*/
@Override
public void onCreate(Context context) {
public DeviceStorageMonitorService(Context context) {
super(context);
mLastReportedFreeMemTime = 0;
mResolver = context.getContentResolver();
//create StatFs object
@ -335,6 +331,10 @@ public class DeviceStorageMonitorService extends SystemService {
mStorageNotFullIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
}
/**
* Initializes the disk space threshold value and posts an empty message to
* kickstart the process.
*/
@Override
public void onStart() {
// cache storage thresholds

View File

@ -65,6 +65,10 @@ public final class TwilightService extends SystemService {
TwilightState mTwilightState;
public TwilightService(Context context) {
super(context);
}
@Override
public void onStart() {
mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);

View File

@ -25,6 +25,7 @@ import com.android.internal.util.JournaledFile;
import com.android.internal.util.XmlUtils;
import com.android.internal.widget.LockPatternUtils;
import com.android.org.conscrypt.TrustedCertificateStore;
import com.android.server.SystemService;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -149,6 +150,26 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
*/
private boolean mHasFeature;
public static final class Lifecycle extends SystemService {
private DevicePolicyManagerService mService;
public Lifecycle(Context context) {
super(context);
mService = new DevicePolicyManagerService(context);
}
@Override
public void onStart() {
publishBinderService(Context.DEVICE_POLICY_SERVICE, mService);
}
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_LOCK_SETTINGS_READY) {
mService.systemReady();
}
}
}
public static class DevicePolicyData {
int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
int mActivePasswordLength = 0;

View File

@ -1,46 +0,0 @@
/*
* Copyright (C) 2013 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 com.android.server.devicepolicy;
import android.content.Context;
import com.android.server.SystemService;
/**
* SystemService wrapper for the DevicePolicyManager implementation. Publishes
* Context.DEVICE_POLICY_SERVICE.
*/
public final class DevicePolicyManagerSystemService extends SystemService {
private DevicePolicyManagerService mDevicePolicyManagerImpl;
@Override
public void onCreate(Context context) {
mDevicePolicyManagerImpl = new DevicePolicyManagerService(context);
}
@Override
public void onStart() {
publishBinderService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManagerImpl);
}
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_LOCK_SETTINGS_READY) {
mDevicePolicyManagerImpl.systemReady();
}
}
}

View File

@ -106,9 +106,9 @@ public final class SystemServer {
* them from the build system somehow.
*/
private static final String BACKUP_MANAGER_SERVICE_CLASS =
"com.android.server.backup.BackupManagerSystemService";
"com.android.server.backup.BackupManagerService$Lifecycle";
private static final String DEVICE_POLICY_MANAGER_SERVICE_CLASS =
"com.android.server.devicepolicy.DevicePolicyManagerSystemService";
"com.android.server.devicepolicy.DevicePolicyManagerService$Lifecycle";
private static final String APPWIDGET_SERVICE_CLASS =
"com.android.server.appwidget.AppWidgetService";
private static final String PRINT_MANAGER_SERVICE_CLASS =
@ -211,6 +211,7 @@ public final class SystemServer {
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Start services.
try {

View File

@ -66,13 +66,13 @@ import java.util.Set;
*/
public final class PrintManagerService extends SystemService {
private final PrintManagerImpl mPrintManagerImpl;
private PrintManagerImpl mPrintManagerImpl;
@Override
public void onCreate(Context context) {
public PrintManagerService(Context context) {
super(context);
mPrintManagerImpl = new PrintManagerImpl(context);
}
@Override
public void onStart() {
publishBinderService(Context.PRINT_SERVICE, mPrintManagerImpl);