blob: d0679e193510f60f578b41147bebf9561856badb [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 Scroggins III052ce662010-09-13 14:44:16 -0400100
101 private static class Folder {
102 String Name;
103 long Id;
104 Folder(String name, long id) {
105 Name = name;
106 Id = id;
107 }
108 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
Ben Murdoch1794fe22009-09-29 18:14:30 +0100110 // Message IDs
111 private static final int SAVE_BOOKMARK = 100;
112
113 private Handler mHandler;
114
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100115 private InputMethodManager getInputMethodManager() {
116 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
117 }
118
Leon Scroggins III052ce662010-09-13 14:44:16 -0400119 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400120 public void onTop(int level, Object data) {
121 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400122 Folder folderData = (Folder) data;
123 long folder = folderData.Id;
124 Uri uri = BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400125 LoaderManager manager = getLoaderManager();
126 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
127 LOADER_ID_FOLDER_CONTENTS));
128 loader.setUri(uri);
129 loader.forceLoad();
130 updateVisible();
131 }
132
133 /**
134 * Update the views shown to only show the two deepest levels of crumbs.
135 * Note that this method depends on internal knowledge of BreadCrumbView.
136 */
137 private void updateVisible() {
138 if (MAX_CRUMBS_SHOWN > 0) {
139 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
140 // This class always uses a back button, which is the first child.
141 int childIndex = 1;
142 if (invisibleCrumbs > 0) {
143 int crumbIndex = 0;
144 while (crumbIndex < invisibleCrumbs) {
145 // Set the crumb to GONE.
146 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
147 childIndex++;
148 // Each crumb is followed by a separator (except the last
149 // one). Also make it GONE
150 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
151 childIndex++;
152 // Move to the next crumb.
153 crumbIndex++;
154 }
155 }
156 // Make sure the last two are visible.
157 int childCount = mCrumbs.getChildCount();
158 while (childIndex < childCount) {
159 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
160 childIndex++;
161 }
162 }
163 }
164
165 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400166 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
167 if (v == mFolderNamer) {
168 if (v.getText().length() > 0) {
169 if (actionId == EditorInfo.IME_NULL) {
170 // Only want to do this once.
171 if (event.getAction() == KeyEvent.ACTION_UP) {
Michael Kolb31829b92010-10-01 11:50:21 -0700172 completeFolderNaming();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400173 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400174 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 }
Michael Kolb31829b92010-10-01 11:50:21 -0700176 // Steal the key press; otherwise a newline will be added
177 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400179 return false;
180 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400182 private void switchToDefaultView(boolean changedFolder) {
183 mFolderSelector.setVisibility(View.GONE);
184 mDefaultView.setVisibility(View.VISIBLE);
185 mCrumbHolder.setVisibility(View.GONE);
186 mFakeTitle.setVisibility(View.VISIBLE);
187 if (changedFolder) {
188 Object data = mCrumbs.getTopData();
189 if (data != null) {
190 Folder folder = (Folder) data;
191 mCurrentFolder = folder.Id;
192 mFolder.setText(folder.Name);
193 }
194 }
195 }
196
Leon Scroggins III052ce662010-09-13 14:44:16 -0400197 @Override
198 public void onClick(View v) {
199 if (v == mButton) {
200 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400201 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700202 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Michael Kolb31829b92010-10-01 11:50:21 -0700203 completeFolderNaming();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700204 } else {
205 // User has selected a folder. Go back to the opening page
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400206 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700207 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400208 } else if (save()) {
209 finish();
210 }
211 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700212 if (mFolderNamer.getVisibility() == View.VISIBLE) {
213 mFolderNamer.setVisibility(View.GONE);
214 mAddNewFolder.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400215 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400216 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
217 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700218 } else {
219 finish();
220 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400221 } else if (v == mFolder) {
222 switchToFolderSelector();
223 } else if (v == mAddNewFolder) {
224 mFolderNamer.setVisibility(View.VISIBLE);
225 mFolderNamer.setText(R.string.new_folder);
226 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700227 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400228 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100229 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400230 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400232 }
233
Michael Kolb31829b92010-10-01 11:50:21 -0700234 private void completeFolderNaming() {
235 if (!TextUtils.isEmpty(mFolderNamer.getText())) {
236 String name = mFolderNamer.getText().toString();
237 long id = addFolderToCurrent(mFolderNamer.getText().toString());
238 descendInto(name, id);
239 mFolderNamer.setVisibility(View.GONE);
240 mAddNewFolder.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400241 mAddSeparator.setVisibility(View.VISIBLE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100242 getInputMethodManager().hideSoftInputFromWindow(
Michael Kolb31829b92010-10-01 11:50:21 -0700243 mFolderNamer.getWindowToken(), 0);
244 }
245 }
246
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700247 private long addFolderToCurrent(String name) {
248 // Add the folder to the database
249 ContentValues values = new ContentValues();
250 values.put(BrowserContract.Bookmarks.TITLE,
251 name);
252 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400253 long currentFolder;
254 Object data = mCrumbs.getTopData();
255 if (data != null) {
256 currentFolder = ((Folder) data).Id;
257 } else {
258 currentFolder = getBookmarksBarId(this);
259 }
260 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700261 Uri uri = getContentResolver().insert(
262 BrowserContract.Bookmarks.CONTENT_URI, values);
263 if (uri != null) {
264 return ContentUris.parseId(uri);
265 } else {
266 return -1;
267 }
268 }
269
Leon Scroggins III052ce662010-09-13 14:44:16 -0400270 private void switchToFolderSelector() {
271 mDefaultView.setVisibility(View.GONE);
272 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400273 mCrumbHolder.setVisibility(View.VISIBLE);
274 mFakeTitle.setVisibility(View.GONE);
275 mAddNewFolder.setVisibility(View.VISIBLE);
276 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400277 }
278
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700279 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700280 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400281 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400282 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700283 }
284 }
285
Leon Scroggins III052ce662010-09-13 14:44:16 -0400286 @Override
287 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
288 String[] projection;
289 switch (id) {
290 case LOADER_ID_ALL_FOLDERS:
291 projection = new String[] {
292 BrowserContract.Bookmarks._ID,
293 BrowserContract.Bookmarks.PARENT,
294 BrowserContract.Bookmarks.TITLE,
295 BrowserContract.Bookmarks.IS_FOLDER
296 };
297 return new CursorLoader(this,
298 BrowserContract.Bookmarks.CONTENT_URI,
299 projection,
300 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
301 null,
302 null);
303 case LOADER_ID_FOLDER_CONTENTS:
304 projection = new String[] {
305 BrowserContract.Bookmarks._ID,
306 BrowserContract.Bookmarks.TITLE,
307 BrowserContract.Bookmarks.IS_FOLDER
308 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400309 return new CursorLoader(this,
310 BrowserContract.Bookmarks.buildFolderUri(
311 mCurrentFolder),
312 projection,
313 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
314 null,
315 null);
316 default:
317 throw new AssertionError("Asking for nonexistant loader!");
318 }
319 }
320
321 @Override
322 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
323 switch (loader.getId()) {
324 case LOADER_ID_FOLDER_CONTENTS:
325 mAdapter.changeCursor(cursor);
326 break;
327 case LOADER_ID_ALL_FOLDERS:
328 long parent = mCurrentFolder;
329 int idIndex = cursor.getColumnIndexOrThrow(
330 BrowserContract.Bookmarks._ID);
331 int titleIndex = cursor.getColumnIndexOrThrow(
332 BrowserContract.Bookmarks.TITLE);
333 int parentIndex = cursor.getColumnIndexOrThrow(
334 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400335 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700336 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
337 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400338 // First, find the folder corresponding to the current
339 // folder
340 if (!cursor.moveToFirst()) {
341 throw new AssertionError("No folders in the database!");
342 }
343 long folder;
344 do {
345 folder = cursor.getLong(idIndex);
346 } while (folder != parent && cursor.moveToNext());
347 if (cursor.isAfterLast()) {
348 throw new AssertionError("Folder(id=" + parent
349 + ") holding this bookmark does not exist!");
350 }
351 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400352 if (parent == mCurrentFolder) {
353 mFolder.setText(name);
354 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400355 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400356 parent = cursor.getLong(parentIndex);
357 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400358 while (!folderStack.isEmpty()) {
359 Folder thisFolder = (Folder) folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400360 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400361 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400362 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400363 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400364 break;
365 default:
366 break;
367 }
368 }
369
Leon Scroggins III052ce662010-09-13 14:44:16 -0400370 @Override
371 public void onItemClick(AdapterView<?> parent, View view, int position,
372 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400373 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400374 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400375 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400376 }
377
378 /**
379 * Shows a list of names of folders.
380 */
381 private class FolderAdapter extends CursorAdapter {
382 public FolderAdapter(Context context) {
383 super(context, null);
384 }
385
386 @Override
387 public void bindView(View view, Context context, Cursor cursor) {
388 ((TextView) view.findViewById(android.R.id.text1)).setText(
389 cursor.getString(cursor.getColumnIndexOrThrow(
390 BrowserContract.Bookmarks.TITLE)));
391 }
392
393 @Override
394 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700395 View view = LayoutInflater.from(context).inflate(
396 R.layout.folder_list_item, null);
397 view.setBackgroundDrawable(context.getResources().
398 getDrawable(android.R.drawable.list_selector_background));
399 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400400 }
401 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800402
403 protected void onCreate(Bundle icicle) {
404 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400405 if (DEBUG_CRASH) {
406 requestWindowFeature(Window.FEATURE_NO_TITLE);
407 } else {
408 requestWindowFeature(Window.FEATURE_LEFT_ICON);
409 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100410
411 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100412
Leon Scroggins III052ce662010-09-13 14:44:16 -0400413 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100414
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400415 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400416 if (!DEBUG_CRASH) {
417 setTitle(R.string.bookmark_this_page);
418 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
419 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700420
The Android Open Source Project0c908882009-03-03 19:32:16 -0800421 String title = null;
422 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100423
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400424 mFakeTitle = (TextView) findViewById(R.id.fake_title);
425
The Android Open Source Project0c908882009-03-03 19:32:16 -0800426 if (mMap != null) {
427 Bundle b = mMap.getBundle("bookmark");
428 if (b != null) {
429 mMap = b;
430 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400431 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400432 if (!DEBUG_CRASH) {
433 setTitle(R.string.bookmark_this_page);
434 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400435 } else {
436 int gravity = mMap.getInt("gravity", -1);
437 if (gravity != -1) {
438 WindowManager.LayoutParams l = window.getAttributes();
439 l.gravity = gravity;
440 window.setAttributes(l);
441 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 }
443 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100444 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400445 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100446 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
Michael Kolb370a4f32010-10-06 10:45:32 -0700447 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400448 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700449 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400450 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451 }
452
453 mTitle = (EditText) findViewById(R.id.title);
454 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100455
Leon Scroggins III052ce662010-09-13 14:44:16 -0400456 mAddress = (EditText) findViewById(R.id.address);
457 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800458
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400460 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461
462 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400463 mCancelButton.setOnClickListener(this);
464
465 mFolder = (TextView) findViewById(R.id.folder);
466 mFolder.setOnClickListener(this);
467
468 mDefaultView = findViewById(R.id.default_view);
469 mFolderSelector = findViewById(R.id.folder_selector);
470
471 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
472 mFolderNamer.setOnEditorActionListener(this);
473
474 mAddNewFolder = findViewById(R.id.add_new_folder);
475 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400476 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400477
Leon Scroggins74dbe012010-10-15 10:54:27 -0400478 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
479 mCrumbs.setUseBackButton(true);
480 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400481 String name = getString(R.string.bookmarks);
482 mCrumbs.pushView(name, false,
483 new Folder(name, BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400484 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400485
Leon Scroggins III052ce662010-09-13 14:44:16 -0400486 mAdapter = new FolderAdapter(this);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400487 ListView list = (ListView) findViewById(R.id.list);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400488 list.setAdapter(mAdapter);
489 list.setOnItemClickListener(this);
490 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500491 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400492 // Find all the folders
493 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
494 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400495 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400496 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
497
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700498
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400499 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 mButton.requestFocus();
501 }
502 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100503
Leon Scroggins25230d72010-09-28 20:09:25 -0400504 // FIXME: Use a CursorLoader
505 private long getBookmarksBarId(Context context) {
506 SharedPreferences prefs
507 = PreferenceManager.getDefaultSharedPreferences(context);
508 String accountName =
509 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
510 String accountType =
511 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
512 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500513 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400514 }
515 Cursor cursor = null;
516 try {
517 cursor = context.getContentResolver().query(
518 BrowserContract.Bookmarks.CONTENT_URI,
519 new String[] { BrowserContract.Bookmarks._ID },
520 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
521 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
522 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
523 new String[] {
524 BrowserContract.ChromeSyncColumns
525 .FOLDER_NAME_BOOKMARKS_BAR,
526 accountName,
527 accountType },
528 null);
529 if (cursor != null && cursor.moveToFirst()) {
530 return cursor.getLong(0);
531 }
532 } finally {
533 if (cursor != null) cursor.close();
534 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500535 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400536 }
537
Leon Scroggins02065b02010-01-04 14:30:13 -0500538 /**
539 * Runnable to save a bookmark, so it can be performed in its own thread.
540 */
541 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400542 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500543 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200544 private Context mContext;
545 public SaveBookmarkRunnable(Context ctx, Message msg) {
546 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500547 mMessage = msg;
548 }
549 public void run() {
550 // Unbundle bookmark data.
551 Bundle bundle = mMessage.getData();
552 String title = bundle.getString("title");
553 String url = bundle.getString("url");
554 boolean invalidateThumbnail = bundle.getBoolean(
555 "invalidateThumbnail");
556 Bitmap thumbnail = invalidateThumbnail ? null
557 : (Bitmap) bundle.getParcelable("thumbnail");
558 String touchIconUrl = bundle.getString("touchIconUrl");
559
560 // Save to the bookmarks DB.
561 try {
562 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400563 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
564 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500565 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200566 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500567 }
568 mMessage.arg1 = 1;
569 } catch (IllegalStateException e) {
570 mMessage.arg1 = 0;
571 }
572 mMessage.sendToTarget();
573 }
574 }
575
Ben Murdoch1794fe22009-09-29 18:14:30 +0100576 private void createHandler() {
577 if (mHandler == null) {
578 mHandler = new Handler() {
579 @Override
580 public void handleMessage(Message msg) {
581 switch (msg.what) {
582 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500583 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100584 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
585 Toast.LENGTH_LONG).show();
586 } else {
587 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
588 Toast.LENGTH_LONG).show();
589 }
590 break;
591 }
592 }
593 };
594 }
595 }
596
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100598 * 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 -0800599 */
600 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100601 createHandler();
602
The Android Open Source Project0c908882009-03-03 19:32:16 -0800603 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100604 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400605 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100606
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 boolean emptyTitle = title.length() == 0;
608 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
609 Resources r = getResources();
610 if (emptyTitle || emptyUrl) {
611 if (emptyTitle) {
612 mTitle.setError(r.getText(R.string.bookmark_needs_title));
613 }
614 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400615 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400617 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100618
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000620 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400621 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000622 // We allow bookmarks with a javascript: scheme, but these will in most cases
623 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
624
625 if (!url.toLowerCase().startsWith("javascript:")) {
626 URI uriObj = new URI(url);
627 String scheme = uriObj.getScheme();
628 if (!Bookmarks.urlHasAcceptableScheme(url)) {
629 // If the scheme was non-null, let the user know that we
630 // can't save their bookmark. If it was null, we'll assume
631 // they meant http when we parse it in the WebAddress class.
632 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400633 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000634 return false;
635 }
636 WebAddress address;
637 try {
638 address = new WebAddress(unfilteredUrl);
639 } catch (ParseException e) {
640 throw new URISyntaxException("", "");
641 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100642 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000643 throw new URISyntaxException("", "");
644 }
645 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100646 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400648 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400649 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400650 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100652
Ben Murdoch1794fe22009-09-29 18:14:30 +0100653 if (mEditingExisting) {
654 mMap.putString("title", title);
655 mMap.putString("url", url);
656 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400657 // FIXME: This does not work yet
658 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100659 setResult(RESULT_OK, (new Intent()).setAction(
660 getIntent().toString()).putExtras(mMap));
661 } else {
662 // Post a message to write to the DB.
663 Bundle bundle = new Bundle();
664 bundle.putString("title", title);
665 bundle.putString("url", url);
666 bundle.putParcelable("thumbnail", mThumbnail);
667 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
668 bundle.putString("touchIconUrl", mTouchIconUrl);
669 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
670 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500671 // Start a new thread so as to not slow down the UI
Henrik Baard980e9952010-09-06 18:13:23 +0200672 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
Leon Scroggins02065b02010-01-04 14:30:13 -0500673 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100674 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000675 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800676 }
677 return true;
678 }
Michael Kolb31829b92010-10-01 11:50:21 -0700679
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680}