Methods Summary |
---|
private static java.lang.String | bundleToString(android.os.Bundle bundle)
if (bundle == null) return null;
StringBuilder sb = new StringBuilder('{");
for (String key : bundle.keySet()) {
if (sb.length() > 1) sb.append(',");
Object v = bundle.get(key);
v = (v instanceof String[]) ? Arrays.asList((String[]) v) : v;
sb.append(key).append('=").append(v);
}
return sb.append('}").toString();
|
private void | checkBound()
if (mDebug) Log.d(mTag, "checkBound mBound=" + mBound);
if (!mBound) {
startService();
}
|
private void | continueStartService()
if (mDebug) Log.d(mTag, "continueStartService");
Intent intent = new Intent().setComponent(mServiceName);
try {
mServiceConnection = new SC();
mBound = mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
if (mDebug) Log.d(mTag, "mBound: " + mBound);
} catch (Throwable t) {
Log.w(mTag, "Error binding to service: " + mServiceName, t);
}
if (!mBound) {
mCallbacks.onNoService();
}
|
private android.content.ComponentName | getComponentNameFromSetting()
String cn = Settings.Secure.getStringForUser(mContext.getContentResolver(),
mSettingKey, UserHandle.USER_CURRENT);
return cn == null ? null : ComponentName.unflattenFromString(cn);
|
private void | packageIntent(android.content.Intent intent)
if (mDebug) Log.d(mTag, "packageIntent intent=" + intent
+ " extras=" + bundleToString(intent.getExtras()));
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
mHandler.sendEmptyMessage(MSG_START_SERVICE);
} else if (Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())) {
PackageManager pm = mContext.getPackageManager();
boolean serviceEnabled =
pm.getApplicationEnabledSetting(mServiceName.getPackageName())
!= PackageManager.COMPONENT_ENABLED_STATE_DISABLED
&& pm.getComponentEnabledSetting(mServiceName)
!= PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
if (mBound && !serviceEnabled) {
stopService();
scheduleCheckBound();
} else if (!mBound && serviceEnabled) {
startService();
}
}
|
private void | scheduleCheckBound()
mHandler.removeMessages(MSG_CHECK_BOUND);
mHandler.sendEmptyMessageDelayed(MSG_CHECK_BOUND, RECHECK_DELAY);
|
private void | serviceDisconnected(android.content.ComponentName serviceName)
if (mDebug) Log.d(mTag, "serviceDisconnected serviceName=" + serviceName
+ " mServiceName=" + mServiceName);
if (serviceName.equals(mServiceName)) {
mBound = false;
scheduleCheckBound();
}
|
public void | start()
// listen for setting changes
ContentResolver cr = mContext.getContentResolver();
cr.registerContentObserver(Settings.Secure.getUriFor(mSettingKey),
false /*notifyForDescendents*/, mSettingObserver, UserHandle.USER_ALL);
// listen for package/component changes
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
mContext.registerReceiver(mBroadcastReceiver, filter);
mHandler.sendEmptyMessage(MSG_START_SERVICE);
|
private void | startService()
mServiceName = getComponentNameFromSetting();
if (mDebug) Log.d(mTag, "startService mServiceName=" + mServiceName);
if (mServiceName == null) {
mBound = false;
mCallbacks.onNoService();
} else {
long delay = mCallbacks.onServiceStartAttempt();
mHandler.sendEmptyMessageDelayed(MSG_CONTINUE_START_SERVICE, delay);
}
|
private void | stopService()
if (mDebug) Log.d(mTag, "stopService");
boolean stopped = mContext.stopService(new Intent().setComponent(mServiceName));
if (mDebug) Log.d(mTag, " stopped=" + stopped);
mContext.unbindService(mServiceConnection);
mBound = false;
|