下面列出了android.content.ComponentName#getClassName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ComponentName startServiceCommon(Intent service, UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess(this);
ComponentName cn = ActivityManagerNative.getDefault().startService(
mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
getContentResolver()), getOpPackageName(), user.getIdentifier());
if (cn != null) {
if (cn.getPackageName().equals("!")) {
throw new SecurityException(
"Not allowed to start service " + service
+ " without permission " + cn.getClassName());
} else if (cn.getPackageName().equals("!!")) {
throw new SecurityException(
"Unable to start service " + service
+ ": " + cn.getClassName());
}
}
return cn;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
public boolean appHasCustomLabel(ComponentName appname) {
String actvname = appname.getClassName();
String pkgname = appname.getPackageName();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(APP_TABLE, new String [] {CUSTOMLABEL}, ACTVNAME + "=? and " + PKGNAME + "=?", new String[]{actvname, pkgname}, null, null, null);
try {
if (cursor.moveToNext()) { //ACTVNAME, PKGNAME, LABEL, CATID
String customlabel = cursor.getString(cursor.getColumnIndex(CUSTOMLABEL));
if (customlabel!=null && !customlabel.isEmpty()) {
return true;
}
}
} finally {
cursor.close();
}
return false;
}
private void handleServiceStart(XC_MethodHook.MethodHookParam param, Intent serviceIntent) {
if (serviceIntent != null) {
ComponentName serviceName = serviceIntent.getComponent();
if (serviceName != null) {
String packageName = serviceName.getPackageName();
String splitServicesName = serviceName.getClassName();
if (HookLoader.servicesList.contains(splitServicesName) && !PreferencesHelper.isWhitelisted(packageName) && !PreferencesHelper.whiteListElements().contains(splitServicesName)) {
if (!PreferencesHelper.isDisableSystemApps()) {
param.setResult(null);
} else {
try {
ApplicationInfo info = ContextUtils.getSystemContext().getPackageManager().getApplicationInfo(packageName, 0);
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
param.setResult(null);
}
} catch (PackageManager.NameNotFoundException e) {
return;
}
}
LogUtils.logRecord("Service Block Success: " + serviceName.flattenToShortString());
}
}
}
}
private ComponentName startServiceCommon(Intent service, UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess();
ComponentName cn = ActivityManagerNative.getDefault().startService(
mMainThread.getApplicationThread(), service,
service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
if (cn != null) {
if (cn.getPackageName().equals("!")) {
throw new SecurityException(
"Not allowed to start service " + service
+ " without permission " + cn.getClassName());
} else if (cn.getPackageName().equals("!!")) {
throw new SecurityException(
"Unable to start service " + service
+ ": " + cn.getClassName());
}
}
return cn;
} catch (RemoteException e) {
return null;
}
}
public static void unBindLaunchModeStubActivity(String activityName, Intent intent) {
if (activityName.startsWith(PluginStubBinding.STUB_ACTIVITY_PRE)) {
if (intent != null) {
ComponentName cn = intent.getComponent();
if (cn != null) {
String pluginActivityName = cn.getClassName();
if (pluginActivityName.equals(singleTaskMapping.get(activityName))) {
singleTaskMapping.put(activityName, null);
} else if (pluginActivityName.equals(singleInstanceMapping.get(activityName))) {
singleInstanceMapping.put(activityName, null);
} else {
//对于standard和singleTop的launchmode,不做处理。
}
}
}
}
}
@Override
public int schedule(JobInfo job) throws RemoteException {
int vuid = VBinder.getCallingUid();
int id = job.getId();
ComponentName service = job.getService();
JobId jobId = new JobId(vuid, service.getPackageName(), id);
JobConfig config = mJobStore.get(jobId);
if (config == null) {
config = new JobConfig(mGlobalJobId++, service.getClassName(), job.getExtras());
mJobStore.put(jobId, config);
} else {
config.serviceName = service.getClassName();
config.extras = job.getExtras();
}
saveJobs();
mirror.android.app.job.JobInfo.jobId.set(job, config.virtualJobId);
mirror.android.app.job.JobInfo.service.set(job, mJobProxyComponent);
return mScheduler.schedule(job);
}
public AppLauncher getApp(ComponentName appname) {
String actvname = appname.getClassName();
String pkgname = appname.getPackageName();
AppLauncher appLauncher = null;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(APP_TABLE, appcolumns, ACTVNAME + "=? and " + PKGNAME + "=?", new String[]{actvname, pkgname}, null, null, null);
try {
if (cursor.moveToNext()) { //ACTVNAME, PKGNAME, LABEL, CATID
//pkgname = cursor.getString(1);
String label = cursor.getString(cursor.getColumnIndex(LABEL));
String catID = cursor.getString(cursor.getColumnIndex(CATID));
boolean widget = cursor.getShort(cursor.getColumnIndex(ISWIDGET)) == 1;
String customlabel = cursor.getString(cursor.getColumnIndex(CUSTOMLABEL));
// Log.d("LaunchDB", "getApp " + pkgname + " " + catID);
appLauncher = AppLauncher.createAppLauncher(actvname, pkgname, customlabel==null?label:customlabel, catID, widget);
}
} finally {
cursor.close();
}
return appLauncher;
}
@Override
public ComponentName startServiceAsUser(Intent service, UserHandle user) {
try {
service.prepareToLeaveProcess();
ComponentName cn = ActivityManagerNative.getDefault().startService(
mMainThread.getApplicationThread(), service,
service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
if (cn != null) {
if (cn.getPackageName().equals("!")) {
throw new SecurityException(
"Not allowed to start service " + service
+ " without permission " + cn.getClassName());
} else if (cn.getPackageName().equals("!!")) {
throw new SecurityException(
"Unable to start service " + service
+ ": " + cn.getClassName());
}
}
return cn;
} catch (RemoteException e) {
return null;
}
}
private ComponentName startServiceCommon(Intent service, UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess(this);
ComponentName cn = ActivityManagerNative.getDefault().startService(
mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
getContentResolver()), getOpPackageName(), user.getIdentifier());
if (cn != null) {
if (cn.getPackageName().equals("!")) {
throw new SecurityException(
"Not allowed to start service " + service
+ " without permission " + cn.getClassName());
} else if (cn.getPackageName().equals("!!")) {
throw new SecurityException(
"Unable to start service " + service
+ ": " + cn.getClassName());
}
}
return cn;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Check if specified service is running or not
* @param context
* @param cls name of service
* @return boolean is running or not
*/
private static boolean isRunningService(Context context, Class<?> cls) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> info = activityManager.getRunningServices(Integer.MAX_VALUE);
if (info != null) {
for(ActivityManager.RunningServiceInfo serviceInfo : info) {
ComponentName compName = serviceInfo.service;
String className = compName.getClassName();
if(className.equals(cls.getName())) {
isRunning = true;
break;
}
}
}
return isRunning;
}
public void deleteApp(ComponentName appname) {
String actvname = appname.getClassName();
String pkgname = appname.getPackageName();
try {
AppLauncher app = getApp(appname);
if (app != null && (app.isLink() || app.isWidget())) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(APP_TABLE, ACTVNAME + "=? and " + PKGNAME + "=?", new String[]{actvname, pkgname});
AppLauncher.removeAppLauncher(actvname,pkgname);
return;
}
} catch (Exception e) {
Log.e("LaunchDB", "Can't delete app " + actvname, e);
}
deleteApp(actvname, pkgname);
}
/**
* Returns Class name of top activity
* @param context Context to provide activity information
* @return String representing class name of top activity
*/
public static String getTopActivityClassName(Context context) {
ComponentName activity = getTopActivity(context);
if (activity == null) {
return null;
}
return activity.getClassName();
}
/**
* 如果 replugin-host-lib 版本小于 2.1.3,使用此 compat 方法。
*/
private static boolean startActivityForResultCompat(Activity activity, Intent intent, int requestCode, Bundle options) {
String plugin = getPluginName(activity, intent);
if (LogDebug.LOG) {
LogDebug.d(TAG, "start activity with startActivityForResult: intent=" + intent);
}
if (TextUtils.isEmpty(plugin)) {
return false;
}
ComponentName cn = intent.getComponent();
if (cn == null) {
return false;
}
String name = cn.getClassName();
ComponentName cnNew = loadPluginActivity(intent, plugin, name, IPluginManager.PROCESS_AUTO);
if (cnNew == null) {
return false;
}
intent.setComponent(cnNew);
if (Build.VERSION.SDK_INT >= 16) {
activity.startActivityForResult(intent, requestCode, options);
} else {
activity.startActivityForResult(intent, requestCode);
}
return true;
}
/**
* 开启一个插件的Activity <p>
* 其中Intent的ComponentName的Key应为插件名(而不是包名),可使用createIntent方法来创建Intent对象
*
* @param context Context对象
* @param intent 要打开Activity的Intent,其中ComponentName的Key必须为插件名
* @return 插件Activity是否被成功打开?
* FIXME 是否需要Exception来做?
* @see #createIntent(String, String)
* @since 1.0.0
*/
public static boolean startActivity(Context context, Intent intent) {
// TODO 先用旧的开启Activity方案,以后再优化
ComponentName cn = intent.getComponent();
if (cn == null) {
// TODO 需要支持Action方案
return false;
}
String plugin = cn.getPackageName();
String cls = cn.getClassName();
return PluginFactory.startActivityWithNoInjectCN(context, intent, plugin, cls, IPluginManager.PROCESS_AUTO);
}
/**
* 查找能够响应这个Intent的Activity
*/
public ActivityInfo resolveActivity(Intent intent) {
if (intent == null) {
return null;
}
if (mActivityIntentInfos != null) {
ComponentName compName = intent.getComponent();
String className = null;
if (compName != null) {
className = compName.getClassName();
}
if (!TextUtils.isEmpty(className)) {
ActivityIntentInfo act = mActivityIntentInfos.get(className);
if (act != null) {
return act.mInfo;
}
} else {
for (ActivityIntentInfo info : mActivityIntentInfos.values()) {
if (info != null && info.mFilter != null) {
for (IntentFilter filter : info.mFilter) {
if (filter.match(intent.getAction(), intent.getType(), intent.getScheme(), intent.getData(),
intent.getCategories(), TAG) > 0) {
return info.mInfo;
}
}
}
}
}
}
return null;
}
private static String topActivity(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
if (list != null && list.size() > 0) {
ComponentName cpn = list.get(0).topActivity;
return cpn.getClassName();
}
return null;
}
@Override
public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
try {
cl.loadClass(className);
Log.i(TAG, String.format("newActivity[%s]", className));
} catch (ClassNotFoundException e) {
ComponentName component = PluginUtil.getComponent(intent);
if (component == null) {
return newActivity(mBase.newActivity(cl, className, intent));
}
String targetClassName = component.getClassName();
Log.i(TAG, String.format("newActivity[%s : %s/%s]", className, component.getPackageName(), targetClassName));
LoadedPlugin plugin = this.mPluginManager.getLoadedPlugin(component);
if (plugin == null) {
// Not found then goto stub activity.
boolean debuggable = false;
try {
Context context = this.mPluginManager.getHostContext();
debuggable = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (Throwable ex) {
}
if (debuggable) {
throw new ActivityNotFoundException("error intent: " + intent.toURI());
}
Log.i(TAG, "Not found. starting the stub activity: " + StubActivity.class);
return newActivity(mBase.newActivity(cl, StubActivity.class.getName(), intent));
}
Activity activity = mBase.newActivity(plugin.getClassLoader(), targetClassName, intent);
activity.setIntent(intent);
// for 4.1+
Reflector.QuietReflector.with(activity).field("mResources").set(plugin.getResources());
return newActivity(activity);
}
return newActivity(mBase.newActivity(cl, className, intent));
}
/**
* Make an ShortcutInfo object for a shortcut that is an application.
*
* If c is not null, then it will be used to fill in missing data like the title and icon.
*/
public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
HashMap<Object, CharSequence> labelCache, boolean allowMissingTarget) {
if (user == null) {
Log.d(TAG, "Null user found in getShortcutInfo");
return null;
}
ComponentName componentName = intent.getComponent();
if (componentName == null) {
Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
return null;
}
Intent newIntent = new Intent(intent.getAction(), null);
newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
newIntent.setComponent(componentName);
LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
if ((lai == null) && !allowMissingTarget) {
Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
return null;
}
final ShortcutInfo info = new ShortcutInfo();
// the resource -- This may implicitly give us back the fallback icon,
// but don't worry about that. All we're doing with usingFallbackIcon is
// to avoid saving lots of copies of that in the database, and most apps
// have icons anyway.
Bitmap icon = mIconCache.getIcon(componentName, lai, labelCache);
// the db
if (icon == null) {
if (c != null) {
icon = getIconFromCursor(c, iconIndex, context);
}
}
// the fallback icon
if (icon == null) {
icon = mIconCache.getDefaultIcon(user);
info.usingFallbackIcon = true;
}
info.setIcon(icon);
// From the cache.
if (labelCache != null) {
info.title = labelCache.get(componentName);
}
// from the resource
if (info.title == null && lai != null) {
info.title = lai.getLabel();
if (labelCache != null) {
labelCache.put(componentName, info.title);
}
}
// from the db
if (info.title == null) {
if (c != null) {
info.title = c.getString(titleIndex);
}
}
// fall back to the class name of the activity
if (info.title == null) {
info.title = componentName.getClassName();
}
info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
info.user = user;
info.contentDescription = mUserManager.getBadgedLabelForUser(
info.title.toString(), info.user);
return info;
}
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
// Should happen before any call to getIntent()
getMetaData();
final Intent intent = getIntent();
if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
}
// Getting Intent properties can only be done after the super.onCreate(...)
final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
final ComponentName cn = intent.getComponent();
final String className = cn.getClassName();
boolean isShowingDashboard = className.equals(Settings2.class.getName());
// This is a "Sub Settings" when:
// - this is a real SubSettings
// - or :settings:show_fragment_as_subsetting is passed to the Intent
final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
// If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
if (isSubSettings) {
// Check also that we are not a Theme Dialog as we don't want to override them
/*
final int themeResId = getTheme(). getThemeResId();
if (themeResId != R.style.Theme_DialogWhenLarge &&
themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
setTheme(R.style.Theme_SubSettings);
}
*/
}
setContentView(R.layout.settings_main_dashboard);
mContent = (ViewGroup) findViewById(android.R.id.content);
getSupportFragmentManager().addOnBackStackChangedListener(this);
if (savedState != null) {
// We are restarting from a previous saved state; used that to initialize, instead
// of starting fresh.
setTitleFromIntent(intent);
ArrayList<DashboardCategory> categories =
savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
if (categories != null) {
mCategories.clear();
mCategories.addAll(categories);
setTitleFromBackStack();
}
mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
} else {
if (!isShowingDashboard) {
mDisplayHomeAsUpEnabled = isSubSettings;
setTitleFromIntent(intent);
Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
switchToFragment(initialFragmentName, initialArguments, true, false,
mInitialTitleResId, mInitialTitle, false);
} else {
mDisplayHomeAsUpEnabled = false;
mInitialTitleResId = R.string.app_name;
switchToFragment(DashboardFragment.class.getName(), null, false, false,
mInitialTitleResId, mInitialTitle, false);
}
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
}
}
/**
* Returns the key for the shared preference used to hold the current state of a given
* complication.
*/
static String getPreferenceKey(ComponentName provider, int complicationId) {
return provider.getClassName() + complicationId;
}