blob: 25c92177859b96e90b4a2d34f58346cb1ab863e8 [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;
Leon Scroggins02081942010-11-01 17:52:42 -040034import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070036import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.net.WebAddress;
38import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010039import android.os.Handler;
40import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040041import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040042import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040043import android.text.TextUtils;
Leon Scroggins III052ce662010-09-13 14:44:16 -040044import android.view.KeyEvent;
45import android.view.LayoutInflater;
Leon Scroggins88d08032010-10-21 15:17:10 -040046import android.view.MenuItem;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040048import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040050import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040051import android.view.inputmethod.EditorInfo;
52import android.view.inputmethod.InputMethodManager;
53import android.widget.AdapterView;
54import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040056import android.widget.ListView;
Leon Scroggins88d08032010-10-21 15:17:10 -040057import android.widget.PopupMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.widget.TextView;
59import android.widget.Toast;
60
Cary Clarkf2407c62009-09-04 12:25:10 -040061import java.net.URI;
62import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040063import java.util.ArrayList;
Leon Scroggins74dbe012010-10-15 10:54:27 -040064import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040065
66public class AddBookmarkPage extends Activity
67 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040068 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
Leon Scroggins88d08032010-10-21 15:17:10 -040069 BreadCrumbView.Controller, PopupMenu.OnMenuItemClickListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080070
Michael Kolb370a4f32010-10-06 10:45:32 -070071 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040072 public static final String TOUCH_ICON_URL = "touch_icon_url";
73 // Place on an edited bookmark to remove the saved thumbnail
74 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
75 public static final String USER_AGENT = "user_agent";
Michael Kolb370a4f32010-10-06 10:45:32 -070076
Leon Scroggins74dbe012010-10-15 10:54:27 -040077 private static final int MAX_CRUMBS_SHOWN = 2;
78
The Android Open Source Project0c908882009-03-03 19:32:16 -080079 private final String LOGTAG = "Bookmarks";
Leon Scroggins74dbe012010-10-15 10:54:27 -040080 // Set to true to see the crash on the code I would like to run.
81 private final boolean DEBUG_CRASH = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080082
Leon Scroggins III052ce662010-09-13 14:44:16 -040083 // IDs for the CursorLoaders that are used.
84 private final int LOADER_ID_FOLDER_CONTENTS = 0;
85 private final int LOADER_ID_ALL_FOLDERS = 1;
86
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 private EditText mTitle;
88 private EditText mAddress;
89 private TextView mButton;
90 private View mCancelButton;
91 private boolean mEditingExisting;
92 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040093 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010094 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040095 private TextView mFolder;
96 private View mDefaultView;
97 private View mFolderSelector;
98 private EditText mFolderNamer;
99 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400100 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400101 private long mCurrentFolder = 0;
102 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400103 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400104 private TextView mFakeTitle;
105 private View mCrumbHolder;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400106 private ListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400107 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400108 private long mRootFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400109
110 private static class Folder {
111 String Name;
112 long Id;
113 Folder(String name, long id) {
114 Name = name;
115 Id = id;
116 }
117 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118
Ben Murdoch1794fe22009-09-29 18:14:30 +0100119 // Message IDs
120 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400121 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100122
123 private Handler mHandler;
124
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100125 private InputMethodManager getInputMethodManager() {
126 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
127 }
128
Leon Scroggins III052ce662010-09-13 14:44:16 -0400129 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400130 public void onTop(int level, Object data) {
131 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400132 Folder folderData = (Folder) data;
133 long folder = folderData.Id;
134 Uri uri = BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400135 LoaderManager manager = getLoaderManager();
136 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
137 LOADER_ID_FOLDER_CONTENTS));
138 loader.setUri(uri);
139 loader.forceLoad();
140 updateVisible();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400141 if (mFolderNamer.getVisibility() == View.VISIBLE) {
142 completeOrCancelFolderNaming(true);
143 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400144 }
145
146 /**
147 * Update the views shown to only show the two deepest levels of crumbs.
148 * Note that this method depends on internal knowledge of BreadCrumbView.
149 */
150 private void updateVisible() {
151 if (MAX_CRUMBS_SHOWN > 0) {
152 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
153 // This class always uses a back button, which is the first child.
154 int childIndex = 1;
155 if (invisibleCrumbs > 0) {
156 int crumbIndex = 0;
157 while (crumbIndex < invisibleCrumbs) {
158 // Set the crumb to GONE.
159 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
160 childIndex++;
161 // Each crumb is followed by a separator (except the last
162 // one). Also make it GONE
163 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
164 childIndex++;
165 // Move to the next crumb.
166 crumbIndex++;
167 }
168 }
169 // Make sure the last two are visible.
170 int childCount = mCrumbs.getChildCount();
171 while (childIndex < childCount) {
172 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
173 childIndex++;
174 }
175 }
176 }
177
178 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400179 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
180 if (v == mFolderNamer) {
181 if (v.getText().length() > 0) {
182 if (actionId == EditorInfo.IME_NULL) {
183 // Only want to do this once.
184 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400185 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400186 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400187 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 }
Michael Kolb31829b92010-10-01 11:50:21 -0700189 // Steal the key press; otherwise a newline will be added
190 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400192 return false;
193 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800194
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400195 private void switchToDefaultView(boolean changedFolder) {
196 mFolderSelector.setVisibility(View.GONE);
197 mDefaultView.setVisibility(View.VISIBLE);
198 mCrumbHolder.setVisibility(View.GONE);
199 mFakeTitle.setVisibility(View.VISIBLE);
200 if (changedFolder) {
201 Object data = mCrumbs.getTopData();
202 if (data != null) {
203 Folder folder = (Folder) data;
204 mCurrentFolder = folder.Id;
Leon Scroggins02081942010-11-01 17:52:42 -0400205 int resource = mCurrentFolder == mRootFolder ?
206 R.drawable.ic_menu_bookmarks :
207 com.android.internal.R.drawable.ic_menu_archive;
208 Drawable drawable = getResources().getDrawable(resource);
209 updateFolderLabel(folder.Name, drawable);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400210 }
211 }
212 }
213
Leon Scroggins III052ce662010-09-13 14:44:16 -0400214 @Override
215 public void onClick(View v) {
216 if (v == mButton) {
217 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400218 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700219 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400220 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700221 } else {
222 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400223 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400224 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700225 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400226 } else if (save()) {
227 finish();
228 }
229 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700230 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400231 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400232 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
233 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700234 } else {
235 finish();
236 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400237 } else if (v == mFolder) {
Leon Scroggins26318a12010-11-08 10:08:40 -0500238 PopupMenu popup = new PopupMenu(this, mFolder);
Leon Scroggins88d08032010-10-21 15:17:10 -0400239 popup.getMenuInflater().inflate(R.menu.folder_choice,
240 popup.getMenu());
241 popup.setOnMenuItemClickListener(this);
242 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400243 } else if (v == mAddNewFolder) {
244 mFolderNamer.setVisibility(View.VISIBLE);
245 mFolderNamer.setText(R.string.new_folder);
246 mFolderNamer.requestFocus();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400247 updateList();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700248 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400249 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100250 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400251 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400253 }
254
Leon Scroggins88d08032010-10-21 15:17:10 -0400255 @Override
256 public boolean onMenuItemClick(MenuItem item) {
257 switch(item.getItemId()) {
258 case R.id.bookmarks:
Leon Scroggins02081942010-11-01 17:52:42 -0400259 mCurrentFolder = mRootFolder;
260 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400261 mSaveToHomeScreen = false;
262 break;
263 case R.id.home_screen:
264 // Create a short cut to the home screen
265 mSaveToHomeScreen = true;
Leon Scroggins02081942010-11-01 17:52:42 -0400266 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400267 break;
268 case R.id.other:
269 switchToFolderSelector();
270 break;
271 default:
272 return false;
273 }
274 return true;
275 }
276
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400277 // Refresh the ListView to hide or show the empty view, as necessary.
278 // Should be called after mFolderNamer is shown or hidden.
279 private void updateList() {
280 if (mAdapter.getCount() == 0) {
281 // XXX: Is there a better way to refresh the ListView?
282 mListView.setAdapter(mAdapter);
283 }
284 }
285
286 private void completeOrCancelFolderNaming(boolean cancel) {
287 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700288 String name = mFolderNamer.getText().toString();
289 long id = addFolderToCurrent(mFolderNamer.getText().toString());
290 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700291 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400292 mFolderNamer.setVisibility(View.GONE);
293 mAddNewFolder.setVisibility(View.VISIBLE);
294 mAddSeparator.setVisibility(View.VISIBLE);
295 getInputMethodManager().hideSoftInputFromWindow(
296 mFolderNamer.getWindowToken(), 0);
297 updateList();
Michael Kolb31829b92010-10-01 11:50:21 -0700298 }
299
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700300 private long addFolderToCurrent(String name) {
301 // Add the folder to the database
302 ContentValues values = new ContentValues();
303 values.put(BrowserContract.Bookmarks.TITLE,
304 name);
305 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
John Recke89daa92010-11-05 14:39:39 -0700306 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
307 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
308 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
309 if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) {
310 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountType);
311 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountName);
312 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400313 long currentFolder;
314 Object data = mCrumbs.getTopData();
315 if (data != null) {
316 currentFolder = ((Folder) data).Id;
317 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400318 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400319 }
320 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700321 Uri uri = getContentResolver().insert(
322 BrowserContract.Bookmarks.CONTENT_URI, values);
323 if (uri != null) {
324 return ContentUris.parseId(uri);
325 } else {
326 return -1;
327 }
328 }
329
Leon Scroggins III052ce662010-09-13 14:44:16 -0400330 private void switchToFolderSelector() {
331 mDefaultView.setVisibility(View.GONE);
332 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400333 mCrumbHolder.setVisibility(View.VISIBLE);
334 mFakeTitle.setVisibility(View.GONE);
335 mAddNewFolder.setVisibility(View.VISIBLE);
336 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400337 }
338
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700339 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700340 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400341 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400342 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700343 }
344 }
345
Leon Scroggins III052ce662010-09-13 14:44:16 -0400346 @Override
347 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
348 String[] projection;
349 switch (id) {
350 case LOADER_ID_ALL_FOLDERS:
351 projection = new String[] {
352 BrowserContract.Bookmarks._ID,
353 BrowserContract.Bookmarks.PARENT,
354 BrowserContract.Bookmarks.TITLE,
355 BrowserContract.Bookmarks.IS_FOLDER
356 };
357 return new CursorLoader(this,
358 BrowserContract.Bookmarks.CONTENT_URI,
359 projection,
360 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
361 null,
362 null);
363 case LOADER_ID_FOLDER_CONTENTS:
364 projection = new String[] {
365 BrowserContract.Bookmarks._ID,
366 BrowserContract.Bookmarks.TITLE,
367 BrowserContract.Bookmarks.IS_FOLDER
368 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400369 return new CursorLoader(this,
370 BrowserContract.Bookmarks.buildFolderUri(
371 mCurrentFolder),
372 projection,
373 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
374 null,
375 null);
376 default:
377 throw new AssertionError("Asking for nonexistant loader!");
378 }
379 }
380
381 @Override
382 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
383 switch (loader.getId()) {
384 case LOADER_ID_FOLDER_CONTENTS:
385 mAdapter.changeCursor(cursor);
386 break;
387 case LOADER_ID_ALL_FOLDERS:
388 long parent = mCurrentFolder;
389 int idIndex = cursor.getColumnIndexOrThrow(
390 BrowserContract.Bookmarks._ID);
391 int titleIndex = cursor.getColumnIndexOrThrow(
392 BrowserContract.Bookmarks.TITLE);
393 int parentIndex = cursor.getColumnIndexOrThrow(
394 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400395 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700396 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
397 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400398 // First, find the folder corresponding to the current
399 // folder
400 if (!cursor.moveToFirst()) {
401 throw new AssertionError("No folders in the database!");
402 }
403 long folder;
404 do {
405 folder = cursor.getLong(idIndex);
406 } while (folder != parent && cursor.moveToNext());
407 if (cursor.isAfterLast()) {
408 throw new AssertionError("Folder(id=" + parent
409 + ") holding this bookmark does not exist!");
410 }
411 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400412 if (parent == mCurrentFolder) {
Leon Scroggins02081942010-11-01 17:52:42 -0400413 Drawable draw = getResources().getDrawable(
414 com.android.internal.R.drawable.ic_menu_archive);
415 updateFolderLabel(name, draw);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400416 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400417 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400418 parent = cursor.getLong(parentIndex);
419 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400420 while (!folderStack.isEmpty()) {
421 Folder thisFolder = (Folder) folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400422 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400423 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400424 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400425 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400426 break;
427 default:
428 break;
429 }
430 }
431
Leon Scroggins02081942010-11-01 17:52:42 -0400432 /**
433 * Update the name and image to show where the bookmark will be added
434 * @param name Name of the location to save (folder name, bookmarks, or home
435 * screen.
436 * @param drawable Image to show corresponding to the save location.
437 */
438 void updateFolderLabel(CharSequence name, Drawable drawable) {
439 mFolder.setText(name);
440 mFolder.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
441 null);
442 }
443
Leon Scroggins III052ce662010-09-13 14:44:16 -0400444 @Override
445 public void onItemClick(AdapterView<?> parent, View view, int position,
446 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400447 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400448 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400449 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400450 }
451
452 /**
453 * Shows a list of names of folders.
454 */
455 private class FolderAdapter extends CursorAdapter {
456 public FolderAdapter(Context context) {
457 super(context, null);
458 }
459
460 @Override
461 public void bindView(View view, Context context, Cursor cursor) {
462 ((TextView) view.findViewById(android.R.id.text1)).setText(
463 cursor.getString(cursor.getColumnIndexOrThrow(
464 BrowserContract.Bookmarks.TITLE)));
465 }
466
467 @Override
468 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700469 View view = LayoutInflater.from(context).inflate(
470 R.layout.folder_list_item, null);
471 view.setBackgroundDrawable(context.getResources().
472 getDrawable(android.R.drawable.list_selector_background));
473 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400474 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400475
476 @Override
477 public boolean isEmpty() {
478 // Do not show the empty view if the user is creating a new folder.
479 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
480 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400481 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482
483 protected void onCreate(Bundle icicle) {
484 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400485 if (DEBUG_CRASH) {
486 requestWindowFeature(Window.FEATURE_NO_TITLE);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400487 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100488
489 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100490
Leon Scroggins III052ce662010-09-13 14:44:16 -0400491 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100492
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400493 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400494 if (!DEBUG_CRASH) {
Leon Scroggins02081942010-11-01 17:52:42 -0400495 setTitle("");
Leon Scroggins74dbe012010-10-15 10:54:27 -0400496 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700497
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 String title = null;
499 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100500
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400501 mFakeTitle = (TextView) findViewById(R.id.fake_title);
502
The Android Open Source Project0c908882009-03-03 19:32:16 -0800503 if (mMap != null) {
504 Bundle b = mMap.getBundle("bookmark");
505 if (b != null) {
506 mMap = b;
507 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400508 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400509 } else {
510 int gravity = mMap.getInt("gravity", -1);
511 if (gravity != -1) {
512 WindowManager.LayoutParams l = window.getAttributes();
513 l.gravity = gravity;
514 window.setAttributes(l);
515 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400517 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
518 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
519 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700520 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400521 }
Leon Scroggins02081942010-11-01 17:52:42 -0400522 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700523 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400524 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 }
526
527 mTitle = (EditText) findViewById(R.id.title);
528 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100529
Leon Scroggins III052ce662010-09-13 14:44:16 -0400530 mAddress = (EditText) findViewById(R.id.address);
531 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400534 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535
536 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400537 mCancelButton.setOnClickListener(this);
538
539 mFolder = (TextView) findViewById(R.id.folder);
540 mFolder.setOnClickListener(this);
541
542 mDefaultView = findViewById(R.id.default_view);
543 mFolderSelector = findViewById(R.id.folder_selector);
544
545 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
546 mFolderNamer.setOnEditorActionListener(this);
547
548 mAddNewFolder = findViewById(R.id.add_new_folder);
549 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400550 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400551
Leon Scroggins74dbe012010-10-15 10:54:27 -0400552 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
553 mCrumbs.setUseBackButton(true);
554 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400555 String name = getString(R.string.bookmarks);
556 mCrumbs.pushView(name, false,
557 new Folder(name, BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400558 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400559
Leon Scroggins III052ce662010-09-13 14:44:16 -0400560 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400561 mListView = (ListView) findViewById(R.id.list);
562 View empty = findViewById(R.id.empty);
563 mListView.setEmptyView(empty);
564 mListView.setAdapter(mAdapter);
565 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400566 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500567 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400568 // Find all the folders
569 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
570 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400571 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400572 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
573
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700574
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400575 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800576 mButton.requestFocus();
577 }
578 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100579
Leon Scroggins25230d72010-09-28 20:09:25 -0400580 // FIXME: Use a CursorLoader
581 private long getBookmarksBarId(Context context) {
582 SharedPreferences prefs
583 = PreferenceManager.getDefaultSharedPreferences(context);
584 String accountName =
585 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
586 String accountType =
587 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
588 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500589 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400590 }
591 Cursor cursor = null;
592 try {
593 cursor = context.getContentResolver().query(
594 BrowserContract.Bookmarks.CONTENT_URI,
595 new String[] { BrowserContract.Bookmarks._ID },
596 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
597 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
598 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
599 new String[] {
600 BrowserContract.ChromeSyncColumns
601 .FOLDER_NAME_BOOKMARKS_BAR,
602 accountName,
603 accountType },
604 null);
605 if (cursor != null && cursor.moveToFirst()) {
606 return cursor.getLong(0);
607 }
608 } finally {
609 if (cursor != null) cursor.close();
610 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500611 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400612 }
613
Leon Scroggins02065b02010-01-04 14:30:13 -0500614 /**
615 * Runnable to save a bookmark, so it can be performed in its own thread.
616 */
617 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400618 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500619 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200620 private Context mContext;
621 public SaveBookmarkRunnable(Context ctx, Message msg) {
622 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500623 mMessage = msg;
624 }
625 public void run() {
626 // Unbundle bookmark data.
627 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400628 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
629 String url = bundle.getString(BrowserContract.Bookmarks.URL);
630 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500631 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400632 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
633 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500634
635 // Save to the bookmarks DB.
636 try {
637 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400638 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
639 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500640 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200641 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500642 }
643 mMessage.arg1 = 1;
644 } catch (IllegalStateException e) {
645 mMessage.arg1 = 0;
646 }
647 mMessage.sendToTarget();
648 }
649 }
650
Ben Murdoch1794fe22009-09-29 18:14:30 +0100651 private void createHandler() {
652 if (mHandler == null) {
653 mHandler = new Handler() {
654 @Override
655 public void handleMessage(Message msg) {
656 switch (msg.what) {
657 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500658 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100659 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
660 Toast.LENGTH_LONG).show();
661 } else {
662 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
663 Toast.LENGTH_LONG).show();
664 }
665 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400666 case TOUCH_ICON_DOWNLOADED:
667 Bundle b = msg.getData();
668 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400669 AddBookmarkPage.this,
670 b.getString(BrowserContract.Bookmarks.URL),
671 b.getString(BrowserContract.Bookmarks.TITLE),
672 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
673 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400674 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100675 }
676 }
677 };
678 }
679 }
680
The Android Open Source Project0c908882009-03-03 19:32:16 -0800681 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100682 * 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 -0800683 */
684 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100685 createHandler();
686
The Android Open Source Project0c908882009-03-03 19:32:16 -0800687 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100688 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700689 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100690
The Android Open Source Project0c908882009-03-03 19:32:16 -0800691 boolean emptyTitle = title.length() == 0;
692 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
693 Resources r = getResources();
694 if (emptyTitle || emptyUrl) {
695 if (emptyTitle) {
696 mTitle.setError(r.getText(R.string.bookmark_needs_title));
697 }
698 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400699 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800700 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400701 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100702
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000704 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400705 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000706 // We allow bookmarks with a javascript: scheme, but these will in most cases
707 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
708
709 if (!url.toLowerCase().startsWith("javascript:")) {
710 URI uriObj = new URI(url);
711 String scheme = uriObj.getScheme();
712 if (!Bookmarks.urlHasAcceptableScheme(url)) {
713 // If the scheme was non-null, let the user know that we
714 // can't save their bookmark. If it was null, we'll assume
715 // they meant http when we parse it in the WebAddress class.
716 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400717 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000718 return false;
719 }
720 WebAddress address;
721 try {
722 address = new WebAddress(unfilteredUrl);
723 } catch (ParseException e) {
724 throw new URISyntaxException("", "");
725 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100726 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000727 throw new URISyntaxException("", "");
728 }
729 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100730 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800731 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400732 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400733 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400734 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800735 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100736
Leon Scroggins88d08032010-10-21 15:17:10 -0400737 if (mSaveToHomeScreen) {
738 mEditingExisting = false;
739 }
740
741 boolean urlUnmodified = url.equals(mOriginalUrl);
742
Ben Murdoch1794fe22009-09-29 18:14:30 +0100743 if (mEditingExisting) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400744 mMap.putString(BrowserContract.Bookmarks.TITLE, title);
745 mMap.putString(BrowserContract.Bookmarks.URL, url);
746 mMap.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400747 // FIXME: This does not work yet
748 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100749 setResult(RESULT_OK, (new Intent()).setAction(
750 getIntent().toString()).putExtras(mMap));
751 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400752 Bitmap thumbnail;
753 Bitmap favicon;
754 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400755 thumbnail = (Bitmap) mMap.getParcelable(
756 BrowserContract.Bookmarks.THUMBNAIL);
757 favicon = (Bitmap) mMap.getParcelable(
758 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400759 } else {
760 thumbnail = null;
761 favicon = null;
762 }
763
Ben Murdoch1794fe22009-09-29 18:14:30 +0100764 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400765 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
766 bundle.putString(BrowserContract.Bookmarks.URL, url);
767 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400768
769 if (mSaveToHomeScreen) {
770 if (mTouchIconUrl != null && urlUnmodified) {
771 Message msg = Message.obtain(mHandler,
772 TOUCH_ICON_DOWNLOADED);
773 msg.setData(bundle);
774 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400775 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400776 icon.execute(mTouchIconUrl);
777 } else {
778 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
779 title, null /*touchIcon*/, favicon));
780 }
781 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400782 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
783 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
784 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400785 // Post a message to write to the DB.
786 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
787 msg.setData(bundle);
788 // Start a new thread so as to not slow down the UI
789 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
790 t.start();
791 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100792 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000793 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800794 }
795 return true;
796 }
Michael Kolb31829b92010-10-01 11:50:21 -0700797
The Android Open Source Project0c908882009-03-03 19:32:16 -0800798}