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.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
97 intent.putExtra(Intent.EXTRA_SUBJECT, res.getString(R.string.history_email_title));
98 intent.putExtra(Intent.EXTRA_TEXT, history);
99 intent.setType("text/plain");
100 activity.startActivity(intent);
102 Result result = items.get(i);
103 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
104 message.sendToTarget();
108 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
109 builder.setTitle(R.string.history_title);
110 builder.setItems(dialogItems, clickListener);
111 return builder.create();
114 public void addHistoryItem(Result result) {
116 if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
117 return; // Do not save this item to the history.
120 SQLiteOpenHelper helper = new DBHelper(activity);
121 SQLiteDatabase db = helper.getWritableDatabase();
122 Cursor cursor = null;
124 cursor = db.query(DBHelper.TABLE_NAME,
126 DBHelper.TEXT_COL + "=?",
127 new String[] { result.getText() },
128 null, null, null, null);
129 if (cursor.moveToNext()) {
132 ContentValues values = new ContentValues();
133 values.put(DBHelper.TEXT_COL, result.getText());
134 values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
135 values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
136 values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
137 db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
139 if (cursor != null) {
146 public void trimHistory() {
147 SQLiteOpenHelper helper = new DBHelper(activity);
148 SQLiteDatabase db = helper.getWritableDatabase();
149 Cursor cursor = null;
151 cursor = db.query(DBHelper.TABLE_NAME,
153 null, null, null, null,
154 DBHelper.TIMESTAMP_COL + " DESC");
156 while (count < MAX_ITEMS && cursor.moveToNext()) {
159 while (cursor.moveToNext()) {
160 db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
163 if (cursor != null) {
170 private String buildHistory() {
171 StringBuilder historyText = new StringBuilder();
172 SQLiteOpenHelper helper = new DBHelper(activity);
173 SQLiteDatabase db = helper.getReadableDatabase();
174 Cursor cursor = null;
176 cursor = db.query(DBHelper.TABLE_NAME,
178 null, null, null, null,
179 DBHelper.TIMESTAMP_COL + " DESC");
180 while (cursor.moveToNext()) {
181 historyText.append(cursor.getString(0)).append('\n');
184 if (cursor != null) {
189 return historyText.toString();
192 void clearHistory() {
193 SQLiteOpenHelper helper = new DBHelper(activity);
194 SQLiteDatabase db = helper.getWritableDatabase();
196 db.delete(DBHelper.TABLE_NAME, null, null);