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