blob: c1bafd63dc50ef62237dda586e47dd17d191cec0 [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 Scroggins III052ce662010-09-13 14:44:16 -040061
62public class AddBookmarkPage extends Activity
63 implements View.OnClickListener, TextView.OnEditorActionListener,
64 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
The Android Open Source Project0c908882009-03-03 19:32:16 -080065
Michael Kolb370a4f32010-10-06 10:45:32 -070066 public static final long DEFAULT_FOLDER_ID = -1;
67
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 private final String LOGTAG = "Bookmarks";
69
Leon Scroggins III052ce662010-09-13 14:44:16 -040070 // IDs for the CursorLoaders that are used.
71 private final int LOADER_ID_FOLDER_CONTENTS = 0;
72 private final int LOADER_ID_ALL_FOLDERS = 1;
73
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 private EditText mTitle;
75 private EditText mAddress;
76 private TextView mButton;
77 private View mCancelButton;
78 private boolean mEditingExisting;
79 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040080 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010081 private Bitmap mThumbnail;
82 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040083 private TextView mFolder;
84 private View mDefaultView;
85 private View mFolderSelector;
86 private EditText mFolderNamer;
87 private View mAddNewFolder;
88 private long mCurrentFolder = 0;
89 private FolderAdapter mAdapter;
90 private ArrayList<Folder> mPaths;
91 private TextView mPath;
92
93 private static class Folder {
94 String Name;
95 long Id;
96 Folder(String name, long id) {
97 Name = name;
98 Id = id;
99 }
100 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101
Ben Murdoch1794fe22009-09-29 18:14:30 +0100102 // Message IDs
103 private static final int SAVE_BOOKMARK = 100;
104
105 private Handler mHandler;
106
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100107 private InputMethodManager getInputMethodManager() {
108 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
109 }
110
Leon Scroggins III052ce662010-09-13 14:44:16 -0400111 @Override
112 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
113 if (v == mFolderNamer) {
114 if (v.getText().length() > 0) {
115 if (actionId == EditorInfo.IME_NULL) {
116 // Only want to do this once.
117 if (event.getAction() == KeyEvent.ACTION_UP) {
Michael Kolb31829b92010-10-01 11:50:21 -0700118 completeFolderNaming();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400119 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400120 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 }
Michael Kolb31829b92010-10-01 11:50:21 -0700122 // Steal the key press; otherwise a newline will be added
123 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400125 return false;
126 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127
Leon Scroggins III052ce662010-09-13 14:44:16 -0400128 @Override
129 public void onClick(View v) {
130 if (v == mButton) {
131 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700132 // We are showing the folder selector.
133 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Michael Kolb31829b92010-10-01 11:50:21 -0700134 completeFolderNaming();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700135 } else {
136 // User has selected a folder. Go back to the opening page
137 mFolderSelector.setVisibility(View.GONE);
138 mDefaultView.setVisibility(View.VISIBLE);
139 setTitle(R.string.bookmark_this_page);
140 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400141 } else if (save()) {
142 finish();
143 }
144 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700145 if (mFolderNamer.getVisibility() == View.VISIBLE) {
146 mFolderNamer.setVisibility(View.GONE);
147 mAddNewFolder.setVisibility(View.VISIBLE);
148 } else {
149 finish();
150 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400151 } else if (v == mFolder) {
152 switchToFolderSelector();
153 } else if (v == mAddNewFolder) {
154 mFolderNamer.setVisibility(View.VISIBLE);
155 mFolderNamer.setText(R.string.new_folder);
156 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700157 mAddNewFolder.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100158 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400159 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400161 }
162
Michael Kolb31829b92010-10-01 11:50:21 -0700163 private void completeFolderNaming() {
164 if (!TextUtils.isEmpty(mFolderNamer.getText())) {
165 String name = mFolderNamer.getText().toString();
166 long id = addFolderToCurrent(mFolderNamer.getText().toString());
167 descendInto(name, id);
168 mFolderNamer.setVisibility(View.GONE);
169 mAddNewFolder.setVisibility(View.VISIBLE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100170 getInputMethodManager().hideSoftInputFromWindow(
Michael Kolb31829b92010-10-01 11:50:21 -0700171 mFolderNamer.getWindowToken(), 0);
172 }
173 }
174
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700175 private long addFolderToCurrent(String name) {
176 // Add the folder to the database
177 ContentValues values = new ContentValues();
178 values.put(BrowserContract.Bookmarks.TITLE,
179 name);
180 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
181 values.put(BrowserContract.Bookmarks.PARENT,
182 mCurrentFolder);
183 Uri uri = getContentResolver().insert(
184 BrowserContract.Bookmarks.CONTENT_URI, values);
185 if (uri != null) {
186 return ContentUris.parseId(uri);
187 } else {
188 return -1;
189 }
190 }
191
Leon Scroggins III052ce662010-09-13 14:44:16 -0400192 private void switchToFolderSelector() {
193 mDefaultView.setVisibility(View.GONE);
194 mFolderSelector.setVisibility(View.VISIBLE);
195 setTitle(R.string.containing_folder);
196 }
197
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700198 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700199 if (id != DEFAULT_FOLDER_ID) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700200 mCurrentFolder = id;
201 mPaths.add(new Folder(foldername, id));
202 updatePathString();
203 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
204 }
205 }
206
Leon Scroggins III052ce662010-09-13 14:44:16 -0400207 @Override
208 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
209 String[] projection;
210 switch (id) {
211 case LOADER_ID_ALL_FOLDERS:
212 projection = new String[] {
213 BrowserContract.Bookmarks._ID,
214 BrowserContract.Bookmarks.PARENT,
215 BrowserContract.Bookmarks.TITLE,
216 BrowserContract.Bookmarks.IS_FOLDER
217 };
218 return new CursorLoader(this,
219 BrowserContract.Bookmarks.CONTENT_URI,
220 projection,
221 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
222 null,
223 null);
224 case LOADER_ID_FOLDER_CONTENTS:
225 projection = new String[] {
226 BrowserContract.Bookmarks._ID,
227 BrowserContract.Bookmarks.TITLE,
228 BrowserContract.Bookmarks.IS_FOLDER
229 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400230 return new CursorLoader(this,
231 BrowserContract.Bookmarks.buildFolderUri(
232 mCurrentFolder),
233 projection,
234 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
235 null,
236 null);
237 default:
238 throw new AssertionError("Asking for nonexistant loader!");
239 }
240 }
241
242 @Override
243 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
244 switch (loader.getId()) {
245 case LOADER_ID_FOLDER_CONTENTS:
246 mAdapter.changeCursor(cursor);
247 break;
248 case LOADER_ID_ALL_FOLDERS:
249 long parent = mCurrentFolder;
250 int idIndex = cursor.getColumnIndexOrThrow(
251 BrowserContract.Bookmarks._ID);
252 int titleIndex = cursor.getColumnIndexOrThrow(
253 BrowserContract.Bookmarks.TITLE);
254 int parentIndex = cursor.getColumnIndexOrThrow(
255 BrowserContract.Bookmarks.PARENT);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700256 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
257 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400258 // First, find the folder corresponding to the current
259 // folder
260 if (!cursor.moveToFirst()) {
261 throw new AssertionError("No folders in the database!");
262 }
263 long folder;
264 do {
265 folder = cursor.getLong(idIndex);
266 } while (folder != parent && cursor.moveToNext());
267 if (cursor.isAfterLast()) {
268 throw new AssertionError("Folder(id=" + parent
269 + ") holding this bookmark does not exist!");
270 }
271 String name = cursor.getString(titleIndex);
272 mPaths.add(1, new Folder(name, parent));
273 parent = cursor.getLong(parentIndex);
274 }
275 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
276 updatePathString();
277 break;
278 default:
279 break;
280 }
281 }
282
283 /**
284 * Update the TextViews in both modes to display the full path of the
285 * current location to insert.
286 */
287 private void updatePathString() {
288 String path = mPaths.get(0).Name;
289 int size = mPaths.size();
290 for (int i = 1; i < size; i++) {
291 path += " / " + mPaths.get(i).Name;
292 }
293 mPath.setText(path);
294 mFolder.setText(path);
295 }
296
297 @Override
298 public void onItemClick(AdapterView<?> parent, View view, int position,
299 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400300 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400301 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400302 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400303 }
304
305 /**
306 * Shows a list of names of folders.
307 */
308 private class FolderAdapter extends CursorAdapter {
309 public FolderAdapter(Context context) {
310 super(context, null);
311 }
312
313 @Override
314 public void bindView(View view, Context context, Cursor cursor) {
315 ((TextView) view.findViewById(android.R.id.text1)).setText(
316 cursor.getString(cursor.getColumnIndexOrThrow(
317 BrowserContract.Bookmarks.TITLE)));
318 }
319
320 @Override
321 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700322 View view = LayoutInflater.from(context).inflate(
323 R.layout.folder_list_item, null);
324 view.setBackgroundDrawable(context.getResources().
325 getDrawable(android.R.drawable.list_selector_background));
326 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400327 }
328 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800329
330 protected void onCreate(Bundle icicle) {
331 super.onCreate(icicle);
332 requestWindowFeature(Window.FEATURE_LEFT_ICON);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100333
334 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100335
Leon Scroggins III052ce662010-09-13 14:44:16 -0400336 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100337
Leon Scroggins III052ce662010-09-13 14:44:16 -0400338 setTitle(R.string.bookmark_this_page);
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400339 Window window = getWindow();
340 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700341
The Android Open Source Project0c908882009-03-03 19:32:16 -0800342 String title = null;
343 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100344
The Android Open Source Project0c908882009-03-03 19:32:16 -0800345 if (mMap != null) {
346 Bundle b = mMap.getBundle("bookmark");
347 if (b != null) {
348 mMap = b;
349 mEditingExisting = true;
350 setTitle(R.string.edit_bookmark);
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400351 } else {
352 int gravity = mMap.getInt("gravity", -1);
353 if (gravity != -1) {
354 WindowManager.LayoutParams l = window.getAttributes();
355 l.gravity = gravity;
356 window.setAttributes(l);
357 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358 }
359 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100360 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400361 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100362 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
Michael Kolb370a4f32010-10-06 10:45:32 -0700363 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400364 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700365 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400366 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 }
368
369 mTitle = (EditText) findViewById(R.id.title);
370 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100371
Leon Scroggins III052ce662010-09-13 14:44:16 -0400372 mAddress = (EditText) findViewById(R.id.address);
373 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800374
The Android Open Source Project0c908882009-03-03 19:32:16 -0800375 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400376 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377
378 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400379 mCancelButton.setOnClickListener(this);
380
381 mFolder = (TextView) findViewById(R.id.folder);
382 mFolder.setOnClickListener(this);
383
384 mDefaultView = findViewById(R.id.default_view);
385 mFolderSelector = findViewById(R.id.folder_selector);
386
387 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
388 mFolderNamer.setOnEditorActionListener(this);
389
390 mAddNewFolder = findViewById(R.id.add_new_folder);
391 mAddNewFolder.setOnClickListener(this);
392
393 mPath = (TextView) findViewById(R.id.path);
394 ListView list = (ListView) findViewById(R.id.list);
395
396 mPaths = new ArrayList<Folder>();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700397 mPaths.add(0, new Folder(getString(R.string.bookmarks), BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400398 mAdapter = new FolderAdapter(this);
399 list.setAdapter(mAdapter);
400 list.setOnItemClickListener(this);
401 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500402 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400403 // Find all the folders
404 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
405 }
406 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
407
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700408
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400409 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800410 mButton.requestFocus();
411 }
412 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100413
Leon Scroggins25230d72010-09-28 20:09:25 -0400414 // FIXME: Use a CursorLoader
415 private long getBookmarksBarId(Context context) {
416 SharedPreferences prefs
417 = PreferenceManager.getDefaultSharedPreferences(context);
418 String accountName =
419 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
420 String accountType =
421 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
422 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500423 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400424 }
425 Cursor cursor = null;
426 try {
427 cursor = context.getContentResolver().query(
428 BrowserContract.Bookmarks.CONTENT_URI,
429 new String[] { BrowserContract.Bookmarks._ID },
430 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
431 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
432 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
433 new String[] {
434 BrowserContract.ChromeSyncColumns
435 .FOLDER_NAME_BOOKMARKS_BAR,
436 accountName,
437 accountType },
438 null);
439 if (cursor != null && cursor.moveToFirst()) {
440 return cursor.getLong(0);
441 }
442 } finally {
443 if (cursor != null) cursor.close();
444 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500445 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400446 }
447
Leon Scroggins III052ce662010-09-13 14:44:16 -0400448 @Override
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700449 public boolean dispatchKeyEvent(KeyEvent event) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400450 if (mFolderSelector.getVisibility() == View.VISIBLE
451 && KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
452 if (KeyEvent.ACTION_UP == event.getAction()) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700453 if (mFolderNamer.getVisibility() == View.VISIBLE) {
454 mFolderNamer.setVisibility(View.GONE);
455 mAddNewFolder.setVisibility(View.VISIBLE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100456 getInputMethodManager().hideSoftInputFromWindow(
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700457 mFolderNamer.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400458 } else {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700459 int size = mPaths.size();
460 if (1 == size) {
461 // We have reached the top level
462 finish();
463 } else {
464 // Go up a level
465 mPaths.remove(size - 1);
466 mCurrentFolder = mPaths.get(size - 2).Id;
467 updatePathString();
468 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
469 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400470 }
471 }
472 return true;
473 }
474 return super.dispatchKeyEvent(event);
475 }
476
Leon Scroggins02065b02010-01-04 14:30:13 -0500477 /**
478 * Runnable to save a bookmark, so it can be performed in its own thread.
479 */
480 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400481 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500482 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200483 private Context mContext;
484 public SaveBookmarkRunnable(Context ctx, Message msg) {
485 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500486 mMessage = msg;
487 }
488 public void run() {
489 // Unbundle bookmark data.
490 Bundle bundle = mMessage.getData();
491 String title = bundle.getString("title");
492 String url = bundle.getString("url");
493 boolean invalidateThumbnail = bundle.getBoolean(
494 "invalidateThumbnail");
495 Bitmap thumbnail = invalidateThumbnail ? null
496 : (Bitmap) bundle.getParcelable("thumbnail");
497 String touchIconUrl = bundle.getString("touchIconUrl");
498
499 // Save to the bookmarks DB.
500 try {
501 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400502 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
503 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500504 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200505 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500506 }
507 mMessage.arg1 = 1;
508 } catch (IllegalStateException e) {
509 mMessage.arg1 = 0;
510 }
511 mMessage.sendToTarget();
512 }
513 }
514
Ben Murdoch1794fe22009-09-29 18:14:30 +0100515 private void createHandler() {
516 if (mHandler == null) {
517 mHandler = new Handler() {
518 @Override
519 public void handleMessage(Message msg) {
520 switch (msg.what) {
521 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500522 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100523 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
524 Toast.LENGTH_LONG).show();
525 } else {
526 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
527 Toast.LENGTH_LONG).show();
528 }
529 break;
530 }
531 }
532 };
533 }
534 }
535
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100537 * 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 -0800538 */
539 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100540 createHandler();
541
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100543 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400544 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100545
The Android Open Source Project0c908882009-03-03 19:32:16 -0800546 boolean emptyTitle = title.length() == 0;
547 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
548 Resources r = getResources();
549 if (emptyTitle || emptyUrl) {
550 if (emptyTitle) {
551 mTitle.setError(r.getText(R.string.bookmark_needs_title));
552 }
553 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400554 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400556 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100557
The Android Open Source Project0c908882009-03-03 19:32:16 -0800558 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000559 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400560 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000561 // We allow bookmarks with a javascript: scheme, but these will in most cases
562 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
563
564 if (!url.toLowerCase().startsWith("javascript:")) {
565 URI uriObj = new URI(url);
566 String scheme = uriObj.getScheme();
567 if (!Bookmarks.urlHasAcceptableScheme(url)) {
568 // If the scheme was non-null, let the user know that we
569 // can't save their bookmark. If it was null, we'll assume
570 // they meant http when we parse it in the WebAddress class.
571 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400572 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000573 return false;
574 }
575 WebAddress address;
576 try {
577 address = new WebAddress(unfilteredUrl);
578 } catch (ParseException e) {
579 throw new URISyntaxException("", "");
580 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100581 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000582 throw new URISyntaxException("", "");
583 }
584 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100585 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800586 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400587 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400588 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400589 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800590 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100591
Ben Murdoch1794fe22009-09-29 18:14:30 +0100592 if (mEditingExisting) {
593 mMap.putString("title", title);
594 mMap.putString("url", url);
595 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400596 // FIXME: This does not work yet
597 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100598 setResult(RESULT_OK, (new Intent()).setAction(
599 getIntent().toString()).putExtras(mMap));
600 } else {
601 // Post a message to write to the DB.
602 Bundle bundle = new Bundle();
603 bundle.putString("title", title);
604 bundle.putString("url", url);
605 bundle.putParcelable("thumbnail", mThumbnail);
606 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
607 bundle.putString("touchIconUrl", mTouchIconUrl);
608 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
609 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500610 // Start a new thread so as to not slow down the UI
Henrik Baard980e9952010-09-06 18:13:23 +0200611 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
Leon Scroggins02065b02010-01-04 14:30:13 -0500612 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100613 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000614 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 }
616 return true;
617 }
Michael Kolb31829b92010-10-01 11:50:21 -0700618
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619}