android.support.customtabs.CustomTabsService Java Examples

The following examples show how to use android.support.customtabs.CustomTabsService. 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: CustomTabsConnection.java    From delion with Apache License 2.0 6 votes vote down vote up
private boolean preconnectUrls(List<Bundle> likelyBundles) {
    boolean atLeastOneUrl = false;
    if (likelyBundles == null) return false;
    WarmupManager warmupManager = WarmupManager.getInstance();
    Profile profile = Profile.getLastUsedProfile();
    for (Bundle bundle : likelyBundles) {
        Uri uri;
        try {
            uri = IntentUtils.safeGetParcelable(bundle, CustomTabsService.KEY_URL);
        } catch (ClassCastException e) {
            continue;
        }
        String url = checkAndConvertUri(uri);
        if (url != null) {
            warmupManager.maybePreconnectUrlAndSubResources(profile, url);
            atLeastOneUrl = true;
        }
    }
    return atLeastOneUrl;
}
 
Example #2
Source File: PostMessageHandler.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Relay a postMessage request through the current channel assigned to this session.
 * @param message The message to be sent.
 * @return The result of the postMessage request. Returning true means the request was accepted,
 *         not necessarily that the postMessage was successful.
 */
public int postMessage(final String message) {
    if (mChannel == null || !mChannel[0].isReady() || mChannel[0].isClosed()) {
        return CustomTabsService.RESULT_FAILURE_MESSAGING_ERROR;
    }
    ThreadUtils.postOnUiThread(new Runnable() {
        @Override
        public void run() {
            // It is still possible that the page has navigated while this task is in the queue.
            // If that happens fail gracefully.
            if (mChannel == null || mChannel[0].isClosed()) return;
            mChannel[0].postMessage(message, null);
        }
    });
    return CustomTabsService.RESULT_SUCCESS;
}
 
Example #3
Source File: CustomTabsConnection.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private boolean preconnectUrls(List<Bundle> likelyBundles) {
    boolean atLeastOneUrl = false;
    if (likelyBundles == null) return false;
    WarmupManager warmupManager = WarmupManager.getInstance();
    Profile profile = Profile.getLastUsedProfile();
    for (Bundle bundle : likelyBundles) {
        Uri uri;
        try {
            uri = IntentUtils.safeGetParcelable(bundle, CustomTabsService.KEY_URL);
        } catch (ClassCastException e) {
            continue;
        }
        String url = checkAndConvertUri(uri);
        if (url != null) {
            warmupManager.maybePreconnectUrlAndSubResources(profile, url);
            atLeastOneUrl = true;
        }
    }
    return atLeastOneUrl;
}
 
Example #4
Source File: CustomTabsConnection.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private boolean preconnectUrls(List<Bundle> likelyBundles) {
    boolean atLeastOneUrl = false;
    if (likelyBundles == null) return false;
    WarmupManager warmupManager = WarmupManager.getInstance();
    Profile profile = Profile.getLastUsedProfile();
    for (Bundle bundle : likelyBundles) {
        Uri uri;
        try {
            uri = IntentUtils.safeGetParcelable(bundle, CustomTabsService.KEY_URL);
        } catch (ClassCastException e) {
            continue;
        }
        String url = checkAndConvertUri(uri);
        if (url != null) {
            warmupManager.maybePreconnectUrlAndSubResources(profile, url);
            atLeastOneUrl = true;
        }
    }
    return atLeastOneUrl;
}
 
Example #5
Source File: PostMessageHandler.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Relay a postMessage request through the current channel assigned to this session.
 * @param message The message to be sent.
 * @return The result of the postMessage request. Returning true means the request was accepted,
 *         not necessarily that the postMessage was successful.
 */
public int postMessageFromClientApp(final String message) {
    if (mChannel == null || !mChannel[0].isReady() || mChannel[0].isClosed()) {
        return CustomTabsService.RESULT_FAILURE_MESSAGING_ERROR;
    }
    if (mWebContents == null || mWebContents.isDestroyed()) {
        return CustomTabsService.RESULT_FAILURE_MESSAGING_ERROR;
    }
    ThreadUtils.postOnUiThread(new Runnable() {
        @Override
        public void run() {
            // It is still possible that the page has navigated while this task is in the queue.
            // If that happens fail gracefully.
            if (mChannel == null || mChannel[0].isClosed()) return;
            mChannel[0].postMessage(message, null);
        }
    });
    return CustomTabsService.RESULT_SUCCESS;
}
 
Example #6
Source File: RobolectricUtils.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures intents with {@link CustomTabsService#ACTION_CUSTOM_TABS_CONNECTION} resolve to a
 * Service with given categories
 */
public static void installCustomTabsService(String providerPackage, List<String> categories) {
    Intent intent = new Intent()
            .setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION)
            .setPackage(providerPackage);

    IntentFilter filter = new IntentFilter();
    for (String category : categories) {
        filter.addCategory(category);
    }
    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.filter = filter;

    ShadowPackageManager manager = Shadows.shadowOf(RuntimeEnvironment.application
            .getPackageManager());
    manager.addResolveInfoForIntent(intent, resolveInfo);
}
 
Example #7
Source File: TwaProviderPicker.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
/** Returns a map from package name to LaunchMode for all available Custom Tabs Services. */
private static Map<String, Integer> getLaunchModesForCustomTabsServices(PackageManager pm) {
    List<ResolveInfo> services = pm.queryIntentServices(
            new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION),
            PackageManager.GET_RESOLVED_FILTER);

    Map<String, Integer> customTabsServices = new HashMap<>();
    for (ResolveInfo service : services) {
        String packageName = service.serviceInfo.packageName;

        if (TrustedWebUtils.SUPPORTED_CHROME_PACKAGES.contains(packageName) &&
                !TrustedWebUtils.chromeNeedsUpdate(pm, packageName)) {
            // Chrome 72-74 support Trusted Web Activites but don't yet have the TWA category on
            // their CustomTabsService.
            customTabsServices.put(packageName, LaunchMode.TRUSTED_WEB_ACTIVITY);
            continue;
        }

        boolean supportsTwas = service.filter != null &&
                service.filter.hasCategory(TRUSTED_WEB_ACTIVITY_CATEGORY);

        customTabsServices.put(packageName,
                supportsTwas ? LaunchMode.TRUSTED_WEB_ACTIVITY : LaunchMode.CUSTOM_TAB);
    }
    return customTabsServices;
}
 
Example #8
Source File: CustomTabsConnection.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public int postMessage(CustomTabsSessionToken session, String message, Bundle extras) {
    int result;
    if (!mWarmupHasBeenCalled.get()) result = CustomTabsService.RESULT_FAILURE_DISALLOWED;
    if (!isCallerForegroundOrSelf() && !CustomTabActivity.isActiveSession(session)) {
        result = CustomTabsService.RESULT_FAILURE_DISALLOWED;
    }
    // If called before a validatePostMessageOrigin, the post message origin will be invalid and
    // will return a failure result here.
    result = mClientManager.postMessage(session, message);
    logCall("postMessage", result);
    return result;
}
 
Example #9
Source File: CustomTabsConnection.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
public int postMessage(CustomTabsSessionToken session, String message, Bundle extras) {
    int result;
    if (!mWarmupHasBeenCalled.get()) result = CustomTabsService.RESULT_FAILURE_DISALLOWED;
    if (!isCallerForegroundOrSelf() && !CustomTabActivity.isActiveSession(session)) {
        result = CustomTabsService.RESULT_FAILURE_DISALLOWED;
    }
    // If called before a validatePostMessageOrigin, the post message origin will be invalid and
    // will return a failure result here.
    result = mClientManager.postMessage(session, message);
    logCall("postMessage", result);
    return result;
}
 
Example #10
Source File: TwaProviderPickerTest.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
private void installCustomTabsProvider(String packageName) {
    installBrowser(packageName);

    Intent intent = new Intent()
            .setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);

    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.serviceInfo = new ServiceInfo();
    resolveInfo.serviceInfo.packageName = packageName;

    mShadowPackageManager.addResolveInfoForIntent(intent, resolveInfo);
}
 
Example #11
Source File: TwaProviderPickerTest.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
private void installTrustedWebActivityProvider(String packageName) {
    installBrowser(packageName);

    Intent intent = new Intent()
            .setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);

    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.serviceInfo = new ServiceInfo();
    resolveInfo.serviceInfo.packageName = packageName;
    resolveInfo.filter = Mockito.mock(IntentFilter.class);
    when(resolveInfo.filter.hasCategory(eq(TRUSTED_WEB_ACTIVITY_CATEGORY))).thenReturn(true);

    mShadowPackageManager.addResolveInfoForIntent(intent, resolveInfo);
}
 
Example #12
Source File: ChromeTabsDelegate.java    From SteamGifts with MIT License 4 votes vote down vote up
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!android.text.TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    }
    return sPackageNameToUse;
}
 
Example #13
Source File: CustomTabsConnectionService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
protected int postMessage(CustomTabsSessionToken sessionToken, String message,
        Bundle extras) {
    if (!isFirstRunDone()) return CustomTabsService.RESULT_FAILURE_DISALLOWED;
    return mConnection.postMessage(sessionToken, message, extras);
}
 
Example #14
Source File: ClientManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public synchronized int postMessage(CustomTabsSessionToken session, String message) {
    SessionParams params = mSessionParams.get(session);
    if (params == null) return CustomTabsService.RESULT_FAILURE_MESSAGING_ERROR;
    return params.postMessageHandler.postMessageFromClientApp(message);
}
 
Example #15
Source File: ChromeCustomTabsHelper.java    From Hews with MIT License 4 votes vote down vote up
/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.
 * <p/>
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
        && !hasSpecializedHandlerIntents(context, activityIntent)
        && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}
 
Example #16
Source File: CustomTabsHelper.java    From droidddle with Apache License 2.0 4 votes vote down vote up
/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.
 * <p>
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}
 
Example #17
Source File: CustomTabsHelper.java    From android-proguards with Apache License 2.0 4 votes vote down vote up
/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}
 
Example #18
Source File: CustomTabsHelper.java    From MaterialHome with Apache License 2.0 4 votes vote down vote up
/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}
 
Example #19
Source File: CustomTabsConnectionService.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
protected int postMessage(CustomTabsSessionToken sessionToken, String message,
        Bundle extras) {
    if (!isFirstRunDone()) return CustomTabsService.RESULT_FAILURE_DISALLOWED;
    return mConnection.postMessage(sessionToken, message, extras);
}
 
Example #20
Source File: ClientManager.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
public synchronized int postMessage(CustomTabsSessionToken session, String message) {
    SessionParams params = mSessionParams.get(session);
    if (params == null) return CustomTabsService.RESULT_FAILURE_MESSAGING_ERROR;
    return params.postMessageHandler.postMessage(message);
}
 
Example #21
Source File: CustomTabsHelper.java    From materialup with Apache License 2.0 4 votes vote down vote up
/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.
 * <p>
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}
 
Example #22
Source File: ChromePackageHelper.java    From ForceDoze with GNU General Public License v3.0 4 votes vote down vote up
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();

    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}