2 * Copyright (C) 2009 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android.history;
19 import android.app.AlertDialog;
20 import android.content.ContentValues;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.content.res.Resources;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.database.sqlite.SQLiteOpenHelper;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.Environment;
29 import android.os.Message;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.OutputStreamWriter;
35 import java.nio.charset.Charset;
36 import java.text.DateFormat;
37 import java.util.Date;
38 import java.util.List;
39 import java.util.ArrayList;
41 import android.util.Log;
42 import com.google.zxing.BarcodeFormat;
43 import com.google.zxing.client.android.Intents;
44 import com.google.zxing.client.android.R;
45 import com.google.zxing.client.android.CaptureActivity;
46 import com.google.zxing.Result;
49 * <p>Manages functionality related to scan history.</p>
53 public final class HistoryManager {
55 private static final String TAG = HistoryManager.class.getSimpleName();
57 private static final int MAX_ITEMS = 50;
58 private static final String[] TEXT_COL_PROJECTION = { DBHelper.TEXT_COL };
59 private static final String[] GET_ITEM_COL_PROJECTION = {
62 DBHelper.TIMESTAMP_COL,
64 private static final String[] EXPORT_COL_PROJECTION = {
68 DBHelper.TIMESTAMP_COL,
70 private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
71 private static final DateFormat EXPORT_DATE_TIME_FORMAT = DateFormat.getDateTimeInstance();
73 private final CaptureActivity activity;
75 public HistoryManager(CaptureActivity activity) {
76 this.activity = activity;
79 List<Result> getHistoryItems() {
80 SQLiteOpenHelper helper = new DBHelper(activity);
81 List<Result> items = new ArrayList<Result>();
82 SQLiteDatabase db = helper.getReadableDatabase();
85 cursor = db.query(DBHelper.TABLE_NAME,
86 GET_ITEM_COL_PROJECTION,
87 null, null, null, null,
88 DBHelper.TIMESTAMP_COL + " DESC");
89 while (cursor.moveToNext()) {
90 Result result = new Result(cursor.getString(0),
93 BarcodeFormat.valueOf(cursor.getString(1)),
106 public AlertDialog buildAlert() {
107 List<Result> items = getHistoryItems();
108 int size = items.size();
109 String[] dialogItems = new String[size + 2];
110 for (int i = 0; i < size; i++) {
111 dialogItems[i] = items.get(i).getText();
113 Resources res = activity.getResources();
114 dialogItems[dialogItems.length - 2] = res.getString(R.string.history_send);
115 dialogItems[dialogItems.length - 1] = res.getString(R.string.history_clear_text);
116 DialogInterface.OnClickListener clickListener = new HistoryClickListener(dialogItems, items);
117 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
118 builder.setTitle(R.string.history_title);
119 builder.setItems(dialogItems, clickListener);
120 return builder.create();
123 public void addHistoryItem(Result result) {
125 if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
126 return; // Do not save this item to the history.
129 SQLiteOpenHelper helper = new DBHelper(activity);
130 SQLiteDatabase db = helper.getWritableDatabase();
131 Cursor cursor = null;
133 cursor = db.query(DBHelper.TABLE_NAME,
135 DBHelper.TEXT_COL + "=?",
136 new String[] { result.getText() },
137 null, null, null, null);
138 if (cursor.moveToNext()) {
141 ContentValues values = new ContentValues();
142 values.put(DBHelper.TEXT_COL, result.getText());
143 values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
144 values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
145 values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
146 db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
148 if (cursor != null) {
155 public void trimHistory() {
156 SQLiteOpenHelper helper = new DBHelper(activity);
157 SQLiteDatabase db = helper.getWritableDatabase();
158 Cursor cursor = null;
160 cursor = db.query(DBHelper.TABLE_NAME,
162 null, null, null, null,
163 DBHelper.TIMESTAMP_COL + " DESC");
165 while (count < MAX_ITEMS && cursor.moveToNext()) {
168 while (cursor.moveToNext()) {
169 db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
172 if (cursor != null) {
180 * <p>Builds a text representation of the scanning history. Each scan is encoded on one
181 * line, terminated by a line break (\r\n). The values in each line are comma-separated,
182 * and double-quoted. Double-quotes within values are escaped with a sequence of two
183 * double-quotes. The fields output are:</p>
187 * <li>Display text</li>
188 * <li>Format (e.g. QR_CODE)</li>
190 * <li>Formatted version of timestamp</li>
193 private CharSequence buildHistory() {
194 StringBuilder historyText = new StringBuilder(1000);
195 SQLiteOpenHelper helper = new DBHelper(activity);
196 SQLiteDatabase db = helper.getReadableDatabase();
197 Cursor cursor = null;
199 cursor = db.query(DBHelper.TABLE_NAME,
200 EXPORT_COL_PROJECTION,
201 null, null, null, null,
202 DBHelper.TIMESTAMP_COL + " DESC");
203 while (cursor.moveToNext()) {
204 for (int col = 0; col < EXPORT_COL_PROJECTION.length; col++) {
205 historyText.append('"').append(massageHistoryField(cursor.getString(col))).append("\",");
207 // Add timestamp again, formatted
208 long timestamp = cursor.getLong(EXPORT_COL_PROJECTION.length - 1);
209 historyText.append('"').append(massageHistoryField(
210 EXPORT_DATE_TIME_FORMAT.format(new Date(timestamp)))).append("\"\r\n");
213 if (cursor != null) {
221 private Uri saveHistory(String history) {
222 File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
223 File historyRoot = new File(bsRoot, "History");
224 if (!historyRoot.exists() && !historyRoot.mkdirs()) {
225 Log.w(TAG, "Couldn't make dir " + historyRoot);
228 File historyFile = new File(historyRoot, "history-" + System.currentTimeMillis() + ".csv");
229 OutputStreamWriter out = null;
231 out = new OutputStreamWriter(new FileOutputStream(historyFile), Charset.forName("UTF-8"));
233 return Uri.parse("file://" + historyFile.getAbsolutePath());
234 } catch (IOException ioe) {
235 Log.w(TAG, "Couldn't access file " + historyFile + " due to " + ioe);
241 } catch (IOException ioe) {
248 private static String massageHistoryField(String value) {
249 return value.replace("\"","\"\"");
252 void clearHistory() {
253 SQLiteOpenHelper helper = new DBHelper(activity);
254 SQLiteDatabase db = helper.getWritableDatabase();
256 db.delete(DBHelper.TABLE_NAME, null, null);
262 private class HistoryClickListener implements DialogInterface.OnClickListener {
264 private final String[] dialogItems;
265 private final List<Result> items;
267 private HistoryClickListener(String[] dialogItems, List<Result> items) {
268 this.dialogItems = dialogItems;
272 public void onClick(DialogInterface dialogInterface, int i) {
273 if (i == dialogItems.length - 1) {
275 } else if (i == dialogItems.length - 2) {
276 CharSequence history = buildHistory();
277 Uri historyFile = saveHistory(history.toString());
278 if (historyFile == null) {
279 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
280 builder.setMessage(R.string.msg_unmount_usb);
281 builder.setPositiveButton(R.string.button_ok, null);
285 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
286 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
287 String subject = activity.getResources().getString(R.string.history_email_title);
288 intent.putExtra(Intent.EXTRA_SUBJECT, subject);
289 intent.putExtra(Intent.EXTRA_TEXT, subject);
290 intent.putExtra(Intent.EXTRA_STREAM, historyFile);
291 intent.setType("text/csv");
292 activity.startActivity(intent);
294 Result result = items.get(i);
295 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
296 message.sendToTarget();