Add history feature; group some functionality into subpackages
[zxing.git] / android / src / com / google / zxing / client / android / share / 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.share;
18
19 import android.app.Activity;
20 import android.content.ContentResolver;
21 import android.content.Intent;
22 import android.database.Cursor;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.provider.Browser;
26 import android.provider.Contacts;
27 import android.provider.BaseColumns;
28 import android.text.ClipboardManager;
29 import android.view.View;
30 import android.widget.Button;
31 import com.google.zxing.client.android.Intents;
32 import com.google.zxing.client.android.Contents;
33 import com.google.zxing.client.android.R;
34
35 /**
36  * Barcode Scanner can share data like contacts and bookmarks by displaying a QR Code on screen,
37  * such that another user can scan the barcode with their phone.
38  *
39  * @author dswitkin@google.com (Daniel Switkin)
40  */
41 public final class ShareActivity extends Activity {
42   private static final int PICK_BOOKMARK = 0;
43   private static final int PICK_CONTACT = 1;
44
45   //private static final int METHODS_ID_COLUMN = 0;
46   private static final int METHODS_KIND_COLUMN = 1;
47   private static final int METHODS_DATA_COLUMN = 2;
48
49   private static final String[] METHODS_PROJECTION = {
50       BaseColumns._ID, // 0
51       Contacts.ContactMethodsColumns.KIND, // 1
52       Contacts.ContactMethodsColumns.DATA, // 2
53   };
54
55   private static final int PHONES_NUMBER_COLUMN = 1;
56
57   private static final String[] PHONES_PROJECTION = {
58       BaseColumns._ID, // 0
59       Contacts.PhonesColumns.NUMBER // 1
60   };
61
62   private Button clipboardButton;
63
64   private final Button.OnClickListener contactListener = new Button.OnClickListener() {
65     public void onClick(View v) {
66       startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.People.CONTENT_URI),
67           PICK_CONTACT);
68     }
69   };
70
71   private final Button.OnClickListener bookmarkListener = new Button.OnClickListener() {
72     public void onClick(View v) {
73       Intent intent = new Intent(Intent.ACTION_PICK);
74       intent.setClassName(ShareActivity.this, BookmarkPickerActivity.class.getName());
75       startActivityForResult(intent, PICK_BOOKMARK);
76     }
77   };
78
79   private final Button.OnClickListener clipboardListener = new Button.OnClickListener() {
80     public void onClick(View v) {
81       ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
82       // Should always be true, because we grey out the clipboard button in onResume() if it's empty
83       if (clipboard.hasText()) {
84         Intent intent = new Intent(Intents.Encode.ACTION);
85         intent.putExtra(Intents.Encode.TYPE, Contents.Type.TEXT);
86         intent.putExtra(Intents.Encode.DATA, clipboard.getText());
87         intent.putExtra(Intents.Encode.FORMAT, Contents.Format.QR_CODE);
88         startActivity(intent);
89       }
90     }
91   };
92
93   @Override
94   public void onCreate(Bundle icicle) {
95     super.onCreate(icicle);
96     setContentView(R.layout.share);
97
98     Button mContactButton = (Button) findViewById(R.id.contact_button);
99     mContactButton.setOnClickListener(contactListener);
100     Button mBookmarkButton = (Button) findViewById(R.id.bookmark_button);
101     mBookmarkButton.setOnClickListener(bookmarkListener);
102     clipboardButton = (Button) findViewById(R.id.clipboard_button);
103     clipboardButton.setOnClickListener(clipboardListener);
104   }
105
106   @Override
107   protected void onResume() {
108     super.onResume();
109
110     ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
111     if (clipboard.hasText()) {
112       clipboardButton.setEnabled(true);
113       clipboardButton.setText(R.string.button_share_clipboard);
114     } else {
115       clipboardButton.setEnabled(false);
116       clipboardButton.setText(R.string.button_clipboard_empty);
117     }
118   }
119
120   @Override
121   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
122     if (resultCode == RESULT_OK) {
123       switch (requestCode) {
124         case PICK_BOOKMARK:
125           showTextAsBarcode(intent.getStringExtra(Browser.BookmarkColumns.URL));
126           break;
127         case PICK_CONTACT:
128           // Data field is content://contacts/people/984
129           showContactAsBarcode(intent.getData());
130           break;
131       }
132     }
133   }
134
135   private void showTextAsBarcode(String text) {
136     Intent intent = new Intent(Intents.Encode.ACTION);
137     intent.putExtra(Intents.Encode.TYPE, Contents.Type.TEXT);
138     intent.putExtra(Intents.Encode.DATA, text);
139     intent.putExtra(Intents.Encode.FORMAT, Contents.Format.QR_CODE);
140     startActivity(intent);
141   }
142
143   /**
144    * Takes a contact Uri and does the necessary database lookups to retrieve that person's info,
145    * then sends an Encode intent to render it as a QR Code.
146    *
147    * @param contactUri A Uri of the form content://contacts/people/17
148    */
149   private void showContactAsBarcode(Uri contactUri) {
150     ContentResolver resolver = getContentResolver();
151     Cursor contactCursor = resolver.query(contactUri, null, null, null, null);
152     Bundle bundle = new Bundle();
153     if (contactCursor != null && contactCursor.moveToFirst()) {
154       int nameColumn = contactCursor.getColumnIndex(Contacts.PeopleColumns.NAME);
155       String name = contactCursor.getString(nameColumn);
156
157       // Don't require a name to be present, this contact might be just a phone number.
158       if (name != null && name.length() > 0) {
159         bundle.putString(Contacts.Intents.Insert.NAME, massageContactData(name));
160       }
161       contactCursor.close();
162
163       Uri phonesUri = Uri.withAppendedPath(contactUri, Contacts.People.Phones.CONTENT_DIRECTORY);
164       Cursor phonesCursor = resolver.query(phonesUri, PHONES_PROJECTION, null, null, null);
165       if (phonesCursor != null) {
166         int foundPhone = 0;
167         while (phonesCursor.moveToNext()) {
168           String number = phonesCursor.getString(PHONES_NUMBER_COLUMN);
169           if (foundPhone < Contents.PHONE_KEYS.length) {
170             bundle.putString(Contents.PHONE_KEYS[foundPhone], massageContactData(number));
171             foundPhone++;
172           }
173         }
174         phonesCursor.close();
175       }
176
177       Uri methodsUri = Uri.withAppendedPath(contactUri,
178           Contacts.People.ContactMethods.CONTENT_DIRECTORY);
179       Cursor methodsCursor = resolver.query(methodsUri, METHODS_PROJECTION, null, null, null);
180       if (methodsCursor != null) {
181         int foundEmail = 0;
182         boolean foundPostal = false;
183         while (methodsCursor.moveToNext()) {
184           int kind = methodsCursor.getInt(METHODS_KIND_COLUMN);
185           String data = methodsCursor.getString(METHODS_DATA_COLUMN);
186           switch (kind) {
187             case Contacts.KIND_EMAIL:
188               if (foundEmail < Contents.EMAIL_KEYS.length) {
189                 bundle.putString(Contents.EMAIL_KEYS[foundEmail], massageContactData(data));
190                 foundEmail++;
191               }
192               break;
193             case Contacts.KIND_POSTAL:
194               if (!foundPostal) {
195                 bundle.putString(Contacts.Intents.Insert.POSTAL, massageContactData(data));
196                 foundPostal = true;
197               }
198               break;
199           }
200         }
201         methodsCursor.close();
202       }
203
204       Intent intent = new Intent(Intents.Encode.ACTION);
205       intent.putExtra(Intents.Encode.TYPE, Contents.Type.CONTACT);
206       intent.putExtra(Intents.Encode.DATA, bundle);
207       intent.putExtra(Intents.Encode.FORMAT, Contents.Format.QR_CODE);
208
209       startActivity(intent);
210     }
211   }
212
213   private static String massageContactData(String data) {
214     // For now -- make sure we don't put newlines in shared contact data. It messes up
215     // any known encoding of contact data. Replace with space.
216     if (data.indexOf('\n') >= 0) {
217       data = data.replace("\n", " ");
218     }
219     if (data.indexOf('\r') >= 0) {
220       data = data.replace("\r", " ");
221     }
222     return data;
223   }
224 }