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