Wrote a new bookmark picker activity for use by the Share button, because I couldn...
[zxing.git] / android / src / com / google / zxing / client / android / ShareActivity.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.Activity;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.provider.Browser;
27 import android.provider.Contacts;
28 import android.text.ClipboardManager;
29 import android.view.View;
30 import android.widget.Button;
31
32 public final class ShareActivity extends Activity {
33
34   private static final int PICK_BOOKMARK = 0;
35   private static final int PICK_CONTACT = 1;
36
37   private static final int METHODS_ID_COLUMN = 0;
38   private static final int METHODS_KIND_COLUMN = 1;
39   private static final int METHODS_DATA_COLUMN = 2;
40
41   private static final String[] METHODS_PROJECTION = new String[] {
42     Contacts.People.ContactMethods._ID, // 0
43     Contacts.People.ContactMethods.KIND, // 1
44     Contacts.People.ContactMethods.DATA, // 2
45   };
46
47   private Button mContactButton;
48   private Button mBookmarkButton;
49   private Button mClipboardButton;
50
51   @Override
52   public void onCreate(Bundle icicle) {
53     super.onCreate(icicle);
54     setContentView(R.layout.share);
55
56     mContactButton = (Button) findViewById(R.id.contact_button);
57     mContactButton.setOnClickListener(mContactListener);
58     mBookmarkButton = (Button) findViewById(R.id.bookmark_button);
59     mBookmarkButton.setOnClickListener(mBookmarkListener);
60     mClipboardButton = (Button) findViewById(R.id.clipboard_button);
61     mClipboardButton.setOnClickListener(mClipboardListener);
62   }
63
64   @Override
65   protected void onResume() {
66     super.onResume();
67
68     ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
69     if (clipboard.hasText()) {
70       mClipboardButton.setEnabled(true);
71       mClipboardButton.setText(R.string.button_share_clipboard);
72     } else {
73       mClipboardButton.setEnabled(false);
74       mClipboardButton.setText(R.string.button_clipboard_empty);
75     }
76   }
77
78   @Override
79   protected void onPause() {
80     super.onPause();
81   }
82
83   private final Button.OnClickListener mContactListener = new Button.OnClickListener() {
84     public void onClick(View v) {
85       startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.People.CONTENT_URI),
86           PICK_CONTACT);
87     }
88   };
89
90   private final Button.OnClickListener mBookmarkListener = new Button.OnClickListener() {
91     public void onClick(View v) {
92       Intent intent = new Intent(Intent.ACTION_PICK);
93       intent.setClassName(ShareActivity.this, BookmarkPickerActivity.class.getName());
94       startActivityForResult(intent, PICK_BOOKMARK);
95     }
96   };
97
98   private final Button.OnClickListener mClipboardListener = new Button.OnClickListener() {
99     public void onClick(View v) {
100       ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
101       // Should always be true, because we grey out the clipboard button in onResume() if it's empty
102       if (clipboard.hasText()) {
103         Intent intent = new Intent(Intents.Encode.ACTION);
104         intent.putExtra(Intents.Encode.TYPE, Contents.Type.TEXT);
105         intent.putExtra(Intents.Encode.DATA, clipboard.getText());
106         startActivity(intent);
107       }
108     }
109   };
110
111   @Override
112   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
113     if (resultCode == RESULT_OK) {
114       switch (requestCode) {
115         case PICK_BOOKMARK:
116           showTextAsBarcode(intent.getStringExtra(Browser.BookmarkColumns.URL));
117           break;
118         case PICK_CONTACT:
119           // Data field is content://contacts/people/984
120           showContactAsBarcode(intent.getData());
121           break;
122       }
123     }
124   }
125
126   private void showTextAsBarcode(String text) {
127     Intent intent = new Intent(Intents.Encode.ACTION);
128     intent.putExtra(Intents.Encode.TYPE, Contents.Type.TEXT);
129     intent.putExtra(Intents.Encode.DATA, text);
130     startActivity(intent);
131   }
132
133   /**
134    * Takes a contact Uri and does the necessary database lookups to retrieve that person's info,
135    * then sends an Encode intent to render it as a QR Code.
136    *  
137    * @param contactUri A Uri of the form content://contacts/people/17
138    */
139   private void showContactAsBarcode(Uri contactUri) {
140     ContentResolver resolver = getContentResolver();
141     Cursor contactCursor = resolver.query(contactUri, null, null, null, null);
142     Bundle bundle = new Bundle();
143     if (contactCursor != null && contactCursor.moveToFirst()) {
144       int nameColumn = contactCursor.getColumnIndex(Contacts.People.NAME);
145       String name = contactCursor.getString(nameColumn);
146       if (name == null || name.length() == 0) {
147         // TODO: Show error
148         return;
149       }
150       bundle.putString(Contacts.Intents.Insert.NAME, name);
151
152       int phoneColumn = contactCursor.getColumnIndex(Contacts.People.NUMBER);
153       bundle.putString(Contacts.Intents.Insert.PHONE, contactCursor.getString(phoneColumn));
154       contactCursor.close();
155
156       Uri methodsUri = Uri.withAppendedPath(contactUri,
157           Contacts.People.ContactMethods.CONTENT_DIRECTORY);
158       Cursor methodsCursor = resolver.query(methodsUri, METHODS_PROJECTION, null, null, null);
159       boolean foundEmail = false;
160       boolean foundPostal = false;
161       if (methodsCursor != null) {
162         while (methodsCursor.moveToNext()) {
163           final int kind = methodsCursor.getInt(METHODS_KIND_COLUMN);
164           final String data = methodsCursor.getString(METHODS_DATA_COLUMN);
165           switch (kind) {
166             case Contacts.KIND_EMAIL:
167               if (!foundEmail) {
168                 // Use the first address encountered, since we can't encode multiple addresses
169                 bundle.putString(Contacts.Intents.Insert.EMAIL, data);
170                 foundEmail = true;
171               }
172               break;
173             case Contacts.KIND_POSTAL:
174               if (!foundPostal) {
175                 bundle.putString(Contacts.Intents.Insert.POSTAL, data);
176                 foundPostal = true;
177               }
178               break;
179           }
180         }
181         methodsCursor.close();
182       }
183
184       Intent intent = new Intent(Intents.Encode.ACTION);
185       intent.putExtra(Intents.Encode.TYPE, Contents.Type.CONTACT);
186       intent.putExtra(Intents.Encode.DATA, bundle);
187       startActivity(intent);
188     }
189   }
190
191 }