Java Code Examples for androidx.browser.customtabs.CustomTabsIntent#launchUrl()

The following examples show how to use androidx.browser.customtabs.CustomTabsIntent#launchUrl() . 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: CustomTabActivityHelper.java    From android-browser-helper with Apache License 2.0 6 votes vote down vote up
/**
 * Opens the URL on a Custom Tab if possible. Otherwise fallsback to opening it on a WebView.
 *
 * @param activity The host activity.
 * @param customTabsIntent a CustomTabsIntent to be used if Custom Tabs is available.
 * @param uri the Uri to be opened.
 * @param fallback a CustomTabFallback to be used if Custom Tabs is not available.
 */
public static void openCustomTab(Activity activity,
                                 CustomTabsIntent customTabsIntent,
                                 Uri uri,
                                 CustomTabFallback fallback) {
    String packageName = CustomTabsHelper.getPackageNameToUse(activity);

    //If we cant find a package name, it means theres no browser that supports
    //Chrome Custom Tabs installed. So, we fallback to the webview
    if (packageName == null) {
        if (fallback != null) {
            fallback.openUri(activity, uri);
        }
    } else {
        customTabsIntent.intent.setPackage(packageName);
        customTabsIntent.launchUrl(activity, uri);
    }
}
 
Example 2
Source File: LaunchActivity.java    From intra42 with Apache License 2.0 6 votes vote down vote up
public void onLoginClick(View view) {
    Analytics.signInAttempt();
    Uri loginUri = Uri.parse(ApiService.API_BASE_URL + "/oauth/authorize?client_id=" + Credential.UID + "&redirect_uri=" + Credential.API_OAUTH_REDIRECT + "&response_type=code&scope=" + Credential.SCOPE);

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setShowTitle(true);
    builder.setInstantAppsEnabled(true);

    Intent defaultBrowserIntent = new Intent(Intent.ACTION_VIEW);
    defaultBrowserIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    defaultBrowserIntent.setData(loginUri);
    PendingIntent defaultBrowserPendingIntent = PendingIntent.getActivity(this, 0, defaultBrowserIntent, 0);

    builder.addMenuItem(getString(R.string.login_custom_chrome_tabs_open_default_browser), defaultBrowserPendingIntent);
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        customTabsIntent.launchUrl(this, loginUri);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        FirebaseCrashlytics.getInstance().recordException(e);
        Toast.makeText(app, R.string.login_error_web_browser_required, Toast.LENGTH_SHORT).show();
    }
}
 
Example 3
Source File: CustomTabActivityHelper.java    From Easy_xkcd with Apache License 2.0 6 votes vote down vote up
/**
 * Opens the URL on a Custom Tab if possible. Otherwise fallsback to opening it on a WebView
 *
 * @param activity The host activity
 * @param customTabsIntent a CustomTabsIntent to be used if Custom Tabs is available
 * @param uri the Uri to be opened
 * @param fallback a CustomTabFallback to be used if Custom Tabs is not available
 */
public static void openCustomTab(Activity activity,
                                 CustomTabsIntent customTabsIntent,
                                 Uri uri,
                                 CustomTabFallback fallback) {
    String packageName = CustomTabsHelper.getPackageNameToUse(activity);

    //If we cant find a package name, it means there's no browser that supports
    //Chrome Custom Tabs installed. So, we fallback to the webview
    if (packageName == null) {
        if (fallback != null) {
            fallback.openUri(activity, uri);
        }
    } else {
        customTabsIntent.intent.setPackage(packageName);
        customTabsIntent.launchUrl(activity, uri);
    }
}
 
Example 4
Source File: TimelineListAdapter.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    TimelineItem item = items.get(this.position);

    try {
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
        intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
        intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
        CustomTabsIntent customTabsIntent = intentBuilder.build();
        customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        customTabsIntent.launchUrl(context, Uri.parse(item.getUrl()));
    }
    catch (Exception ignored) { }

}
 
Example 5
Source File: TimelineListAdapter.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Link clickable.
 *
 * @param strBuilder
 *   A string builder.
 * @param span
 *   The span with url.
 */
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            try {
                CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
                intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
                intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
                CustomTabsIntent customTabsIntent = intentBuilder.build();
                customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                customTabsIntent.launchUrl(context, Uri.parse(span.getURL()));
            }
            catch (Exception ignored) { }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 6
Source File: TimelineDetailActivity.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Link clickable.
 *
 * @param strBuilder
 *   A string builder.
 * @param span
 *   The span with url.
 */
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(@NonNull View view) {
            try {
                CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
                intentBuilder.setToolbarColor(ContextCompat.getColor(TimelineDetailActivity.this, R.color.colorPrimary));
                intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(TimelineDetailActivity.this, R.color.colorPrimaryDark));
                CustomTabsIntent customTabsIntent = intentBuilder.build();
                customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                customTabsIntent.launchUrl(TimelineDetailActivity.this, Uri.parse(span.getURL()));
            }
            catch (Exception ignored) { }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 7
Source File: SettingsActivity.java    From MaxLock with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.toolbar_info:
            @SuppressWarnings("deprecation") CustomTabsIntent intent = new CustomTabsIntent.Builder(ctSession)
                    .setShowTitle(true)
                    .enableUrlBarHiding()
                    .setToolbarColor(getResources().getColor(R.color.primary_red))
                    .build();
            intent.launchUrl(this, Common.WEBSITE_URI);
            return true;
        case android.R.id.home:
            onBackPressed();
            return false;
        default:
            return false;
    }
}
 
Example 8
Source File: MainActivity.java    From pandora with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    String url = "https://github.com/whataa/pandora";
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri githubUrl = Uri.parse(url);
        intent.setData(githubUrl);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    } else {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.setToolbarColor(getResources().getColor(R.color.colorPrimary));
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(this, Uri.parse(url));
    }
    return true;
}
 
Example 9
Source File: TimelineDetailActivity.java    From indigenous-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(View v) {

    try {
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
        intentBuilder.setToolbarColor(ContextCompat.getColor(TimelineDetailActivity.this, R.color.colorPrimary));
        intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(TimelineDetailActivity.this, R.color.colorPrimaryDark));
        CustomTabsIntent customTabsIntent = intentBuilder.build();
        customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        customTabsIntent.launchUrl(TimelineDetailActivity.this, Uri.parse(item.getUrl()));
    }
    catch (Exception ignored) { }

}
 
Example 10
Source File: ExtraUtils.java    From Bop with Apache License 2.0 5 votes vote down vote up
public static void openCustomTabs(Context context, String url) {
    CustomTabsIntent.Builder builderq = new CustomTabsIntent.Builder();
    builderq.setToolbarColor(context.getResources().getColor(R.color.primaryColor));
    builderq.addDefaultShareMenuItem().enableUrlBarHiding();
    CustomTabsIntent customTabsIntent = builderq.build();
    customTabsIntent.launchUrl(context, Uri.parse(url));
}
 
Example 11
Source File: Utils.java    From SmsCode with GNU General Public License v3.0 5 votes vote down vote up
public static void showWebPage(Context context, String url) {
    try {
        CustomTabsIntent cti = new CustomTabsIntent.Builder().build();
        cti.launchUrl(context, Uri.parse(url));
    } catch (Exception e) {
        Toast.makeText(context, R.string.browser_install_or_enable_prompt, Toast.LENGTH_SHORT).show();
    }
}
 
Example 12
Source File: Utils.java    From XposedSmsCode with GNU General Public License v3.0 5 votes vote down vote up
public static void showWebPage(Context context, String url) {
    try {
        CustomTabsIntent cti = new CustomTabsIntent.Builder().build();
        cti.launchUrl(context, Uri.parse(url));
    } catch (Exception e) {
        Toast.makeText(context, R.string.browser_install_or_enable_prompt, Toast.LENGTH_SHORT).show();
    }
}
 
Example 13
Source File: FileActivity.java    From lrkFM with MIT License 5 votes vote down vote up
/**
 * Opens the bug report page.
 */
private void launchBugReportTab() {
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setToolbarColor(getColorByAttr(R.attr.colorPrimary));
    CustomTabsIntent build = builder.build();
    build.launchUrl(this, Uri.parse("https://github.com/lfuelling/lrkFM/issues/new"));
}
 
Example 14
Source File: AboutShortcuts.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
private static void openUrl(Context ctx, String url) {
    try {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.setToolbarColor(ctx.getResources().getColor(R.color.colorPrimary));
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(ctx, Uri.parse(url));
    } catch (ActivityNotFoundException e) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        ctx.startActivity(i);
    }
}
 
Example 15
Source File: PUI.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Shows a web in a different screen.
 * Once the web is opened, we loose the control of the script
 *
 * @param url
 * @status TOREVIEW
 * @advanced
 */
@PhonkMethod
public void showWeb(String url) {
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setToolbarColor(Color.BLUE);
    builder.addDefaultShareMenuItem();
    builder.setInstantAppsEnabled(true);

    // builder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left);
    // builder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_out_right);

    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(getActivity(), Uri.parse(url));
}
 
Example 16
Source File: Utils.java    From XMiTools with GNU General Public License v3.0 5 votes vote down vote up
public static void showWebPage(Context context, String url) {
    try {
        CustomTabsIntent cti = new CustomTabsIntent.Builder().build();
        cti.launchUrl(context, Uri.parse(url));
    } catch (Exception e) {
        Toast.makeText(context, R.string.browser_install_or_enable_prompt, Toast.LENGTH_SHORT).show();
    }
}
 
Example 17
Source File: NavigationUtils.java    From zephyr with MIT License 5 votes vote down vote up
public static void openUrl(@NonNull Context context, @NonNull String url) {
    try {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.setToolbarColor(ContextCompat.getColor(context, R.color.primary));
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(context, Uri.parse(url));
    } catch (ActivityNotFoundException e) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        context.startActivity(browserIntent);
    }
}
 
Example 18
Source File: CustomTabHelper.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Help user to visit web page by {@link CustomTabsIntent}.
 * */
public static void startCustomTabActivity(Context context, String url) {
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setShowTitle(true);
    builder.setToolbarColor(ThemeManager.getPrimaryColor(context));
    builder.setStartAnimations(context, R.anim.activity_slide_in, R.anim.activity_fade_out);
    builder.setExitAnimations(context, R.anim.activity_slide_in, R.anim.activity_fade_out);

    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(context, Uri.parse(url));
}
 
Example 19
Source File: AboutShortcuts.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
private static void openUrl(Context ctx, String url) {
    try {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.setToolbarColor(ctx.getResources().getColor(R.color.colorPrimary));
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(ctx, Uri.parse(url));
    } catch (ActivityNotFoundException e) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        ctx.startActivity(i);
    }
}
 
Example 20
Source File: ChromeCustomTabs.java    From leafpicrevived with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Launches a Chrome Custom Tab without warmup / service.
 *
 * @param context The context - used for launching an Activity.
 * @param url     The URL to load.
 */
public static void launchUrl(@NonNull Context context, @NonNull String url) {
    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
    customTabsIntent.launchUrl(context, Uri.parse(url));
}