Improved history function
[zxing.git] / android / src / com / google / zxing / client / android / history / HistoryManager.java
1 /*
2  * Copyright (C) 2009 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.android.history;
18
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;
26
27 import java.util.List;
28 import java.util.ArrayList;
29
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;
34
35 /**
36  * <p>Manages functionality related to scan history.</p>
37  * 
38  * @author Sean Owen
39  */
40 public final class HistoryManager {
41
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 };
46
47   private final CaptureActivity activity;
48
49   public HistoryManager(CaptureActivity activity) {
50     this.activity = activity;
51   }
52
53   List<Result> getHistoryItems() {
54     SQLiteOpenHelper helper = new DBHelper(activity);
55     List<Result> items = new ArrayList<Result>();
56     SQLiteDatabase db = helper.getReadableDatabase();
57     Cursor cursor = null;
58     try {
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)));
65         items.add(result);
66       }
67     } finally {
68       if (cursor != null) {
69         cursor.close();
70       }
71       db.close();
72     }
73     return items;
74   }
75
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();
81     }
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) {
86           clearHistory();
87         } else {
88           Result result = items.get(i);
89           Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
90           message.sendToTarget();
91         }
92       }
93     };
94     AlertDialog.Builder builder = new AlertDialog.Builder(activity);
95     builder.setTitle(R.string.history_title);
96     builder.setItems(dialogItems, clickListener);
97     return builder.create();
98   }
99
100   public void addHistoryItem(Result result) {
101
102     SQLiteOpenHelper helper = new DBHelper(activity);
103     SQLiteDatabase db = helper.getWritableDatabase();
104     Cursor cursor = null;
105     try {
106       cursor = db.query(DBHelper.TABLE_NAME,
107                         TEXT_COL_PROJECTION,
108                         DBHelper.TEXT_COL + "=?",
109                         new String[] { result.getText() },
110                         null, null, null, null);
111       if (cursor.moveToNext()) {
112         return;
113       }
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);
120     } finally {
121       if (cursor != null) {
122         cursor.close();
123       }
124       db.close();
125     }
126   }
127
128   public void trimHistory() {
129     SQLiteOpenHelper helper = new DBHelper(activity);
130     SQLiteDatabase db = helper.getWritableDatabase();
131     Cursor cursor = null;
132     try {
133       cursor = db.query(DBHelper.TABLE_NAME,
134                         ID_COL_PROJECTION,
135                         null, null, null, null,
136                         DBHelper.TIMESTAMP_COL + " DESC");
137       int count = 0;
138       while (count < MAX_ITEMS && cursor.moveToNext()) {
139         count++;
140       }
141       while (cursor.moveToNext()) {
142         db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
143       }
144     } finally {
145       if (cursor != null) {
146         cursor.close();
147       }
148       db.close();
149     }
150   }
151
152   void clearHistory() {
153     SQLiteOpenHelper helper = new DBHelper(activity);
154     SQLiteDatabase db = helper.getWritableDatabase();
155     try {
156       db.delete(DBHelper.TABLE_NAME, null, null);
157     } finally {
158       db.close();
159     }
160   }
161
162 }