-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartView.java
339 lines (288 loc) · 10.3 KB
/
SmartView.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.bbsmart.pda.blackberry.smartview;
import com.bbsmart.pda.blackberry.smartview.io.PersistenceManager;
import com.bbsmart.pda.blackberry.smartview.io.Preferences;
import com.bbsmart.pda.blackberry.smartview.ui.customfield.ImageLoadingDialog;
import com.bbsmart.pda.blackberry.smartview.ui.screens.CommsScreen;
import com.bbsmart.pda.blackberry.smartview.ui.screens.EmailViewScreen;
import com.bbsmart.pda.blackberry.smartview.ui.screens.NewTemplateScreen;
import com.bbsmart.pda.blackberry.smartview.ui.screens.OptionsScreen;
import com.bbsmart.pda.blackberry.smartview.ui.screens.RegisterScreen;
import com.bbsmart.pda.blackberry.smartview.ui.screens.SmartSayScreen;
import com.bbsmart.pda.blackberry.smartview.ui.screens.TrialEndedScreen;
import com.bbsmart.pda.blackberry.smartview.ui.util.UiUtilities;
import com.bbsmart.pda.blackberry.smartview.util.EmailFormatter;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.event.MessageEvent;
import net.rim.blackberry.api.mail.event.ViewListener;
import net.rim.blackberry.api.menuitem.ApplicationMenuItem;
import net.rim.blackberry.api.menuitem.ApplicationMenuItemRepository;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ActiveAutoTextEditField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.VerticalFieldManager;
public final class SmartView extends Application implements ViewListener {
private PersistenceManager persistManager;
private Preferences prefs;
// Flag to indicate whether this is a trial version
public static boolean DEFAULT_TRIAL = false;
public static final long TRIAL_DURATION_DAYS = 4; // In days
public static final String VERSION_STRING = "2.3";
public static final String VERSION = "v" + VERSION_STRING;
// Flag to indicate whether the full version has been registered
public static boolean DEFAULT_REGISTERED = true;
public static void main(String[] args) {
SmartView sv = new SmartView();
sv.enterEventDispatcher();
}
public SmartView() {
// On startup, register the Message View menu items
ApplicationMenuItemRepository repository = ApplicationMenuItemRepository
.getInstance();
BBSmartViewMenuItem viewMenuItem = new BBSmartViewMenuItem();
BBSmartOptionsMenuItem optionsMenuItem = new BBSmartOptionsMenuItem();
SmartSayMenuItem smartSayMenuItem = new SmartSayMenuItem();
CreateTemplateMenuItem createTemplateMenuItem = new CreateTemplateMenuItem();
// Add "BBSmart View" to all view and edit email screens
repository
.addMenuItem(ApplicationMenuItemRepository.MENUITEM_EMAIL_VIEW,
viewMenuItem);
repository
.addMenuItem(ApplicationMenuItemRepository.MENUITEM_EMAIL_EDIT,
viewMenuItem);
// Add "BBSmart Options" to all email screens
repository.addMenuItem(
ApplicationMenuItemRepository.MENUITEM_MESSAGE_LIST,
optionsMenuItem);
repository.addMenuItem(
ApplicationMenuItemRepository.MENUITEM_EMAIL_VIEW,
optionsMenuItem);
repository.addMenuItem(
ApplicationMenuItemRepository.MENUITEM_EMAIL_EDIT,
optionsMenuItem);
// Add "SmartSay" menu options
repository.addMenuItem(
ApplicationMenuItemRepository.MENUITEM_EMAIL_EDIT,
smartSayMenuItem);
repository.addMenuItem(
ApplicationMenuItemRepository.MENUITEM_EMAIL_VIEW,
createTemplateMenuItem);
// Also register listeners for message opened events
Session.addViewListener(this);
// Enable key up events for this application
Application.getApplication().enableKeyUpEvents(true);
persistManager = PersistenceManager.getInstance();
prefs = PersistenceManager.getInstance().getPreferences();
if (prefs.isTrial()) {
// Record the time the application was first run
// Used to determine when trial version has expired
if (prefs.isFirstTime()) {
prefs.setFirstTimeRun(System.currentTimeMillis());
persistManager.savePreferences(prefs);
}
}
}
public void open(MessageEvent e) {
Message m = e.getMessage();
if (m.getStatus() == Message.Status.TX_COMPOSING) {
return;
}
if (prefs.isDontAskMeAgain()) {
if (prefs.isDefaultDisplaySmartView()) {
if (prefs.isFirstTime()) {
// On first time running, pop up the Comms Setup Screen
UiApplication.getUiApplication().pushModalScreen(
new CommsScreen());
}
// Display the SmartView application
displayEmailViewer(m);
} else {
// Don't do anything - user does not want to see smartview by
// default
}
} else {
Dialog d = new Dialog(Dialog.D_YES_NO,
"View with BBSmart Email Viewer?", Dialog.NO, Bitmap
.getPredefinedBitmap(Bitmap.QUESTION), 0, true);
d.doModal();
prefs.setDontAskMeAgain(d.getDontAskAgainValue());
prefs
.setDefaultDisplaySmartView(d.getSelectedValue() == Dialog.YES);
persistManager.savePreferences(prefs);
if (d.getSelectedValue() == Dialog.YES) {
if (prefs.isFirstTime()) {
// On first time running, pop up the Comms Setup Screen
UiApplication.getUiApplication().pushModalScreen(
new CommsScreen());
}
// Display the SmartView application
displayEmailViewer(m);
} else {
// Don't do anything - user does not want to see smartview by
// default
}
}
}
public void close(MessageEvent e) {
// Do nothing on email close (don't care)
}
private void displayEmailViewer(final Message m) {
if (hasExpired()) {
UiApplication.getUiApplication().pushScreen(new TrialEndedScreen());
} else {
if (!prefs.isTrial() && !prefs.isRegistered()) {
// Recently purchased full copy
UiApplication.getUiApplication().pushScreen(
new RegisterScreen());
} else {
if (prefs.isDisplayImagesInEmail()
&& !prefs.isLastConnSuccessful()) {
prefs.setDisplayImagesInEmail(false);
persistManager.savePreferences(prefs);
Dialog d = new Dialog(
Dialog.D_OK,
"BBSmart Email Viewer has detected that your "
+ "connection settings may not be correct and has disabled images from displaying.\n\n"
+ "To configure your connection settings, please go to the Connection Setup screen ("
+ "available from the BBSmart Options screen) and test your connection.\n\n"
+ "Images can be re-enabled from the Options Screen.",
Dialog.OK, Bitmap
.getPredefinedBitmap(Bitmap.EXCLAMATION), 0);
d.doModal();
}
// format the email prior to launching the new display
EmailFormatter formatter = new EmailFormatter(m);
formatter.formatEmail();
UiApplication.getUiApplication().pushScreen(
new EmailViewScreen(m, formatter));
if (formatter.getNumImagesInEmail() > 0) {
UiApplication.getUiApplication().pushScreen(
new ImageLoadingDialog(formatter
.getNumImagesInEmail()));
}
}
}
}
public boolean hasExpired() {
if (prefs.isTrial()) {
final long MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
long currentTime = System.currentTimeMillis();
long startTime = prefs.getFirstTimeRun();
if (currentTime < startTime) { // Catch out funny business...
return true;
}
if (currentTime - startTime >= MILLIS_IN_DAY * TRIAL_DURATION_DAYS) {
// Trial period has expired
return true;
}
}
return false;
}
class BBSmartViewMenuItem extends ApplicationMenuItem {
public BBSmartViewMenuItem() {
super(0);
}
public Object run(Object context) {
if (context == null || !(context instanceof Message)) {
UiUtilities
.alertDialog("No email selected. Please select an email first");
return null;
} else {
if (PersistenceManager.getInstance().getPreferences()
.isFirstTime()) {
// On first time running, pop up the Comms Setup Screen
UiApplication.getUiApplication().pushModalScreen(
new CommsScreen());
}
Message message = (Message) context;
displayEmailViewer(message);
return message;
}
}
public String toString() {
return "BBSmart View";
}
}
class BBSmartOptionsMenuItem extends ApplicationMenuItem {
public BBSmartOptionsMenuItem() {
super(0);
}
public Object run(Object context) {
UiApplication.getUiApplication().pushScreen(new OptionsScreen());
return context;
}
public String toString() {
return "BBSmart Options";
}
}
class SmartSayMenuItem extends ApplicationMenuItem {
public SmartSayMenuItem() {
super(0);
}
public Object run(Object context) {
if (hasExpired()) {
UiApplication.getUiApplication().pushScreen(
new TrialEndedScreen());
} else {
Screen s = UiApplication.getUiApplication().getActiveScreen();
// Due to layout changes in OS 4.3, need to grab the edit
// field to insert template text in a different way
if (BuildVersion.OS_VERSION.equals("4.3.0")) {
Field f = s.getLeafFieldWithFocus();
if (f instanceof ActiveAutoTextEditField) {
UiApplication.getUiApplication()
.pushScreen(
new SmartSayScreen(
(ActiveAutoTextEditField) f));
} else {
// Did not have the text entry field highlighted
UiUtilities
.alertDialog("Please position your cursor where you would like the template inserted first");
}
} else {
VerticalFieldManager vfm = (VerticalFieldManager) s
.getDelegate().getField(0);
ActiveAutoTextEditField editField;
try {
editField = (ActiveAutoTextEditField) vfm.getField(vfm
.getFieldCount() - 1);
} catch (Exception e) {
editField = (ActiveAutoTextEditField) vfm.getField(vfm
.getFieldCount() - 2);
}
UiApplication.getUiApplication().pushScreen(
new SmartSayScreen(editField));
}
}
return context;
}
public String toString() {
return "SmartSay...";
}
}
class CreateTemplateMenuItem extends ApplicationMenuItem {
public CreateTemplateMenuItem() {
super(0);
}
public Object run(Object context) {
if (hasExpired()) {
UiApplication.getUiApplication().pushScreen(
new TrialEndedScreen());
} else {
if (context != null && context instanceof Message) {
Message m = (Message) context;
UiApplication.getUiApplication().pushScreen(
new NewTemplateScreen(m.getBodyText()));
}
}
return context;
}
public String toString() {
return "Save as Template";
}
}
}