blob: 1292bf6e8f5119cd9574e23728aea928808ae7a9 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 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
Leon Scroggins25230d72010-09-28 20:09:25 -040019import com.android.browser.provider.BrowserProvider2;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040022import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070024import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040025import android.content.ContentValues;
26import android.content.Context;
27import android.content.CursorLoader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.Intent;
Leon Scroggins III052ce662010-09-13 14:44:16 -040029import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040030import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040032import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010033import android.graphics.Bitmap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070035import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.WebAddress;
37import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010038import android.os.Handler;
39import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040040import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040041import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040042import android.text.TextUtils;
Leon Scroggins III052ce662010-09-13 14:44:16 -040043import android.view.KeyEvent;
44import android.view.LayoutInflater;
Leon Scroggins88d08032010-10-21 15:17:10 -040045import android.view.MenuItem;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040047import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040049import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040050import android.view.inputmethod.EditorInfo;
51import android.view.inputmethod.InputMethodManager;
52import android.widget.AdapterView;
53import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040055import android.widget.ListView;
Leon Scroggins88d08032010-10-21 15:17:10 -040056import android.widget.PopupMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057import android.widget.TextView;
58import android.widget.Toast;
59
Cary Clarkf2407c62009-09-04 12:25:10 -040060import java.net.URI;
61import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040062import java.util.ArrayList;
Leon Scroggins74dbe012010-10-15 10:54:27 -040063import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040064
65public class AddBookmarkPage extends Activity
66 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040067 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
Leon Scroggins88d08032010-10-21 15:17:10 -040068 BreadCrumbView.Controller, PopupMenu.OnMenuItemClickListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080069
Michael Kolb370a4f32010-10-06 10:45:32 -070070 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040071 public static final String TOUCH_ICON_URL = "touch_icon_url";
72 // Place on an edited bookmark to remove the saved thumbnail
73 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
74 public static final String USER_AGENT = "user_agent";
Michael Kolb370a4f32010-10-06 10:45:32 -070075
Leon Scroggins74dbe012010-10-15 10:54:27 -040076 private static final int MAX_CRUMBS_SHOWN = 2;
77
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 private final String LOGTAG = "Bookmarks";
Leon Scroggins74dbe012010-10-15 10:54:27 -040079 // Set to true to see the crash on the code I would like to run.
80 private final boolean DEBUG_CRASH = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081
Leon Scroggins III052ce662010-09-13 14:44:16 -040082 // IDs for the CursorLoaders that are used.
83 private final int LOADER_ID_FOLDER_CONTENTS = 0;
84 private final int LOADER_ID_ALL_FOLDERS = 1;
85
The Android Open Source Project0c908882009-03-03 19:32:16 -080086 private EditText mTitle;
87 private EditText mAddress;
88 private TextView mButton;
89 private View mCancelButton;
90 private boolean mEditingExisting;
91 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040092 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010093 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040094 private TextView mFolder;
95 private View mDefaultView;
96 private View mFolderSelector;
97 private EditText mFolderNamer;
98 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040099 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400100 private long mCurrentFolder = 0;
101 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400102 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400103 private TextView mFakeTitle;
104 private View mCrumbHolder;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400105 private ListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400106 private boolean mSaveToHomeScreen;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400107
108 private static class Folder {
109 String Name;
110 long Id;
111 Folder(String name, long id) {
112 Name = name;
113 Id = id;
114 }
115 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116
Ben Murdoch1794fe22009-09-29 18:14:30 +0100117 // Message IDs
118 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400119 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100120
121 private Handler mHandler;
122
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100123 private InputMethodManager getInputMethodManager() {
124 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
125 }
126
Leon Scroggins III052ce662010-09-13 14:44:16 -0400127 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400128 public void onTop(int level, Object data) {
129 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400130 Folder folderData = (Folder) data;
131 long folder = folderData.Id;
132 Uri uri = BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400133 LoaderManager manager = getLoaderManager();
134 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
135 LOADER_ID_FOLDER_CONTENTS));
136 loader.setUri(uri);
137 loader.forceLoad();
138 updateVisible();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400139 if (mFolderNamer.getVisibility() == View.VISIBLE) {
140 completeOrCancelFolderNaming(true);
141 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400142 }
143
144 /**
145 * Update the views shown to only show the two deepest levels of crumbs.
146 * Note that this method depends on internal knowledge of BreadCrumbView.
147 */
148 private void updateVisible() {
149 if (MAX_CRUMBS_SHOWN > 0) {
150 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
151 // This class always uses a back button, which is the first child.
152 int childIndex = 1;
153 if (invisibleCrumbs > 0) {
154 int crumbIndex = 0;
155 while (crumbIndex < invisibleCrumbs) {
156 // Set the crumb to GONE.
157 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
158 childIndex++;
159 // Each crumb is followed by a separator (except the last
160 // one). Also make it GONE
161 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
162 childIndex++;
163 // Move to the next crumb.
164 crumbIndex++;
165 }
166 }
167 // Make sure the last two are visible.
168 int childCount = mCrumbs.getChildCount();
169 while (childIndex < childCount) {
170 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
171 childIndex++;
172 }
173 }
174 }
175
176 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400177 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
178 if (v == mFolderNamer) {
179 if (v.getText().length() > 0) {
180 if (actionId == EditorInfo.IME_NULL) {
181 // Only want to do this once.
182 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400183 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400184 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400185 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
Michael Kolb31829b92010-10-01 11:50:21 -0700187 // Steal the key press; otherwise a newline will be added
188 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400190 return false;
191 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400193 private void switchToDefaultView(boolean changedFolder) {
194 mFolderSelector.setVisibility(View.GONE);
195 mDefaultView.setVisibility(View.VISIBLE);
196 mCrumbHolder.setVisibility(View.GONE);
197 mFakeTitle.setVisibility(View.VISIBLE);
198 if (changedFolder) {
199 Object data = mCrumbs.getTopData();
200 if (data != null) {
201 Folder folder = (Folder) data;
202 mCurrentFolder = folder.Id;
203 mFolder.setText(folder.Name);
204 }
205 }
206 }
207
Leon Scroggins III052ce662010-09-13 14:44:16 -0400208 @Override
209 public void onClick(View v) {
210 if (v == mButton) {
211 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400212 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700213 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400214 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700215 } else {
216 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400217 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400218 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700219 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400220 } else if (save()) {
221 finish();
222 }
223 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700224 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400225 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400226 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
227 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700228 } else {
229 finish();
230 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400231 } else if (v == mFolder) {
Leon Scroggins88d08032010-10-21 15:17:10 -0400232 // FIXME: We want to use mFolder as an anchor, but cannot until we
233 // fix the issue that the PopupMenu will not extend past the edge of
234 // the dialog.
235 PopupMenu popup = new PopupMenu(this, mFakeTitle);
236 popup.getMenuInflater().inflate(R.menu.folder_choice,
237 popup.getMenu());
238 popup.setOnMenuItemClickListener(this);
239 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400240 } else if (v == mAddNewFolder) {
241 mFolderNamer.setVisibility(View.VISIBLE);
242 mFolderNamer.setText(R.string.new_folder);
243 mFolderNamer.requestFocus();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400244 updateList();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700245 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400246 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100247 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400248 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400250 }
251
Leon Scroggins88d08032010-10-21 15:17:10 -0400252 @Override
253 public boolean onMenuItemClick(MenuItem item) {
254 switch(item.getItemId()) {
255 case R.id.bookmarks:
256 mCurrentFolder = getBookmarksBarId(this);
257 mFolder.setText(item.getTitle());
258 mSaveToHomeScreen = false;
259 break;
260 case R.id.home_screen:
261 // Create a short cut to the home screen
262 mSaveToHomeScreen = true;
263 mFolder.setText(item.getTitle());
264 break;
265 case R.id.other:
266 switchToFolderSelector();
267 break;
268 default:
269 return false;
270 }
271 return true;
272 }
273
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400274 // Refresh the ListView to hide or show the empty view, as necessary.
275 // Should be called after mFolderNamer is shown or hidden.
276 private void updateList() {
277 if (mAdapter.getCount() == 0) {
278 // XXX: Is there a better way to refresh the ListView?
279 mListView.setAdapter(mAdapter);
280 }
281 }
282
283 private void completeOrCancelFolderNaming(boolean cancel) {
284 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700285 String name = mFolderNamer.getText().toString();
286 long id = addFolderToCurrent(mFolderNamer.getText().toString());
287 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700288 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400289 mFolderNamer.setVisibility(View.GONE);
290 mAddNewFolder.setVisibility(View.VISIBLE);
291 mAddSeparator.setVisibility(View.VISIBLE);
292 getInputMethodManager().hideSoftInputFromWindow(
293 mFolderNamer.getWindowToken(), 0);
294 updateList();
Michael Kolb31829b92010-10-01 11:50:21 -0700295 }
296
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700297 private long addFolderToCurrent(String name) {
298 // Add the folder to the database
299 ContentValues values = new ContentValues();
300 values.put(BrowserContract.Bookmarks.TITLE,
301 name);
302 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400303 long currentFolder;
304 Object data = mCrumbs.getTopData();
305 if (data != null) {
306 currentFolder = ((Folder) data).Id;
307 } else {
308 currentFolder = getBookmarksBarId(this);
309 }
310 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700311 Uri uri = getContentResolver().insert(
312 BrowserContract.Bookmarks.CONTENT_URI, values);
313 if (uri != null) {
314 return ContentUris.parseId(uri);
315 } else {
316 return -1;
317 }
318 }
319
Leon Scroggins III052ce662010-09-13 14:44:16 -0400320 private void switchToFolderSelector() {
321 mDefaultView.setVisibility(View.GONE);
322 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400323 mCrumbHolder.setVisibility(View.VISIBLE);
324 mFakeTitle.setVisibility(View.GONE);
325 mAddNewFolder.setVisibility(View.VISIBLE);
326 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400327 }
328
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700329 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700330 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400331 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400332 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700333 }
334 }
335
Leon Scroggins III052ce662010-09-13 14:44:16 -0400336 @Override
337 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
338 String[] projection;
339 switch (id) {
340 case LOADER_ID_ALL_FOLDERS:
341 projection = new String[] {
342 BrowserContract.Bookmarks._ID,
343 BrowserContract.Bookmarks.PARENT,
344 BrowserContract.Bookmarks.TITLE,
345 BrowserContract.Bookmarks.IS_FOLDER
346 };
347 return new CursorLoader(this,
348 BrowserContract.Bookmarks.CONTENT_URI,
349 projection,
350 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
351 null,
352 null);
353 case LOADER_ID_FOLDER_CONTENTS:
354 projection = new String[] {
355 BrowserContract.Bookmarks._ID,
356 BrowserContract.Bookmarks.TITLE,
357 BrowserContract.Bookmarks.IS_FOLDER
358 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400359 return new CursorLoader(this,
360 BrowserContract.Bookmarks.buildFolderUri(
361 mCurrentFolder),
362 projection,
363 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
364 null,
365 null);
366 default:
367 throw new AssertionError("Asking for nonexistant loader!");
368 }
369 }
370
371 @Override
372 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
373 switch (loader.getId()) {
374 case LOADER_ID_FOLDER_CONTENTS:
375 mAdapter.changeCursor(cursor);
376 break;
377 case LOADER_ID_ALL_FOLDERS:
378 long parent = mCurrentFolder;
379 int idIndex = cursor.getColumnIndexOrThrow(
380 BrowserContract.Bookmarks._ID);
381 int titleIndex = cursor.getColumnIndexOrThrow(
382 BrowserContract.Bookmarks.TITLE);
383 int parentIndex = cursor.getColumnIndexOrThrow(
384 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400385 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700386 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
387 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400388 // First, find the folder corresponding to the current
389 // folder
390 if (!cursor.moveToFirst()) {
391 throw new AssertionError("No folders in the database!");
392 }
393 long folder;
394 do {
395 folder = cursor.getLong(idIndex);
396 } while (folder != parent && cursor.moveToNext());
397 if (cursor.isAfterLast()) {
398 throw new AssertionError("Folder(id=" + parent
399 + ") holding this bookmark does not exist!");
400 }
401 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400402 if (parent == mCurrentFolder) {
403 mFolder.setText(name);
404 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400405 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400406 parent = cursor.getLong(parentIndex);
407 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400408 while (!folderStack.isEmpty()) {
409 Folder thisFolder = (Folder) folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400410 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400411 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400412 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400413 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400414 break;
415 default:
416 break;
417 }
418 }
419
Leon Scroggins III052ce662010-09-13 14:44:16 -0400420 @Override
421 public void onItemClick(AdapterView<?> parent, View view, int position,
422 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400423 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400424 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400425 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400426 }
427
428 /**
429 * Shows a list of names of folders.
430 */
431 private class FolderAdapter extends CursorAdapter {
432 public FolderAdapter(Context context) {
433 super(context, null);
434 }
435
436 @Override
437 public void bindView(View view, Context context, Cursor cursor) {
438 ((TextView) view.findViewById(android.R.id.text1)).setText(
439 cursor.getString(cursor.getColumnIndexOrThrow(
440 BrowserContract.Bookmarks.TITLE)));
441 }
442
443 @Override
444 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700445 View view = LayoutInflater.from(context).inflate(
446 R.layout.folder_list_item, null);
447 view.setBackgroundDrawable(context.getResources().
448 getDrawable(android.R.drawable.list_selector_background));
449 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400450 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400451
452 @Override
453 public boolean isEmpty() {
454 // Do not show the empty view if the user is creating a new folder.
455 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
456 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400457 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800458
459 protected void onCreate(Bundle icicle) {
460 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400461 if (DEBUG_CRASH) {
462 requestWindowFeature(Window.FEATURE_NO_TITLE);
463 } else {
464 requestWindowFeature(Window.FEATURE_LEFT_ICON);
465 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100466
467 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100468
Leon Scroggins III052ce662010-09-13 14:44:16 -0400469 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100470
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400471 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400472 if (!DEBUG_CRASH) {
473 setTitle(R.string.bookmark_this_page);
474 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
475 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700476
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 String title = null;
478 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100479
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400480 mFakeTitle = (TextView) findViewById(R.id.fake_title);
481
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482 if (mMap != null) {
483 Bundle b = mMap.getBundle("bookmark");
484 if (b != null) {
485 mMap = b;
486 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400487 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400488 if (!DEBUG_CRASH) {
489 setTitle(R.string.bookmark_this_page);
490 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400491 } else {
492 int gravity = mMap.getInt("gravity", -1);
493 if (gravity != -1) {
494 WindowManager.LayoutParams l = window.getAttributes();
495 l.gravity = gravity;
496 window.setAttributes(l);
497 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400499 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
500 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
501 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700502 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400503 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700504 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400505 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 }
507
508 mTitle = (EditText) findViewById(R.id.title);
509 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100510
Leon Scroggins III052ce662010-09-13 14:44:16 -0400511 mAddress = (EditText) findViewById(R.id.address);
512 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800513
The Android Open Source Project0c908882009-03-03 19:32:16 -0800514 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400515 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516
517 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400518 mCancelButton.setOnClickListener(this);
519
520 mFolder = (TextView) findViewById(R.id.folder);
521 mFolder.setOnClickListener(this);
522
523 mDefaultView = findViewById(R.id.default_view);
524 mFolderSelector = findViewById(R.id.folder_selector);
525
526 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
527 mFolderNamer.setOnEditorActionListener(this);
528
529 mAddNewFolder = findViewById(R.id.add_new_folder);
530 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400531 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400532
Leon Scroggins74dbe012010-10-15 10:54:27 -0400533 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
534 mCrumbs.setUseBackButton(true);
535 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400536 String name = getString(R.string.bookmarks);
537 mCrumbs.pushView(name, false,
538 new Folder(name, BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400539 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400540
Leon Scroggins III052ce662010-09-13 14:44:16 -0400541 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400542 mListView = (ListView) findViewById(R.id.list);
543 View empty = findViewById(R.id.empty);
544 mListView.setEmptyView(empty);
545 mListView.setAdapter(mAdapter);
546 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400547 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500548 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400549 // Find all the folders
550 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
551 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400552 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400553 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
554
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700555
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400556 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 mButton.requestFocus();
558 }
559 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100560
Leon Scroggins25230d72010-09-28 20:09:25 -0400561 // FIXME: Use a CursorLoader
562 private long getBookmarksBarId(Context context) {
563 SharedPreferences prefs
564 = PreferenceManager.getDefaultSharedPreferences(context);
565 String accountName =
566 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
567 String accountType =
568 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
569 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500570 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400571 }
572 Cursor cursor = null;
573 try {
574 cursor = context.getContentResolver().query(
575 BrowserContract.Bookmarks.CONTENT_URI,
576 new String[] { BrowserContract.Bookmarks._ID },
577 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
578 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
579 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
580 new String[] {
581 BrowserContract.ChromeSyncColumns
582 .FOLDER_NAME_BOOKMARKS_BAR,
583 accountName,
584 accountType },
585 null);
586 if (cursor != null && cursor.moveToFirst()) {
587 return cursor.getLong(0);
588 }
589 } finally {
590 if (cursor != null) cursor.close();
591 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500592 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400593 }
594
Leon Scroggins02065b02010-01-04 14:30:13 -0500595 /**
596 * Runnable to save a bookmark, so it can be performed in its own thread.
597 */
598 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400599 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500600 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200601 private Context mContext;
602 public SaveBookmarkRunnable(Context ctx, Message msg) {
603 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500604 mMessage = msg;
605 }
606 public void run() {
607 // Unbundle bookmark data.
608 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400609 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
610 String url = bundle.getString(BrowserContract.Bookmarks.URL);
611 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500612 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400613 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
614 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500615
616 // Save to the bookmarks DB.
617 try {
618 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400619 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
620 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500621 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200622 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500623 }
624 mMessage.arg1 = 1;
625 } catch (IllegalStateException e) {
626 mMessage.arg1 = 0;
627 }
628 mMessage.sendToTarget();
629 }
630 }
631
Ben Murdoch1794fe22009-09-29 18:14:30 +0100632 private void createHandler() {
633 if (mHandler == null) {
634 mHandler = new Handler() {
635 @Override
636 public void handleMessage(Message msg) {
637 switch (msg.what) {
638 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500639 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100640 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
641 Toast.LENGTH_LONG).show();
642 } else {
643 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
644 Toast.LENGTH_LONG).show();
645 }
646 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400647 case TOUCH_ICON_DOWNLOADED:
648 Bundle b = msg.getData();
649 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400650 AddBookmarkPage.this,
651 b.getString(BrowserContract.Bookmarks.URL),
652 b.getString(BrowserContract.Bookmarks.TITLE),
653 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
654 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400655 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100656 }
657 }
658 };
659 }
660 }
661
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100663 * Parse the data entered in the dialog and post a message to update the bookmarks database.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 */
665 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100666 createHandler();
667
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100669 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400670 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100671
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 boolean emptyTitle = title.length() == 0;
673 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
674 Resources r = getResources();
675 if (emptyTitle || emptyUrl) {
676 if (emptyTitle) {
677 mTitle.setError(r.getText(R.string.bookmark_needs_title));
678 }
679 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400680 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800681 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400682 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100683
The Android Open Source Project0c908882009-03-03 19:32:16 -0800684 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000685 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400686 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000687 // We allow bookmarks with a javascript: scheme, but these will in most cases
688 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
689
690 if (!url.toLowerCase().startsWith("javascript:")) {
691 URI uriObj = new URI(url);
692 String scheme = uriObj.getScheme();
693 if (!Bookmarks.urlHasAcceptableScheme(url)) {
694 // If the scheme was non-null, let the user know that we
695 // can't save their bookmark. If it was null, we'll assume
696 // they meant http when we parse it in the WebAddress class.
697 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400698 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000699 return false;
700 }
701 WebAddress address;
702 try {
703 address = new WebAddress(unfilteredUrl);
704 } catch (ParseException e) {
705 throw new URISyntaxException("", "");
706 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100707 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000708 throw new URISyntaxException("", "");
709 }
710 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100711 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800712 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400713 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400714 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400715 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800716 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100717
Leon Scroggins88d08032010-10-21 15:17:10 -0400718 if (mSaveToHomeScreen) {
719 mEditingExisting = false;
720 }
721
722 boolean urlUnmodified = url.equals(mOriginalUrl);
723
Ben Murdoch1794fe22009-09-29 18:14:30 +0100724 if (mEditingExisting) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400725 mMap.putString(BrowserContract.Bookmarks.TITLE, title);
726 mMap.putString(BrowserContract.Bookmarks.URL, url);
727 mMap.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400728 // FIXME: This does not work yet
729 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100730 setResult(RESULT_OK, (new Intent()).setAction(
731 getIntent().toString()).putExtras(mMap));
732 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400733 Bitmap thumbnail;
734 Bitmap favicon;
735 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400736 thumbnail = (Bitmap) mMap.getParcelable(
737 BrowserContract.Bookmarks.THUMBNAIL);
738 favicon = (Bitmap) mMap.getParcelable(
739 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400740 } else {
741 thumbnail = null;
742 favicon = null;
743 }
744
Ben Murdoch1794fe22009-09-29 18:14:30 +0100745 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400746 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
747 bundle.putString(BrowserContract.Bookmarks.URL, url);
748 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400749
750 if (mSaveToHomeScreen) {
751 if (mTouchIconUrl != null && urlUnmodified) {
752 Message msg = Message.obtain(mHandler,
753 TOUCH_ICON_DOWNLOADED);
754 msg.setData(bundle);
755 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400756 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400757 icon.execute(mTouchIconUrl);
758 } else {
759 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
760 title, null /*touchIcon*/, favicon));
761 }
762 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400763 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
764 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
765 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400766 // Post a message to write to the DB.
767 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
768 msg.setData(bundle);
769 // Start a new thread so as to not slow down the UI
770 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
771 t.start();
772 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100773 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000774 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800775 }
776 return true;
777 }
Michael Kolb31829b92010-10-01 11:50:21 -0700778
The Android Open Source Project0c908882009-03-03 19:32:16 -0800779}