Issue 336: set Intent flag to make sure task's launched activities don't stay on...
[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.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);          
97           intent.putExtra(Intent.EXTRA_SUBJECT, res.getString(R.string.history_email_title));
98           intent.putExtra(Intent.EXTRA_TEXT, history);
99           intent.setType("text/plain");
100           activity.startActivity(intent);
101         } else {
102           Result result = items.get(i);
103           Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
104           message.sendToTarget();
105         }
106       }
107     };
108     AlertDialog.Builder builder = new AlertDialog.Builder(activity);
109     builder.setTitle(R.string.history_title);
110     builder.setItems(dialogItems, clickListener);
111     return builder.create();
112   }
113
114   public void addHistoryItem(Result result) {
115
116     if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)) {
117       return; // Do not save this item to the history.
118     }
119
120     SQLiteOpenHelper helper = new DBHelper(activity);
121     SQLiteDatabase db = helper.getWritableDatabase();
122     Cursor cursor = null;
123     try {
124       cursor = db.query(DBHelper.TABLE_NAME,
125                         TEXT_COL_PROJECTION,
126                         DBHelper.TEXT_COL + "=?",
127                         new String[] { result.getText() },
128                         null, null, null, null);
129       if (cursor.moveToNext()) {
130         return;
131       }
132       ContentValues values = new ContentValues();
133       values.put(DBHelper.TEXT_COL, result.getText());
134       values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
135       values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
136       values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
137       db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
138     } finally {
139       if (cursor != null) {
140         cursor.close();
141       }
142       db.close();
143     }
144   }
145
146   public void trimHistory() {
147     SQLiteOpenHelper helper = new DBHelper(activity);
148     SQLiteDatabase db = helper.getWritableDatabase();
149     Cursor cursor = null;
150     try {
151       cursor = db.query(DBHelper.TABLE_NAME,
152                         ID_COL_PROJECTION,
153                         null, null, null, null,
154                         DBHelper.TIMESTAMP_COL + " DESC");
155       int count = 0;
156       while (count < MAX_ITEMS && cursor.moveToNext()) {
157         count++;
158       }
159       while (cursor.moveToNext()) {
160         db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
161       }
162     } finally {
163       if (cursor != null) {
164         cursor.close();
165       }
166       db.close();
167     }
168   }
169
170   private String buildHistory() {
171     StringBuilder historyText = new StringBuilder();
172     SQLiteOpenHelper helper = new DBHelper(activity);
173     SQLiteDatabase db = helper.getReadableDatabase();
174     Cursor cursor = null;
175     try {
176       cursor = db.query(DBHelper.TABLE_NAME,
177                         TEXT_COL_PROJECTION,
178                         null, null, null, null,
179                         DBHelper.TIMESTAMP_COL + " DESC");
180       while (cursor.moveToNext()) {
181         historyText.append(cursor.getString(0)).append('\n');
182       }
183     } finally {
184       if (cursor != null) {
185         cursor.close();
186       }
187       db.close();
188     }
189     return historyText.toString();
190   }
191
192   void clearHistory() {
193     SQLiteOpenHelper helper = new DBHelper(activity);
194     SQLiteDatabase db = helper.getWritableDatabase();
195     try {
196       db.delete(DBHelper.TABLE_NAME, null, null);
197     } finally {
198       db.close();
199     }
200   }
201
202 }