From 4f75ba226fb7fd1cf62caf807123dee8f5e65b13 Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Tue, 27 Oct 2009 11:48:28 +0000 Subject: Add an option to the long press on image menu to save that image as the home screen wallpaper. See b/2210111 Change-Id: I911633772358504f0e82fe940f9f2efc4ef403de --- AndroidManifest.xml | 1 + res/menu/browsercontext.xml | 2 + res/values/strings.xml | 6 +++ src/com/android/browser/BrowserActivity.java | 80 ++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 3d970ab..c5324cd 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -27,6 +27,7 @@ + diff --git a/res/menu/browsercontext.xml b/res/menu/browsercontext.xml index 8b2678e..70cf8d4 100644 --- a/res/menu/browsercontext.xml +++ b/res/menu/browsercontext.xml @@ -54,6 +54,8 @@ android:title="@string/contextmenu_download_image"/> + diff --git a/res/values/strings.xml b/res/values/strings.xml index cc9857a..01f728a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -266,6 +266,9 @@ Save image View image + + Set as wallpaper + Dial\u2026 @@ -767,6 +770,9 @@ Overview + + Setting wallpaper... + Show JavaScript console JavaScript console Evaluate JavaScript diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java index c9d5393..7b14528 100644 --- a/src/com/android/browser/BrowserActivity.java +++ b/src/com/android/browser/BrowserActivity.java @@ -109,6 +109,8 @@ import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -1681,6 +1683,8 @@ public class BrowserActivity extends Activity new Intent(Intent.ACTION_VIEW, Uri.parse(extra))); menu.findItem(R.id.download_context_menu_id). setOnMenuItemClickListener(new Download(extra)); + menu.findItem(R.id.set_wallpaper_context_menu_id). + setOnMenuItemClickListener(new SetAsWallpaper(extra)); break; default: @@ -1826,6 +1830,82 @@ public class BrowserActivity extends Activity } } + private class SetAsWallpaper extends Thread implements + OnMenuItemClickListener, DialogInterface.OnCancelListener { + private URL mUrl; + private ProgressDialog mWallpaperProgress; + private boolean mCanceled = false; + + public SetAsWallpaper(String url) { + try { + mUrl = new URL(url); + } catch (MalformedURLException e) { + mUrl = null; + } + } + + public void onCancel(DialogInterface dialog) { + mCanceled = true; + } + + public boolean onMenuItemClick(MenuItem item) { + if (mUrl != null) { + // The user may have tried to set a image with a large file size as their + // background so it may take a few moments to perform the operation. Display + // a progress spinner while it is working. + mWallpaperProgress = new ProgressDialog(BrowserActivity.this); + mWallpaperProgress.setIndeterminate(true); + mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper)); + mWallpaperProgress.setCancelable(true); + mWallpaperProgress.setOnCancelListener(this); + mWallpaperProgress.show(); + start(); + } + return true; + } + + public void run() { + Drawable oldWallpaper = BrowserActivity.this.getWallpaper(); + try { + // TODO: This will cause the resource to be downloaded again, when we + // should in most cases be able to grab it from the cache. To fix this + // we should query WebCore to see if we can access a cached version and + // instead open an input stream on that. This pattern could also be used + // in the download manager where the same problem exists. + InputStream inputstream = mUrl.openStream(); + if (inputstream != null) { + setWallpaper(inputstream); + } + } catch (IOException e) { + Log.e(LOGTAG, "Unable to set new wallpaper"); + // Act as though the user canceled the operation so we try to + // restore the old wallpaper. + mCanceled = true; + } + + if (mCanceled) { + // Restore the old wallpaper if the user cancelled whilst we were setting + // the new wallpaper. + int width = oldWallpaper.getIntrinsicWidth(); + int height = oldWallpaper.getIntrinsicHeight(); + Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); + Canvas canvas = new Canvas(bm); + oldWallpaper.setBounds(0, 0, width, height); + oldWallpaper.draw(canvas); + try { + setWallpaper(bm); + } catch (IOException e) { + Log.e(LOGTAG, "Unable to restore old wallpaper."); + } + mCanceled = false; + } + + if (mWallpaperProgress.isShowing()) { + mWallpaperProgress.dismiss(); + } + } + } + private void copy(CharSequence text) { try { IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard")); -- cgit v1.1