OSDN Git Service

Merge change 8986
[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.WebIconDatabase;
35 import android.webkit.WebStorage;
36 import android.widget.ArrayAdapter;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.OnItemClickListener;
39 import android.widget.ImageView;
40 import android.widget.TextView;
41
42 import java.util.HashMap;
43 import java.util.HashSet;
44 import java.util.Iterator;
45 import java.util.Map;
46 import java.util.Set;
47 import java.util.Vector;
48
49 /**
50  * Manage the settings for an origin.
51  * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
52  * and Geolocation.
53  */
54 public 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;
64         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;
73
74         public Site(String origin) {
75             mOrigin = origin;
76             mTitle = null;
77             mIcon = null;
78             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;
116         }
117
118         public String getOrigin() {
119             return mOrigin;
120         }
121
122         public void setTitle(String title) {
123             mTitle = title;
124         }
125
126         public void setIcon(Bitmap icon) {
127             mIcon = icon;
128         }
129
130         public Bitmap getIcon() {
131             return mIcon;
132         }
133
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         }
146     }
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;
154
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
164         /**
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
179         public void populateOrigins() {
180             clear();
181
182             // 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.
190             Set origins = WebStorage.getInstance().getOrigins();
191             Map sites = new HashMap<String, Site>();
192             if (origins != null) {
193                 Iterator<String> iter = origins.iterator();
194                 while (iter.hasNext()) {
195                     addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
196                 }
197             }
198
199             // 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> >();
202             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();
208                 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
220             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();
231                     if (hosts.containsKey(host)) {
232                         String title = c.getString(titleIndex);
233                         Bitmap bmp = null;
234                         byte[] data = c.getBlob(faviconIndex);
235                         if (data != null) {
236                             bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
237                         }
238                         Set matchingSites = (Set) hosts.get(host);
239                         Iterator<Site> sitesIter = matchingSites.iterator();
240                         while (sitesIter.hasNext()) {
241                             Site site = sitesIter.next();
242                             site.setTitle(title);
243                             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
252             keys = sites.keySet();
253             originIter = keys.iterator();
254             while (originIter.hasNext()) {
255                 String origin = originIter.next();
256                 Site site = (Site) sites.get(origin);
257                 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             }
269             return mCurrentSite.getFeatureCount();
270         }
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);
316                 title.setText(site.getPrettyTitle());
317                 subtitle.setText(site.getPrettyOrigin());
318                 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);
329                 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;
334
335                         title.setText(R.string.webstorage_clear_data_title);
336                         subtitle.setText(usage);
337                         break;
338                 }
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) {
349                 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;
366                 }
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 }