blob: 89e5963e0c960055bc97ce6da5f31f2f8f6b1199 [file] [log] [blame]
Nicolas Roarde46990e2009-06-19 16:27:49 +01001/*
2 * Copyright (C) 2009 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.AlertDialog;
20import android.app.ListActivity;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.database.Cursor;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.Browser;
29import android.util.Log;
30import android.view.KeyEvent;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
Steve Blockf344d032009-07-30 10:50:45 +010034import android.webkit.GeolocationPermissions;
Nicolas Roarde46990e2009-06-19 16:27:49 +010035import android.webkit.WebIconDatabase;
36import android.webkit.WebStorage;
37import android.widget.ArrayAdapter;
38import android.widget.AdapterView;
39import android.widget.AdapterView.OnItemClickListener;
40import android.widget.ImageView;
41import android.widget.TextView;
42
43import java.util.HashMap;
Steve Blockee0d6392009-07-28 13:49:15 +010044import java.util.HashSet;
Nicolas Roarde46990e2009-06-19 16:27:49 +010045import java.util.Iterator;
Steve Block089ce3a2009-07-29 17:16:01 +010046import java.util.Map;
Nicolas Roarde46990e2009-06-19 16:27:49 +010047import java.util.Set;
48import java.util.Vector;
49
50/**
51 * Manage the settings for an origin.
Steve Block089ce3a2009-07-29 17:16:01 +010052 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
53 * and Geolocation.
Nicolas Roarde46990e2009-06-19 16:27:49 +010054 */
55public class WebsiteSettingsActivity extends ListActivity {
56
57 private String LOGTAG = "WebsiteSettingsActivity";
58 private static String sMBStored = null;
59 private SiteAdapter mAdapter = null;
60
61 class Site {
62 private String mOrigin;
63 private String mTitle;
64 private Bitmap mIcon;
Steve Block089ce3a2009-07-29 17:16:01 +010065 private int mFeatures;
66
67 // These constants provide the set of features that a site may support
68 // They must be consecutive. To add a new feature, add a new FEATURE_XXX
69 // variable with value equal to the current value of FEATURE_COUNT, then
70 // increment FEATURE_COUNT.
71 private final static int FEATURE_WEB_STORAGE = 0;
Steve Blockf344d032009-07-30 10:50:45 +010072 private final static int FEATURE_GEOLOCATION = 1;
Steve Block089ce3a2009-07-29 17:16:01 +010073 // The number of features available.
Steve Blockf344d032009-07-30 10:50:45 +010074 private final static int FEATURE_COUNT = 2;
Nicolas Roarde46990e2009-06-19 16:27:49 +010075
Steve Block1ad98cf2009-07-28 11:07:10 +010076 public Site(String origin) {
Nicolas Roarde46990e2009-06-19 16:27:49 +010077 mOrigin = origin;
Steve Block1ad98cf2009-07-28 11:07:10 +010078 mTitle = null;
79 mIcon = null;
Steve Block089ce3a2009-07-29 17:16:01 +010080 mFeatures = 0;
81 }
82
83 public void addFeature(int feature) {
84 mFeatures |= (1 << feature);
85 }
86
87 public boolean hasFeature(int feature) {
88 return (mFeatures & (1 << feature)) != 0;
89 }
90
91 /**
92 * Gets the number of features supported by this site.
93 */
94 public int getFeatureCount() {
95 int count = 0;
96 for (int i = 0; i < FEATURE_COUNT; ++i) {
97 count += hasFeature(i) ? 1 : 0;
98 }
99 return count;
100 }
101
102 /**
103 * Gets the ID of the nth (zero-based) feature supported by this site.
104 * The return value is a feature ID - one of the FEATURE_XXX values.
105 * This is required to determine which feature is displayed at a given
106 * position in the list of features for this site. This is used both
Steve Blockf344d032009-07-30 10:50:45 +0100107 * when populating the view and when responding to clicks on the list.
Steve Block089ce3a2009-07-29 17:16:01 +0100108 */
109 public int getFeatureByIndex(int n) {
110 int j = -1;
111 for (int i = 0; i < FEATURE_COUNT; ++i) {
112 j += hasFeature(i) ? 1 : 0;
113 if (j == n) {
114 return i;
115 }
116 }
117 return -1;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100118 }
119
120 public String getOrigin() {
121 return mOrigin;
122 }
123
124 public void setTitle(String title) {
125 mTitle = title;
126 }
127
Nicolas Roarde46990e2009-06-19 16:27:49 +0100128 public void setIcon(Bitmap icon) {
129 mIcon = icon;
130 }
131
132 public Bitmap getIcon() {
133 return mIcon;
134 }
Steve Block1ad98cf2009-07-28 11:07:10 +0100135
136 public String getPrettyOrigin() {
137 return mTitle == null ? null : hideHttp(mOrigin);
138 }
139
140 public String getPrettyTitle() {
141 return mTitle == null ? hideHttp(mOrigin) : mTitle;
142 }
143
144 private String hideHttp(String str) {
145 Uri uri = Uri.parse(str);
146 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
147 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100148 }
149
150 class SiteAdapter extends ArrayAdapter<Site>
151 implements AdapterView.OnItemClickListener {
152 private int mResource;
153 private LayoutInflater mInflater;
154 private Bitmap mDefaultIcon;
155 private Site mCurrentSite;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100156
157 public SiteAdapter(Context context, int rsc) {
158 super(context, rsc);
159 mResource = rsc;
160 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
161 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
162 R.drawable.ic_launcher_shortcut_browser_bookmark);
163 populateOrigins();
164 }
165
Steve Block089ce3a2009-07-29 17:16:01 +0100166 /**
167 * Adds the specified feature to the site corresponding to supplied
168 * origin in the map. Creates the site if it does not already exist.
169 */
170 private void addFeatureToSite(Map sites, String origin, int feature) {
171 Site site = null;
172 if (sites.containsKey(origin)) {
173 site = (Site) sites.get(origin);
174 } else {
175 site = new Site(origin);
176 sites.put(origin, site);
177 }
178 site.addFeature(feature);
179 }
180
Nicolas Roarde46990e2009-06-19 16:27:49 +0100181 public void populateOrigins() {
182 clear();
183
Steve Block089ce3a2009-07-29 17:16:01 +0100184 // Get the list of origins we want to display.
185 // All 'HTML 5 modules' (Database, Geolocation etc) form these
186 // origin strings using WebCore::SecurityOrigin::toString(), so it's
187 // safe to group origins here. Note that WebCore::SecurityOrigin
188 // uses 0 (which is not printed) for the port if the port is the
189 // default for the protocol. Eg http://www.google.com and
190 // http://www.google.com:80 both record a port of 0 and hence
191 // toString() == 'http://www.google.com' for both.
Andrei Popescu824faeb2009-07-21 18:24:06 +0100192 Set origins = WebStorage.getInstance().getOrigins();
Steve Block089ce3a2009-07-29 17:16:01 +0100193 Map sites = new HashMap<String, Site>();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100194 if (origins != null) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100195 Iterator<String> iter = origins.iterator();
196 while (iter.hasNext()) {
Steve Block089ce3a2009-07-29 17:16:01 +0100197 addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100198 }
199 }
Steve Blockf344d032009-07-30 10:50:45 +0100200 origins = GeolocationPermissions.getInstance().getOrigins();
201 if (origins != null) {
202 Iterator<String> iter = origins.iterator();
203 while (iter.hasNext()) {
204 addFeatureToSite(sites, iter.next(), Site.FEATURE_GEOLOCATION);
205 }
206 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100207
Steve Blockee0d6392009-07-28 13:49:15 +0100208 // Create a map from host to origin. This is used to add metadata
209 // (title, icon) for this origin from the bookmarks DB.
210 HashMap hosts = new HashMap<String, Set<Site> >();
Steve Block089ce3a2009-07-29 17:16:01 +0100211 Set keys = sites.keySet();
212 Iterator<String> originIter = keys.iterator();
213 while (originIter.hasNext()) {
214 String origin = originIter.next();
215 Site site = (Site) sites.get(origin);
216 String host = Uri.parse(origin).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100217 Set hostSites = null;
218 if (hosts.containsKey(host)) {
219 hostSites = (Set) hosts.get(host);
220 } else {
221 hostSites = new HashSet<Site>();
222 hosts.put(host, hostSites);
223 }
224 hostSites.add(site);
225 }
226
227 // Check the bookmark DB. If we have data for a host used by any of
228 // our origins, use it to set their title and favicon
Nicolas Roarde46990e2009-06-19 16:27:49 +0100229 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
230 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
231 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
232
233 if ((c != null) && c.moveToFirst()) {
234 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
235 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
236 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
237 do {
238 String url = c.getString(urlIndex);
239 String host = Uri.parse(url).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100240 if (hosts.containsKey(host)) {
Nicolas Roarde46990e2009-06-19 16:27:49 +0100241 String title = c.getString(titleIndex);
Steve Blockee0d6392009-07-28 13:49:15 +0100242 Bitmap bmp = null;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100243 byte[] data = c.getBlob(faviconIndex);
244 if (data != null) {
Steve Blockee0d6392009-07-28 13:49:15 +0100245 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
246 }
247 Set matchingSites = (Set) hosts.get(host);
Steve Block089ce3a2009-07-29 17:16:01 +0100248 Iterator<Site> sitesIter = matchingSites.iterator();
Steve Blockee0d6392009-07-28 13:49:15 +0100249 while (sitesIter.hasNext()) {
250 Site site = sitesIter.next();
251 site.setTitle(title);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100252 if (bmp != null) {
253 site.setIcon(bmp);
254 }
255 }
256 }
257 } while (c.moveToNext());
258 }
259
260 // We can now simply populate our array with Site instances
Steve Block089ce3a2009-07-29 17:16:01 +0100261 keys = sites.keySet();
262 originIter = keys.iterator();
263 while (originIter.hasNext()) {
264 String origin = originIter.next();
265 Site site = (Site) sites.get(origin);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100266 add(site);
267 }
268
269 if (getCount() == 0) {
270 finish(); // we close the screen
271 }
272 }
273
274 public int getCount() {
275 if (mCurrentSite == null) {
276 return super.getCount();
277 }
Steve Block089ce3a2009-07-29 17:16:01 +0100278 return mCurrentSite.getFeatureCount();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100279 }
280
281 public String sizeValueToString(long value) {
282 float mb = (float) value / (1024.0F * 1024.0F);
283 int val = (int) (mb * 10);
284 float ret = (float) (val / 10.0F);
285 if (ret <= 0) {
286 return "0";
287 }
288 return String.valueOf(ret);
289 }
290
291 /*
292 * If we receive the back event and are displaying
293 * site's settings, we want to go back to the main
294 * list view. If not, we just do nothing (see
295 * dispatchKeyEvent() below).
296 */
297 public boolean backKeyPressed() {
298 if (mCurrentSite != null) {
299 mCurrentSite = null;
300 populateOrigins();
301 notifyDataSetChanged();
302 return true;
303 }
304 return false;
305 }
306
307 public View getView(int position, View convertView, ViewGroup parent) {
308 View view;
309 TextView title;
310 TextView subtitle;
311 ImageView icon;
312
313 if (convertView == null) {
314 view = mInflater.inflate(mResource, parent, false);
315 } else {
316 view = convertView;
317 }
318
319 title = (TextView) view.findViewById(R.id.title);
320 subtitle = (TextView) view.findViewById(R.id.subtitle);
321 icon = (ImageView) view.findViewById(R.id.icon);
322
323 if (mCurrentSite == null) {
324 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100325 title.setText(site.getPrettyTitle());
326 subtitle.setText(site.getPrettyOrigin());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100327 icon.setVisibility(View.VISIBLE);
328 Bitmap bmp = site.getIcon();
329 if (bmp == null) {
330 bmp = mDefaultIcon;
331 }
332 icon.setImageBitmap(bmp);
333 // We set the site as the view's tag,
334 // so that we can get it in onItemClick()
335 view.setTag(site);
336 } else {
337 icon.setVisibility(View.GONE);
Steve Block089ce3a2009-07-29 17:16:01 +0100338 String origin = mCurrentSite.getOrigin();
339 switch (mCurrentSite.getFeatureByIndex(position)) {
340 case Site.FEATURE_WEB_STORAGE:
341 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
342 String usage = sizeValueToString(usageValue) + " " + sMBStored;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100343
Steve Block089ce3a2009-07-29 17:16:01 +0100344 title.setText(R.string.webstorage_clear_data_title);
345 subtitle.setText(usage);
346 break;
Steve Blockf344d032009-07-30 10:50:45 +0100347 case Site.FEATURE_GEOLOCATION:
348 title.setText(R.string.geolocation_settings_page_title);
349 boolean allowed = GeolocationPermissions.getInstance().getAllowed(origin);
350 subtitle.setText(allowed ?
351 R.string.geolocation_settings_page_summary_allowed :
352 R.string.geolocation_settings_page_summary_not_allowed);
353 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100354 }
355 }
356
357 return view;
358 }
359
360 public void onItemClick(AdapterView<?> parent,
361 View view,
362 int position,
363 long id) {
364 if (mCurrentSite != null) {
Steve Block089ce3a2009-07-29 17:16:01 +0100365 switch (mCurrentSite.getFeatureByIndex(position)) {
366 case Site.FEATURE_WEB_STORAGE:
367 new AlertDialog.Builder(getContext())
368 .setTitle(R.string.webstorage_clear_data_dialog_title)
369 .setMessage(R.string.webstorage_clear_data_dialog_message)
370 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
371 new AlertDialog.OnClickListener() {
372 public void onClick(DialogInterface dlg, int which) {
373 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
374 mCurrentSite = null;
375 populateOrigins();
376 notifyDataSetChanged();
377 }})
378 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
379 .setIcon(android.R.drawable.ic_dialog_alert)
380 .show();
381 break;
Steve Blockf344d032009-07-30 10:50:45 +0100382 case Site.FEATURE_GEOLOCATION:
383 new AlertDialog.Builder(getContext())
384 .setTitle(R.string.geolocation_settings_page_dialog_title)
385 .setMessage(R.string.geolocation_settings_page_dialog_message)
386 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button,
387 new AlertDialog.OnClickListener() {
388 public void onClick(DialogInterface dlg, int which) {
389 GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin());
390 mCurrentSite = null;
391 populateOrigins();
392 notifyDataSetChanged();
393 }})
394 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null)
395 .setIcon(android.R.drawable.ic_dialog_alert)
396 .show();
397 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100398 }
399 } else {
400 mCurrentSite = (Site) view.getTag();
401 notifyDataSetChanged();
402 }
403 }
404 }
405
406 /**
407 * Intercepts the back key to immediately notify
408 * NativeDialog that we are done.
409 */
410 public boolean dispatchKeyEvent(KeyEvent event) {
411 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
412 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
413 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
414 return true; // event consumed
415 }
416 }
417 return super.dispatchKeyEvent(event);
418 }
419
420 @Override
421 protected void onCreate(Bundle icicle) {
422 super.onCreate(icicle);
423 if (sMBStored == null) {
424 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
425 }
426 mAdapter = new SiteAdapter(this, R.layout.application);
427 setListAdapter(mAdapter);
428 getListView().setOnItemClickListener(mAdapter);
429 }
430}