Example usage for android.content Intent getComponent

List of usage examples for android.content Intent getComponent

Introduction

In this page you can find the example usage for android.content Intent getComponent.

Prototype

public @Nullable ComponentName getComponent() 

Source Link

Document

Retrieve the concrete component associated with the intent.

Usage

From source file:Main.java

static boolean isSystemApp(Context context, Intent intent) {
    PackageManager pm = context.getPackageManager();
    ComponentName cn = intent.getComponent();
    String packageName = null;//  www  . j ava  2 s  .co m
    if (cn == null) {
        ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if ((info != null) && (info.activityInfo != null)) {
            packageName = info.activityInfo.packageName;
        }
    } else {
        packageName = cn.getPackageName();
    }
    if (packageName != null) {
        try {
            PackageInfo info = pm.getPackageInfo(packageName, 0);
            return (info != null) && (info.applicationInfo != null)
                    && ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
        } catch (NameNotFoundException e) {
            return false;
        }
    } else {
        return false;
    }
}

From source file:Main.java

/**
 * Given an AppTask retrieves the task class name.
 * @param task The app task to use.//from   www . j a v  a  2s  .co m
 * @param pm The package manager to use for resolving intent.
 * @return Fully qualified class name or null if we were not able to
 * determine it.
 */
public static String getTaskClassName(AppTask task, PackageManager pm) {
    RecentTaskInfo info = getTaskInfoFromTask(task);
    if (info == null)
        return null;

    Intent baseIntent = info.baseIntent;
    if (baseIntent == null) {
        return null;
    } else if (baseIntent.getComponent() != null) {
        return baseIntent.getComponent().getClassName();
    } else {
        ResolveInfo resolveInfo = pm.resolveActivity(baseIntent, 0);
        if (resolveInfo == null)
            return null;
        return resolveInfo.activityInfo.name;
    }
}

From source file:Main.java

public static String getLauncherActivity(Context context, String pkgName) {
    if (context == null || pkgName == null) {
        return null;
    }//  www.j a  v a 2s.c  o  m

    try {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkgName);
        if (intent != null) {
            return intent.getComponent().getClassName();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:free.rm.skytube.app.SkyTubeApp.java

/**
 * Restart the app.//from  ww w.  jav  a  2  s . com
 */
public static void restartApp() {
    Context context = getContext();
    PackageManager packageManager = context.getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
    ComponentName componentName = intent.getComponent();
    Intent mainIntent = IntentCompat.makeRestartActivityTask(componentName);
    context.startActivity(mainIntent);
    System.exit(0);
}

From source file:cz.maresmar.sfm.service.plugin.PluginUtils.java

/**
 * Starts plugin according to API level/*ww  w  .jav a 2  s.  co m*/
 *
 * @param context Some valid context
 * @param intent  Intent of plugin to be started
 * @see JobIntentService#enqueueWork(Context, ComponentName, int, Intent)
 */
public static void startPlugin(@NonNull Context context, @NonNull Intent intent) {
    // Test if the plugin exists
    PackageManager manager = context.getPackageManager();
    if (manager.queryIntentServices(intent, 0).size() != 1) {
        Timber.e("Plugin %s not found", intent.getComponent());
        throw new IllegalArgumentException("Plugin not found " + intent.getComponent());
    }

    // Finds jobId for selected plugin
    String pluginName = ProviderContract.buildString(intent.getComponent());
    int jobId;
    if (!mPluginsIds.containsKey(pluginName)) {
        jobId = mPluginsIds.size();
        mPluginsIds.put(pluginName, jobId);
    } else {
        jobId = mPluginsIds.get(pluginName);
    }

    // Starts plugin according to API level
    if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            || (context.getPackageName().equals(intent.getComponent().getPackageName()))) {
        JobIntentService.enqueueWork(context, intent.getComponent(), jobId, intent);
    } else {
        // On Android >= O with external plugin use BroadcastContract.BROADCAST_PLAN_RUN to
        // start plugin, because planning of external APK's service is not allowed

        Intent plan = new Intent();
        // Explicitly select a package to communicate with
        plan.setPackage(intent.getComponent().getPackageName());
        plan.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

        plan.setAction(ActionContract.BROADCAST_PLAN_RUN);
        plan.putExtra(ActionContract.EXTRA_JOB_ID, jobId);
        plan.putExtra(ActionContract.EXTRA_INTENT_TO_DO, intent);
        context.sendBroadcast(plan);
    }
}

From source file:org.mozilla.gecko.GeckoActivity.java

private static boolean checkIfGeckoActivity(Intent intent) {
    // Whenever we call our own activity, the component and its package name is set.
    // If we call an activity from another package, or an open intent (leaving android to resolve)
    // component has a different package name or it is null.
    ComponentName component = intent.getComponent();
    return (component != null && AppConstants.ANDROID_PACKAGE_NAME.equals(component.getPackageName()));
}

From source file:Main.java

public static String getAppName(Context context, Intent appIntent) {

    if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
        return appIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    }//  w  ww . ja v a2s.c o  m

    if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_INTENT)) {
        appIntent = appIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    }
    ComponentName componentName = appIntent.getComponent();

    PackageManager pm = context.getPackageManager();

    ApplicationInfo appInfo = null;
    ActivityInfo activityInfo = null;
    try {
        appInfo = pm.getApplicationInfo(componentName.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        appInfo = null;
    }
    try {
        activityInfo = pm.getActivityInfo(componentName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (appInfo == null) {
        return null;
    } else {
        CharSequence appName = pm.getApplicationLabel(appInfo);
        CharSequence activityName = null;

        if (activityInfo != null) {
            activityName = activityInfo.loadLabel(pm);
        }

        if (activityName != null) {
            return activityName.toString();
        }

        if (appName != null) {
            appName.toString();
        }

        return null;
    }
}

From source file:Main.java

/**
 * Converts an {@link android.content.Intent} object to a {@code String}.
 *
 * @param intent The converted intent.//from ww w .  ja v a2  s.  co  m
 * @return The string representation of the intent.
 */
@SuppressWarnings("PMD.ConsecutiveLiteralAppends")
@NonNull
public static String toString(@Nullable final Intent intent) {
    if (intent == null) {
        return "null";
    }

    final StringBuilder stringBuilder = new StringBuilder("Intent{action=\"").append(intent.getAction())
            .append('"').append(ITEM_DIVIDER).append("data=\"").append(intent.getDataString()).append('"')
            .append(ITEM_DIVIDER).append("component=\"").append(intent.getComponent()).append('"')
            .append(ITEM_DIVIDER);

    final Bundle extras = intent.getExtras();
    stringBuilder.append("extras=").append(extras == null ? null : toString(extras)).append('}');
    return stringBuilder.toString();
}

From source file:cc.flydev.launcher.InstallShortcutReceiver.java

public static void removeFromInstallQueue(SharedPreferences sharedPrefs, ArrayList<String> packageNames) {
    synchronized (sLock) {
        Set<String> strings = sharedPrefs.getStringSet(APPS_PENDING_INSTALL, null);
        if (DBG) {
            Log.d(TAG, "APPS_PENDING_INSTALL: " + strings + ", removing packages: " + packageNames);
        }//from  w w w. j ava2s .co m
        if (strings != null) {
            Set<String> newStrings = new HashSet<String>(strings);
            Iterator<String> newStringsIter = newStrings.iterator();
            while (newStringsIter.hasNext()) {
                String json = newStringsIter.next();
                try {
                    JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
                    Intent launchIntent = Intent.parseUri(object.getString(LAUNCH_INTENT_KEY), 0);
                    String pn = launchIntent.getPackage();
                    if (pn == null) {
                        pn = launchIntent.getComponent().getPackageName();
                    }
                    if (packageNames.contains(pn)) {
                        newStringsIter.remove();
                    }
                } catch (org.json.JSONException e) {
                    Log.d(TAG, "Exception reading shortcut to remove: " + e);
                } catch (java.net.URISyntaxException e) {
                    Log.d(TAG, "Exception reading shortcut to remove: " + e);
                }
            }
            sharedPrefs.edit().putStringSet(APPS_PENDING_INSTALL, new HashSet<String>(newStrings)).commit();
        }
    }
}

From source file:com.android.launcher3.InstallShortcutReceiver.java

/**
 * Returns true if the intent is a valid launch intent for a shortcut.
 * This is used to identify shortcuts which are different from the ones exposed by the
 * applications' manifest file./*ww w .j  ava2  s.co  m*/
 *
 * When DISABLE_ALL_APPS is true, shortcuts exposed via the app's manifest should never be
 * duplicated or removed(unless the app is un-installed).
 *
 * @param launchIntent The intent that will be launched when the shortcut is clicked.
 */
static boolean isValidShortcutLaunchIntent(Intent launchIntent) {
    if (launchIntent != null && Intent.ACTION_MAIN.equals(launchIntent.getAction())
            && launchIntent.getComponent() != null && launchIntent.getCategories() != null
            && launchIntent.getCategories().size() == 1 && launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER)
            && launchIntent.getExtras() == null && TextUtils.isEmpty(launchIntent.getDataString())) {
        return false;
    }
    return true;
}