5bc5c33177de6ca59d6be0467c462a6bdd4e8142
[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.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;
29
30 import java.util.List;
31 import java.util.ArrayList;
32
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;
38
39 /**
40  * <p>Manages functionality related to scan history.</p>
41  * 
42  * @author Sean Owen
43  */
44 public final class HistoryManager {
45
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 };
50
51   private final CaptureActivity activity;
52
53   public HistoryManager(CaptureActivity activity) {
54     this.activity = activity;
55   }
56
57   List<Result> getHistoryItems() {
58     SQLiteOpenHelper helper = new DBHelper(activity);
59     List<Result> items = new ArrayList<Result>();
60     SQLiteDatabase db = helper.getReadableDatabase();
61     Cursor cursor = null;
62     try {
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)));
69         items.add(result);
70       }
71     } finally {
72       if (cursor != null) {
73         cursor.close();
74       }
75       db.close();
76     }
77     return items;
78   }
79
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();
85     }
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) {
92           clearHistory();
93         } else if (i == dialogItems.length - 2) {
94           String history = buildHistory();
95           Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
96           intent.putExtra(Intent.EXTRA_SUBJECT, res.getString(R.string.history_email_title));
97           intent.putExtra(Intent.EXTRA_TEXT, history);
98           intent.setType("text/plain");
99           activity.startActivity(intent);
100         } else {
101           Result result = items.get(i);
102           Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
103           message.sendToTarget();
104         }
105       }
106     };
107     AlertDialog.Builder builder = new AlertDialog.Builder(activity);
108     builder.setTitle(R.string.history_title);
109     builder.setItems(dialogItems, clickListener);
110     return builder.create();
111   }
112
113   public void addHistoryItem(Result result) {
114
115     if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
116       return; // Do not save this item to the history.
117     }
118
119     SQLiteOpenHelper helper = new DBHelper(activity);
120     SQLiteDatabase db = helper.getWritableDatabase();
121     Cursor cursor = null;
122     try {
123       cursor = db.query(DBHelper.TABLE_NAME,
124                         TEXT_COL_PROJECTION,
125                         DBHelper.TEXT_COL + "=?",
126                         new String[] { result.getText() },
127                         null, null, null, null);
128       if (cursor.moveToNext()) {
129         return;
130       }
131       ContentValues values = new ContentValues();
132       values.put(DBHelper.TEXT_COL, result.getText());
133       values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
134       values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
135       values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
136       db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
137     } finally {
138       if (cursor != null) {
139         cursor.close();
140       }
141       db.close();
142     }
143   }
144
145   public void trimHistory() {
146     SQLiteOpenHelper helper = new DBHelper(activity);
147     SQLiteDatabase db = helper.getWritableDatabase();
148     Cursor cursor = null;
149     try {
150       cursor = db.query(DBHelper.TABLE_NAME,
151                         ID_COL_PROJECTION,
152                         null, null, null, null,
153                         DBHelper.TIMESTAMP_COL + " DESC");
154       int count = 0;
155       while (count < MAX_ITEMS && cursor.moveToNext()) {
156         count++;
157       }
158       while (cursor.moveToNext()) {
159         db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
160       }
161     } finally {
162       if (cursor != null) {
163         cursor.close();
164       }
165       db.close();
166     }
167   }
168
169   private String buildHistory() {
170     StringBuilder historyText = new StringBuilder();
171     SQLiteOpenHelper helper = new DBHelper(activity);
172     SQLiteDatabase db = helper.getReadableDatabase();
173     Cursor cursor = null;
174     try {
175       cursor = db.query(DBHelper.TABLE_NAME,
176                         TEXT_COL_PROJECTION,
177                         null, null, null, null,
178                         DBHelper.TIMESTAMP_COL + " DESC");
179       while (cursor.moveToNext()) {
180         historyText.append(cursor.getString(0)).append('\n');
181       }
182     } finally {
183       if (cursor != null) {
184         cursor.close();
185       }
186       db.close();
187     }
188     return historyText.toString();
189   }
190
191   void clearHistory() {
192     SQLiteOpenHelper helper = new DBHelper(activity);
193     SQLiteDatabase db = helper.getWritableDatabase();
194     try {
195       db.delete(DBHelper.TABLE_NAME, null, null);
196     } finally {
197       db.close();
198     }
199   }
200
201 }