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.database.sqlite.SQLiteDatabase;
23 import android.database.sqlite.SQLiteOpenHelper;
24 import android.database.Cursor;
25 import android.os.Message;
27 import java.util.List;
28 import java.util.ArrayList;
30 import com.google.zxing.BarcodeFormat;
31 import com.google.zxing.client.android.R;
32 import com.google.zxing.client.android.CaptureActivity;
33 import com.google.zxing.Result;
36 * <p>Manages functionality related to scan history.</p>
40 public final class HistoryManager {
42 private static final int MAX_ITEMS = 20;
43 private static final String[] TEXT_COL_PROJECTION = { DBHelper.TEXT_COL };
44 private static final String[] TEXT_FORMAT_COL_PROJECTION = { DBHelper.TEXT_COL, DBHelper.FORMAT_COL };
45 private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
47 private final CaptureActivity activity;
49 public HistoryManager(CaptureActivity activity) {
50 this.activity = activity;
53 List<Result> getHistoryItems() {
54 SQLiteOpenHelper helper = new DBHelper(activity);
55 List<Result> items = new ArrayList<Result>();
56 SQLiteDatabase db = helper.getReadableDatabase();
59 cursor = db.query(DBHelper.TABLE_NAME,
60 TEXT_FORMAT_COL_PROJECTION,
61 null, null, null, null,
62 DBHelper.TIMESTAMP_COL + " DESC");
63 while (cursor.moveToNext()) {
64 Result result = new Result(cursor.getString(0), null, null, BarcodeFormat.valueOf(cursor.getString(1)));
76 public AlertDialog buildAlert() {
77 final List<Result> items = getHistoryItems();
78 final String[] dialogItems = new String[items.size() + 1];
79 for (int i = 0; i < items.size(); i++) {
80 dialogItems[i] = items.get(i).getText();
82 dialogItems[dialogItems.length - 1] = activity.getResources().getString(R.string.history_clear_text);
83 DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
84 public void onClick(DialogInterface dialogInterface, int i) {
85 if (i == dialogItems.length - 1) {
88 Result result = items.get(i);
89 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
90 message.sendToTarget();
94 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
95 builder.setTitle(R.string.history_title);
96 builder.setItems(dialogItems, clickListener);
97 return builder.create();
100 public void addHistoryItem(Result result) {
102 SQLiteOpenHelper helper = new DBHelper(activity);
103 SQLiteDatabase db = helper.getWritableDatabase();
104 Cursor cursor = null;
106 cursor = db.query(DBHelper.TABLE_NAME,
108 DBHelper.TEXT_COL + "=?",
109 new String[] { result.getText() },
110 null, null, null, null);
111 if (cursor.moveToNext()) {
114 ContentValues values = new ContentValues();
115 values.put(DBHelper.TEXT_COL, result.getText());
116 values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
117 values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
118 values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
119 db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
121 if (cursor != null) {
128 public void trimHistory() {
129 SQLiteOpenHelper helper = new DBHelper(activity);
130 SQLiteDatabase db = helper.getWritableDatabase();
131 Cursor cursor = null;
133 cursor = db.query(DBHelper.TABLE_NAME,
135 null, null, null, null,
136 DBHelper.TIMESTAMP_COL + " DESC");
138 while (count < MAX_ITEMS && cursor.moveToNext()) {
141 while (cursor.moveToNext()) {
142 db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
145 if (cursor != null) {
152 void clearHistory() {
153 SQLiteOpenHelper helper = new DBHelper(activity);
154 SQLiteDatabase db = helper.getWritableDatabase();
156 db.delete(DBHelper.TABLE_NAME, null, null);