FileDocCategorySizeDatePackage
DesktopAndroid.javaAPI DocAndroid 1.5 API4522Wed May 06 22:41:56 BST 2009android.webkit.gears

DesktopAndroid

public class DesktopAndroid extends Object
Utility class to create a shortcut on Android

Fields Summary
private static final String
TAG
private static final String
EXTRA_SHORTCUT_DUPLICATE
private static final String
ACTION_INSTALL_SHORTCUT
private static int
MAX_WIDTH
private static int
MAX_HEIGHT
Constructors Summary
Methods Summary
private static android.graphics.BitmapgetBitmap(java.lang.String path)
Small utility function returning a Bitmap object.

param
path the icon path


                 
       
    return BitmapFactory.decodeFile(path);
  
public static voidsetShortcut(android.webkit.WebView webview, java.lang.String title, java.lang.String url, java.lang.String imagePath)
Create a shortcut for a webpage.

To set a shortcut on Android, we use the ACTION_INSTALL_SHORTCUT from the default Home application. We only have to create an Intent containing extra parameters specifying the shortcut.

Note: the shortcut mechanism is not system wide and depends on the Home application; if phone carriers decide to rewrite a Home application that does not accept this Intent, no shortcut will be added.

param
webview the webview we are called from
param
title the shortcut's title
param
url the shortcut's url
param
imagePath the local path of the shortcut's icon

    Context context = webview.getContext();

    Intent viewWebPage = new Intent(Intent.ACTION_VIEW);
    viewWebPage.setData(Uri.parse(url));
    long urlHash = url.hashCode();
    long uniqueId = (urlHash << 32) | viewWebPage.hashCode();
    viewWebPage.putExtra(Browser.EXTRA_APPLICATION_ID,
            Long.toString(uniqueId));

    Intent intent = new Intent(ACTION_INSTALL_SHORTCUT);
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, viewWebPage);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);

    // We disallow the creation of duplicate shortcuts (i.e. same
    // url, same title, but different screen position).
    intent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);

    Bitmap bmp = getBitmap(imagePath);
    if (bmp != null) {
      if ((bmp.getWidth() > MAX_WIDTH) ||
          (bmp.getHeight() > MAX_HEIGHT)) {
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp,
            MAX_WIDTH, MAX_HEIGHT, true);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
      } else {
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bmp);
      }
    } else {
      // This should not happen as we just downloaded the icon
      Log.e(TAG, "icon file <" + imagePath + "> not found");
    }

    context.sendBroadcast(intent);