blob: 4a9eb55b24105874f16676edb7facd69e4301fe7 [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;
43import java.util.Iterator;
44import java.util.Set;
45import java.util.Vector;
46
47/**
48 * Manage the settings for an origin.
49 * We use it to keep track of the HTML5 settings, i.e. database (webstorage).
50 */
51public class WebsiteSettingsActivity extends ListActivity {
52
53 private String LOGTAG = "WebsiteSettingsActivity";
54 private static String sMBStored = null;
55 private SiteAdapter mAdapter = null;
56
57 class Site {
58 private String mOrigin;
59 private String mTitle;
60 private Bitmap mIcon;
61
Steve Block1ad98cf2009-07-28 11:07:10 +010062 public Site(String origin) {
Nicolas Roarde46990e2009-06-19 16:27:49 +010063 mOrigin = origin;
Steve Block1ad98cf2009-07-28 11:07:10 +010064 mTitle = null;
65 mIcon = null;
Nicolas Roarde46990e2009-06-19 16:27:49 +010066 }
67
68 public String getOrigin() {
69 return mOrigin;
70 }
71
72 public void setTitle(String title) {
73 mTitle = title;
74 }
75
Nicolas Roarde46990e2009-06-19 16:27:49 +010076 public void setIcon(Bitmap icon) {
77 mIcon = icon;
78 }
79
80 public Bitmap getIcon() {
81 return mIcon;
82 }
Steve Block1ad98cf2009-07-28 11:07:10 +010083
84 public String getPrettyOrigin() {
85 return mTitle == null ? null : hideHttp(mOrigin);
86 }
87
88 public String getPrettyTitle() {
89 return mTitle == null ? hideHttp(mOrigin) : mTitle;
90 }
91
92 private String hideHttp(String str) {
93 Uri uri = Uri.parse(str);
94 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
95 }
Nicolas Roarde46990e2009-06-19 16:27:49 +010096 }
97
98 class SiteAdapter extends ArrayAdapter<Site>
99 implements AdapterView.OnItemClickListener {
100 private int mResource;
101 private LayoutInflater mInflater;
102 private Bitmap mDefaultIcon;
103 private Site mCurrentSite;
104 private final static int STORED_DATA = 0;
105
106 public SiteAdapter(Context context, int rsc) {
107 super(context, rsc);
108 mResource = rsc;
109 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
110 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
111 R.drawable.ic_launcher_shortcut_browser_bookmark);
112 populateOrigins();
113 }
114
115 public void populateOrigins() {
116 clear();
117
118 // Get the list of origins we want to display
119 HashMap<String, Site> uris = new HashMap<String, Site>();
Andrei Popescu824faeb2009-07-21 18:24:06 +0100120 Set origins = WebStorage.getInstance().getOrigins();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100121 if (origins != null) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100122 Iterator<String> iter = origins.iterator();
123 while (iter.hasNext()) {
124 String origin = iter.next();
Steve Block1ad98cf2009-07-28 11:07:10 +0100125 Site site = new Site(origin);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100126 uris.put(Uri.parse(origin).getHost(), site);
127 }
128 }
129
130 // Check the bookmark db -- if one of our origin matches,
131 // we set its title and favicon
132 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
133 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
134 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
135
136 if ((c != null) && c.moveToFirst()) {
137 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
138 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
139 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
140 do {
141 String url = c.getString(urlIndex);
142 String host = Uri.parse(url).getHost();
143 if (uris.containsKey(host)) {
144 String title = c.getString(titleIndex);
145 Site site = uris.get(host);
146 site.setTitle(title);
147 byte[] data = c.getBlob(faviconIndex);
148 if (data != null) {
149 Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
150 if (bmp != null) {
151 site.setIcon(bmp);
152 }
153 }
154 }
155 } while (c.moveToNext());
156 }
157
158 // We can now simply populate our array with Site instances
159 Set keys = uris.keySet();
160 Iterator iter = keys.iterator();
161 while (iter.hasNext()) {
162 String origin = (String) iter.next();
163 Site site = uris.get(origin);
164 add(site);
165 }
166
167 if (getCount() == 0) {
168 finish(); // we close the screen
169 }
170 }
171
172 public int getCount() {
173 if (mCurrentSite == null) {
174 return super.getCount();
175 }
176 return 1; // db view
177 }
178
179 public String sizeValueToString(long value) {
180 float mb = (float) value / (1024.0F * 1024.0F);
181 int val = (int) (mb * 10);
182 float ret = (float) (val / 10.0F);
183 if (ret <= 0) {
184 return "0";
185 }
186 return String.valueOf(ret);
187 }
188
189 /*
190 * If we receive the back event and are displaying
191 * site's settings, we want to go back to the main
192 * list view. If not, we just do nothing (see
193 * dispatchKeyEvent() below).
194 */
195 public boolean backKeyPressed() {
196 if (mCurrentSite != null) {
197 mCurrentSite = null;
198 populateOrigins();
199 notifyDataSetChanged();
200 return true;
201 }
202 return false;
203 }
204
205 public View getView(int position, View convertView, ViewGroup parent) {
206 View view;
207 TextView title;
208 TextView subtitle;
209 ImageView icon;
210
211 if (convertView == null) {
212 view = mInflater.inflate(mResource, parent, false);
213 } else {
214 view = convertView;
215 }
216
217 title = (TextView) view.findViewById(R.id.title);
218 subtitle = (TextView) view.findViewById(R.id.subtitle);
219 icon = (ImageView) view.findViewById(R.id.icon);
220
221 if (mCurrentSite == null) {
222 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100223 title.setText(site.getPrettyTitle());
224 subtitle.setText(site.getPrettyOrigin());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100225 icon.setVisibility(View.VISIBLE);
226 Bitmap bmp = site.getIcon();
227 if (bmp == null) {
228 bmp = mDefaultIcon;
229 }
230 icon.setImageBitmap(bmp);
231 // We set the site as the view's tag,
232 // so that we can get it in onItemClick()
233 view.setTag(site);
234 } else {
235 icon.setVisibility(View.GONE);
236 if (position == STORED_DATA) {
237 String origin = mCurrentSite.getOrigin();
238 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
239 String usage = sizeValueToString(usageValue) + " " + sMBStored;
240
241 title.setText(R.string.webstorage_clear_data_title);
242 subtitle.setText(usage);
243 }
244 }
245
246 return view;
247 }
248
249 public void onItemClick(AdapterView<?> parent,
250 View view,
251 int position,
252 long id) {
253 if (mCurrentSite != null) {
254 if (position == STORED_DATA) {
255 new AlertDialog.Builder(getContext())
256 .setTitle(R.string.webstorage_clear_data_dialog_title)
257 .setMessage(R.string.webstorage_clear_data_dialog_message)
258 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
259 new AlertDialog.OnClickListener() {
260 public void onClick(DialogInterface dlg, int which) {
261 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
262 mCurrentSite = null;
263 populateOrigins();
264 notifyDataSetChanged();
265 }})
266 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
267 .setIcon(android.R.drawable.ic_dialog_alert)
268 .show();
269 }
270 } else {
271 mCurrentSite = (Site) view.getTag();
272 notifyDataSetChanged();
273 }
274 }
275 }
276
277 /**
278 * Intercepts the back key to immediately notify
279 * NativeDialog that we are done.
280 */
281 public boolean dispatchKeyEvent(KeyEvent event) {
282 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
283 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
284 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
285 return true; // event consumed
286 }
287 }
288 return super.dispatchKeyEvent(event);
289 }
290
291 @Override
292 protected void onCreate(Bundle icicle) {
293 super.onCreate(icicle);
294 if (sMBStored == null) {
295 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
296 }
297 mAdapter = new SiteAdapter(this, R.layout.application);
298 setListAdapter(mAdapter);
299 getListView().setOnItemClickListener(mAdapter);
300 }
301}