This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
HistoryActivity.java
159 lines (144 loc) · 5.45 KB
/
HistoryActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright 2012 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.android.history;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.google.zxing.FakeR;
import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.Intents;
import java.util.List;
public final class HistoryActivity extends ListActivity {
private static final String TAG = HistoryActivity.class.getSimpleName();
private HistoryManager historyManager;
private HistoryItemAdapter adapter;
private static FakeR fakeR;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
fakeR = new FakeR(this);
this.historyManager = new HistoryManager(this);
adapter = new HistoryItemAdapter(this);
setListAdapter(adapter);
ListView listview = getListView();
registerForContextMenu(listview);
}
@Override
protected void onResume() {
super.onResume();
reloadHistoryItems();
}
private void reloadHistoryItems() {
List<HistoryItem> items = historyManager.buildHistoryItems();
adapter.clear();
for (HistoryItem item : items) {
adapter.add(item);
}
if (adapter.isEmpty()) {
adapter.add(new HistoryItem(null, null, null));
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if (adapter.getItem(position).getResult() != null) {
Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(Intents.History.ITEM_NUMBER, position);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
@Override
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenu.ContextMenuInfo menuInfo) {
int position = ((AdapterView.AdapterContextMenuInfo) menuInfo).position;
if (position >= adapter.getCount() || adapter.getItem(position).getResult() != null) {
menu.add(Menu.NONE, position, position, fakeR.getId("string", "history_clear_one_history_text"));
} // else it's just that dummy "Empty" message
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int position = item.getItemId();
historyManager.deleteHistoryItem(position);
reloadHistoryItems();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (historyManager.hasHistoryItems()) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(fakeR.getId("menu", "history"), menu);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == fakeR.getId("id", "menu_history_send")) {
CharSequence history = historyManager.buildHistory();
Uri historyFile = HistoryManager.saveHistory(history.toString());
if (historyFile == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(fakeR.getId("string", "msg_unmount_usb"));
builder.setPositiveButton(fakeR.getId("string", "button_ok"), null);
builder.show();
} else {
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
String subject = getResources().getString(fakeR.getId("string", "history_email_title"));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, subject);
intent.putExtra(Intent.EXTRA_STREAM, historyFile);
intent.setType("text/csv");
try {
startActivity(intent);
} catch (ActivityNotFoundException anfe) {
Log.w(TAG, anfe.toString());
}
}
} else if (itemId == fakeR.getId("id", "menu_history_clear_text")) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(fakeR.getId("string", "msg_sure"));
builder.setCancelable(true);
builder.setPositiveButton(fakeR.getId("string", "button_ok"), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i2) {
historyManager.clearHistory();
dialog.dismiss();
finish();
}
});
builder.setNegativeButton(fakeR.getId("string", "button_cancel"), null);
builder.show();
} else {
return super.onOptionsItemSelected(item);
}
return true;
}
}