OSDN Git Service

merge from open-source master
[android-x86/packages-apps-Browser.git] / src / com / android / browser / WebsiteSettingsActivity.java
1 /*
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
17 package com.android.browser;
18
19 import android.app.AlertDialog;
20 import android.app.ListActivity;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.database.Cursor;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.provider.Browser;
29 import android.util.Log;
30 import android.view.KeyEvent;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.webkit.GeolocationPermissions;
35 import android.webkit.WebIconDatabase;
36 import android.webkit.WebStorage;
37 import android.widget.ArrayAdapter;
38 import android.widget.AdapterView;
39 import android.widget.AdapterView.OnItemClickListener;
40 import android.widget.ImageView;
41 import android.widget.TextView;
42
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Set;
48 import java.util.Vector;
49
50 /**
51  * Manage the settings for an origin.
52  * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
53  * and Geolocation.
54  */
55 public 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;
65         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;
72         private final static int FEATURE_GEOLOCATION = 1;
73         // The number of features available.
74         private final static int FEATURE_COUNT = 2;
75
76         public Site(String origin) {
77             mOrigin = origin;
78             mTitle = null;
79             mIcon = null;
80             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
107          * when populating the view and when responding to clicks on the list.
108          */
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;
118         }
119
120         public String getOrigin() {
121             return mOrigin;
122         }
123
124         public void setTitle(String title) {
125             mTitle = title;
126         }
127
128         public void setIcon(Bitmap icon) {
129             mIcon = icon;
130         }
131
132         public Bitmap getIcon() {
133             return mIcon;
134         }
135
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         }
148     }
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;
156
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
166         /**
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
181         public void populateOrigins() {
182             clear();
183
184             // 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.
192             Set origins = WebStorage.getInstance().getOrigins();
193             Map sites = new HashMap<String, Site>();
194             if (origins != null) {
195                 Iterator<String> iter = origins.iterator();
196                 while (iter.hasNext()) {
197                     addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
198                 }
199             }
200             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             }
207
208             // 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> >();
211             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();
217                 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
229             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();
240                     if (hosts.containsKey(host)) {
241                         String title = c.getString(titleIndex);
242                         Bitmap bmp = null;
243                         byte[] data = c.getBlob(faviconIndex);
244                         if (data != null) {
245                             bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
246                         }
247                         Set matchingSites = (Set) hosts.get(host);
248                         Iterator<Site> sitesIter = matchingSites.iterator();
249                         while (sitesIter.hasNext()) {
250                             Site site = sitesIter.next();
251                             site.setTitle(title);
252                             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
261             keys = sites.keySet();
262             originIter = keys.iterator();
263             while (originIter.hasNext()) {
264                 String origin = originIter.next();
265                 Site site = (Site) sites.get(origin);
266                 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             }
278             return mCurrentSite.getFeatureCount();
279         }
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);
325                 title.setText(site.getPrettyTitle());
326                 subtitle.setText(site.getPrettyOrigin());
327                 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);
338                 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;
343
344                         title.setText(R.string.webstorage_clear_data_title);
345                         subtitle.setText(usage);
346                         break;
347                     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;
354                 }
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) {
365                 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;
382                     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;
398                 }
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 }