Style-related changes
[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.Environment;
29 import android.os.Message;
30
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.OutputStreamWriter;
35 import java.nio.charset.Charset;
36 import java.text.DateFormat;
37 import java.util.Date;
38 import java.util.List;
39 import java.util.ArrayList;
40
41 import android.util.Log;
42 import com.google.zxing.BarcodeFormat;
43 import com.google.zxing.client.android.Intents;
44 import com.google.zxing.client.android.R;
45 import com.google.zxing.client.android.CaptureActivity;
46 import com.google.zxing.Result;
47
48 /**
49  * <p>Manages functionality related to scan history.</p>
50  * 
51  * @author Sean Owen
52  */
53 public final class HistoryManager {
54
55   private static final String TAG = HistoryManager.class.getSimpleName();
56
57   private static final int MAX_ITEMS = 50;
58   private static final String[] TEXT_COL_PROJECTION = { DBHelper.TEXT_COL };
59   private static final String[] GET_ITEM_COL_PROJECTION = {
60       DBHelper.TEXT_COL,
61       DBHelper.FORMAT_COL,
62       DBHelper.TIMESTAMP_COL,
63   };
64   private static final String[] EXPORT_COL_PROJECTION = {
65       DBHelper.TEXT_COL,
66       DBHelper.DISPLAY_COL,
67       DBHelper.FORMAT_COL,
68       DBHelper.TIMESTAMP_COL,
69   };
70   private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
71   private static final DateFormat EXPORT_DATE_TIME_FORMAT = DateFormat.getDateTimeInstance();
72
73   private final CaptureActivity activity;
74
75   public HistoryManager(CaptureActivity activity) {
76     this.activity = activity;
77   }
78
79   List<Result> getHistoryItems() {
80     SQLiteOpenHelper helper = new DBHelper(activity);
81     List<Result> items = new ArrayList<Result>();
82     SQLiteDatabase db = helper.getReadableDatabase();
83     Cursor cursor = null;
84     try {
85       cursor = db.query(DBHelper.TABLE_NAME,
86                         GET_ITEM_COL_PROJECTION,
87                         null, null, null, null,
88                         DBHelper.TIMESTAMP_COL + " DESC");
89       while (cursor.moveToNext()) {
90         Result result = new Result(cursor.getString(0),
91                                    null,
92                                    null,
93                                    BarcodeFormat.valueOf(cursor.getString(1)),
94                                    cursor.getLong(2));
95         items.add(result);
96       }
97     } finally {
98       if (cursor != null) {
99         cursor.close();
100       }
101       db.close();
102     }
103     return items;
104   }
105
106   public AlertDialog buildAlert() {
107     List<Result> items = getHistoryItems();
108     int size = items.size();
109     String[] dialogItems = new String[size + 2];
110     for (int i = 0; i < size; i++) {
111       dialogItems[i] = items.get(i).getText();
112     }
113     Resources res = activity.getResources();
114     dialogItems[dialogItems.length - 2] = res.getString(R.string.history_send);
115     dialogItems[dialogItems.length - 1] = res.getString(R.string.history_clear_text);
116     DialogInterface.OnClickListener clickListener = new HistoryClickListener(dialogItems, items);
117     AlertDialog.Builder builder = new AlertDialog.Builder(activity);
118     builder.setTitle(R.string.history_title);
119     builder.setItems(dialogItems, clickListener);
120     return builder.create();
121   }
122
123   public void addHistoryItem(Result result) {
124
125     if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
126       return; // Do not save this item to the history.
127     }
128
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                         TEXT_COL_PROJECTION,
135                         DBHelper.TEXT_COL + "=?",
136                         new String[] { result.getText() },
137                         null, null, null, null);
138       if (cursor.moveToNext()) {
139         return;
140       }
141       ContentValues values = new ContentValues();
142       values.put(DBHelper.TEXT_COL, result.getText());
143       values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
144       values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
145       values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
146       db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
147     } finally {
148       if (cursor != null) {
149         cursor.close();
150       }
151       db.close();
152     }
153   }
154
155   public void trimHistory() {
156     SQLiteOpenHelper helper = new DBHelper(activity);
157     SQLiteDatabase db = helper.getWritableDatabase();
158     Cursor cursor = null;
159     try {
160       cursor = db.query(DBHelper.TABLE_NAME,
161                         ID_COL_PROJECTION,
162                         null, null, null, null,
163                         DBHelper.TIMESTAMP_COL + " DESC");
164       int count = 0;
165       while (count < MAX_ITEMS && cursor.moveToNext()) {
166         count++;
167       }
168       while (cursor.moveToNext()) {
169         db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
170       }
171     } finally {
172       if (cursor != null) {
173         cursor.close();
174       }
175       db.close();
176     }
177   }
178
179   /**
180    * <p>Builds a text representation of the scanning history. Each scan is encoded on one
181    * line, terminated by a line break (\r\n). The values in each line are comma-separated,
182    * and double-quoted. Double-quotes within values are escaped with a sequence of two
183    * double-quotes. The fields output are:</p>
184    *
185    * <ul>
186    *  <li>Raw text</li>
187    *  <li>Display text</li>
188    *  <li>Format (e.g. QR_CODE)</li>
189    *  <li>Timestamp</li>
190    *  <li>Formatted version of timestamp</li>
191    * </ul>
192    */
193   private CharSequence buildHistory() {
194     StringBuilder historyText = new StringBuilder(1000);
195     SQLiteOpenHelper helper = new DBHelper(activity);
196     SQLiteDatabase db = helper.getReadableDatabase();
197     Cursor cursor = null;
198     try {
199       cursor = db.query(DBHelper.TABLE_NAME,
200                         EXPORT_COL_PROJECTION,
201                         null, null, null, null,
202                         DBHelper.TIMESTAMP_COL + " DESC");
203       while (cursor.moveToNext()) {
204         for (int col = 0; col < EXPORT_COL_PROJECTION.length; col++) {
205           historyText.append('"').append(massageHistoryField(cursor.getString(col))).append("\",");
206         }
207         // Add timestamp again, formatted
208         long timestamp = cursor.getLong(EXPORT_COL_PROJECTION.length - 1);
209         historyText.append('"').append(massageHistoryField(
210             EXPORT_DATE_TIME_FORMAT.format(new Date(timestamp)))).append("\"\r\n");
211       }
212     } finally {
213       if (cursor != null) {
214         cursor.close();
215       }
216       db.close();
217     }
218     return historyText;
219   }
220
221   private Uri saveHistory(String history) {
222     File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
223     File historyRoot = new File(bsRoot, "History");
224     if (!historyRoot.exists() && !historyRoot.mkdirs()) {
225       Log.w(TAG, "Couldn't make dir " + historyRoot);
226       return null;
227     }
228     File historyFile = new File(historyRoot, "history-" + System.currentTimeMillis() + ".csv");
229     OutputStreamWriter out = null;
230     try {
231       out = new OutputStreamWriter(new FileOutputStream(historyFile), Charset.forName("UTF-8"));
232       out.write(history);
233       return Uri.parse("file://" + historyFile.getAbsolutePath());
234     } catch (IOException ioe) {
235       Log.w(TAG, "Couldn't access file " + historyFile + " due to " + ioe);
236       return null;
237     } finally {
238       if (out != null) {
239         try {
240           out.close();
241         } catch (IOException ioe) {
242           // do nothing
243         }
244       }
245     }
246   }
247
248   private static String massageHistoryField(String value) {
249     return value.replace("\"","\"\"");
250   }
251
252   void clearHistory() {
253     SQLiteOpenHelper helper = new DBHelper(activity);
254     SQLiteDatabase db = helper.getWritableDatabase();
255     try {
256       db.delete(DBHelper.TABLE_NAME, null, null);
257     } finally {
258       db.close();
259     }
260   }
261
262   private class HistoryClickListener implements DialogInterface.OnClickListener {
263
264     private final String[] dialogItems;
265     private final List<Result> items;
266
267     private HistoryClickListener(String[] dialogItems, List<Result> items) {
268       this.dialogItems = dialogItems;
269       this.items = items;
270     }
271
272     public void onClick(DialogInterface dialogInterface, int i) {
273       if (i == dialogItems.length - 1) {
274         clearHistory();
275       } else if (i == dialogItems.length - 2) {
276         CharSequence history = buildHistory();
277         Uri historyFile = saveHistory(history.toString());
278         if (historyFile == null) {
279           AlertDialog.Builder builder = new AlertDialog.Builder(activity);
280           builder.setMessage(R.string.msg_unmount_usb);
281           builder.setPositiveButton(R.string.button_ok, null);
282           builder.show();
283           return;
284         }
285         Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
286         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
287         String subject = activity.getResources().getString(R.string.history_email_title);
288         intent.putExtra(Intent.EXTRA_SUBJECT, subject);
289         intent.putExtra(Intent.EXTRA_TEXT, subject);
290         intent.putExtra(Intent.EXTRA_STREAM, historyFile);
291         intent.setType("text/csv");
292         activity.startActivity(intent);
293       } else {
294         Result result = items.get(i);
295         Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
296         message.sendToTarget();
297       }
298     }
299   }
300 }