blob: 9e5b349c8f3eedda7c50ceaa6ecb5c111c436551 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040046import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040048import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040049import android.view.inputmethod.EditorInfo;
50import android.view.inputmethod.InputMethodManager;
51import android.widget.AdapterView;
52import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040054import android.widget.ListView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.TextView;
56import android.widget.Toast;
57
Cary Clarkf2407c62009-09-04 12:25:10 -040058import java.net.URI;
59import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040060import java.util.ArrayList;
Leon Scroggins74dbe012010-10-15 10:54:27 -040061import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040062
63public class AddBookmarkPage extends Activity
64 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040065 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
66 BreadCrumbView.Controller {
The Android Open Source Project0c908882009-03-03 19:32:16 -080067
Michael Kolb370a4f32010-10-06 10:45:32 -070068 public static final long DEFAULT_FOLDER_ID = -1;
69
Leon Scroggins74dbe012010-10-15 10:54:27 -040070 private static final int MAX_CRUMBS_SHOWN = 2;
71
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 private final String LOGTAG = "Bookmarks";
Leon Scroggins74dbe012010-10-15 10:54:27 -040073 // Set to true to see the crash on the code I would like to run.
74 private final boolean DEBUG_CRASH = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080075
Leon Scroggins III052ce662010-09-13 14:44:16 -040076 // IDs for the CursorLoaders that are used.
77 private final int LOADER_ID_FOLDER_CONTENTS = 0;
78 private final int LOADER_ID_ALL_FOLDERS = 1;
79
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 private EditText mTitle;
81 private EditText mAddress;
82 private TextView mButton;
83 private View mCancelButton;
84 private boolean mEditingExisting;
85 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040086 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010087 private Bitmap mThumbnail;
88 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040089 private TextView mFolder;
90 private View mDefaultView;
91 private View mFolderSelector;
92 private EditText mFolderNamer;
93 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040094 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -040095 private long mCurrentFolder = 0;
96 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -040097 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040098 private TextView mFakeTitle;
99 private View mCrumbHolder;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400100 private ListView mListView;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400101
102 private static class Folder {
103 String Name;
104 long Id;
105 Folder(String name, long id) {
106 Name = name;
107 Id = id;
108 }
109 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110
Ben Murdoch1794fe22009-09-29 18:14:30 +0100111 // Message IDs
112 private static final int SAVE_BOOKMARK = 100;
113
114 private Handler mHandler;
115
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100116 private InputMethodManager getInputMethodManager() {
117 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
118 }
119
Leon Scroggins III052ce662010-09-13 14:44:16 -0400120 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400121 public void onTop(int level, Object data) {
122 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400123 Folder folderData = (Folder) data;
124 long folder = folderData.Id;
125 Uri uri = BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400126 LoaderManager manager = getLoaderManager();
127 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
128 LOADER_ID_FOLDER_CONTENTS));
129 loader.setUri(uri);
130 loader.forceLoad();
131 updateVisible();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400132 if (mFolderNamer.getVisibility() == View.VISIBLE) {
133 completeOrCancelFolderNaming(true);
134 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400135 }
136
137 /**
138 * Update the views shown to only show the two deepest levels of crumbs.
139 * Note that this method depends on internal knowledge of BreadCrumbView.
140 */
141 private void updateVisible() {
142 if (MAX_CRUMBS_SHOWN > 0) {
143 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
144 // This class always uses a back button, which is the first child.
145 int childIndex = 1;
146 if (invisibleCrumbs > 0) {
147 int crumbIndex = 0;
148 while (crumbIndex < invisibleCrumbs) {
149 // Set the crumb to GONE.
150 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
151 childIndex++;
152 // Each crumb is followed by a separator (except the last
153 // one). Also make it GONE
154 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
155 childIndex++;
156 // Move to the next crumb.
157 crumbIndex++;
158 }
159 }
160 // Make sure the last two are visible.
161 int childCount = mCrumbs.getChildCount();
162 while (childIndex < childCount) {
163 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
164 childIndex++;
165 }
166 }
167 }
168
169 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400170 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
171 if (v == mFolderNamer) {
172 if (v.getText().length() > 0) {
173 if (actionId == EditorInfo.IME_NULL) {
174 // Only want to do this once.
175 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400176 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400177 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400178 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 }
Michael Kolb31829b92010-10-01 11:50:21 -0700180 // Steal the key press; otherwise a newline will be added
181 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400183 return false;
184 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400186 private void switchToDefaultView(boolean changedFolder) {
187 mFolderSelector.setVisibility(View.GONE);
188 mDefaultView.setVisibility(View.VISIBLE);
189 mCrumbHolder.setVisibility(View.GONE);
190 mFakeTitle.setVisibility(View.VISIBLE);
191 if (changedFolder) {
192 Object data = mCrumbs.getTopData();
193 if (data != null) {
194 Folder folder = (Folder) data;
195 mCurrentFolder = folder.Id;
196 mFolder.setText(folder.Name);
197 }
198 }
199 }
200
Leon Scroggins III052ce662010-09-13 14:44:16 -0400201 @Override
202 public void onClick(View v) {
203 if (v == mButton) {
204 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400205 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700206 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400207 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700208 } else {
209 // User has selected a folder. Go back to the opening page
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400210 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700211 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400212 } else if (save()) {
213 finish();
214 }
215 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700216 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400217 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400218 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
219 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700220 } else {
221 finish();
222 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400223 } else if (v == mFolder) {
224 switchToFolderSelector();
225 } else if (v == mAddNewFolder) {
226 mFolderNamer.setVisibility(View.VISIBLE);
227 mFolderNamer.setText(R.string.new_folder);
228 mFolderNamer.requestFocus();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400229 updateList();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700230 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400231 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100232 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400233 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400235 }
236
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400237 // Refresh the ListView to hide or show the empty view, as necessary.
238 // Should be called after mFolderNamer is shown or hidden.
239 private void updateList() {
240 if (mAdapter.getCount() == 0) {
241 // XXX: Is there a better way to refresh the ListView?
242 mListView.setAdapter(mAdapter);
243 }
244 }
245
246 private void completeOrCancelFolderNaming(boolean cancel) {
247 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700248 String name = mFolderNamer.getText().toString();
249 long id = addFolderToCurrent(mFolderNamer.getText().toString());
250 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700251 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400252 mFolderNamer.setVisibility(View.GONE);
253 mAddNewFolder.setVisibility(View.VISIBLE);
254 mAddSeparator.setVisibility(View.VISIBLE);
255 getInputMethodManager().hideSoftInputFromWindow(
256 mFolderNamer.getWindowToken(), 0);
257 updateList();
Michael Kolb31829b92010-10-01 11:50:21 -0700258 }
259
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700260 private long addFolderToCurrent(String name) {
261 // Add the folder to the database
262 ContentValues values = new ContentValues();
263 values.put(BrowserContract.Bookmarks.TITLE,
264 name);
265 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400266 long currentFolder;
267 Object data = mCrumbs.getTopData();
268 if (data != null) {
269 currentFolder = ((Folder) data).Id;
270 } else {
271 currentFolder = getBookmarksBarId(this);
272 }
273 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700274 Uri uri = getContentResolver().insert(
275 BrowserContract.Bookmarks.CONTENT_URI, values);
276 if (uri != null) {
277 return ContentUris.parseId(uri);
278 } else {
279 return -1;
280 }
281 }
282
Leon Scroggins III052ce662010-09-13 14:44:16 -0400283 private void switchToFolderSelector() {
284 mDefaultView.setVisibility(View.GONE);
285 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400286 mCrumbHolder.setVisibility(View.VISIBLE);
287 mFakeTitle.setVisibility(View.GONE);
288 mAddNewFolder.setVisibility(View.VISIBLE);
289 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400290 }
291
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700292 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700293 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400294 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400295 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700296 }
297 }
298
Leon Scroggins III052ce662010-09-13 14:44:16 -0400299 @Override
300 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
301 String[] projection;
302 switch (id) {
303 case LOADER_ID_ALL_FOLDERS:
304 projection = new String[] {
305 BrowserContract.Bookmarks._ID,
306 BrowserContract.Bookmarks.PARENT,
307 BrowserContract.Bookmarks.TITLE,
308 BrowserContract.Bookmarks.IS_FOLDER
309 };
310 return new CursorLoader(this,
311 BrowserContract.Bookmarks.CONTENT_URI,
312 projection,
313 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
314 null,
315 null);
316 case LOADER_ID_FOLDER_CONTENTS:
317 projection = new String[] {
318 BrowserContract.Bookmarks._ID,
319 BrowserContract.Bookmarks.TITLE,
320 BrowserContract.Bookmarks.IS_FOLDER
321 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400322 return new CursorLoader(this,
323 BrowserContract.Bookmarks.buildFolderUri(
324 mCurrentFolder),
325 projection,
326 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
327 null,
328 null);
329 default:
330 throw new AssertionError("Asking for nonexistant loader!");
331 }
332 }
333
334 @Override
335 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
336 switch (loader.getId()) {
337 case LOADER_ID_FOLDER_CONTENTS:
338 mAdapter.changeCursor(cursor);
339 break;
340 case LOADER_ID_ALL_FOLDERS:
341 long parent = mCurrentFolder;
342 int idIndex = cursor.getColumnIndexOrThrow(
343 BrowserContract.Bookmarks._ID);
344 int titleIndex = cursor.getColumnIndexOrThrow(
345 BrowserContract.Bookmarks.TITLE);
346 int parentIndex = cursor.getColumnIndexOrThrow(
347 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400348 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700349 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
350 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400351 // First, find the folder corresponding to the current
352 // folder
353 if (!cursor.moveToFirst()) {
354 throw new AssertionError("No folders in the database!");
355 }
356 long folder;
357 do {
358 folder = cursor.getLong(idIndex);
359 } while (folder != parent && cursor.moveToNext());
360 if (cursor.isAfterLast()) {
361 throw new AssertionError("Folder(id=" + parent
362 + ") holding this bookmark does not exist!");
363 }
364 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400365 if (parent == mCurrentFolder) {
366 mFolder.setText(name);
367 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400368 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400369 parent = cursor.getLong(parentIndex);
370 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400371 while (!folderStack.isEmpty()) {
372 Folder thisFolder = (Folder) folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400373 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400374 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400375 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400376 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400377 break;
378 default:
379 break;
380 }
381 }
382
Leon Scroggins III052ce662010-09-13 14:44:16 -0400383 @Override
384 public void onItemClick(AdapterView<?> parent, View view, int position,
385 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400386 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400387 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400388 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400389 }
390
391 /**
392 * Shows a list of names of folders.
393 */
394 private class FolderAdapter extends CursorAdapter {
395 public FolderAdapter(Context context) {
396 super(context, null);
397 }
398
399 @Override
400 public void bindView(View view, Context context, Cursor cursor) {
401 ((TextView) view.findViewById(android.R.id.text1)).setText(
402 cursor.getString(cursor.getColumnIndexOrThrow(
403 BrowserContract.Bookmarks.TITLE)));
404 }
405
406 @Override
407 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700408 View view = LayoutInflater.from(context).inflate(
409 R.layout.folder_list_item, null);
410 view.setBackgroundDrawable(context.getResources().
411 getDrawable(android.R.drawable.list_selector_background));
412 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400413 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400414
415 @Override
416 public boolean isEmpty() {
417 // Do not show the empty view if the user is creating a new folder.
418 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
419 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400420 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800421
422 protected void onCreate(Bundle icicle) {
423 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400424 if (DEBUG_CRASH) {
425 requestWindowFeature(Window.FEATURE_NO_TITLE);
426 } else {
427 requestWindowFeature(Window.FEATURE_LEFT_ICON);
428 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100429
430 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100431
Leon Scroggins III052ce662010-09-13 14:44:16 -0400432 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100433
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400434 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400435 if (!DEBUG_CRASH) {
436 setTitle(R.string.bookmark_this_page);
437 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
438 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700439
The Android Open Source Project0c908882009-03-03 19:32:16 -0800440 String title = null;
441 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100442
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400443 mFakeTitle = (TextView) findViewById(R.id.fake_title);
444
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 if (mMap != null) {
446 Bundle b = mMap.getBundle("bookmark");
447 if (b != null) {
448 mMap = b;
449 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400450 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400451 if (!DEBUG_CRASH) {
452 setTitle(R.string.bookmark_this_page);
453 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400454 } else {
455 int gravity = mMap.getInt("gravity", -1);
456 if (gravity != -1) {
457 WindowManager.LayoutParams l = window.getAttributes();
458 l.gravity = gravity;
459 window.setAttributes(l);
460 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 }
462 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100463 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400464 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100465 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
Michael Kolb370a4f32010-10-06 10:45:32 -0700466 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400467 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700468 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400469 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 }
471
472 mTitle = (EditText) findViewById(R.id.title);
473 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100474
Leon Scroggins III052ce662010-09-13 14:44:16 -0400475 mAddress = (EditText) findViewById(R.id.address);
476 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400479 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480
481 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400482 mCancelButton.setOnClickListener(this);
483
484 mFolder = (TextView) findViewById(R.id.folder);
485 mFolder.setOnClickListener(this);
486
487 mDefaultView = findViewById(R.id.default_view);
488 mFolderSelector = findViewById(R.id.folder_selector);
489
490 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
491 mFolderNamer.setOnEditorActionListener(this);
492
493 mAddNewFolder = findViewById(R.id.add_new_folder);
494 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400495 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400496
Leon Scroggins74dbe012010-10-15 10:54:27 -0400497 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
498 mCrumbs.setUseBackButton(true);
499 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400500 String name = getString(R.string.bookmarks);
501 mCrumbs.pushView(name, false,
502 new Folder(name, BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400503 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400504
Leon Scroggins III052ce662010-09-13 14:44:16 -0400505 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400506 mListView = (ListView) findViewById(R.id.list);
507 View empty = findViewById(R.id.empty);
508 mListView.setEmptyView(empty);
509 mListView.setAdapter(mAdapter);
510 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400511 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500512 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400513 // Find all the folders
514 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
515 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400516 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400517 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
518
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700519
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400520 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 mButton.requestFocus();
522 }
523 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100524
Leon Scroggins25230d72010-09-28 20:09:25 -0400525 // FIXME: Use a CursorLoader
526 private long getBookmarksBarId(Context context) {
527 SharedPreferences prefs
528 = PreferenceManager.getDefaultSharedPreferences(context);
529 String accountName =
530 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
531 String accountType =
532 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
533 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500534 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400535 }
536 Cursor cursor = null;
537 try {
538 cursor = context.getContentResolver().query(
539 BrowserContract.Bookmarks.CONTENT_URI,
540 new String[] { BrowserContract.Bookmarks._ID },
541 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
542 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
543 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
544 new String[] {
545 BrowserContract.ChromeSyncColumns
546 .FOLDER_NAME_BOOKMARKS_BAR,
547 accountName,
548 accountType },
549 null);
550 if (cursor != null && cursor.moveToFirst()) {
551 return cursor.getLong(0);
552 }
553 } finally {
554 if (cursor != null) cursor.close();
555 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500556 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400557 }
558
Leon Scroggins02065b02010-01-04 14:30:13 -0500559 /**
560 * Runnable to save a bookmark, so it can be performed in its own thread.
561 */
562 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400563 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500564 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200565 private Context mContext;
566 public SaveBookmarkRunnable(Context ctx, Message msg) {
567 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500568 mMessage = msg;
569 }
570 public void run() {
571 // Unbundle bookmark data.
572 Bundle bundle = mMessage.getData();
573 String title = bundle.getString("title");
574 String url = bundle.getString("url");
575 boolean invalidateThumbnail = bundle.getBoolean(
576 "invalidateThumbnail");
577 Bitmap thumbnail = invalidateThumbnail ? null
578 : (Bitmap) bundle.getParcelable("thumbnail");
579 String touchIconUrl = bundle.getString("touchIconUrl");
580
581 // Save to the bookmarks DB.
582 try {
583 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400584 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
585 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500586 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200587 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500588 }
589 mMessage.arg1 = 1;
590 } catch (IllegalStateException e) {
591 mMessage.arg1 = 0;
592 }
593 mMessage.sendToTarget();
594 }
595 }
596
Ben Murdoch1794fe22009-09-29 18:14:30 +0100597 private void createHandler() {
598 if (mHandler == null) {
599 mHandler = new Handler() {
600 @Override
601 public void handleMessage(Message msg) {
602 switch (msg.what) {
603 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500604 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100605 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
606 Toast.LENGTH_LONG).show();
607 } else {
608 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
609 Toast.LENGTH_LONG).show();
610 }
611 break;
612 }
613 }
614 };
615 }
616 }
617
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100619 * 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 -0800620 */
621 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100622 createHandler();
623
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100625 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400626 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100627
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 boolean emptyTitle = title.length() == 0;
629 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
630 Resources r = getResources();
631 if (emptyTitle || emptyUrl) {
632 if (emptyTitle) {
633 mTitle.setError(r.getText(R.string.bookmark_needs_title));
634 }
635 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400636 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800637 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400638 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100639
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000641 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400642 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000643 // We allow bookmarks with a javascript: scheme, but these will in most cases
644 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
645
646 if (!url.toLowerCase().startsWith("javascript:")) {
647 URI uriObj = new URI(url);
648 String scheme = uriObj.getScheme();
649 if (!Bookmarks.urlHasAcceptableScheme(url)) {
650 // If the scheme was non-null, let the user know that we
651 // can't save their bookmark. If it was null, we'll assume
652 // they meant http when we parse it in the WebAddress class.
653 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400654 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000655 return false;
656 }
657 WebAddress address;
658 try {
659 address = new WebAddress(unfilteredUrl);
660 } catch (ParseException e) {
661 throw new URISyntaxException("", "");
662 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100663 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000664 throw new URISyntaxException("", "");
665 }
666 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100667 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400669 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400670 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400671 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100673
Ben Murdoch1794fe22009-09-29 18:14:30 +0100674 if (mEditingExisting) {
675 mMap.putString("title", title);
676 mMap.putString("url", url);
677 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400678 // FIXME: This does not work yet
679 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100680 setResult(RESULT_OK, (new Intent()).setAction(
681 getIntent().toString()).putExtras(mMap));
682 } else {
683 // Post a message to write to the DB.
684 Bundle bundle = new Bundle();
685 bundle.putString("title", title);
686 bundle.putString("url", url);
687 bundle.putParcelable("thumbnail", mThumbnail);
688 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
689 bundle.putString("touchIconUrl", mTouchIconUrl);
690 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
691 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500692 // Start a new thread so as to not slow down the UI
Henrik Baard980e9952010-09-06 18:13:23 +0200693 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
Leon Scroggins02065b02010-01-04 14:30:13 -0500694 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100695 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000696 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800697 }
698 return true;
699 }
Michael Kolb31829b92010-10-01 11:50:21 -0700700
The Android Open Source Project0c908882009-03-03 19:32:16 -0800701}