@Test public void testOpenExternalUrlNoConnection() { shadowOf((ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE)) .setActiveNetworkInfo(null); AppUtils.openWebUrlExternal(context, new TestHnItem(1L) { @Override public String getUrl() { return "http: } }, "http: assertThat(shadowOf(context).getNextStartedActivity()) .hasComponent(context, OfflineWebActivity.class) .hasExtra(OfflineWebActivity.EXTRA_URL, "http: }
public static void openWebUrlExternal(Context context, @Nullable WebItem item, String url, @Nullable CustomTabsSession session) { if (!hasConnection(context)) { context.startActivity(new Intent(context, OfflineWebActivity.class) .putExtra(OfflineWebActivity.EXTRA_URL, url)); return; } Intent intent = createViewIntent(context, item, url, session); if (!HackerNewsClient.BASE_WEB_URL.contains(Uri.parse(url).getHost())) { if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } return; } List<ResolveInfo> activities = context.getPackageManager() .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); ArrayList<Intent> intents = new ArrayList<>(); for (ResolveInfo info : activities) { if (info.activityInfo.packageName.equalsIgnoreCase(context.getPackageName())) { continue; } intents.add(createViewIntent(context, item, url, session) .setPackage(info.activityInfo.packageName)); } if (intents.isEmpty()) { return; } if (intents.size() == 1) { context.startActivity(intents.remove(0)); } else { context.startActivity(Intent.createChooser(intents.remove(0), context.getString(R.string.chooser_title)) .putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[intents.size()]))); } }
AppUtils { public static void openWebUrlExternal(Context context, @Nullable WebItem item, String url, @Nullable CustomTabsSession session) { if (!hasConnection(context)) { context.startActivity(new Intent(context, OfflineWebActivity.class) .putExtra(OfflineWebActivity.EXTRA_URL, url)); return; } Intent intent = createViewIntent(context, item, url, session); if (!HackerNewsClient.BASE_WEB_URL.contains(Uri.parse(url).getHost())) { if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } return; } List<ResolveInfo> activities = context.getPackageManager() .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); ArrayList<Intent> intents = new ArrayList<>(); for (ResolveInfo info : activities) { if (info.activityInfo.packageName.equalsIgnoreCase(context.getPackageName())) { continue; } intents.add(createViewIntent(context, item, url, session) .setPackage(info.activityInfo.packageName)); } if (intents.isEmpty()) { return; } if (intents.size() == 1) { context.startActivity(intents.remove(0)); } else { context.startActivity(Intent.createChooser(intents.remove(0), context.getString(R.string.chooser_title)) .putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[intents.size()]))); } } }
AppUtils { public static void openWebUrlExternal(Context context, @Nullable WebItem item, String url, @Nullable CustomTabsSession session) { if (!hasConnection(context)) { context.startActivity(new Intent(context, OfflineWebActivity.class) .putExtra(OfflineWebActivity.EXTRA_URL, url)); return; } Intent intent = createViewIntent(context, item, url, session); if (!HackerNewsClient.BASE_WEB_URL.contains(Uri.parse(url).getHost())) { if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } return; } List<ResolveInfo> activities = context.getPackageManager() .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); ArrayList<Intent> intents = new ArrayList<>(); for (ResolveInfo info : activities) { if (info.activityInfo.packageName.equalsIgnoreCase(context.getPackageName())) { continue; } intents.add(createViewIntent(context, item, url, session) .setPackage(info.activityInfo.packageName)); } if (intents.isEmpty()) { return; } if (intents.size() == 1) { context.startActivity(intents.remove(0)); } else { context.startActivity(Intent.createChooser(intents.remove(0), context.getString(R.string.chooser_title)) .putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[intents.size()]))); } }  }
AppUtils { public static void openWebUrlExternal(Context context, @Nullable WebItem item, String url, @Nullable CustomTabsSession session) { if (!hasConnection(context)) { context.startActivity(new Intent(context, OfflineWebActivity.class) .putExtra(OfflineWebActivity.EXTRA_URL, url)); return; } Intent intent = createViewIntent(context, item, url, session); if (!HackerNewsClient.BASE_WEB_URL.contains(Uri.parse(url).getHost())) { if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } return; } List<ResolveInfo> activities = context.getPackageManager() .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); ArrayList<Intent> intents = new ArrayList<>(); for (ResolveInfo info : activities) { if (info.activityInfo.packageName.equalsIgnoreCase(context.getPackageName())) { continue; } intents.add(createViewIntent(context, item, url, session) .setPackage(info.activityInfo.packageName)); } if (intents.isEmpty()) { return; } if (intents.size() == 1) { context.startActivity(intents.remove(0)); } else { context.startActivity(Intent.createChooser(intents.remove(0), context.getString(R.string.chooser_title)) .putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[intents.size()]))); } }  static void openWebUrlExternal(Context context, @Nullable WebItem item,
                                          String url, @Nullable CustomTabsSession session); static void setTextWithLinks(TextView textView, CharSequence html); static CharSequence fromHtml(String htmlText); static CharSequence fromHtml(String htmlText, boolean compact); static Intent makeSendIntentChooser(Context context, Uri data); static void openExternal(@NonNull final Context context,
                             @NonNull PopupMenu popupMenu,
                             @NonNull View anchor,
                             @NonNull final WebItem item,
                             final CustomTabsSession session); static void share(@NonNull final Context context,
                             @NonNull PopupMenu popupMenu,
                             @NonNull View anchor,
                             @NonNull final WebItem item); static int getThemedResId(Context context, @AttrRes int attr); static float getDimension(Context context, @StyleRes int styleResId, @AttrRes int attr); static boolean isHackerNewsUrl(WebItem item); static int getDimensionInDp(Context context, @DimenRes int dimenResId); static void restart(Activity activity, boolean transition); static String getAbbreviatedTimeSpan(long timeMillis); static boolean isOnWiFi(Context context); static boolean hasConnection(Context context); @SuppressLint("MissingPermission") static Pair<String, String> getCredentials(Context context); @SuppressLint("MissingPermission") static void showLogin(Context context, AlertDialogBuilder alertDialogBuilder); @SuppressLint("MissingPermission") static void registerAccountsUpdatedListener(final Context context); @SuppressWarnings("deprecation") @TargetApi(Build.VERSION_CODES.LOLLIPOP) static void openPlayStore(Context context); @SuppressLint("MissingPermission") static void showAccountChooser(final Context context, AlertDialogBuilder alertDialogBuilder,
                                           Account[] accounts); static void toggleFab(FloatingActionButton fab, boolean visible); static void toggleFabAction(FloatingActionButton fab, WebItem item, boolean commentMode); static String toHtmlColor(Context context, @AttrRes int colorAttr); static void toggleWebViewZoom(WebSettings webSettings, boolean enabled); static void setStatusBarDim(Window window, boolean dim); static void setStatusBarColor(Window window, int color); static void navigate(int direction, AppBarLayout appBarLayout, Navigable navigable); static int getDisplayHeight(Context context); static LayoutInflater createLayoutInflater(Context context); static void share(Context context, String subject, String text); static Uri createItemUri(@NonNull String itemId); static Uri createUserUri(@NonNull String userId); static String getDataUriId(@NonNull Intent intent, String altParamId); static String wrapHtml(Context context, String html); @SuppressLint("InlinedApi") static Intent multiWindowIntent(Activity activity, Intent intent); static void setTextAppearance(TextView textView, @StyleRes int textAppearance); static boolean urlEquals(String thisUrl, String thatUrl); }
AppUtils { public static void openWebUrlExternal(Context context, @Nullable WebItem item, String url, @Nullable CustomTabsSession session) { if (!hasConnection(context)) { context.startActivity(new Intent(context, OfflineWebActivity.class) .putExtra(OfflineWebActivity.EXTRA_URL, url)); return; } Intent intent = createViewIntent(context, item, url, session); if (!HackerNewsClient.BASE_WEB_URL.contains(Uri.parse(url).getHost())) { if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } return; } List<ResolveInfo> activities = context.getPackageManager() .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); ArrayList<Intent> intents = new ArrayList<>(); for (ResolveInfo info : activities) { if (info.activityInfo.packageName.equalsIgnoreCase(context.getPackageName())) { continue; } intents.add(createViewIntent(context, item, url, session) .setPackage(info.activityInfo.packageName)); } if (intents.isEmpty()) { return; } if (intents.size() == 1) { context.startActivity(intents.remove(0)); } else { context.startActivity(Intent.createChooser(intents.remove(0), context.getString(R.string.chooser_title)) .putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[intents.size()]))); } }  static void openWebUrlExternal(Context context, @Nullable WebItem item,
                                          String url, @Nullable CustomTabsSession session); static void setTextWithLinks(TextView textView, CharSequence html); static CharSequence fromHtml(String htmlText); static CharSequence fromHtml(String htmlText, boolean compact); static Intent makeSendIntentChooser(Context context, Uri data); static void openExternal(@NonNull final Context context,
                             @NonNull PopupMenu popupMenu,
                             @NonNull View anchor,
                             @NonNull final WebItem item,
                             final CustomTabsSession session); static void share(@NonNull final Context context,
                             @NonNull PopupMenu popupMenu,
                             @NonNull View anchor,
                             @NonNull final WebItem item); static int getThemedResId(Context context, @AttrRes int attr); static float getDimension(Context context, @StyleRes int styleResId, @AttrRes int attr); static boolean isHackerNewsUrl(WebItem item); static int getDimensionInDp(Context context, @DimenRes int dimenResId); static void restart(Activity activity, boolean transition); static String getAbbreviatedTimeSpan(long timeMillis); static boolean isOnWiFi(Context context); static boolean hasConnection(Context context); @SuppressLint("MissingPermission") static Pair<String, String> getCredentials(Context context); @SuppressLint("MissingPermission") static void showLogin(Context context, AlertDialogBuilder alertDialogBuilder); @SuppressLint("MissingPermission") static void registerAccountsUpdatedListener(final Context context); @SuppressWarnings("deprecation") @TargetApi(Build.VERSION_CODES.LOLLIPOP) static void openPlayStore(Context context); @SuppressLint("MissingPermission") static void showAccountChooser(final Context context, AlertDialogBuilder alertDialogBuilder,
                                           Account[] accounts); static void toggleFab(FloatingActionButton fab, boolean visible); static void toggleFabAction(FloatingActionButton fab, WebItem item, boolean commentMode); static String toHtmlColor(Context context, @AttrRes int colorAttr); static void toggleWebViewZoom(WebSettings webSettings, boolean enabled); static void setStatusBarDim(Window window, boolean dim); static void setStatusBarColor(Window window, int color); static void navigate(int direction, AppBarLayout appBarLayout, Navigable navigable); static int getDisplayHeight(Context context); static LayoutInflater createLayoutInflater(Context context); static void share(Context context, String subject, String text); static Uri createItemUri(@NonNull String itemId); static Uri createUserUri(@NonNull String userId); static String getDataUriId(@NonNull Intent intent, String altParamId); static String wrapHtml(Context context, String html); @SuppressLint("InlinedApi") static Intent multiWindowIntent(Activity activity, Intent intent); static void setTextAppearance(TextView textView, @StyleRes int textAppearance); static boolean urlEquals(String thisUrl, String thatUrl); static final int HOT_THRESHOLD_HIGH; static final int HOT_THRESHOLD_NORMAL; static final int HOT_FACTOR; }