Add history feature; group some functionality into subpackages
[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.client.android.R;
31 import com.google.zxing.client.android.CaptureActivity;
32 import com.google.zxing.Result;
33
34 /**
35  * @author Sean Owen
36  */
37 public final class HistoryManager {
38
39   private final CaptureActivity activity;
40
41   public HistoryManager(CaptureActivity activity) {
42     this.activity = activity;
43   }
44
45   List<String> getHistoryItems() {
46
47     SQLiteOpenHelper helper = new DBHelper(activity);
48     SQLiteDatabase db = helper.getReadableDatabase();
49     List<String> items = new ArrayList<String>();
50     try {
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));
57       }
58     } finally {
59       db.close();
60     }
61     return items;
62   }
63
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);
69     }
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) {
74           clearHistory();
75         } else {
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();
79         }
80       }
81     };
82     AlertDialog.Builder builder = new AlertDialog.Builder(activity);
83     builder.setTitle(R.string.history_title);
84     builder.setItems(dialogItems, clickListener);
85     return builder.create();
86   }
87
88   public void addHistoryItem(String text) {
89
90     if (getHistoryItems().contains(text)) {
91       return;
92     }
93
94     SQLiteOpenHelper helper = new DBHelper(activity);
95     SQLiteDatabase db = helper.getWritableDatabase();
96     try {
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);
101     } finally {
102       db.close();
103     }
104   }
105
106   void clearHistory() {
107     SQLiteOpenHelper helper = new DBHelper(activity);
108     SQLiteDatabase db = helper.getWritableDatabase();
109     try {
110       db.delete(DBHelper.TABLE_NAME, null, null);
111     } finally {
112       db.close();
113     }
114   }
115
116 }