blob: c930dc8629bb30bbb658605e98a7eed1eea42cad [file] [log] [blame]
The Android Open Source Projected217d92008-12-17 18:05:52 -08001/*
2 * Copyright (C) 2008 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.Activity;
20import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24import android.os.Handler;
25import android.util.Log;
26import android.view.InflateException;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.Button;
31import android.widget.ImageView;
32import android.widget.TextView;
33
34import java.io.InputStream;
35import java.io.IOException;
36import java.lang.ClassCastException;
37import java.net.HttpURLConnection;
38import java.net.MalformedURLException;
39import java.net.URL;
40
41import org.json.JSONException;
42import org.json.JSONObject;
43
44/**
45 * Base dialog class for gears
46 */
47class GearsBaseDialog {
48
49 private static final String TAG = "GearsNativeDialog";
50 protected Handler mHandler;
51 protected Activity mActivity;
52 protected String mDialogArguments;
53
54 private Bitmap mIcon;
55 private final int MAX_ICON_SIZE = 64;
56 protected int mChoosenIconSize;
57
58 // Dialog closing types
59 public static final int CANCEL = 0;
60 public static final int ALWAYS_DENY = 1;
61 public static final int ALLOW = 2;
62 public static final int DENY = 3;
63 public static final int NEW_ICON = 4;
64 public static final int UPDATE_ICON = 5;
65 public static final int REQUEST_ICON = 6;
66 public static final int PAUSE_REQUEST_ICON = 7;
67
68 protected final String LOCAL_DATA_STRING = "localData";
69 protected final String LOCAL_STORAGE_STRING = "localStorage";
70 protected final String LOCATION_DATA_STRING = "locationData";
71
72 protected String mGearsVersion = "UNDEFINED";
73 protected boolean mDebug = false;
74
75 public GearsBaseDialog(Activity activity, Handler handler, String arguments) {
76 mActivity = activity;
77 mHandler = handler;
78 mDialogArguments = arguments;
79 }
80
81 Resources getResources() {
82 return mActivity.getResources();
83 }
84
85 Object getSystemService(String name) {
86 return mActivity.getSystemService(name);
87 }
88
89 View findViewById(int id) {
90 return mActivity.findViewById(id);
91 }
92
93 private String getString(int id) {
94 return mActivity.getString(id);
95 }
96
97 public void setDebug(boolean debug) {
98 mDebug = debug;
99 }
100
101 public void setGearsVersion(String version) {
102 mGearsVersion = version;
103 }
104
105 public String closeDialog(int closingType) {
106 return null;
107 }
108
109 /*
110 * Utility methods for setting up the dialogs elements
111 */
112
113 /**
114 * Inflate a given layout in a view (which has to be
115 * a ViewGroup, e.g. LinearLayout).
116 * This is used to share the basic dialog outline among
117 * the different dialog types.
118 */
119 void inflate(int layout, int viewID) {
120 LayoutInflater inflater = (LayoutInflater) getSystemService(
121 Context.LAYOUT_INFLATER_SERVICE);
122 View view = findViewById(viewID);
123 if (view != null) {
124 try {
125 ViewGroup viewGroup = (ViewGroup) view;
126 inflater.inflate(layout, viewGroup);
127 } catch (ClassCastException e) {
128 String msg = "exception, the view (" + view + ")";
129 msg += " is not a ViewGroup";
130 Log.e(TAG, msg, e);
131 } catch (InflateException e) {
132 Log.e(TAG, "exception while inflating the layout", e);
133 }
134 } else {
135 String msg = "problem, trying to inflate a non-existent view";
136 msg += " (" + viewID + ")";
137 Log.e(TAG, msg);
138 }
139 }
140
141 /**
142 * Button setup.
143 * Set the button's text and its listener. If the text resource's id
144 * is 0, makes the button invisible.
145 */
146 void setupButton(int buttonRscID,
147 int rscString,
148 View.OnClickListener listener) {
149 View view = findViewById(buttonRscID);
150 if (view == null) {
151 return;
152 }
153 Button button = (Button) view;
154
155 if (rscString == 0) {
156 button.setVisibility(View.GONE);
157 } else {
158 CharSequence text = getString(rscString);
159 button.setText(text);
160 button.setOnClickListener(listener);
161 }
162 }
163
164 /**
165 * Utility method to setup the three dialog buttons.
166 */
167 void setupButtons(int alwaysDenyRsc, int allowRsc, int denyRsc) {
168 setupButton(R.id.button_alwaysdeny, alwaysDenyRsc,
169 new Button.OnClickListener() {
170 public void onClick(View v) {
171 mHandler.sendEmptyMessage(ALWAYS_DENY);
172 }
173 });
174
175 setupButton(R.id.button_allow, allowRsc,
176 new Button.OnClickListener() {
177 public void onClick(View v) {
178 mHandler.sendEmptyMessage(ALLOW);
179 }
180 });
181
182 setupButton(R.id.button_deny, denyRsc,
183 new Button.OnClickListener() {
184 public void onClick(View v) {
185 mHandler.sendEmptyMessage(DENY);
186 }
187 });
188 }
189
190 /**
191 * Utility method to set elements' text indicated in
192 * the dialogs' arguments.
193 */
194 void setLabel(JSONObject json, String name, int rsc) {
195 try {
196 if (json.has(name)) {
197 String text = json.getString(name);
198 View view = findViewById(rsc);
199 if (view != null && text != null) {
200 TextView textView = (TextView) view;
201 textView.setText(text);
202 textView.setVisibility(View.VISIBLE);
203 }
204 }
205 } catch (JSONException e) {
206 Log.e(TAG, "json exception", e);
207 }
208 }
209
210 /**
211 * Utility class to download an icon in the background.
212 * Once done ask the UI thread to update the icon.
213 */
214 class IconDownload implements Runnable {
215 private String mUrlString;
216
217 IconDownload(String url) {
218 mUrlString = url;
219 }
220
221 public void run() {
222 if (mUrlString == null) {
223 return;
224 }
225 try {
226 URL url = new URL(mUrlString);
227 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
228 connection.setDoInput(true);
229 connection.connect();
230 int length = connection.getContentLength();
231 InputStream is = connection.getInputStream();
232 Bitmap customIcon = BitmapFactory.decodeStream(is);
233 if (customIcon != null) {
234 mIcon = customIcon;
235 mHandler.sendEmptyMessage(UPDATE_ICON);
236 }
237 } catch (ClassCastException e) {
238 Log.e(TAG, "Class cast exception (" + mUrlString + ")", e);
239 } catch (MalformedURLException e) {
240 Log.e(TAG, "Malformed url (" + mUrlString + ") ", e);
241 } catch (IOException e) {
242 Log.e(TAG, "Exception downloading icon (" + mUrlString + ") ", e);
243 }
244 }
245 }
246
247 /**
248 * Utility method to update the icon.
249 * Called on the UI thread.
250 */
251 public void updateIcon() {
252 if (mIcon == null) {
253 return;
254 }
255 View view = findViewById(R.id.origin_icon);
256 if (view != null) {
257 ImageView imageView = (ImageView) view;
258 imageView.setMaxHeight(MAX_ICON_SIZE);
259 imageView.setMaxWidth(MAX_ICON_SIZE);
260 imageView.setScaleType(ImageView.ScaleType.FIT_XY);
261 imageView.setImageBitmap(mIcon);
262 imageView.setVisibility(View.VISIBLE);
263 }
264 }
265
266 /**
267 * Utility method to download an icon from a url and set
268 * it to the GUI element R.id.origin_icon.
269 * It is used both in the shortcut dialog and the
270 * permission dialog.
271 * The actual download is done in the background via
272 * IconDownload; once the icon is downlowded the UI is updated
273 * via updateIcon().
274 * The icon size is included in the layout with the choosen
275 * size, although not displayed, to limit text reflow once
276 * the icon is received.
277 */
278 void downloadIcon(String url) {
279 if (url == null) {
280 return;
281 }
282 View view = findViewById(R.id.origin_icon);
283 if (view != null) {
284 view.setMinimumWidth(mChoosenIconSize);
285 view.setMinimumHeight(mChoosenIconSize);
286 view.setVisibility(View.INVISIBLE);
287 }
288 Thread thread = new Thread(new IconDownload(url));
289 thread.start();
290 }
291
292 /**
293 * Utility method that get the dialogMessage
294 * and icon and ask the setupDialog(message,icon)
295 * method to set the values.
296 */
297 public void setupDialog() {
298 TextView dialogMessage = null;
299 ImageView icon = null;
300
301 View view = findViewById(R.id.dialog_message);
302 if (view != null) {
303 dialogMessage = (TextView) view;
304 }
305
306 View iconView = findViewById(R.id.icon);
307 if (iconView != null) {
308 icon = (ImageView) iconView;
309 }
310
311 if ((dialogMessage != null) && (icon != null)) {
312 setupDialog(dialogMessage, icon);
313 dialogMessage.setVisibility(View.VISIBLE);
314 }
315 }
316
317 /*
318 * Set the message and icon of the dialog
319 */
320 public void setupDialog(TextView message, ImageView icon) {
321 message.setText(R.string.unrecognized_dialog_message);
322 icon.setImageResource(R.drawable.gears_icon_48x48);
323 message.setVisibility(View.VISIBLE);
324 }
325
326 /**
327 * Setup the dialog
328 * By default, just display a simple message.
329 */
330 public void setup() {
331 setupButtons(0, 0, R.string.default_button);
332 setupDialog();
333 }
334
335
336}