Java Code Examples for android.content.ComponentName#flattenToShortString()

The following examples show how to use android.content.ComponentName#flattenToShortString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuggestionsAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: WakeLockUtil.java    From android-job with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 3
Source File: SuggestionsAdapter.java    From zen4android with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: MainActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
@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));
}
 
Example 5
Source File: SuggestionsAdapter.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: SuggestionsAdapter.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: ServiceRecord.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: TrustAgentService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example 9
Source File: IntentFirewall.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 10
Source File: PreferredComponent.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 11
Source File: ScalarSensorServiceFinder.java    From science-journal with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 12
Source File: PackageParser.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
public String getComponentShortName() {
    if (componentShortName != null) {
        return componentShortName;
    }
    ComponentName component = getComponentName();
    if (component != null) {
        componentShortName = component.flattenToShortString();
    }
    return componentShortName;
}
 
Example 13
Source File: ApiActivity.java    From island with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 14
Source File: HongbaoService.java    From WeChatHongBao with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前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 "";
    }
}
 
Example 15
Source File: PackageParser.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
public String getComponentShortName() {
    if (componentShortName != null) {
        return componentShortName;
    }
    ComponentName component = getComponentName();
    if (component != null) {
        componentShortName = component.flattenToShortString();
    }
    return componentShortName;
}
 
Example 16
Source File: PackageParser.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
public String getComponentShortName() {
    if (componentShortName != null) {
        return componentShortName;
    }
    ComponentName component = getComponentName();
    if (component != null) {
        componentShortName = component.flattenToShortString();
    }
    return componentShortName;
}
 
Example 17
Source File: IconCacheHelper.java    From Hangar with GNU General Public License v3.0 4 votes vote down vote up
protected static String getResourceFilename(ComponentName component) {
    return component.flattenToShortString();
}
 
Example 18
Source File: IconCache.java    From LB-Launcher with Apache License 2.0 4 votes vote down vote up
private static String getResourceFilename(ComponentName component) {
    String resourceName = component.flattenToShortString();
    String filename = resourceName.replace(File.separatorChar, '_');
    return RESOURCE_FILE_PREFIX + filename;
}
 
Example 19
Source File: IconCache.java    From TurboLauncher with Apache License 2.0 4 votes vote down vote up
private static String getResourceFilename(ComponentName component) {
	String resourceName = component.flattenToShortString();
	String filename = resourceName.replace(File.separatorChar, '_');
	return RESOURCE_FILE_PREFIX + filename;
}
 
Example 20
Source File: UTest.java    From Taskbar with Apache License 2.0 4 votes vote down vote up
@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
    );
}