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.util.List;
31 import java.util.ArrayList;
33 import com.google.zxing.BarcodeFormat;
34 import com.google.zxing.client.android.Intents;
35 import com.google.zxing.client.android.R;
36 import com.google.zxing.client.android.CaptureActivity;
37 import com.google.zxing.Result;
40 * <p>Manages functionality related to scan history.</p>
44 public final class HistoryManager {
46 private static final int MAX_ITEMS = 50;
47 private static final String[] TEXT_COL_PROJECTION = { DBHelper.TEXT_COL };
48 private static final String[] TEXT_FORMAT_COL_PROJECTION = { DBHelper.TEXT_COL, DBHelper.FORMAT_COL };
49 private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
51 private final CaptureActivity activity;
53 public HistoryManager(CaptureActivity activity) {
54 this.activity = activity;
57 List<Result> getHistoryItems() {
58 SQLiteOpenHelper helper = new DBHelper(activity);
59 List<Result> items = new ArrayList<Result>();
60 SQLiteDatabase db = helper.getReadableDatabase();
63 cursor = db.query(DBHelper.TABLE_NAME,
64 TEXT_FORMAT_COL_PROJECTION,
65 null, null, null, null,
66 DBHelper.TIMESTAMP_COL + " DESC");
67 while (cursor.moveToNext()) {
68 Result result = new Result(cursor.getString(0), null, null, BarcodeFormat.valueOf(cursor.getString(1)));
80 public AlertDialog buildAlert() {
81 final List<Result> items = getHistoryItems();
82 final String[] dialogItems = new String[items.size() + 2];
83 for (int i = 0; i < items.size(); i++) {
84 dialogItems[i] = items.get(i).getText();
86 final Resources res = activity.getResources();
87 dialogItems[dialogItems.length - 2] = res.getString(R.string.history_send);
88 dialogItems[dialogItems.length - 1] = res.getString(R.string.history_clear_text);
89 DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
90 public void onClick(DialogInterface dialogInterface, int i) {
91 if (i == dialogItems.length - 1) {
93 } else if (i == dialogItems.length - 2) {
94 String history = buildHistory();
95 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
96 intent.putExtra(Intent.EXTRA_SUBJECT, res.getString(R.string.history_email_title));
97 intent.putExtra(Intent.EXTRA_TEXT, history);
98 intent.setType("text/plain");
99 activity.startActivity(intent);
101 Result result = items.get(i);
102 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
103 message.sendToTarget();
107 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
108 builder.setTitle(R.string.history_title);
109 builder.setItems(dialogItems, clickListener);
110 return builder.create();
113 public void addHistoryItem(Result result) {
115 if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
116 return; // Do not save this item to the history.
119 SQLiteOpenHelper helper = new DBHelper(activity);
120 SQLiteDatabase db = helper.getWritableDatabase();
121 Cursor cursor = null;
123 cursor = db.query(DBHelper.TABLE_NAME,
125 DBHelper.TEXT_COL + "=?",
126 new String[] { result.getText() },
127 null, null, null, null);
128 if (cursor.moveToNext()) {
131 ContentValues values = new ContentValues();
132 values.put(DBHelper.TEXT_COL, result.getText());
133 values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
134 values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
135 values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
136 db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
138 if (cursor != null) {
145 public void trimHistory() {
146 SQLiteOpenHelper helper = new DBHelper(activity);
147 SQLiteDatabase db = helper.getWritableDatabase();
148 Cursor cursor = null;
150 cursor = db.query(DBHelper.TABLE_NAME,
152 null, null, null, null,
153 DBHelper.TIMESTAMP_COL + " DESC");
155 while (count < MAX_ITEMS && cursor.moveToNext()) {
158 while (cursor.moveToNext()) {
159 db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
162 if (cursor != null) {
169 private String buildHistory() {
170 StringBuilder historyText = new StringBuilder();
171 SQLiteOpenHelper helper = new DBHelper(activity);
172 SQLiteDatabase db = helper.getReadableDatabase();
173 Cursor cursor = null;
175 cursor = db.query(DBHelper.TABLE_NAME,
177 null, null, null, null,
178 DBHelper.TIMESTAMP_COL + " DESC");
179 while (cursor.moveToNext()) {
180 historyText.append(cursor.getString(0)).append('\n');
183 if (cursor != null) {
188 return historyText.toString();
191 void clearHistory() {
192 SQLiteOpenHelper helper = new DBHelper(activity);
193 SQLiteDatabase db = helper.getWritableDatabase();
195 db.delete(DBHelper.TABLE_NAME, null, null);