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.
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);