blob: 0c88a50fd189ce40c0c834cc06985c34fff92daf [file] [log] [blame]
John Reck3527dd12011-02-22 10:35:29 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.app.ProgressDialog;
20import android.app.WallpaperManager;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.drawable.Drawable;
26import android.util.Log;
27import android.view.MenuItem;
28import android.view.MenuItem.OnMenuItemClickListener;
29
30import java.io.IOException;
31import java.io.InputStream;
32import java.net.MalformedURLException;
33import java.net.URL;
34
35/**
36 * Handle setWallpaper requests
37 *
38 */
39public class WallpaperHandler extends Thread
40 implements OnMenuItemClickListener, DialogInterface.OnCancelListener {
41
42
43 private static final String LOGTAG = "WallpaperHandler";
44
45 private Context mContext;
46 private URL mUrl;
47 private ProgressDialog mWallpaperProgress;
48 private boolean mCanceled = false;
49
50 public WallpaperHandler(Context context, String url) {
51 mContext = context;
52 try {
53 mUrl = new URL(url);
54 } catch (MalformedURLException e) {
55 mUrl = null;
56 }
57 }
58
59 @Override
60 public void onCancel(DialogInterface dialog) {
61 mCanceled = true;
62 }
63
64 @Override
65 public boolean onMenuItemClick(MenuItem item) {
66 if (mUrl != null) {
67 // The user may have tried to set a image with a large file size as
68 // their background so it may take a few moments to perform the
69 // operation.
70 // Display a progress spinner while it is working.
71 mWallpaperProgress = new ProgressDialog(mContext);
72 mWallpaperProgress.setIndeterminate(true);
73 mWallpaperProgress.setMessage(mContext.getResources()
74 .getText(R.string.progress_dialog_setting_wallpaper));
75 mWallpaperProgress.setCancelable(true);
76 mWallpaperProgress.setOnCancelListener(this);
77 mWallpaperProgress.show();
78 start();
79 }
80 return true;
81 }
82
83 @Override
84 public void run() {
85 Drawable oldWallpaper =
86 WallpaperManager.getInstance(mContext).getDrawable();
87 try {
88 // TODO: This will cause the resource to be downloaded again, when
89 // we should in most cases be able to grab it from the cache. To fix
90 // this we should query WebCore to see if we can access a cached
91 // version and instead open an input stream on that. This pattern
92 // could also be used in the download manager where the same problem
93 // exists.
94 InputStream inputstream = mUrl.openStream();
95 if (inputstream != null) {
96 WallpaperManager.getInstance(mContext).setStream(inputstream);
97 }
98 } catch (IOException e) {
99 Log.e(LOGTAG, "Unable to set new wallpaper");
100 // Act as though the user canceled the operation so we try to
101 // restore the old wallpaper.
102 mCanceled = true;
103 }
104
105 if (mCanceled) {
106 // Restore the old wallpaper if the user cancelled whilst we were
107 // setting
108 // the new wallpaper.
109 int width = oldWallpaper.getIntrinsicWidth();
110 int height = oldWallpaper.getIntrinsicHeight();
111 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
112 Canvas canvas = new Canvas(bm);
113 oldWallpaper.setBounds(0, 0, width, height);
114 oldWallpaper.draw(canvas);
115 try {
116 WallpaperManager.getInstance(mContext).setBitmap(bm);
117 } catch (IOException e) {
118 Log.e(LOGTAG, "Unable to restore old wallpaper.");
119 }
120 mCanceled = false;
121 }
122
123 if (mWallpaperProgress.isShowing()) {
124 mWallpaperProgress.dismiss();
125 }
126 }
127}