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.Message;
30 import java.text.DateFormat;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.ArrayList;
35 import com.google.zxing.BarcodeFormat;
36 import com.google.zxing.client.android.Intents;
37 import com.google.zxing.client.android.R;
38 import com.google.zxing.client.android.CaptureActivity;
39 import com.google.zxing.Result;
42 * <p>Manages functionality related to scan history.</p>
46 public final class HistoryManager {
48 private static final int MAX_ITEMS = 50;
49 private static final String[] TEXT_COL_PROJECTION = { DBHelper.TEXT_COL };
50 private static final String[] GET_ITEM_COL_PROJECTION = {
53 DBHelper.TIMESTAMP_COL,
55 private static final String[] EXPORT_COL_PROJECTION = {
59 DBHelper.TIMESTAMP_COL,
61 private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
62 private static final DateFormat EXPORT_DATE_TIME_FORMAT = DateFormat.getDateTimeInstance();
64 private final CaptureActivity activity;
66 public HistoryManager(CaptureActivity activity) {
67 this.activity = activity;
70 List<Result> getHistoryItems() {
71 SQLiteOpenHelper helper = new DBHelper(activity);
72 List<Result> items = new ArrayList<Result>();
73 SQLiteDatabase db = helper.getReadableDatabase();
76 cursor = db.query(DBHelper.TABLE_NAME,
77 GET_ITEM_COL_PROJECTION,
78 null, null, null, null,
79 DBHelper.TIMESTAMP_COL + " DESC");
80 while (cursor.moveToNext()) {
81 Result result = new Result(cursor.getString(0),
84 BarcodeFormat.valueOf(cursor.getString(1)),
97 public AlertDialog buildAlert() {
98 final List<Result> items = getHistoryItems();
99 final String[] dialogItems = new String[items.size() + 2];
100 for (int i = 0; i < items.size(); i++) {
101 dialogItems[i] = items.get(i).getText();
103 final Resources res = activity.getResources();
104 dialogItems[dialogItems.length - 2] = res.getString(R.string.history_send);
105 dialogItems[dialogItems.length - 1] = res.getString(R.string.history_clear_text);
106 DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
107 public void onClick(DialogInterface dialogInterface, int i) {
108 if (i == dialogItems.length - 1) {
110 } else if (i == dialogItems.length - 2) {
111 CharSequence history = buildHistory();
112 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
113 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
114 intent.putExtra(Intent.EXTRA_SUBJECT, res.getString(R.string.history_email_title));
115 intent.putExtra(Intent.EXTRA_TEXT, history);
116 intent.setType("text/csv");
117 activity.startActivity(intent);
119 Result result = items.get(i);
120 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
121 message.sendToTarget();
125 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
126 builder.setTitle(R.string.history_title);
127 builder.setItems(dialogItems, clickListener);
128 return builder.create();
131 public void addHistoryItem(Result result) {
133 if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
134 return; // Do not save this item to the history.
137 SQLiteOpenHelper helper = new DBHelper(activity);
138 SQLiteDatabase db = helper.getWritableDatabase();
139 Cursor cursor = null;
141 cursor = db.query(DBHelper.TABLE_NAME,
143 DBHelper.TEXT_COL + "=?",
144 new String[] { result.getText() },
145 null, null, null, null);
146 if (cursor.moveToNext()) {
149 ContentValues values = new ContentValues();
150 values.put(DBHelper.TEXT_COL, result.getText());
151 values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
152 values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
153 values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
154 db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
156 if (cursor != null) {
163 public void trimHistory() {
164 SQLiteOpenHelper helper = new DBHelper(activity);
165 SQLiteDatabase db = helper.getWritableDatabase();
166 Cursor cursor = null;
168 cursor = db.query(DBHelper.TABLE_NAME,
170 null, null, null, null,
171 DBHelper.TIMESTAMP_COL + " DESC");
173 while (count < MAX_ITEMS && cursor.moveToNext()) {
176 while (cursor.moveToNext()) {
177 db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
180 if (cursor != null) {
188 * <p>Builds a text representation of the scanning history. Each scan is encoded on one
189 * line, terminated by a line break (\n). The values in each line are comma-separated,
190 * and double-quoted. Double-quotes within values are escaped with a sequence of two
191 * double-quotes. The fields output are:</p>
195 * <li>Display text</li>
196 * <li>Format (e.g. QR_CODE)</li>
198 * <li>Formatted version of timestamp</li>
201 private CharSequence buildHistory() {
202 StringBuilder historyText = new StringBuilder(1000);
203 SQLiteOpenHelper helper = new DBHelper(activity);
204 SQLiteDatabase db = helper.getReadableDatabase();
205 Cursor cursor = null;
207 cursor = db.query(DBHelper.TABLE_NAME,
208 EXPORT_COL_PROJECTION,
209 null, null, null, null,
210 DBHelper.TIMESTAMP_COL + " DESC");
211 while (cursor.moveToNext()) {
212 for (int col = 0; col < EXPORT_COL_PROJECTION.length; col++) {
213 historyText.append('"').append(massageHistoryField(cursor.getString(col)));
215 // Add timestamp again, formatted
216 long timestamp = cursor.getLong(EXPORT_COL_PROJECTION.length - 1);
217 historyText.append('"').append(massageHistoryField(EXPORT_DATE_TIME_FORMAT.format(new Date(timestamp))))
218 .append('"').append('\n');
221 if (cursor != null) {
229 private static String massageHistoryField(String value) {
230 return value.replace("\"","\"\"");
233 void clearHistory() {
234 SQLiteOpenHelper helper = new DBHelper(activity);
235 SQLiteDatabase db = helper.getWritableDatabase();
237 db.delete(DBHelper.TABLE_NAME, null, null);