e544bf75577c462fe2e1549914765e8366e6072a
[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.text.DateFormat;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.ArrayList;
34
35 import com.google.zxing.BarcodeFormat;
36 import com.google.zxing.client.android.Intents;
37 import com.google.zxing.client.android.R;
38 import com.google.zxing.client.android.CaptureActivity;
39 import com.google.zxing.Result;
40
41 /**
42  * <p>Manages functionality related to scan history.</p>
43  * 
44  * @author Sean Owen
45  */
46 public final class HistoryManager {
47
48   private static final int MAX_ITEMS = 50;
49   private static final String[] TEXT_COL_PROJECTION = { DBHelper.TEXT_COL };
50   private static final String[] GET_ITEM_COL_PROJECTION = {
51       DBHelper.TEXT_COL,
52       DBHelper.FORMAT_COL,
53       DBHelper.TIMESTAMP_COL,
54   };
55   private static final String[] EXPORT_COL_PROJECTION = {
56       DBHelper.TEXT_COL,
57       DBHelper.DISPLAY_COL,
58       DBHelper.FORMAT_COL,
59       DBHelper.TIMESTAMP_COL,
60   };
61   private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
62   private static final DateFormat EXPORT_DATE_TIME_FORMAT = DateFormat.getDateTimeInstance();
63
64   private final CaptureActivity activity;
65
66   public HistoryManager(CaptureActivity activity) {
67     this.activity = activity;
68   }
69
70   List<Result> getHistoryItems() {
71     SQLiteOpenHelper helper = new DBHelper(activity);
72     List<Result> items = new ArrayList<Result>();
73     SQLiteDatabase db = helper.getReadableDatabase();
74     Cursor cursor = null;
75     try {
76       cursor = db.query(DBHelper.TABLE_NAME,
77                         GET_ITEM_COL_PROJECTION,
78                         null, null, null, null,
79                         DBHelper.TIMESTAMP_COL + " DESC");
80       while (cursor.moveToNext()) {
81         Result result = new Result(cursor.getString(0),
82                                    null,
83                                    null,
84                                    BarcodeFormat.valueOf(cursor.getString(1)),
85                                    cursor.getLong(2));
86         items.add(result);
87       }
88     } finally {
89       if (cursor != null) {
90         cursor.close();
91       }
92       db.close();
93     }
94     return items;
95   }
96
97   public AlertDialog buildAlert() {
98     final List<Result> items = getHistoryItems();
99     final String[] dialogItems = new String[items.size() + 2];
100     for (int i = 0; i < items.size(); i++) {
101       dialogItems[i] = items.get(i).getText();
102     }
103     final Resources res = activity.getResources();
104     dialogItems[dialogItems.length - 2] = res.getString(R.string.history_send);
105     dialogItems[dialogItems.length - 1] = res.getString(R.string.history_clear_text);
106     DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
107       public void onClick(DialogInterface dialogInterface, int i) {
108         if (i == dialogItems.length - 1) {
109           clearHistory();
110         } else if (i == dialogItems.length - 2) {
111           CharSequence history = buildHistory();
112           Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
113           intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);          
114           intent.putExtra(Intent.EXTRA_SUBJECT, res.getString(R.string.history_email_title));
115           intent.putExtra(Intent.EXTRA_TEXT, history);
116           intent.setType("text/csv");
117           activity.startActivity(intent);
118         } else {
119           Result result = items.get(i);
120           Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
121           message.sendToTarget();
122         }
123       }
124     };
125     AlertDialog.Builder builder = new AlertDialog.Builder(activity);
126     builder.setTitle(R.string.history_title);
127     builder.setItems(dialogItems, clickListener);
128     return builder.create();
129   }
130
131   public void addHistoryItem(Result result) {
132
133     if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
134       return; // Do not save this item to the history.
135     }
136
137     SQLiteOpenHelper helper = new DBHelper(activity);
138     SQLiteDatabase db = helper.getWritableDatabase();
139     Cursor cursor = null;
140     try {
141       cursor = db.query(DBHelper.TABLE_NAME,
142                         TEXT_COL_PROJECTION,
143                         DBHelper.TEXT_COL + "=?",
144                         new String[] { result.getText() },
145                         null, null, null, null);
146       if (cursor.moveToNext()) {
147         return;
148       }
149       ContentValues values = new ContentValues();
150       values.put(DBHelper.TEXT_COL, result.getText());
151       values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
152       values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
153       values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
154       db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
155     } finally {
156       if (cursor != null) {
157         cursor.close();
158       }
159       db.close();
160     }
161   }
162
163   public void trimHistory() {
164     SQLiteOpenHelper helper = new DBHelper(activity);
165     SQLiteDatabase db = helper.getWritableDatabase();
166     Cursor cursor = null;
167     try {
168       cursor = db.query(DBHelper.TABLE_NAME,
169                         ID_COL_PROJECTION,
170                         null, null, null, null,
171                         DBHelper.TIMESTAMP_COL + " DESC");
172       int count = 0;
173       while (count < MAX_ITEMS && cursor.moveToNext()) {
174         count++;
175       }
176       while (cursor.moveToNext()) {
177         db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
178       }
179     } finally {
180       if (cursor != null) {
181         cursor.close();
182       }
183       db.close();
184     }
185   }
186
187   /**
188    * <p>Builds a text representation of the scanning history. Each scan is encoded on one
189    * line, terminated by a line break (\n). The values in each line are comma-separated,
190    * and double-quoted. Double-quotes within values are escaped with a sequence of two
191    * double-quotes. The fields output are:</p>
192    *
193    * <ul>
194    *  <li>Raw text</li>
195    *  <li>Display text</li>
196    *  <li>Format (e.g. QR_CODE)</li>
197    *  <li>Timestamp</li>
198    *  <li>Formatted version of timestamp</li>
199    * </ul>
200    */
201   private CharSequence buildHistory() {
202     StringBuilder historyText = new StringBuilder(1000);
203     SQLiteOpenHelper helper = new DBHelper(activity);
204     SQLiteDatabase db = helper.getReadableDatabase();
205     Cursor cursor = null;
206     try {
207       cursor = db.query(DBHelper.TABLE_NAME,
208                         EXPORT_COL_PROJECTION,
209                         null, null, null, null,
210                         DBHelper.TIMESTAMP_COL + " DESC");
211       while (cursor.moveToNext()) {
212         for (int col = 0; col < EXPORT_COL_PROJECTION.length; col++) {
213           historyText.append('"').append(massageHistoryField(cursor.getString(col)));
214         }
215         // Add timestamp again, formatted
216         long timestamp = cursor.getLong(EXPORT_COL_PROJECTION.length - 1);
217         historyText.append('"').append(massageHistoryField(EXPORT_DATE_TIME_FORMAT.format(new Date(timestamp))))
218             .append('"').append('\n');
219       }
220     } finally {
221       if (cursor != null) {
222         cursor.close();
223       }
224       db.close();
225     }
226     return historyText;
227   }
228
229   private static String massageHistoryField(String value) {
230     return value.replace("\"","\"\"");
231   }
232
233   void clearHistory() {
234     SQLiteOpenHelper helper = new DBHelper(activity);
235     SQLiteDatabase db = helper.getWritableDatabase();
236     try {
237       db.delete(DBHelper.TABLE_NAME, null, null);
238     } finally {
239       db.close();
240     }
241   }
242
243 }