blob: 577fc1cea6acdcf39134545336c9f163e61c6047 [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
Steve Block764f0c92009-09-10 15:01:37 +0100281 public String sizeValueToString(long bytes) {
282 // We display the size in MB, to 1dp, rounding up to the next 0.1MB.
283 // bytes should always be greater than zero.
284 if (bytes <= 0) {
285 Log.e(LOGTAG, "sizeValueToString called with non-positive value");
Nicolas Roarde46990e2009-06-19 16:27:49 +0100286 return "0";
287 }
Steve Block764f0c92009-09-10 15:01:37 +0100288 float megabytes = (float) bytes / (1024.0F * 1024.0F);
289 int truncated = (int) Math.ceil(megabytes * 10.0F);
290 float result = (float) (truncated / 10.0F);
291 return String.valueOf(result);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100292 }
293
294 /*
295 * If we receive the back event and are displaying
296 * site's settings, we want to go back to the main
297 * list view. If not, we just do nothing (see
298 * dispatchKeyEvent() below).
299 */
300 public boolean backKeyPressed() {
301 if (mCurrentSite != null) {
302 mCurrentSite = null;
303 populateOrigins();
304 notifyDataSetChanged();
305 return true;
306 }
307 return false;
308 }
309
310 public View getView(int position, View convertView, ViewGroup parent) {
311 View view;
312 TextView title;
313 TextView subtitle;
314 ImageView icon;
315
316 if (convertView == null) {
317 view = mInflater.inflate(mResource, parent, false);
318 } else {
319 view = convertView;
320 }
321
322 title = (TextView) view.findViewById(R.id.title);
323 subtitle = (TextView) view.findViewById(R.id.subtitle);
324 icon = (ImageView) view.findViewById(R.id.icon);
325
326 if (mCurrentSite == null) {
Steve Block4d055a52009-07-31 08:38:58 +0100327 setTitle(getString(R.string.pref_extras_website_settings));
328
Nicolas Roarde46990e2009-06-19 16:27:49 +0100329 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100330 title.setText(site.getPrettyTitle());
331 subtitle.setText(site.getPrettyOrigin());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100332 icon.setVisibility(View.VISIBLE);
333 Bitmap bmp = site.getIcon();
334 if (bmp == null) {
335 bmp = mDefaultIcon;
336 }
337 icon.setImageBitmap(bmp);
338 // We set the site as the view's tag,
339 // so that we can get it in onItemClick()
340 view.setTag(site);
341 } else {
Steve Block4d055a52009-07-31 08:38:58 +0100342 setTitle(mCurrentSite.getPrettyTitle());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100343 icon.setVisibility(View.GONE);
Steve Block089ce3a2009-07-29 17:16:01 +0100344 String origin = mCurrentSite.getOrigin();
345 switch (mCurrentSite.getFeatureByIndex(position)) {
346 case Site.FEATURE_WEB_STORAGE:
347 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
348 String usage = sizeValueToString(usageValue) + " " + sMBStored;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100349
Steve Block089ce3a2009-07-29 17:16:01 +0100350 title.setText(R.string.webstorage_clear_data_title);
351 subtitle.setText(usage);
352 break;
Steve Blockf344d032009-07-30 10:50:45 +0100353 case Site.FEATURE_GEOLOCATION:
354 title.setText(R.string.geolocation_settings_page_title);
355 boolean allowed = GeolocationPermissions.getInstance().getAllowed(origin);
356 subtitle.setText(allowed ?
357 R.string.geolocation_settings_page_summary_allowed :
358 R.string.geolocation_settings_page_summary_not_allowed);
359 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100360 }
361 }
362
363 return view;
364 }
365
366 public void onItemClick(AdapterView<?> parent,
367 View view,
368 int position,
369 long id) {
370 if (mCurrentSite != null) {
Steve Block089ce3a2009-07-29 17:16:01 +0100371 switch (mCurrentSite.getFeatureByIndex(position)) {
372 case Site.FEATURE_WEB_STORAGE:
373 new AlertDialog.Builder(getContext())
374 .setTitle(R.string.webstorage_clear_data_dialog_title)
375 .setMessage(R.string.webstorage_clear_data_dialog_message)
376 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
377 new AlertDialog.OnClickListener() {
378 public void onClick(DialogInterface dlg, int which) {
379 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
380 mCurrentSite = null;
381 populateOrigins();
382 notifyDataSetChanged();
383 }})
384 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
385 .setIcon(android.R.drawable.ic_dialog_alert)
386 .show();
387 break;
Steve Blockf344d032009-07-30 10:50:45 +0100388 case Site.FEATURE_GEOLOCATION:
389 new AlertDialog.Builder(getContext())
390 .setTitle(R.string.geolocation_settings_page_dialog_title)
391 .setMessage(R.string.geolocation_settings_page_dialog_message)
392 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button,
393 new AlertDialog.OnClickListener() {
394 public void onClick(DialogInterface dlg, int which) {
395 GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin());
396 mCurrentSite = null;
397 populateOrigins();
398 notifyDataSetChanged();
399 }})
400 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null)
401 .setIcon(android.R.drawable.ic_dialog_alert)
402 .show();
403 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100404 }
405 } else {
406 mCurrentSite = (Site) view.getTag();
407 notifyDataSetChanged();
408 }
409 }
410 }
411
412 /**
413 * Intercepts the back key to immediately notify
414 * NativeDialog that we are done.
415 */
416 public boolean dispatchKeyEvent(KeyEvent event) {
417 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
418 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
419 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
420 return true; // event consumed
421 }
422 }
423 return super.dispatchKeyEvent(event);
424 }
425
426 @Override
427 protected void onCreate(Bundle icicle) {
428 super.onCreate(icicle);
429 if (sMBStored == null) {
430 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
431 }
432 mAdapter = new SiteAdapter(this, R.layout.application);
433 setListAdapter(mAdapter);
434 getListView().setOnItemClickListener(mAdapter);
435 }
436}