blob: fc219670478ef7af34206f610b411a27672d3a9e [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;
34import android.webkit.WebIconDatabase;
35import android.webkit.WebStorage;
36import android.widget.ArrayAdapter;
37import android.widget.AdapterView;
38import android.widget.AdapterView.OnItemClickListener;
39import android.widget.ImageView;
40import android.widget.TextView;
41
42import java.util.HashMap;
Steve Blockee0d6392009-07-28 13:49:15 +010043import java.util.HashSet;
Nicolas Roarde46990e2009-06-19 16:27:49 +010044import java.util.Iterator;
Steve Block089ce3a2009-07-29 17:16:01 +010045import java.util.Map;
Nicolas Roarde46990e2009-06-19 16:27:49 +010046import java.util.Set;
47import java.util.Vector;
48
49/**
50 * Manage the settings for an origin.
Steve Block089ce3a2009-07-29 17:16:01 +010051 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
52 * and Geolocation.
Nicolas Roarde46990e2009-06-19 16:27:49 +010053 */
54public class WebsiteSettingsActivity extends ListActivity {
55
56 private String LOGTAG = "WebsiteSettingsActivity";
57 private static String sMBStored = null;
58 private SiteAdapter mAdapter = null;
59
60 class Site {
61 private String mOrigin;
62 private String mTitle;
63 private Bitmap mIcon;
Steve Block089ce3a2009-07-29 17:16:01 +010064 private int mFeatures;
65
66 // These constants provide the set of features that a site may support
67 // They must be consecutive. To add a new feature, add a new FEATURE_XXX
68 // variable with value equal to the current value of FEATURE_COUNT, then
69 // increment FEATURE_COUNT.
70 private final static int FEATURE_WEB_STORAGE = 0;
71 // The number of features available.
72 private final static int FEATURE_COUNT = 1;
Nicolas Roarde46990e2009-06-19 16:27:49 +010073
Steve Block1ad98cf2009-07-28 11:07:10 +010074 public Site(String origin) {
Nicolas Roarde46990e2009-06-19 16:27:49 +010075 mOrigin = origin;
Steve Block1ad98cf2009-07-28 11:07:10 +010076 mTitle = null;
77 mIcon = null;
Steve Block089ce3a2009-07-29 17:16:01 +010078 mFeatures = 0;
79 }
80
81 public void addFeature(int feature) {
82 mFeatures |= (1 << feature);
83 }
84
85 public boolean hasFeature(int feature) {
86 return (mFeatures & (1 << feature)) != 0;
87 }
88
89 /**
90 * Gets the number of features supported by this site.
91 */
92 public int getFeatureCount() {
93 int count = 0;
94 for (int i = 0; i < FEATURE_COUNT; ++i) {
95 count += hasFeature(i) ? 1 : 0;
96 }
97 return count;
98 }
99
100 /**
101 * Gets the ID of the nth (zero-based) feature supported by this site.
102 * The return value is a feature ID - one of the FEATURE_XXX values.
103 * This is required to determine which feature is displayed at a given
104 * position in the list of features for this site. This is used both
105 * when populateing the view and when responding to clicks on the list.
106 */
107 public int getFeatureByIndex(int n) {
108 int j = -1;
109 for (int i = 0; i < FEATURE_COUNT; ++i) {
110 j += hasFeature(i) ? 1 : 0;
111 if (j == n) {
112 return i;
113 }
114 }
115 return -1;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100116 }
117
118 public String getOrigin() {
119 return mOrigin;
120 }
121
122 public void setTitle(String title) {
123 mTitle = title;
124 }
125
Nicolas Roarde46990e2009-06-19 16:27:49 +0100126 public void setIcon(Bitmap icon) {
127 mIcon = icon;
128 }
129
130 public Bitmap getIcon() {
131 return mIcon;
132 }
Steve Block1ad98cf2009-07-28 11:07:10 +0100133
134 public String getPrettyOrigin() {
135 return mTitle == null ? null : hideHttp(mOrigin);
136 }
137
138 public String getPrettyTitle() {
139 return mTitle == null ? hideHttp(mOrigin) : mTitle;
140 }
141
142 private String hideHttp(String str) {
143 Uri uri = Uri.parse(str);
144 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
145 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100146 }
147
148 class SiteAdapter extends ArrayAdapter<Site>
149 implements AdapterView.OnItemClickListener {
150 private int mResource;
151 private LayoutInflater mInflater;
152 private Bitmap mDefaultIcon;
153 private Site mCurrentSite;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100154
155 public SiteAdapter(Context context, int rsc) {
156 super(context, rsc);
157 mResource = rsc;
158 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
159 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
160 R.drawable.ic_launcher_shortcut_browser_bookmark);
161 populateOrigins();
162 }
163
Steve Block089ce3a2009-07-29 17:16:01 +0100164 /**
165 * Adds the specified feature to the site corresponding to supplied
166 * origin in the map. Creates the site if it does not already exist.
167 */
168 private void addFeatureToSite(Map sites, String origin, int feature) {
169 Site site = null;
170 if (sites.containsKey(origin)) {
171 site = (Site) sites.get(origin);
172 } else {
173 site = new Site(origin);
174 sites.put(origin, site);
175 }
176 site.addFeature(feature);
177 }
178
Nicolas Roarde46990e2009-06-19 16:27:49 +0100179 public void populateOrigins() {
180 clear();
181
Steve Block089ce3a2009-07-29 17:16:01 +0100182 // Get the list of origins we want to display.
183 // All 'HTML 5 modules' (Database, Geolocation etc) form these
184 // origin strings using WebCore::SecurityOrigin::toString(), so it's
185 // safe to group origins here. Note that WebCore::SecurityOrigin
186 // uses 0 (which is not printed) for the port if the port is the
187 // default for the protocol. Eg http://www.google.com and
188 // http://www.google.com:80 both record a port of 0 and hence
189 // toString() == 'http://www.google.com' for both.
Andrei Popescu824faeb2009-07-21 18:24:06 +0100190 Set origins = WebStorage.getInstance().getOrigins();
Steve Block089ce3a2009-07-29 17:16:01 +0100191 Map sites = new HashMap<String, Site>();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100192 if (origins != null) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100193 Iterator<String> iter = origins.iterator();
194 while (iter.hasNext()) {
Steve Block089ce3a2009-07-29 17:16:01 +0100195 addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100196 }
197 }
198
Steve Blockee0d6392009-07-28 13:49:15 +0100199 // Create a map from host to origin. This is used to add metadata
200 // (title, icon) for this origin from the bookmarks DB.
201 HashMap hosts = new HashMap<String, Set<Site> >();
Steve Block089ce3a2009-07-29 17:16:01 +0100202 Set keys = sites.keySet();
203 Iterator<String> originIter = keys.iterator();
204 while (originIter.hasNext()) {
205 String origin = originIter.next();
206 Site site = (Site) sites.get(origin);
207 String host = Uri.parse(origin).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100208 Set hostSites = null;
209 if (hosts.containsKey(host)) {
210 hostSites = (Set) hosts.get(host);
211 } else {
212 hostSites = new HashSet<Site>();
213 hosts.put(host, hostSites);
214 }
215 hostSites.add(site);
216 }
217
218 // Check the bookmark DB. If we have data for a host used by any of
219 // our origins, use it to set their title and favicon
Nicolas Roarde46990e2009-06-19 16:27:49 +0100220 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
221 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
222 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
223
224 if ((c != null) && c.moveToFirst()) {
225 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
226 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
227 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
228 do {
229 String url = c.getString(urlIndex);
230 String host = Uri.parse(url).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100231 if (hosts.containsKey(host)) {
Nicolas Roarde46990e2009-06-19 16:27:49 +0100232 String title = c.getString(titleIndex);
Steve Blockee0d6392009-07-28 13:49:15 +0100233 Bitmap bmp = null;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100234 byte[] data = c.getBlob(faviconIndex);
235 if (data != null) {
Steve Blockee0d6392009-07-28 13:49:15 +0100236 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
237 }
238 Set matchingSites = (Set) hosts.get(host);
Steve Block089ce3a2009-07-29 17:16:01 +0100239 Iterator<Site> sitesIter = matchingSites.iterator();
Steve Blockee0d6392009-07-28 13:49:15 +0100240 while (sitesIter.hasNext()) {
241 Site site = sitesIter.next();
242 site.setTitle(title);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100243 if (bmp != null) {
244 site.setIcon(bmp);
245 }
246 }
247 }
248 } while (c.moveToNext());
249 }
250
251 // We can now simply populate our array with Site instances
Steve Block089ce3a2009-07-29 17:16:01 +0100252 keys = sites.keySet();
253 originIter = keys.iterator();
254 while (originIter.hasNext()) {
255 String origin = originIter.next();
256 Site site = (Site) sites.get(origin);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100257 add(site);
258 }
259
260 if (getCount() == 0) {
261 finish(); // we close the screen
262 }
263 }
264
265 public int getCount() {
266 if (mCurrentSite == null) {
267 return super.getCount();
268 }
Steve Block089ce3a2009-07-29 17:16:01 +0100269 return mCurrentSite.getFeatureCount();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100270 }
271
272 public String sizeValueToString(long value) {
273 float mb = (float) value / (1024.0F * 1024.0F);
274 int val = (int) (mb * 10);
275 float ret = (float) (val / 10.0F);
276 if (ret <= 0) {
277 return "0";
278 }
279 return String.valueOf(ret);
280 }
281
282 /*
283 * If we receive the back event and are displaying
284 * site's settings, we want to go back to the main
285 * list view. If not, we just do nothing (see
286 * dispatchKeyEvent() below).
287 */
288 public boolean backKeyPressed() {
289 if (mCurrentSite != null) {
290 mCurrentSite = null;
291 populateOrigins();
292 notifyDataSetChanged();
293 return true;
294 }
295 return false;
296 }
297
298 public View getView(int position, View convertView, ViewGroup parent) {
299 View view;
300 TextView title;
301 TextView subtitle;
302 ImageView icon;
303
304 if (convertView == null) {
305 view = mInflater.inflate(mResource, parent, false);
306 } else {
307 view = convertView;
308 }
309
310 title = (TextView) view.findViewById(R.id.title);
311 subtitle = (TextView) view.findViewById(R.id.subtitle);
312 icon = (ImageView) view.findViewById(R.id.icon);
313
314 if (mCurrentSite == null) {
315 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100316 title.setText(site.getPrettyTitle());
317 subtitle.setText(site.getPrettyOrigin());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100318 icon.setVisibility(View.VISIBLE);
319 Bitmap bmp = site.getIcon();
320 if (bmp == null) {
321 bmp = mDefaultIcon;
322 }
323 icon.setImageBitmap(bmp);
324 // We set the site as the view's tag,
325 // so that we can get it in onItemClick()
326 view.setTag(site);
327 } else {
328 icon.setVisibility(View.GONE);
Steve Block089ce3a2009-07-29 17:16:01 +0100329 String origin = mCurrentSite.getOrigin();
330 switch (mCurrentSite.getFeatureByIndex(position)) {
331 case Site.FEATURE_WEB_STORAGE:
332 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
333 String usage = sizeValueToString(usageValue) + " " + sMBStored;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100334
Steve Block089ce3a2009-07-29 17:16:01 +0100335 title.setText(R.string.webstorage_clear_data_title);
336 subtitle.setText(usage);
337 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100338 }
339 }
340
341 return view;
342 }
343
344 public void onItemClick(AdapterView<?> parent,
345 View view,
346 int position,
347 long id) {
348 if (mCurrentSite != null) {
Steve Block089ce3a2009-07-29 17:16:01 +0100349 switch (mCurrentSite.getFeatureByIndex(position)) {
350 case Site.FEATURE_WEB_STORAGE:
351 new AlertDialog.Builder(getContext())
352 .setTitle(R.string.webstorage_clear_data_dialog_title)
353 .setMessage(R.string.webstorage_clear_data_dialog_message)
354 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
355 new AlertDialog.OnClickListener() {
356 public void onClick(DialogInterface dlg, int which) {
357 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
358 mCurrentSite = null;
359 populateOrigins();
360 notifyDataSetChanged();
361 }})
362 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
363 .setIcon(android.R.drawable.ic_dialog_alert)
364 .show();
365 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100366 }
367 } else {
368 mCurrentSite = (Site) view.getTag();
369 notifyDataSetChanged();
370 }
371 }
372 }
373
374 /**
375 * Intercepts the back key to immediately notify
376 * NativeDialog that we are done.
377 */
378 public boolean dispatchKeyEvent(KeyEvent event) {
379 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
380 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
381 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
382 return true; // event consumed
383 }
384 }
385 return super.dispatchKeyEvent(event);
386 }
387
388 @Override
389 protected void onCreate(Bundle icicle) {
390 super.onCreate(icicle);
391 if (sMBStored == null) {
392 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
393 }
394 mAdapter = new SiteAdapter(this, R.layout.application);
395 setListAdapter(mAdapter);
396 getListView().setOnItemClickListener(mAdapter);
397 }
398}