org.chromium.webapk.lib.common.WebApkConstants Java Examples

The following examples show how to use org.chromium.webapk.lib.common.WebApkConstants. 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: WebappInfo.java    From delion with Apache License 2.0 7 votes vote down vote up
public static int displayModeFromIntent(Intent intent) {
    String displayMode =
            IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEBAPK_DISPLAY_MODE);
    if (displayMode == null) {
        return IntentUtils.safeGetIntExtra(
                intent, ShortcutHelper.EXTRA_DISPLAY_MODE, WebDisplayMode.Standalone);
    }

    // {@link displayMode} should be one of
    // https://w3c.github.io/manifest/#dfn-display-modes-values
    if (displayMode.equals("fullscreen")) {
        return WebDisplayMode.Fullscreen;
    } else if (displayMode.equals("minimal-ui")) {
        return WebDisplayMode.MinimalUi;
    } else if (displayMode.equals("browser")) {
        return WebDisplayMode.Browser;
    } else {
        return WebDisplayMode.Standalone;
    }
}
 
Example #2
Source File: WebApkMetaDataUtils.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Populates {@link WebappInfo} with meta data extracted from WebAPK's Android Manifest.
 * @param webApkPackageName Package name of the WebAPK to extract meta data from.
 * @param url WebAPK start URL.
 * @param source {@link ShortcutSource} that the WebAPK was opened from.
 */
public static WebApkInfo extractWebappInfoFromWebApk(
        String webApkPackageName, String url, int source) {
    WebApkMetaData metaData = extractMetaDataFromWebApk(webApkPackageName);
    if (metaData == null) return null;

    PackageManager packageManager = ContextUtils.getApplicationContext().getPackageManager();
    Resources resources;
    try {
        resources = packageManager.getResourcesForApplication(webApkPackageName);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }
    Bitmap icon = BitmapFactory.decodeResource(resources, metaData.iconId);
    String encodedIcon = ShortcutHelper.encodeBitmapAsString(icon);

    return WebApkInfo.create(WebApkConstants.WEBAPK_ID_PREFIX + webApkPackageName, url,
            metaData.scope, encodedIcon, metaData.name, metaData.shortName,
            metaData.displayMode, metaData.orientation, source, metaData.themeColor,
            metaData.backgroundColor, TextUtils.isEmpty(metaData.iconUrl), webApkPackageName);
}
 
Example #3
Source File: WebappLauncherActivity.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the WebAPK package specified in the intent is a valid WebAPK and whether the
 * url specified in the intent can be fulfilled by the WebAPK.
 *
 * @param intent The intent
 * @return true iff all validation criteria are met.
 */
private boolean isValidWebApk(Intent intent) {
    if (!ChromeWebApkHost.isEnabled()) return false;

    String webApkPackage =
            IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME);
    if (TextUtils.isEmpty(webApkPackage)) return false;

    String url = IntentUtils.safeGetStringExtra(intent, ShortcutHelper.EXTRA_URL);
    if (TextUtils.isEmpty(url)) return false;

    if (!webApkPackage.equals(WebApkValidator.queryWebApkPackage(this, url))) {
        Log.d(TAG, "%s is not within scope of %s WebAPK", url, webApkPackage);
        return false;
    }
    return true;
}
 
Example #4
Source File: WebApkInfo.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a WebApkInfo from the passed in Intent and <meta-data> in the WebAPK's Android
 * manifest.
 * @param intent Intent containing info about the app.
 */
public static WebApkInfo create(Intent intent) {
    String webApkPackageName =
            IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME);
    if (TextUtils.isEmpty(webApkPackageName)) {
        return null;
    }

    String url = urlFromIntent(intent);
    int source = sourceFromIntent(intent);

    // Force navigation if the extra is not specified to avoid breaking deep linking for old
    // WebAPKs which don't specify the {@link WebApkConstants#EXTRA_WEBAPK_FORCE_NAVIGATION}
    // intent extra.
    boolean forceNavigation = IntentUtils.safeGetBooleanExtra(
            intent, WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, true);

    return create(webApkPackageName, url, source, forceNavigation);
}
 
Example #5
Source File: WebappInfo.java    From delion with Apache License 2.0 5 votes vote down vote up
public static int orientationFromIntent(Intent intent) {
    String orientation =
            IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEBAPK_ORIENTATION);
    if (orientation == null) {
        return IntentUtils.safeGetIntExtra(
                intent, ShortcutHelper.EXTRA_ORIENTATION, ScreenOrientationValues.DEFAULT);
    }

    // {@link orientation} should be one of
    // w3c.github.io/screen-orientation/#orientationlocktype-enum
    if (orientation.equals("any")) {
        return ScreenOrientationValues.ANY;
    } else if (orientation.equals("natural")) {
        return ScreenOrientationValues.NATURAL;
    } else if (orientation.equals("landscape")) {
        return ScreenOrientationValues.LANDSCAPE;
    } else if (orientation.equals("landscape-primary")) {
        return ScreenOrientationValues.LANDSCAPE_PRIMARY;
    } else if (orientation.equals("landscape-secondary")) {
        return ScreenOrientationValues.LANDSCAPE_SECONDARY;
    } else if (orientation.equals("portrait")) {
        return ScreenOrientationValues.PORTRAIT;
    } else if (orientation.equals("portrait-primary")) {
        return ScreenOrientationValues.PORTRAIT_PRIMARY;
    } else if (orientation.equals("portrait-secondary")) {
        return ScreenOrientationValues.PORTRAIT_SECONDARY;
    } else {
        return ScreenOrientationValues.DEFAULT;
    }
}
 
Example #6
Source File: WebApkNavigationClient.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Creates intent to launch a WebAPK.
 * @param webApkPackageName Package name of the WebAPK to launch.
 * @param url URL to navigate WebAPK to.
 * @param forceNavigation Whether the WebAPK should be navigated to the url if the WebAPK is
 *        already open. If {@link forceNavigation} is false and the WebAPK is already running,
 *        the WebAPK will be brought to the foreground.
 * @return The intent.
 */
public static Intent createLaunchWebApkIntent(
        String webApkPackageName, String url, boolean forceNavigation) {
    Intent intent;
    try {
        intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
    } catch (Exception e) {
        return null;
    }

    intent.setPackage(webApkPackageName);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, forceNavigation);
    return intent;
}
 
Example #7
Source File: WebApkUma.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** Records the current Shell APK version. */
public static void recordShellApkVersion(int shellApkVersion, String packageName) {
    String name = packageName.startsWith(WebApkConstants.WEBAPK_PACKAGE_PREFIX)
            ? "WebApk.ShellApkVersion.BrowserApk"
            : "WebApk.ShellApkVersion.UnboundApk";
    RecordHistogram.recordSparseSlowlyHistogram(name, shellApkVersion);
}
 
Example #8
Source File: WebApkInstaller.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Installs a WebAPK and monitors the installation.
 * @param packageName The package name of the WebAPK to install.
 * @param version The version of WebAPK to install.
 * @param title The title of the WebAPK to display during installation.
 * @param token The token from WebAPK Server.
 * @param url The start URL of the WebAPK to install.
 */
@CalledByNative
private void installWebApkAsync(final String packageName, int version, String title,
        String token, String url, final int source) {
    // Check whether the WebAPK package is already installed. The WebAPK may have been installed
    // by another Chrome version (e.g. Chrome Dev). We have to do this check because the Play
    // install API fails silently if the package is already installed.
    if (isWebApkInstalled(packageName)) {
        notify(WebApkInstallResult.SUCCESS);
        return;
    }

    if (mInstallDelegate == null) {
        notify(WebApkInstallResult.FAILURE);
        WebApkUma.recordGooglePlayInstallResult(
                WebApkUma.GOOGLE_PLAY_INSTALL_FAILED_NO_DELEGATE);
        return;
    }

    Callback<Integer> callback = new Callback<Integer>() {
        @Override
        public void onResult(Integer result) {
            WebApkInstaller.this.notify(result);
            if (result == WebApkInstallResult.FAILURE) return;

            // Stores the source info of WebAPK in WebappDataStorage.
            WebappRegistry.getInstance().register(
                    WebApkConstants.WEBAPK_ID_PREFIX + packageName,
                    new WebappRegistry.FetchWebappDataStorageCallback() {
                        @Override
                        public void onWebappDataStorageRetrieved(WebappDataStorage storage) {
                            storage.updateSource(source);
                            storage.updateTimeOfLastCheckForUpdatedWebManifest();
                        }
                    });
        }
    };
    mInstallDelegate.installAsync(packageName, version, title, token, url, callback);
}
 
Example #9
Source File: WebApkInfo.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void setWebappIntentExtras(Intent intent) {
    // For launching a {@link WebApkActivity}.
    intent.putExtra(ShortcutHelper.EXTRA_URL, uri().toString());
    intent.putExtra(ShortcutHelper.EXTRA_SOURCE, source());
    intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackageName());
    intent.putExtra(WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, mForceNavigation);
}
 
Example #10
Source File: WebappDataStorage.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the data stored in this object to match that in the supplied intent.
 * @param shortcutIntent The intent to pull web app data from.
 */
public void updateFromShortcutIntent(Intent shortcutIntent) {
    if (shortcutIntent == null) return;

    SharedPreferences.Editor editor = mPreferences.edit();
    boolean updated = false;

    // The URL and scope may have been deleted by the user clearing their history. Check whether
    // they are present, and update if necessary.
    String url = mPreferences.getString(KEY_URL, URL_INVALID);
    if (url.equals(URL_INVALID)) {
        url = IntentUtils.safeGetStringExtra(shortcutIntent, ShortcutHelper.EXTRA_URL);
        editor.putString(KEY_URL, url);
        updated = true;
    }

    if (mPreferences.getString(KEY_SCOPE, URL_INVALID).equals(URL_INVALID)) {
        String scope = IntentUtils.safeGetStringExtra(
                shortcutIntent, ShortcutHelper.EXTRA_SCOPE);
        if (scope == null) {
            scope = ShortcutHelper.getScopeFromUrl(url);
        }
        editor.putString(KEY_SCOPE, scope);
        updated = true;
    }

    // For all other fields, assume that if the version key is present and equal to
    // ShortcutHelper.WEBAPP_SHORTCUT_VERSION, then all fields are present and do not need to be
    // updated. All fields except for the last used time, scope, and URL are either set or
    // cleared together.
    if (mPreferences.getInt(KEY_VERSION, VERSION_INVALID)
            != ShortcutHelper.WEBAPP_SHORTCUT_VERSION) {
        editor.putString(KEY_NAME, IntentUtils.safeGetStringExtra(
                    shortcutIntent, ShortcutHelper.EXTRA_NAME));
        editor.putString(KEY_SHORT_NAME, IntentUtils.safeGetStringExtra(
                    shortcutIntent, ShortcutHelper.EXTRA_SHORT_NAME));
        editor.putString(KEY_ICON, IntentUtils.safeGetStringExtra(
                    shortcutIntent, ShortcutHelper.EXTRA_ICON));
        editor.putInt(KEY_VERSION, ShortcutHelper.WEBAPP_SHORTCUT_VERSION);

        // "Standalone" was the original assumed default for all web apps.
        editor.putInt(KEY_DISPLAY_MODE,
                IntentUtils.safeGetIntExtra(shortcutIntent, ShortcutHelper.EXTRA_DISPLAY_MODE,
                        WebDisplayMode.STANDALONE));
        editor.putInt(KEY_ORIENTATION, IntentUtils.safeGetIntExtra(
                    shortcutIntent, ShortcutHelper.EXTRA_ORIENTATION,
                    ScreenOrientationValues.DEFAULT));
        editor.putLong(KEY_THEME_COLOR, IntentUtils.safeGetLongExtra(
                    shortcutIntent, ShortcutHelper.EXTRA_THEME_COLOR,
                    ShortcutHelper.MANIFEST_COLOR_INVALID_OR_MISSING));
        editor.putLong(KEY_BACKGROUND_COLOR, IntentUtils.safeGetLongExtra(
                    shortcutIntent, ShortcutHelper.EXTRA_BACKGROUND_COLOR,
                    ShortcutHelper.MANIFEST_COLOR_INVALID_OR_MISSING));
        editor.putBoolean(KEY_IS_ICON_GENERATED, IntentUtils.safeGetBooleanExtra(
                    shortcutIntent, ShortcutHelper.EXTRA_IS_ICON_GENERATED, false));
        editor.putString(KEY_ACTION, shortcutIntent.getAction());

        String webApkPackageName = IntentUtils.safeGetStringExtra(
                shortcutIntent, WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME);
        editor.putString(KEY_WEBAPK_PACKAGE_NAME, webApkPackageName);

        if (TextUtils.isEmpty(webApkPackageName)) {
            editor.putInt(KEY_SOURCE,
                    IntentUtils.safeGetIntExtra(shortcutIntent, ShortcutHelper.EXTRA_SOURCE,
                            ShortcutSource.UNKNOWN));
        }
        updated = true;
    }
    if (updated) editor.apply();
}
 
Example #11
Source File: WebApkInfo.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a WebApkInfo from the passed in parameters and <meta-data> in the WebAPK's Android
 * manifest.
 *
 * @param webApkPackageName The package name of the WebAPK.
 * @param url Url that the WebAPK should navigate to when launched.
 * @param source Source that the WebAPK was launched from.
 * @param forceNavigation Whether the WebAPK should navigate to {@link url} if it is already
 *     running.
 */
public static WebApkInfo create(
        String webApkPackageName, String url, int source, boolean forceNavigation) {
    // Unlike non-WebAPK web apps, WebAPK ids are predictable. A malicious actor may send an
    // intent with a valid start URL and arbitrary other data. Only use the start URL, the
    // package name and the ShortcutSource from the launch intent and extract the remaining data
    // from the <meta-data> in the WebAPK's Android manifest.

    Bundle bundle = extractWebApkMetaData(webApkPackageName);
    if (bundle == null) {
        return null;
    }

    String name = IntentUtils.safeGetString(bundle, WebApkMetaDataKeys.NAME);
    String shortName = IntentUtils.safeGetString(bundle, WebApkMetaDataKeys.SHORT_NAME);
    String scope = IntentUtils.safeGetString(bundle, WebApkMetaDataKeys.SCOPE);

    int displayMode = displayModeFromString(
            IntentUtils.safeGetString(bundle, WebApkMetaDataKeys.DISPLAY_MODE));
    int orientation = orientationFromString(
            IntentUtils.safeGetString(bundle, WebApkMetaDataKeys.ORIENTATION));
    long themeColor = getLongFromMetaData(bundle, WebApkMetaDataKeys.THEME_COLOR,
            ShortcutHelper.MANIFEST_COLOR_INVALID_OR_MISSING);
    long backgroundColor = getLongFromMetaData(bundle, WebApkMetaDataKeys.BACKGROUND_COLOR,
            ShortcutHelper.MANIFEST_COLOR_INVALID_OR_MISSING);

    int shellApkVersion =
            IntentUtils.safeGetInt(bundle, WebApkMetaDataKeys.SHELL_APK_VERSION, 0);

    String manifestUrl = IntentUtils.safeGetString(bundle, WebApkMetaDataKeys.WEB_MANIFEST_URL);
    String manifestStartUrl = IntentUtils.safeGetString(bundle, WebApkMetaDataKeys.START_URL);
    Map<String, String> iconUrlToMurmur2HashMap = getIconUrlAndIconMurmur2HashMap(bundle);

    int iconId = IntentUtils.safeGetInt(bundle, WebApkMetaDataKeys.ICON_ID, 0);
    Bitmap icon = decodeImageResource(webApkPackageName, iconId);

    return create(WebApkConstants.WEBAPK_ID_PREFIX + webApkPackageName, url, forceNavigation,
            scope, new Icon(icon), name, shortName, displayMode, orientation, source,
            themeColor, backgroundColor, webApkPackageName, shellApkVersion, manifestUrl,
            manifestStartUrl, iconUrlToMurmur2HashMap);
}
 
Example #12
Source File: WebApkManagedActivity.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
protected String getActivityId() {
    return WebApkConstants.WEBAPK_ID_PREFIX + String.valueOf(mActivityIndex);
}