android.support.customtabs.CustomTabsClient Java Examples

The following examples show how to use android.support.customtabs.CustomTabsClient. 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: ManageDataLauncherActivity.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String chromePackage = CustomTabsClient.getPackageName(this,
            TrustedWebUtils.SUPPORTED_CHROME_PACKAGES, false);
    if (chromePackage == null) {
        onError(new RuntimeException("No valid build of Chrome found"));
        finish();
        return;
    }

    View loadingView = createLoadingView();
    if (loadingView != null) {
        setContentView(loadingView);
    }

    CustomTabsClient.bindCustomTabsService(this, chromePackage, mServiceConnection);
}
 
Example #2
Source File: ManageDataLauncherActivity.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName componentName,
        CustomTabsClient client) {
    Uri uri = getUrlForManagingSpace();
    if (uri == null) {
        finish();
        return;
    }

    mSession = client.newSession(mCustomTabsCallback);
    if (mSession == null) {
        onError(new RuntimeException("Failed to create CustomTabsSession"));
        finish();
        return;
    }

    // Warm up is needed for origin verification.
    client.warmup(0);
    mSession.validateRelationship(RELATION_HANDLE_ALL_URLS, uri, null);
}
 
Example #3
Source File: TwaLauncher.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName componentName,
        CustomTabsClient client) {
    if (TrustedWebUtils.warmupIsRequired(mContext, mProviderPackage)) {
        client.warmup(0);
    }
    mSession = client.newSession(null, mSessionId);

    if (mSession != null && mOnSessionCreatedRunnable != null) {
        mOnSessionCreatedRunnable.run();
    } else if (mSession == null && mOnSessionCreationFailedRunnable != null) {
        mOnSessionCreationFailedRunnable.run();
    }

    mOnSessionCreatedRunnable = null;
    mOnSessionCreationFailedRunnable = null;
}
 
Example #4
Source File: CustomTabConnectionRule.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
/**
 * Binds the CustomTabsService, creates a {@link CustomTabsSession} and returns it.
 */
public CustomTabsSession establishSessionBlocking(Context context) {
    mContext = context;
    if (!CustomTabsClient.bindCustomTabsService(context, context.getPackageName(),
            mConnection)) {
        fail("Failed to bind the service");
        return null;
    }
    boolean success = false;
    try {
        success = mConnectionLatch.await(2, TimeUnit.SECONDS);
    } catch (InterruptedException e) {}
    if (!success) {
        fail("Failed to connect to service");
        return null;
    }
    return mSession;
}
 
Example #5
Source File: CustomTabs.java    From ForceDoze with GNU General Public License v3.0 6 votes vote down vote up
private Warmer warm(){
    mCustomTabServiceConnection =
            new CustomTabsServiceConnection() {
                @Override
                public void onCustomTabsServiceConnected(ComponentName componentName,
                                                         CustomTabsClient customTabsClient) {
                    mCustomTabsClient = customTabsClient;
                    mCustomTabsClient.warmup(0);
                    mCustomTabsSession = mCustomTabsClient.newSession(null);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    mCustomTabsClient = null;
                }
            };

    packageNameToUse = ChromePackageHelper.getPackageNameToUse(context);

    if (packageNameToUse != null){
        CustomTabsClient.bindCustomTabsService(context, packageNameToUse,
                mCustomTabServiceConnection);
    }
    return this;
}
 
Example #6
Source File: CustomTabActivityHelper.java    From Focus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Binds the Activity to the Custom Tabs Service.
 * @param activity the activity to be binded to the service.
 */
public void bindCustomTabsService(Activity activity) {
    if (mClient != null) return;

    String packageName = CustomTabsHelper.getPackageNameToUse(activity);
    if (packageName == null) return;

    mConnection = new ServiceConnection(this);
    CustomTabsClient.bindCustomTabsService(activity, packageName, mConnection);
}
 
Example #7
Source File: CustomTabActivityHelper.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
/**
 * Binds the Activity to the Custom Tabs Service.
 * @param activity the activity to be binded to the service.
 */
public void bindCustomTabsService(Activity activity) {
    if (mClient != null) return;

    String packageName = CustomTabsHelper.getPackageNameToUse(activity);
    if (packageName == null) return;

    mConnection = new ServiceConnection(this);
    CustomTabsClient.bindCustomTabsService(activity, packageName, mConnection);
}
 
Example #8
Source File: CustomTabsController.java    From Auth0.Android with MIT License 5 votes vote down vote up
/**
 * Attempts to bind the Custom Tabs Service to the Context.
 */
public void bindService() {
    Log.v(TAG, "Trying to bind the service");
    Context context = this.context.get();
    isBound = false;
    if (context != null && preferredPackage != null) {
        isBound = CustomTabsClient.bindCustomTabsService(context, preferredPackage, this);
    }
    Log.v(TAG, "Bind request result: " + isBound);
}
 
Example #9
Source File: CustomTabsController.java    From Auth0.Android with MIT License 5 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) {
    if (customTabsClient == null) {
        return;
    }
    Log.d(TAG, "CustomTabs Service connected");
    customTabsClient.warmup(0L);
    session.set(customTabsClient.newSession(null));
    sessionLatch.countDown();
}
 
Example #10
Source File: CustomTabActivityHelper.java    From AndroidProjects with MIT License 5 votes vote down vote up
/**
     * Binds the Activity to the Custom Tabs Service.
     *
     * @param activity the activity to be binded to the service.
     */
    public void bindCustomTabsService(Activity activity) {
        if (mClient != null) return;

        String packageName = CustomTabsHelper.getPackageNameToUse(activity);
        if (packageName == null) return;

//        mConnection = new ServiceConnection(this);
        mConnection = new org.chromium.customtabsclient.shared.ServiceConnection(this);
        CustomTabsClient.bindCustomTabsService(activity, packageName, mConnection);
    }
 
Example #11
Source File: TwaLauncher.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
private void launchTwa(TrustedWebActivityIntentBuilder twaBuilder,
        @Nullable SplashScreenStrategy splashScreenStrategy,
        @Nullable Runnable completionCallback) {
    if (splashScreenStrategy != null) {
        splashScreenStrategy.onTwaLaunchInitiated(mProviderPackage, twaBuilder);
    }

    Runnable onSessionCreatedRunnable = () ->
            launchWhenSessionEstablished(twaBuilder, splashScreenStrategy, completionCallback);

    if (mSession != null) {
        onSessionCreatedRunnable.run();
        return;
    }

    Runnable onSessionCreationFailedRunnable = () -> {
        // The provider has been unable to create a session for us, we can't launch a
        // Trusted Web Activity. We could either exit, forcing the user to try again,
        // hopefully successfully this time or we could launch a Custom Tab giving the user
        // a subpar experience (compared to a TWA).
        // We'll open in a CCT, but pay attention to what users want.
        launchCct(twaBuilder, completionCallback);
    };

    if (mServiceConnection == null) {
        mServiceConnection = new TwaCustomTabsServiceConnection();
    }

    mServiceConnection.setSessionCreationRunnables(
            onSessionCreatedRunnable, onSessionCreationFailedRunnable);
    CustomTabsClient.bindCustomTabsService(mContext, mProviderPackage, mServiceConnection);
}
 
Example #12
Source File: MainActivity.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
private void bindCustomTabsService() {
    if (mClient != null) return;
    if (TextUtils.isEmpty(mPackageNameToBind)) {
        mPackageNameToBind = CustomTabsHelper.getPackageNameToUse(this);
        if (mPackageNameToBind == null) return;
    }
    mConnection = new ServiceConnection(this);
    boolean ok = CustomTabsClient.bindCustomTabsService(this, mPackageNameToBind, mConnection);
    if (ok) {
        mConnectButton.setEnabled(false);
    } else {
        mConnection = null;
    }
}
 
Example #13
Source File: MainActivity.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
@Override
public void onServiceConnected(CustomTabsClient client) {
    mClient = client;
    mConnectButton.setEnabled(false);
    mWarmupButton.setEnabled(true);
    mMayLaunchButton.setEnabled(true);
    mLaunchButton.setEnabled(true);
}
 
Example #14
Source File: CustomTabsHelper.java    From rides-android-sdk with MIT License 5 votes vote down vote up
/**
 * Opens the URL on a Custom Tab if possible. Otherwise fallsback to opening it on a WebView.
 *
 * @param context The host context.
 * @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 void openCustomTab(
        final Context context,
        final CustomTabsIntent customTabsIntent,
        final Uri uri,
        CustomTabFallback fallback) {
    final String packageName = getPackageNameToUse(context);

    if (packageName != null) {
        connection = new CustomTabsServiceConnection() {
            @Override
            public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient client) {
                client.warmup(0L); // This prevents backgrounding after redirection

                customTabsIntent.intent.setPackage(packageName);
                customTabsIntent.intent.setData(uri);
                customTabsIntent.launchUrl(context, uri);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };
        CustomTabsClient.bindCustomTabsService(context, packageName, connection);
    } else if (fallback != null) {
        fallback.openUri(context, uri);
    } else {
        Log.e(UberSdk.UBER_SDK_LOG_TAG, "Use of openCustomTab without Customtab support or a fallback set");
    }
}
 
Example #15
Source File: SettingsActivity.java    From home-assistant-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //noinspection ConstantConditions
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    String packageName = CustomTabsClient.getPackageName(this, null);
    CustomTabsClient.bindCustomTabsService(this, !TextUtils.isEmpty(packageName) ? packageName : "com.android.chrome", chromeConnection);
}
 
Example #16
Source File: SettingsActivity.java    From home-assistant-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    client.warmup(0);
    customTabsSession = client.newSession(new CustomTabsCallback());
    if (customTabsSession == null) {
        return;
    }
    // Delay to not slow down native app loading
    customTabsSession.mayLaunchUrl(Uri.parse(Common.CROWDIN_URL), null, null);
}
 
Example #17
Source File: HassActivity.java    From home-assistant-Android with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (menu instanceof MenuBuilder) {
        ((MenuBuilder) menu).setOptionalIconsVisible(true);
    }
    String packageName = CustomTabsClient.getPackageName(this, null);
    CustomTabsClient.bindCustomTabsService(this, !TextUtils.isEmpty(packageName) ? packageName : "com.android.chrome", chromeConnection);
    getMenuInflater().inflate(R.menu.hass, menu);
    return super.onCreateOptionsMenu(menu);
}
 
Example #18
Source File: HassActivity.java    From home-assistant-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    client.warmup(0);
    customTabsSession = client.newSession(new CustomTabsCallback());
    if (customTabsSession == null) {
        return;
    }
    // Delay to not slow down native app loading
    communicationHandler.postDelayed(() -> customTabsSession.mayLaunchUrl(Uri.parse(Utils.getUrl(HassActivity.this)), null, null), 1500);
}
 
Example #19
Source File: CustomTabServiceHelper.java    From cordova-plugin-safariviewcontroller with MIT License 5 votes vote down vote up
/**
 * Binds the Activity to the Custom Tabs Service.
 * @param activity the activity to be binded to the service.
 */
public boolean bindCustomTabsService(Activity activity) {
    if (mClient != null) return false;

    if (mPackageNameToBind == null) return false;

    mConnection = new ServiceConnection(this);
    CustomTabsClient.bindCustomTabsService(activity, mPackageNameToBind, mConnection);
    return true;
}
 
Example #20
Source File: CustomTabActivityHelper.java    From Focus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Binds the Activity to the Custom Tabs Service.
 * @param activity the activity to be binded to the service.
 */
public void bindCustomTabsService(Activity activity) {
    if (mClient != null) return;

    String packageName = CustomTabsHelper.getPackageNameToUse(activity);
    if (packageName == null) return;

    mConnection = new ServiceConnection(this);
    CustomTabsClient.bindCustomTabsService(activity, packageName, mConnection);
}
 
Example #21
Source File: CustomTabConnectionRule.java    From custom-tabs-client with Apache License 2.0 4 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    mSession = client.newSession(null);
    mConnectionLatch.countDown();
}
 
Example #22
Source File: ServiceConnection.java    From cordova-plugin-safariviewcontroller with MIT License 4 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    ServiceConnectionCallback connectionCallback = mConnectionCallback.get();
    if (connectionCallback != null) connectionCallback.onServiceConnected(client);
}
 
Example #23
Source File: CustomTabServiceHelper.java    From cordova-plugin-safariviewcontroller with MIT License 4 votes vote down vote up
@Override
public void onServiceConnected(CustomTabsClient client) {
    mClient = client;
    if (mConnectionCallback != null) mConnectionCallback.onCustomTabsConnected();
}
 
Example #24
Source File: CustomTabServiceHelper.java    From cordova-plugin-safariviewcontroller with MIT License 4 votes vote down vote up
public CustomTabsClient getClient() {
    return mClient;
}
 
Example #25
Source File: ServiceConnection.java    From Focus with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    ServiceConnectionCallback connectionCallback = mConnectionCallback.get();
    if (connectionCallback != null) connectionCallback.onServiceConnected(client);
}
 
Example #26
Source File: ChromeCustomTabPlugin.java    From cordova-plugin-safariviewcontroller with MIT License 4 votes vote down vote up
private boolean warmUp(){
    boolean success = false;
    final CustomTabsClient client = mCustomTabPluginHelper.getClient();
    if (client != null) success = client.warmup(0);
    return success;
}
 
Example #27
Source File: ServiceConnection.java    From custom-tabs-client with Apache License 2.0 4 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    ServiceConnectionCallback connectionCallback = mConnectionCallback.get();
    if (connectionCallback != null) connectionCallback.onServiceConnected(client);
}
 
Example #28
Source File: ChromeCustomTabsManager.java    From Anecdote with Apache License 2.0 4 votes vote down vote up
/**
 * Bins to custom chrome tab service
 */
public void bindCustomTabsService(Context context) {
    if (mClient != null) return;
    mConnection = new ChromeCustomTabsConnection(this);
    CustomTabsClient.bindCustomTabsService(context, CHROME_PACKAGE, mConnection);
}
 
Example #29
Source File: CustomTabActivityHelper.java    From Focus with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onServiceConnected(CustomTabsClient client) {
    mClient = client;
    mClient.warmup(0L);
    if (mConnectionCallback != null) mConnectionCallback.onCustomTabsConnected();
}
 
Example #30
Source File: ServiceConnection.java    From Focus with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    ServiceConnectionCallback connectionCallback = mConnectionCallback.get();
    if (connectionCallback != null) connectionCallback.onServiceConnected(client);
}