下面列出了android.content.ComponentName#flattenToShortString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the activity or application icon for an activity.
* Uses the local icon cache for fast repeated lookups.
*
* @param component Name of an activity.
* @return A drawable, or {@code null} if neither the activity nor the application
* has an icon set.
*/
private Drawable getActivityIconWithCache(ComponentName component) {
// First check the icon cache
String componentIconKey = component.flattenToShortString();
// Using containsKey() since we also store null values.
if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
}
// Then try the activity or application icon
Drawable drawable = getActivityIcon(component);
// Stick it in the cache so we don't do this lookup again.
Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
mOutsideDrawablesCache.put(componentIconKey, toCache);
return drawable;
}
ServiceRecord(ActivityManagerService ams,
BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name,
Intent.FilterComparison intent, ServiceInfo sInfo, boolean callerIsFg,
Runnable restarter) {
this.ams = ams;
this.stats = servStats;
this.name = name;
shortName = name.flattenToShortString();
this.intent = intent;
serviceInfo = sInfo;
appInfo = sInfo.applicationInfo;
packageName = sInfo.applicationInfo.packageName;
processName = sInfo.processName;
permission = sInfo.permission;
exported = sInfo.exported;
this.restarter = restarter;
createRealTime = SystemClock.elapsedRealtime();
lastActivity = SystemClock.uptimeMillis();
userId = UserHandle.getUserId(appInfo.uid);
createdFromFg = callerIsFg;
}
/**
* Gets the activity or application icon for an activity.
* Uses the local icon cache for fast repeated lookups.
*
* @param component Name of an activity.
* @return A drawable, or {@code null} if neither the activity nor the application
* has an icon set.
*/
private Drawable getActivityIconWithCache(ComponentName component) {
// First check the icon cache
String componentIconKey = component.flattenToShortString();
// Using containsKey() since we also store null values.
if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
}
// Then try the activity or application icon
Drawable drawable = getActivityIcon(component);
// Stick it in the cache so we don't do this lookup again.
Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
mOutsideDrawablesCache.put(componentIconKey, toCache);
return drawable;
}
/**
* Gets the activity or application icon for an activity.
* Uses the local icon cache for fast repeated lookups.
*
* @param component Name of an activity.
* @return A drawable, or {@code null} if neither the activity nor the application
* has an icon set.
*/
private Drawable getActivityIconWithCache(ComponentName component) {
// First check the icon cache
String componentIconKey = component.flattenToShortString();
// Using containsKey() since we also store null values.
if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
}
// Then try the activity or application icon
Drawable drawable = getActivityIcon(component);
// Stick it in the cache so we don't do this lookup again.
Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
mOutsideDrawablesCache.put(componentIconKey, toCache);
return drawable;
}
@Test
public void testFirstRunStartsIntro()
{
prefs.edit().remove("firstrun").commit();
ActivityController controller = Robolectric.buildActivity(MainActivity.class).create();
Activity activity = (Activity)controller.get();
assertTrue(activity.isFinishing() == false);
Intent next = shadowOf(activity).getNextStartedActivity();
ComponentName componentName = next.getComponent();
String name = componentName.flattenToShortString();
assertEquals("protect.budgetwatch/.intro.IntroActivity", name);
Bundle extras = next.getExtras();
assertNull(extras);
assertEquals(false, prefs.getBoolean("firstrun", true));
}
/**
* Do a {@link android.content.Context#startService(android.content.Intent)
* Context.startService}, but holding a wake lock while the service starts.
* This will modify the Intent to hold an extra identifying the wake lock;
* when the service receives it in {@link android.app.Service#onStartCommand
* Service.onStartCommand}, it should pass back the Intent it receives there to
* {@link #completeWakefulIntent(android.content.Intent)} in order to release
* the wake lock.
*
* @param context The Context in which it operate.
* @param intent The Intent with which to start the service, as per
* {@link android.content.Context#startService(android.content.Intent)
* Context.startService}.
*/
public static ComponentName startWakefulService(Context context, Intent intent) {
synchronized (ACTIVE_WAKE_LOCKS) {
int id = nextId;
nextId++;
if (nextId <= 0) {
nextId = 1;
}
intent.putExtra(EXTRA_WAKE_LOCK_ID, id);
ComponentName comp = context.startService(intent);
if (comp == null) {
return null;
}
String tag = "wake:" + comp.flattenToShortString();
PowerManager.WakeLock wakeLock = acquireWakeLock(context, tag, TimeUnit.MINUTES.toMillis(3));
if (wakeLock != null) {
ACTIVE_WAKE_LOCKS.put(id, wakeLock);
}
return comp;
}
}
/**
* Gets the activity or application icon for an activity.
* Uses the local icon cache for fast repeated lookups.
*
* @param component Name of an activity.
* @return A drawable, or {@code null} if neither the activity nor the application
* has an icon set.
*/
private Drawable getActivityIconWithCache(ComponentName component) {
// First check the icon cache
String componentIconKey = component.flattenToShortString();
// Using containsKey() since we also store null values.
if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
}
// Then try the activity or application icon
Drawable drawable = getActivityIcon(component);
// Stick it in the cache so we don't do this lookup again.
Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
mOutsideDrawablesCache.put(componentIconKey, toCache);
return drawable;
}
public String getComponentShortName() {
if (componentShortName != null) {
return componentShortName;
}
ComponentName component = getComponentName();
if (component != null) {
componentShortName = component.flattenToShortString();
}
return componentShortName;
}
public String getComponentShortName() {
if (componentShortName != null) {
return componentShortName;
}
ComponentName component = getComponentName();
if (component != null) {
componentShortName = component.flattenToShortString();
}
return componentShortName;
}
public String getComponentShortName() {
if (componentShortName != null) {
return componentShortName;
}
ComponentName component = getComponentName();
if (component != null) {
componentShortName = component.flattenToShortString();
}
return componentShortName;
}
public PreferredComponent(Callbacks callbacks, int match, ComponentName[] set,
ComponentName component, boolean always) {
mCallbacks = callbacks;
mMatch = match&IntentFilter.MATCH_CATEGORY_MASK;
mComponent = component;
mAlways = always;
mShortComponent = component.flattenToShortString();
mParseError = null;
if (set != null) {
final int N = set.length;
String[] myPackages = new String[N];
String[] myClasses = new String[N];
String[] myComponents = new String[N];
for (int i=0; i<N; i++) {
ComponentName cn = set[i];
if (cn == null) {
mSetPackages = null;
mSetClasses = null;
mSetComponents = null;
return;
}
myPackages[i] = cn.getPackageName().intern();
myClasses[i] = cn.getClassName().intern();
myComponents[i] = cn.flattenToShortString();
}
mSetPackages = myPackages;
mSetClasses = myClasses;
mSetComponents = myComponents;
} else {
mSetPackages = null;
mSetClasses = null;
mSetComponents = null;
}
}
private static void logIntent(int intentType, Intent intent, int callerUid,
String resolvedType) {
// The component shouldn't be null, but let's double check just to be safe
ComponentName cn = intent.getComponent();
String shortComponent = null;
if (cn != null) {
shortComponent = cn.flattenToShortString();
}
String callerPackages = null;
int callerPackageCount = 0;
IPackageManager pm = AppGlobals.getPackageManager();
if (pm != null) {
try {
String[] callerPackagesArray = pm.getPackagesForUid(callerUid);
if (callerPackagesArray != null) {
callerPackageCount = callerPackagesArray.length;
callerPackages = joinPackages(callerPackagesArray);
}
} catch (RemoteException ex) {
Slog.e(TAG, "Remote exception while retrieving packages", ex);
}
}
EventLogTags.writeIfwIntentMatched(intentType, shortComponent, callerUid,
callerPackageCount, callerPackages, intent.getAction(), resolvedType,
intent.getDataString(), intent.getFlags());
}
@Override
public void onCreate() {
super.onCreate();
ComponentName component = new ComponentName(this, getClass());
try {
ServiceInfo serviceInfo = getPackageManager().getServiceInfo(component, 0 /* flags */);
if (!Manifest.permission.BIND_TRUST_AGENT.equals(serviceInfo.permission)) {
throw new IllegalStateException(component.flattenToShortString()
+ " is not declared with the permission "
+ "\"" + Manifest.permission.BIND_TRUST_AGENT + "\"");
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Can't get ServiceInfo for " + component.toShortString());
}
}
private static String extractServiceId(ComponentName name, Bundle metaData) {
if (metaData != null && metaData.containsKey(METADATA_KEY_CLASS_NAME_OVERRIDE)) {
return name.getPackageName() + "/" + metaData.getString(METADATA_KEY_CLASS_NAME_OVERRIDE);
} else {
return name.flattenToShortString();
}
}
private void onIntent(final Intent intent) {
Log.i(TAG, "API request: " + intent.toUri(0));
if (intent.getAction() != null) {
String result;
final ComponentName caller = getCallingActivity();
if (caller != null) try { @SuppressLint("WrongConstant") // Invoked with startActivityForResult()
final ActivityInfo info = getPackageManager().getActivityInfo(caller, Hacks.MATCH_ANY_USER_AND_UNINSTALLED);
int uid = info.applicationInfo.uid;
if (UserHandles.getUserId(uid) != 0) {
final String[] potential_pkgs = getPackageManager().getPackagesForUid(uid);
if (potential_pkgs == null) uid = UserHandles.getAppId(uid); // Fix the incorrect UID when MATCH_ANY_USER is used.
}
result = ApiDispatcher.verifyCaller(this, intent, caller.getPackageName(), uid);
} catch (final PackageManager.NameNotFoundException e) {
result = "Unverifiable caller activity: " + caller.flattenToShortString();
} else result = ApiDispatcher.verifyCaller(this, intent, null, - 1);
if (result == null) {
result = ApiDispatcher.dispatch(this, intent);
if (result == null) {
setResult(Activity.RESULT_OK);
Log.i(TAG, "API result: Success");
} else {
setResult(Activity.RESULT_CANCELED, new Intent(result));
Log.i(TAG, "API result: " + result);
}
} else {
setResult(Api.latest.RESULT_UNVERIFIED_IDENTITY, new Intent(result));
Log.w(TAG, "Unverified client: " + result);
}
} else setResult(RESULT_CANCELED, new Intent("No action"));
finish();
}
/**
* 获取当前activity名称
* @param event
* @return
*
*/
private String getCurrentActivity(AccessibilityEvent event) {
ComponentName componentName = new ComponentName(
event.getPackageName().toString(),
event.getClassName().toString()
);
try {
getPackageManager().getActivityInfo(componentName, 0);
return componentName.flattenToShortString();
} catch (PackageManager.NameNotFoundException e) {
return "";
}
}
@Test
public void testIsAccessibilityServiceEnabled() {
String enabledServices =
Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
);
ComponentName componentName = new ComponentName(context, PowerMenuService.class);
String flattenString = componentName.flattenToString();
String flattenShortString = componentName.flattenToShortString();
String newEnabledService =
enabledServices == null ?
"" :
enabledServices
.replaceAll(":" + flattenString, "")
.replaceAll(":" + flattenShortString, "")
.replaceAll(flattenString, "")
.replaceAll(flattenShortString, "");
Settings.Secure.putString(
context.getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
newEnabledService
);
assertFalse(U.isAccessibilityServiceEnabled(context));
Settings.Secure.putString(
context.getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
newEnabledService + ":" + flattenString
);
assertTrue(U.isAccessibilityServiceEnabled(context));
Settings.Secure.putString(
context.getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
newEnabledService + ":" + flattenShortString
);
assertTrue(U.isAccessibilityServiceEnabled(context));
Settings.Secure.putString(
context.getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
enabledServices
);
}
private static String getResourceFilename(ComponentName component) {
String resourceName = component.flattenToShortString();
String filename = resourceName.replace(File.separatorChar, '_');
return RESOURCE_FILE_PREFIX + filename;
}
private static String getResourceFilename(ComponentName component) {
String resourceName = component.flattenToShortString();
String filename = resourceName.replace(File.separatorChar, '_');
return RESOURCE_FILE_PREFIX + filename;
}
protected static String getResourceFilename(ComponentName component) {
return component.flattenToShortString();
}