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