blob: 1f14629ecf4cd26a6f63dfd2acc4207a08bfd05b [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;
94 private long mCurrentFolder = 0;
95 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -040096 private BreadCrumbView mCrumbs;
97 private View mFakeTitleBar;
Leon Scroggins III052ce662010-09-13 14:44:16 -040098
99 private static class Folder {
100 String Name;
101 long Id;
102 Folder(String name, long id) {
103 Name = name;
104 Id = id;
105 }
106 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107
Ben Murdoch1794fe22009-09-29 18:14:30 +0100108 // Message IDs
109 private static final int SAVE_BOOKMARK = 100;
110
111 private Handler mHandler;
112
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100113 private InputMethodManager getInputMethodManager() {
114 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
115 }
116
Leon Scroggins III052ce662010-09-13 14:44:16 -0400117 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400118 public void onTop(int level, Object data) {
119 if (null == data) return;
120 mCurrentFolder = (Long) data;
121 Uri uri = BrowserContract.Bookmarks.buildFolderUri(mCurrentFolder);
122 LoaderManager manager = getLoaderManager();
123 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
124 LOADER_ID_FOLDER_CONTENTS));
125 loader.setUri(uri);
126 loader.forceLoad();
127 updateVisible();
128 }
129
130 /**
131 * Update the views shown to only show the two deepest levels of crumbs.
132 * Note that this method depends on internal knowledge of BreadCrumbView.
133 */
134 private void updateVisible() {
135 if (MAX_CRUMBS_SHOWN > 0) {
136 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
137 // This class always uses a back button, which is the first child.
138 int childIndex = 1;
139 if (invisibleCrumbs > 0) {
140 int crumbIndex = 0;
141 while (crumbIndex < invisibleCrumbs) {
142 // Set the crumb to GONE.
143 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
144 childIndex++;
145 // Each crumb is followed by a separator (except the last
146 // one). Also make it GONE
147 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
148 childIndex++;
149 // Move to the next crumb.
150 crumbIndex++;
151 }
152 }
153 // Make sure the last two are visible.
154 int childCount = mCrumbs.getChildCount();
155 while (childIndex < childCount) {
156 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
157 childIndex++;
158 }
159 }
160 }
161
162 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400163 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
164 if (v == mFolderNamer) {
165 if (v.getText().length() > 0) {
166 if (actionId == EditorInfo.IME_NULL) {
167 // Only want to do this once.
168 if (event.getAction() == KeyEvent.ACTION_UP) {
Michael Kolb31829b92010-10-01 11:50:21 -0700169 completeFolderNaming();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400170 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400171 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 }
Michael Kolb31829b92010-10-01 11:50:21 -0700173 // Steal the key press; otherwise a newline will be added
174 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400176 return false;
177 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178
Leon Scroggins III052ce662010-09-13 14:44:16 -0400179 @Override
180 public void onClick(View v) {
181 if (v == mButton) {
182 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700183 // We are showing the folder selector.
184 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Michael Kolb31829b92010-10-01 11:50:21 -0700185 completeFolderNaming();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700186 } else {
187 // User has selected a folder. Go back to the opening page
188 mFolderSelector.setVisibility(View.GONE);
189 mDefaultView.setVisibility(View.VISIBLE);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400190 mCrumbs.setVisibility(View.GONE);
191 mFakeTitleBar.setVisibility(View.VISIBLE);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700192 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400193 } else if (save()) {
194 finish();
195 }
196 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700197 if (mFolderNamer.getVisibility() == View.VISIBLE) {
198 mFolderNamer.setVisibility(View.GONE);
199 mAddNewFolder.setVisibility(View.VISIBLE);
200 } else {
201 finish();
202 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400203 } else if (v == mFolder) {
204 switchToFolderSelector();
205 } else if (v == mAddNewFolder) {
206 mFolderNamer.setVisibility(View.VISIBLE);
207 mFolderNamer.setText(R.string.new_folder);
208 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700209 mAddNewFolder.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100210 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400211 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400213 }
214
Michael Kolb31829b92010-10-01 11:50:21 -0700215 private void completeFolderNaming() {
216 if (!TextUtils.isEmpty(mFolderNamer.getText())) {
217 String name = mFolderNamer.getText().toString();
218 long id = addFolderToCurrent(mFolderNamer.getText().toString());
219 descendInto(name, id);
220 mFolderNamer.setVisibility(View.GONE);
221 mAddNewFolder.setVisibility(View.VISIBLE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100222 getInputMethodManager().hideSoftInputFromWindow(
Michael Kolb31829b92010-10-01 11:50:21 -0700223 mFolderNamer.getWindowToken(), 0);
224 }
225 }
226
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700227 private long addFolderToCurrent(String name) {
228 // Add the folder to the database
229 ContentValues values = new ContentValues();
230 values.put(BrowserContract.Bookmarks.TITLE,
231 name);
232 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
233 values.put(BrowserContract.Bookmarks.PARENT,
234 mCurrentFolder);
235 Uri uri = getContentResolver().insert(
236 BrowserContract.Bookmarks.CONTENT_URI, values);
237 if (uri != null) {
238 return ContentUris.parseId(uri);
239 } else {
240 return -1;
241 }
242 }
243
Leon Scroggins III052ce662010-09-13 14:44:16 -0400244 private void switchToFolderSelector() {
245 mDefaultView.setVisibility(View.GONE);
246 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400247 mCrumbs.setVisibility(View.VISIBLE);
248 mFakeTitleBar.setVisibility(View.GONE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400249 }
250
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700251 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700252 if (id != DEFAULT_FOLDER_ID) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700253 mCurrentFolder = id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400254 mCrumbs.pushView(foldername, id);
255 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700256 }
257 }
258
Leon Scroggins III052ce662010-09-13 14:44:16 -0400259 @Override
260 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
261 String[] projection;
262 switch (id) {
263 case LOADER_ID_ALL_FOLDERS:
264 projection = new String[] {
265 BrowserContract.Bookmarks._ID,
266 BrowserContract.Bookmarks.PARENT,
267 BrowserContract.Bookmarks.TITLE,
268 BrowserContract.Bookmarks.IS_FOLDER
269 };
270 return new CursorLoader(this,
271 BrowserContract.Bookmarks.CONTENT_URI,
272 projection,
273 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
274 null,
275 null);
276 case LOADER_ID_FOLDER_CONTENTS:
277 projection = new String[] {
278 BrowserContract.Bookmarks._ID,
279 BrowserContract.Bookmarks.TITLE,
280 BrowserContract.Bookmarks.IS_FOLDER
281 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400282 return new CursorLoader(this,
283 BrowserContract.Bookmarks.buildFolderUri(
284 mCurrentFolder),
285 projection,
286 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
287 null,
288 null);
289 default:
290 throw new AssertionError("Asking for nonexistant loader!");
291 }
292 }
293
294 @Override
295 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
296 switch (loader.getId()) {
297 case LOADER_ID_FOLDER_CONTENTS:
298 mAdapter.changeCursor(cursor);
299 break;
300 case LOADER_ID_ALL_FOLDERS:
301 long parent = mCurrentFolder;
302 int idIndex = cursor.getColumnIndexOrThrow(
303 BrowserContract.Bookmarks._ID);
304 int titleIndex = cursor.getColumnIndexOrThrow(
305 BrowserContract.Bookmarks.TITLE);
306 int parentIndex = cursor.getColumnIndexOrThrow(
307 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400308 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700309 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
310 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400311 // First, find the folder corresponding to the current
312 // folder
313 if (!cursor.moveToFirst()) {
314 throw new AssertionError("No folders in the database!");
315 }
316 long folder;
317 do {
318 folder = cursor.getLong(idIndex);
319 } while (folder != parent && cursor.moveToNext());
320 if (cursor.isAfterLast()) {
321 throw new AssertionError("Folder(id=" + parent
322 + ") holding this bookmark does not exist!");
323 }
324 String name = cursor.getString(titleIndex);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400325 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400326 parent = cursor.getLong(parentIndex);
327 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400328 while (!folderStack.isEmpty()) {
329 Folder thisFolder = (Folder) folderStack.pop();
330 mCrumbs.pushView(thisFolder.Name, thisFolder.Id);
331 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400332 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400333 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400334 break;
335 default:
336 break;
337 }
338 }
339
Leon Scroggins III052ce662010-09-13 14:44:16 -0400340 @Override
341 public void onItemClick(AdapterView<?> parent, View view, int position,
342 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400343 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400344 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400345 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400346 }
347
348 /**
349 * Shows a list of names of folders.
350 */
351 private class FolderAdapter extends CursorAdapter {
352 public FolderAdapter(Context context) {
353 super(context, null);
354 }
355
356 @Override
357 public void bindView(View view, Context context, Cursor cursor) {
358 ((TextView) view.findViewById(android.R.id.text1)).setText(
359 cursor.getString(cursor.getColumnIndexOrThrow(
360 BrowserContract.Bookmarks.TITLE)));
361 }
362
363 @Override
364 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700365 View view = LayoutInflater.from(context).inflate(
366 R.layout.folder_list_item, null);
367 view.setBackgroundDrawable(context.getResources().
368 getDrawable(android.R.drawable.list_selector_background));
369 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400370 }
371 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800372
373 protected void onCreate(Bundle icicle) {
374 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400375 if (DEBUG_CRASH) {
376 requestWindowFeature(Window.FEATURE_NO_TITLE);
377 } else {
378 requestWindowFeature(Window.FEATURE_LEFT_ICON);
379 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100380
381 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100382
Leon Scroggins III052ce662010-09-13 14:44:16 -0400383 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100384
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400385 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400386 if (!DEBUG_CRASH) {
387 setTitle(R.string.bookmark_this_page);
388 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
389 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700390
The Android Open Source Project0c908882009-03-03 19:32:16 -0800391 String title = null;
392 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100393
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 if (mMap != null) {
395 Bundle b = mMap.getBundle("bookmark");
396 if (b != null) {
397 mMap = b;
398 mEditingExisting = true;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400399 TextView fakeTitle = (TextView) findViewById(R.id.fake_title);
400 fakeTitle.setText(R.string.edit_bookmark);
401 if (!DEBUG_CRASH) {
402 setTitle(R.string.bookmark_this_page);
403 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400404 } else {
405 int gravity = mMap.getInt("gravity", -1);
406 if (gravity != -1) {
407 WindowManager.LayoutParams l = window.getAttributes();
408 l.gravity = gravity;
409 window.setAttributes(l);
410 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 }
412 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100413 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400414 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100415 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
Michael Kolb370a4f32010-10-06 10:45:32 -0700416 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400417 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700418 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400419 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800420 }
421
422 mTitle = (EditText) findViewById(R.id.title);
423 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100424
Leon Scroggins III052ce662010-09-13 14:44:16 -0400425 mAddress = (EditText) findViewById(R.id.address);
426 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800427
The Android Open Source Project0c908882009-03-03 19:32:16 -0800428 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400429 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430
431 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400432 mCancelButton.setOnClickListener(this);
433
434 mFolder = (TextView) findViewById(R.id.folder);
435 mFolder.setOnClickListener(this);
436
437 mDefaultView = findViewById(R.id.default_view);
438 mFolderSelector = findViewById(R.id.folder_selector);
439
440 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
441 mFolderNamer.setOnEditorActionListener(this);
442
443 mAddNewFolder = findViewById(R.id.add_new_folder);
444 mAddNewFolder.setOnClickListener(this);
445
Leon Scroggins74dbe012010-10-15 10:54:27 -0400446 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
447 mCrumbs.setUseBackButton(true);
448 mCrumbs.setController(this);
449 mCrumbs.pushView(getString(R.string.bookmarks), false,
450 BrowserProvider2.FIXED_ID_ROOT);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400451
Leon Scroggins74dbe012010-10-15 10:54:27 -0400452 mFakeTitleBar = findViewById(R.id.fake_title_bar);
453
Leon Scroggins III052ce662010-09-13 14:44:16 -0400454 mAdapter = new FolderAdapter(this);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400455 ListView list = (ListView) findViewById(R.id.list);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400456 list.setAdapter(mAdapter);
457 list.setOnItemClickListener(this);
458 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500459 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400460 // Find all the folders
461 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
462 }
463 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
464
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700465
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400466 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 mButton.requestFocus();
468 }
469 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100470
Leon Scroggins25230d72010-09-28 20:09:25 -0400471 // FIXME: Use a CursorLoader
472 private long getBookmarksBarId(Context context) {
473 SharedPreferences prefs
474 = PreferenceManager.getDefaultSharedPreferences(context);
475 String accountName =
476 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
477 String accountType =
478 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
479 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500480 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400481 }
482 Cursor cursor = null;
483 try {
484 cursor = context.getContentResolver().query(
485 BrowserContract.Bookmarks.CONTENT_URI,
486 new String[] { BrowserContract.Bookmarks._ID },
487 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
488 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
489 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
490 new String[] {
491 BrowserContract.ChromeSyncColumns
492 .FOLDER_NAME_BOOKMARKS_BAR,
493 accountName,
494 accountType },
495 null);
496 if (cursor != null && cursor.moveToFirst()) {
497 return cursor.getLong(0);
498 }
499 } finally {
500 if (cursor != null) cursor.close();
501 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500502 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400503 }
504
Leon Scroggins02065b02010-01-04 14:30:13 -0500505 /**
506 * Runnable to save a bookmark, so it can be performed in its own thread.
507 */
508 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400509 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500510 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200511 private Context mContext;
512 public SaveBookmarkRunnable(Context ctx, Message msg) {
513 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500514 mMessage = msg;
515 }
516 public void run() {
517 // Unbundle bookmark data.
518 Bundle bundle = mMessage.getData();
519 String title = bundle.getString("title");
520 String url = bundle.getString("url");
521 boolean invalidateThumbnail = bundle.getBoolean(
522 "invalidateThumbnail");
523 Bitmap thumbnail = invalidateThumbnail ? null
524 : (Bitmap) bundle.getParcelable("thumbnail");
525 String touchIconUrl = bundle.getString("touchIconUrl");
526
527 // Save to the bookmarks DB.
528 try {
529 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400530 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
531 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500532 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200533 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500534 }
535 mMessage.arg1 = 1;
536 } catch (IllegalStateException e) {
537 mMessage.arg1 = 0;
538 }
539 mMessage.sendToTarget();
540 }
541 }
542
Ben Murdoch1794fe22009-09-29 18:14:30 +0100543 private void createHandler() {
544 if (mHandler == null) {
545 mHandler = new Handler() {
546 @Override
547 public void handleMessage(Message msg) {
548 switch (msg.what) {
549 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500550 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100551 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
552 Toast.LENGTH_LONG).show();
553 } else {
554 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
555 Toast.LENGTH_LONG).show();
556 }
557 break;
558 }
559 }
560 };
561 }
562 }
563
The Android Open Source Project0c908882009-03-03 19:32:16 -0800564 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100565 * 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 -0800566 */
567 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100568 createHandler();
569
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100571 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400572 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100573
The Android Open Source Project0c908882009-03-03 19:32:16 -0800574 boolean emptyTitle = title.length() == 0;
575 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
576 Resources r = getResources();
577 if (emptyTitle || emptyUrl) {
578 if (emptyTitle) {
579 mTitle.setError(r.getText(R.string.bookmark_needs_title));
580 }
581 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400582 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800583 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400584 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100585
The Android Open Source Project0c908882009-03-03 19:32:16 -0800586 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000587 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400588 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000589 // We allow bookmarks with a javascript: scheme, but these will in most cases
590 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
591
592 if (!url.toLowerCase().startsWith("javascript:")) {
593 URI uriObj = new URI(url);
594 String scheme = uriObj.getScheme();
595 if (!Bookmarks.urlHasAcceptableScheme(url)) {
596 // If the scheme was non-null, let the user know that we
597 // can't save their bookmark. If it was null, we'll assume
598 // they meant http when we parse it in the WebAddress class.
599 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400600 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000601 return false;
602 }
603 WebAddress address;
604 try {
605 address = new WebAddress(unfilteredUrl);
606 } catch (ParseException e) {
607 throw new URISyntaxException("", "");
608 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100609 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000610 throw new URISyntaxException("", "");
611 }
612 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100613 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400615 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400617 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100619
Ben Murdoch1794fe22009-09-29 18:14:30 +0100620 if (mEditingExisting) {
621 mMap.putString("title", title);
622 mMap.putString("url", url);
623 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400624 // FIXME: This does not work yet
625 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100626 setResult(RESULT_OK, (new Intent()).setAction(
627 getIntent().toString()).putExtras(mMap));
628 } else {
629 // Post a message to write to the DB.
630 Bundle bundle = new Bundle();
631 bundle.putString("title", title);
632 bundle.putString("url", url);
633 bundle.putParcelable("thumbnail", mThumbnail);
634 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
635 bundle.putString("touchIconUrl", mTouchIconUrl);
636 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
637 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500638 // Start a new thread so as to not slow down the UI
Henrik Baard980e9952010-09-06 18:13:23 +0200639 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
Leon Scroggins02065b02010-01-04 14:30:13 -0500640 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100641 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000642 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 }
644 return true;
645 }
Michael Kolb31829b92010-10-01 11:50:21 -0700646
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647}