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.client.android.R;
31 import com.google.zxing.client.android.CaptureActivity;
32 import com.google.zxing.Result;
37 public final class HistoryManager {
39 private final CaptureActivity activity;
41 public HistoryManager(CaptureActivity activity) {
42 this.activity = activity;
45 List<String> getHistoryItems() {
47 SQLiteOpenHelper helper = new DBHelper(activity);
48 SQLiteDatabase db = helper.getReadableDatabase();
49 List<String> items = new ArrayList<String>();
51 Cursor cursor = db.query(DBHelper.TABLE_NAME,
52 new String[] {DBHelper.TEXT_COL},
53 null, null, null, null,
54 DBHelper.TIMESTAMP_COL + " DESC");
55 while (cursor.moveToNext()) {
56 items.add(cursor.getString(0));
64 public AlertDialog buildAlert() {
65 List<String> items = getHistoryItems();
66 final String[] dialogItems = new String[items.size() + 1];
67 for (int i = 0; i < items.size(); i++) {
68 dialogItems[i] = items.get(i);
70 dialogItems[dialogItems.length - 1] = activity.getResources().getString(R.string.history_clear_text);
71 DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
72 public void onClick(DialogInterface dialogInterface, int i) {
73 if (i == dialogItems.length - 1) {
76 Result result = new Result(dialogItems[i], null, null, null);
77 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
78 message.sendToTarget();
82 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
83 builder.setTitle(R.string.history_title);
84 builder.setItems(dialogItems, clickListener);
85 return builder.create();
88 public void addHistoryItem(String text) {
90 if (getHistoryItems().contains(text)) {
94 SQLiteOpenHelper helper = new DBHelper(activity);
95 SQLiteDatabase db = helper.getWritableDatabase();
97 ContentValues values = new ContentValues();
98 values.put(DBHelper.TEXT_COL, text);
99 values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
100 db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
106 void clearHistory() {
107 SQLiteOpenHelper helper = new DBHelper(activity);
108 SQLiteDatabase db = helper.getWritableDatabase();
110 db.delete(DBHelper.TABLE_NAME, null, null);