Wrote a new bookmark picker activity for use by the Share button, because I couldn...
[zxing.git] / android / src / com / google / zxing / client / android / BookmarkPickerActivity.java
1 /*
2  * Copyright (C) 2008 ZXing authors
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
17 package com.google.zxing.client.android;
18
19 import android.app.ListActivity;
20 import android.content.Intent;
21 import android.database.Cursor;
22 import android.os.Bundle;
23 import android.provider.Browser;
24 import android.view.View;
25 import android.widget.ListAdapter;
26 import android.widget.ListView;
27 import android.widget.SimpleCursorAdapter;
28
29 /**
30  * This class is only needed because I can't successfully send an ACTION_PICK intent to
31  * com.android.browser.BrowserBookmarksPage. It can go away if that starts working in the future.
32  */
33 public class BookmarkPickerActivity extends ListActivity {
34
35   private static final String[] BOOKMARK_PROJECTION = {
36       Browser.BookmarkColumns.TITLE,
37       Browser.BookmarkColumns.URL
38   };
39
40   private static final int[] TWO_LINE_VIEW_IDS = {
41       R.id.bookmark_title,
42       R.id.bookmark_url
43   };
44
45   private static final int TITLE_COLUMN = 0;
46   private static final int URL_COLUMN = 1;
47
48   // Without this selection, we'd get all the history entries too
49   private static final String BOOKMARK_SELECTION = "bookmark = 1";
50
51   private Cursor mCursor;
52
53   @Override
54   protected void onCreate(Bundle icicle) {
55     super.onCreate(icicle);
56
57     mCursor = getContentResolver().query(Browser.BOOKMARKS_URI, BOOKMARK_PROJECTION,
58         BOOKMARK_SELECTION, null, null);
59     startManagingCursor(mCursor);
60
61     ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.bookmark_picker_list_item,
62         mCursor, BOOKMARK_PROJECTION, TWO_LINE_VIEW_IDS);
63     setListAdapter(adapter);
64   }
65
66   @Override
67   protected void onListItemClick(ListView l, View view, int position, long id) {
68     if (mCursor.moveToPosition(position)) {
69       Intent intent = new Intent();
70       intent.putExtra(Browser.BookmarkColumns.TITLE, mCursor.getString(TITLE_COLUMN));
71       intent.putExtra(Browser.BookmarkColumns.URL, mCursor.getString(URL_COLUMN));
72       setResult(RESULT_OK, intent);
73     } else {
74       setResult(RESULT_CANCELED);
75     }
76     finish();
77   }
78
79 }