Search in sources :

Example 46 with CalledByNative

use of org.chromium.base.annotations.CalledByNative in project AndroidChromium by JackyAndroid.

the class ServiceTabLauncher method launchTab.

/**
     * Launches the browser activity and launches a tab for |url|.
     *
     * @param context The context using which the URL is being loaded.
     * @param requestId Id of the request for launching this tab.
     * @param incognito Whether the tab should be launched in incognito mode.
     * @param url The URL which should be launched in a tab.
     * @param disposition The disposition requested by the navigation source.
     * @param referrerUrl URL of the referrer which is opening the page.
     * @param referrerPolicy The referrer policy to consider when applying the referrer.
     * @param extraHeaders Extra headers to apply when requesting the tab's URL.
     * @param postData Post-data to include in the tab URL's request body.
     */
@CalledByNative
public static void launchTab(final Context context, final int requestId, final boolean incognito, final String url, final int disposition, final String referrerUrl, final int referrerPolicy, final String extraHeaders, final ResourceRequestBody postData) {
    final TabDelegate tabDelegate = new TabDelegate(incognito);
    // 1. Launch WebAPK if one matches the target URL.
    if (CommandLine.getInstance().hasSwitch(ChromeSwitches.ENABLE_WEBAPK)) {
        String webApkPackageName = WebApkValidator.queryWebApkPackage(context, url);
        if (webApkPackageName != null) {
            Intent intent = WebApkNavigationClient.createLaunchWebApkIntent(webApkPackageName, url);
            if (intent != null) {
                intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.NOTIFICATION);
                context.startActivity(intent);
                return;
            }
        }
    }
    // 2. Launch WebappActivity if one matches the target URL and was opened recently.
    // Otherwise, open the URL in a tab.
    FetchWebappDataStorageCallback callback = new FetchWebappDataStorageCallback() {

        @Override
        public void onWebappDataStorageRetrieved(final WebappDataStorage storage) {
            // been opened recently enough, open the URL in a tab.
            if (storage == null || !storage.wasLaunchedRecently()) {
                LoadUrlParams loadUrlParams = new LoadUrlParams(url, PageTransition.LINK);
                loadUrlParams.setPostData(postData);
                loadUrlParams.setVerbatimHeaders(extraHeaders);
                loadUrlParams.setReferrer(new Referrer(referrerUrl, referrerPolicy));
                AsyncTabCreationParams asyncParams = new AsyncTabCreationParams(loadUrlParams, requestId);
                tabDelegate.createNewTab(asyncParams, TabLaunchType.FROM_CHROME_UI, Tab.INVALID_TAB_ID);
            } else {
                // The URL is within the scope of a recently launched standalone-capable web app
                // on the home screen, so open it a standalone web app frame. An AsyncTask is
                // used because WebappDataStorage.createWebappLaunchIntent contains a Bitmap
                // decode operation and should not be run on the UI thread.
                //
                // This currently assumes that the only source is notifications; any future use
                // which adds a different source will need to change this.
                new AsyncTask<Void, Void, Intent>() {

                    @Override
                    protected final Intent doInBackground(Void... nothing) {
                        return storage.createWebappLaunchIntent();
                    }

                    @Override
                    protected final void onPostExecute(Intent intent) {
                        // Replace the web app URL with the URL from the notification. This is
                        // within the webapp's scope, so it is valid.
                        intent.putExtra(ShortcutHelper.EXTRA_URL, url);
                        intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.NOTIFICATION);
                        tabDelegate.createNewStandaloneFrame(intent);
                    }
                }.execute();
            }
        }
    };
    WebappRegistry.getWebappDataStorageForUrl(url, callback);
}
Also used : FetchWebappDataStorageCallback(org.chromium.chrome.browser.webapps.WebappRegistry.FetchWebappDataStorageCallback) Referrer(org.chromium.content_public.common.Referrer) Intent(android.content.Intent) TabDelegate(org.chromium.chrome.browser.tabmodel.document.TabDelegate) LoadUrlParams(org.chromium.content_public.browser.LoadUrlParams) AsyncTabCreationParams(org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams) WebappDataStorage(org.chromium.chrome.browser.webapps.WebappDataStorage) CalledByNative(org.chromium.base.annotations.CalledByNative)

Example 47 with CalledByNative

use of org.chromium.base.annotations.CalledByNative in project AndroidChromium by JackyAndroid.

the class ShortcutHelper method addShortcut.

/**
     * Adds home screen shortcut which opens in the browser Activity.
     */
@SuppressWarnings("unused")
@CalledByNative
private static void addShortcut(String url, String userTitle, Bitmap icon, int source) {
    Context context = ContextUtils.getApplicationContext();
    final Intent shortcutIntent = createShortcutIntent(url);
    shortcutIntent.putExtra(EXTRA_SOURCE, source);
    shortcutIntent.setPackage(context.getPackageName());
    sDelegate.sendBroadcast(context, createAddToHomeIntent(userTitle, icon, shortcutIntent));
    showAddedToHomescreenToast(userTitle);
}
Also used : Context(android.content.Context) Intent(android.content.Intent) CalledByNative(org.chromium.base.annotations.CalledByNative)

Example 48 with CalledByNative

use of org.chromium.base.annotations.CalledByNative in project AndroidChromium by JackyAndroid.

the class ShortcutHelper method createHomeScreenIconFromWebIcon.

/**
     * Adapts a website's icon (e.g. favicon or touch icon) to make it suitable for the home screen.
     * This involves adding padding if the icon is a full sized square.
     *
     * @param context Context used to create the intent.
     * @param webIcon The website's favicon or touch icon.
     * @return Bitmap Either the touch-icon or the newly created favicon.
     */
@CalledByNative
public static Bitmap createHomeScreenIconFromWebIcon(Bitmap webIcon) {
    // getLauncherLargeIconSize() is just a guess at the launcher icon size, and is often
    // wrong -- the launcher can show icons at any size it pleases. Instead of resizing the
    // icon to the supposed launcher size and then having the launcher resize the icon again,
    // just leave the icon at its original size and let the launcher do a single rescaling.
    // Unless the icon is much too big; then scale it down here too.
    Context context = ContextUtils.getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int maxInnerSize = Math.round(am.getLauncherLargeIconSize() * MAX_INNER_SIZE_RATIO);
    int innerSize = Math.min(maxInnerSize, Math.max(webIcon.getWidth(), webIcon.getHeight()));
    int padding = Math.round(ICON_PADDING_RATIO * innerSize);
    int outerSize = innerSize + 2 * padding;
    Bitmap bitmap = null;
    try {
        bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError e) {
        Log.w(TAG, "OutOfMemoryError while creating bitmap for home screen icon.");
        return webIcon;
    }
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);
    Rect innerBounds;
    // don't add padding.
    if (shouldPadIcon(webIcon)) {
        innerBounds = new Rect(padding, padding, outerSize - padding, outerSize - padding);
    } else {
        innerBounds = new Rect(0, 0, outerSize, outerSize);
    }
    canvas.drawBitmap(webIcon, null, innerBounds, paint);
    return bitmap;
}
Also used : Context(android.content.Context) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) ActivityManager(android.app.ActivityManager) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) CalledByNative(org.chromium.base.annotations.CalledByNative)

Example 49 with CalledByNative

use of org.chromium.base.annotations.CalledByNative in project AndroidChromium by JackyAndroid.

the class ShortcutHelper method generateHomeScreenIcon.

/**
     * Generates a generic icon to be used in the launcher. This is just a rounded rectangle with
     * a letter in the middle taken from the website's domain name.
     *
     * @param url URL of the shortcut.
     * @param red Red component of the dominant icon color.
     * @param green Green component of the dominant icon color.
     * @param blue Blue component of the dominant icon color.
     * @return Bitmap Either the touch-icon or the newly created favicon.
     */
@CalledByNative
public static Bitmap generateHomeScreenIcon(String url, int red, int green, int blue) {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    final int outerSize = am.getLauncherLargeIconSize();
    final int iconDensity = am.getLauncherLargeIconDensity();
    Bitmap bitmap = null;
    try {
        bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError e) {
        Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas.");
        return null;
    }
    Canvas canvas = new Canvas(bitmap);
    // Draw the drop shadow.
    int padding = (int) (GENERATED_ICON_PADDING_RATIO * outerSize);
    Rect outerBounds = new Rect(0, 0, outerSize, outerSize);
    Bitmap iconShadow = getBitmapFromResourceId(context, R.mipmap.shortcut_icon_shadow, iconDensity);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(iconShadow, null, outerBounds, paint);
    // Draw the rounded rectangle and letter.
    int innerSize = outerSize - 2 * padding;
    int cornerRadius = Math.round(ICON_CORNER_RADIUS_RATIO * outerSize);
    int fontSize = Math.round(GENERATED_ICON_FONT_SIZE_RATIO * outerSize);
    int color = Color.rgb(red, green, blue);
    RoundedIconGenerator generator = new RoundedIconGenerator(innerSize, innerSize, cornerRadius, color, fontSize);
    Bitmap icon = generator.generateIconForUrl(url);
    // Bookmark URL does not have a domain.
    if (icon == null)
        return null;
    canvas.drawBitmap(icon, padding, padding, null);
    return bitmap;
}
Also used : Context(android.content.Context) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) ActivityManager(android.app.ActivityManager) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) RoundedIconGenerator(org.chromium.chrome.browser.widget.RoundedIconGenerator) CalledByNative(org.chromium.base.annotations.CalledByNative)

Example 50 with CalledByNative

use of org.chromium.base.annotations.CalledByNative in project AndroidChromium by JackyAndroid.

the class ShortcutHelper method isIconLargeEnoughForLauncher.

/**
     * Returns whether the given icon matches the size requirements to be used on the home screen.
     * @param width Icon width, in pixels.
     * @param height Icon height, in pixels.
     * @return whether the given icon matches the size requirements to be used on the home screen.
     */
@CalledByNative
public static boolean isIconLargeEnoughForLauncher(int width, int height) {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    final int minimalSize = am.getLauncherLargeIconSize() / 2;
    return width >= minimalSize && height >= minimalSize;
}
Also used : Context(android.content.Context) ActivityManager(android.app.ActivityManager) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) CalledByNative(org.chromium.base.annotations.CalledByNative)

Aggregations

CalledByNative (org.chromium.base.annotations.CalledByNative)74 Activity (android.app.Activity)11 Context (android.content.Context)11 Intent (android.content.Intent)10 CastMediaRouteProvider (org.chromium.chrome.browser.media.router.cast.CastMediaRouteProvider)8 DownloadNotifier (org.chromium.chrome.browser.download.DownloadNotifier)6 View (android.view.View)5 TextView (android.widget.TextView)5 DownloadInfo (org.chromium.chrome.browser.download.DownloadInfo)5 SuppressLint (android.annotation.SuppressLint)4 PackageManager (android.content.pm.PackageManager)4 Bitmap (android.graphics.Bitmap)4 Paint (android.graphics.Paint)4 ScrollView (android.widget.ScrollView)4 ActivityManager (android.app.ActivityManager)3 ImageView (android.widget.ImageView)3 LinearLayout (android.widget.LinearLayout)3 VisibleForTesting (org.chromium.base.VisibleForTesting)3 Account (android.accounts.Account)2 ActivityNotFoundException (android.content.ActivityNotFoundException)2