IntentUtils { public static boolean deviceCanHandleIntent(final Context context, final Intent intent) { try { final PackageManager packageManager = context.getPackageManager(); final List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); return !activities.isEmpty(); } catch (NullPointerException e) { return false; } } private  IntentUtils(); static Intent getStartActivityIntent(final Context context, final Class clazz, final Bundle extras); static boolean deviceCanHandleIntent(final Context context, final Intent intent); static boolean isHttpUrl(final String url); static boolean isDeepLink(final String url); static boolean canHandleTwitterUrl(Context context); static boolean canHandleApplicationUrl(Context context, String url); static boolean canHandleApplicationUrl(Context context, String url, boolean logError);  }
@Test public void deviceCanHandleIntent_whenActivityCanResolveIntent_shouldReturnTrue() throws Exception { Context context = mock(Context.class); PackageManager packageManager = mock(PackageManager.class); List<ResolveInfo> resolveInfos = new ArrayList<ResolveInfo>(); resolveInfos.add(new ResolveInfo()); stub(context.getPackageManager()).toReturn(packageManager); Intent specificIntent = new Intent(); specificIntent.setData(Uri.parse("specificIntent:")); stub(packageManager.queryIntentActivities(eq(specificIntent), eq(0))).toReturn(resolveInfos); assertThat(IntentUtils.deviceCanHandleIntent(context, specificIntent)).isTrue(); }
@Test public void deviceCanHandleIntent_whenActivityCanNotResolveIntent_shouldReturnFalse() throws Exception { Context context = mock(Context.class); PackageManager packageManager = mock(PackageManager.class); List<ResolveInfo> resolveInfos = new ArrayList<ResolveInfo>(); resolveInfos.add(new ResolveInfo()); stub(context.getPackageManager()).toReturn(packageManager); Intent specificIntent = new Intent(); specificIntent.setData(Uri.parse("specificIntent:")); Intent otherIntent = new Intent(); otherIntent.setData(Uri.parse("other:")); stub(packageManager.queryIntentActivities(eq(specificIntent), eq(0))).toReturn(resolveInfos); assertThat(IntentUtils.deviceCanHandleIntent(context, otherIntent)).isFalse(); }