forked from eleybourn/Book-Catalogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartupActivity.java
419 lines (361 loc) · 12.9 KB
/
StartupActivity.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* @copyright 2012 Philip Warner
* @license GNU General Public License
*
* This file is part of Book Catalogue.
*
* Book Catalogue is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Book Catalogue is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Book Catalogue. If not, see <http://www.gnu.org/licenses/>.
*/
package com.eleybourn.bookcatalogue;
import java.lang.ref.WeakReference;
import com.eleybourn.bookcatalogue.BookCatalogueApp.BookCataloguePreferences;
import com.eleybourn.bookcatalogue.SimpleTaskQueue.OnTaskFinishListener;
import com.eleybourn.bookcatalogue.SimpleTaskQueue.SimpleTask;
import com.eleybourn.bookcatalogue.SimpleTaskQueue.SimpleTaskContext;
import com.eleybourn.bookcatalogue.booklist.BooklistPreferencesActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
/**
* Single Activity to be the 'Main' activity for the app. I does app-startup stuff which is initially
* to start the 'real' main activity.
*
* Note that calling the desired main activity first resulted in MainMenu's 'singleInstance' property
* NOT being honoured. So we call MainMenu anyway, but set a flag in the Intent to indicate this is
* a startup. This approach mostly works, but results in an apparent misordering of the activity
* stack, which we can live with for now.
*
* @author Philip Warner
*/
public class StartupActivity extends Activity {
private static String TAG = "StartupActivity";
/** Flag to indicate FTS rebuild is required at startup */
private static String PREF_FTS_REBUILD_REQUIRED = TAG + ".FtsRebuildRequired";
private static String PREF_AUTHOR_SERIES_FIXUP_REQUIRED = TAG + ".FAuthorSeriesFixupRequired";
private static final String STATE_OPENED = "state_opened";
private static final int BACKUP_PROMPT_WAIT = 5;
/** Indicates the upgrade message has been shown */
private static boolean mUpgradeMessageShown = false;
/** Flag set to true on first call */
private static boolean mIsReallyStartup = true;
/** Flag indicating a StartupActivity has been created in this session */
private static boolean mHasBeenCalled = false;
/** Queue for executing startup tasks, if any */
private SimpleTaskQueue mTaskQueue = null;
/** Progress Dialog for startup tasks */
private ProgressDialog mProgress = null;
/** Flag indicating THIS instance was really the startup instance */
private boolean mWasReallyStartup = false;
/** Flag indicating an export is required after startup */
private boolean mExportRequired = false;
/** Database connection */
//CatalogueDBAdapter mDb = null;
/** Handler to post runnables to UI thread */
private Handler mHandler = new Handler();
/**UI thread */
private Thread mUiThread;
/** Set the flag to indicate an FTS rebuild is required */
public static void scheduleFtsRebuild() {
BookCatalogueApp.getAppPreferences().setBoolean(PREF_FTS_REBUILD_REQUIRED, true);
}
/** Set the flag to indicate an FTS rebuild is required */
public static void scheduleAuthorSeriesFixup() {
BookCatalogueApp.getAppPreferences().setBoolean(PREF_AUTHOR_SERIES_FIXUP_REQUIRED, true);
}
public static WeakReference<StartupActivity> mStartupActivity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHasBeenCalled = true;
mUiThread = Thread.currentThread();
// Create a progress dialog; we may not use it...but we need it to be created in the UI thread.
mProgress = ProgressDialog.show(this, getString(R.string.book_catalogue_startup), getString(R.string.starting_up), true, true, new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// Cancelling the list cancels the activity.
StartupActivity.this.finish();
}});
mWasReallyStartup = mIsReallyStartup;
// If it's a real application startup...cleanup old stuff
if (mWasReallyStartup) {
mStartupActivity = new WeakReference<StartupActivity>(this);
updateProgress("Starting");
SimpleTaskQueue q = getQueue();
// Always enqueue it; it will get a DB and check if required...
q.enqueue(new RebuildFtsTask());
q.enqueue(new AnalyzeDbTask());
// Remove old logs
Logger.clearLog();
// Clear the flag
mIsReallyStartup = false;
// ENHANCE: add checks for new Events/crashes
}
// If no tasks were queued, then move on to stage 2. Otherwise, the completed
// tasks will cause stage 2 to start.
if (mTaskQueue == null)
stage2Startup();
}
/**
* Kludge to get a reference to the currently running StartupActivity, if defined.
*
* @return Reference or null.
*/
public static StartupActivity getActiveActivity() {
if (mStartupActivity != null)
return mStartupActivity.get();
else
return null;
}
/**
* Update the progress dialog, if it has not been dismissed.
*
* @param message
*/
public void updateProgress(final int stringId) {
updateProgress(getString(stringId));
}
/**
* Update the progress dialog, if it has not been dismissed.
*
* @param message
*/
public void updateProgress(final String message) {
// If mProgress is null, it has been dismissed. Don't update.
if (mProgress == null) {
return;
}
// If we are in the UI thread, update the progress.
if (Thread.currentThread().equals(mUiThread)) {
// There is a small chance that this message could be set to display *after* the activity is finished,
// so we check and we also trap, log and ignore errors.
// See http://code.google.com/p/android/issues/detail?id=3953
if (!isFinishing()) {
try {
mProgress.setMessage(message);
if (!mProgress.isShowing())
mProgress.show();
} catch (Exception e) {
Logger.logError(e);
}
}
} else {
// If we are NOT in the UI thread, queue it to the UI thread.
mHandler.post(new Runnable() {
@Override
public void run() {
updateProgress(message);
}});
}
}
/**
* Called in the UI thread when any startup task completes. If no more tasks, start stage 2.
* Because it is in the UI thread, it is not possible for this code to be called until after
* onCreate() completes, so a race condition is not possible. Equally well, tasks should only
* be queued in onCreate().
*/
private void taskCompleted(SimpleTask task) {
System.out.println("Task Completed: " + task.getClass().getSimpleName());
if (!mTaskQueue.hasActiveTasks()) {
System.out.println("Task Completed - no more");
stage2Startup();
}
}
/**
* Get (or create) the task queue.
*
* @return
*/
private SimpleTaskQueue getQueue() {
if (mTaskQueue == null) {
mTaskQueue = new SimpleTaskQueue("startup-tasks", 1);
// Listen for task completions
mTaskQueue.setTaskFinishListener(new OnTaskFinishListener() {
@Override
public void onTaskFinish(SimpleTask task, Exception e) {
taskCompleted(task);
}});
}
return mTaskQueue;
}
/**
* Called in UI thread after last startup task completes, or if there are no tasks to queue.
*/
private void stage2Startup() {
// Remove the weak reference. Only used by db onUpgrade.
mStartupActivity.clear();
// Get rid of the progress dialog
if (mProgress != null) {
mProgress.dismiss();
mProgress = null;
}
// Display upgrade message if necessary, otherwise go on to stage 3
if (mUpgradeMessageShown || CatalogueDBAdapter.message.equals("")) {
stage3Startup();
} else {
upgradePopup(CatalogueDBAdapter.message);
}
}
/**
* Start the main book list
*/
private void doMyBooks() {
Intent i = new Intent(this, BooksOnBookshelf.class);
if (mWasReallyStartup)
i.putExtra("startup", true);
startActivity(i);
}
/**
* Start the main menu
*/
private void doMainMenu() {
Intent i = new Intent(this, MainMenu.class);
if (mWasReallyStartup)
i.putExtra("startup", true);
startActivity(i);
}
public void stage3Startup() {
BookCataloguePreferences prefs = BookCatalogueApp.getAppPreferences();
int opened = prefs.getInt(STATE_OPENED, BACKUP_PROMPT_WAIT);
Editor ed = prefs.edit();
if (opened == 0) {
ed.putInt(STATE_OPENED, BACKUP_PROMPT_WAIT);
} else {
ed.putInt(STATE_OPENED, opened - 1);
}
ed.commit();
mExportRequired = false;
if (opened == 0) {
AlertDialog alertDialog = new AlertDialog.Builder(this).setMessage(R.string.backup_request).create();
alertDialog.setTitle(R.string.backup_title);
alertDialog.setIcon(android.R.drawable.ic_menu_info_details);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mExportRequired = true;
dialog.dismiss();
}
});
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
stage4Startup();
}});
alertDialog.show();
} else {
stage4Startup();
}
}
/**
* Start whatever activity the user expects
*/
private void stage4Startup() {
BookCataloguePreferences prefs = BookCatalogueApp.getAppPreferences();
Bundle extras = this.getIntent().getExtras();
// Handle startup specially.
// Check if we really want to start this activity.
if (prefs.getStartInMyBook()) {
doMyBooks();
} else {
doMainMenu();
}
if (mExportRequired)
Administration.adminPage(StartupActivity.this, "export", R.id.ACTIVITY_ADMIN);
// We are done
finish();
}
/**
* This will display a popup with a provided message to the user. This will be
* mostly used for upgrade notifications
*
* @param message The message to display in the popup
*/
public void upgradePopup(String message) {
AlertDialog alertDialog = new AlertDialog.Builder(this).setMessage(message).create();
alertDialog.setTitle(R.string.upgrade_title);
alertDialog.setIcon(android.R.drawable.ic_menu_info_details);
alertDialog.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
stage3Startup();
}
});
alertDialog.show();
mUpgradeMessageShown = true;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mTaskQueue != null)
mTaskQueue.finish();
}
/**
* Task to rebuild FTS in background. Can take several seconds, so not done in onUpgrade().
*
* @author Philip Warner
*
*/
public class RebuildFtsTask implements SimpleTask {
@Override
public void run(SimpleTaskContext taskContext) {
// Get a DB to make sure the FTS rebuild flag is set appropriately
CatalogueDBAdapter db = taskContext.getDb();
BookCataloguePreferences prefs = BookCatalogueApp.getAppPreferences();
if (prefs.getBoolean(PREF_FTS_REBUILD_REQUIRED, false)) {
updateProgress(getString(R.string.rebuilding_search_index));
db.rebuildFts();
prefs.setBoolean(PREF_FTS_REBUILD_REQUIRED, false);
}
}
@Override
public void onFinish() {}
@Override
public boolean requiresOnFinish() { return false; }
}
public class AnalyzeDbTask implements SimpleTask {
@Override
public void run(SimpleTaskContext taskContext) {
CatalogueDBAdapter db = taskContext.getDb();
BookCataloguePreferences prefs = BookCatalogueApp.getAppPreferences();
updateProgress(getString(R.string.optimizing_databases));
// Analyze DB
db.analyzeDb();
if (BooklistPreferencesActivity.isThumbnailCacheEnabled()) {
// Analyze the covers DB
Utils utils = taskContext.getUtils();
utils.analyzeCovers();
}
if (prefs.getBoolean(PREF_AUTHOR_SERIES_FIXUP_REQUIRED, false)) {
db.fixupAuthorsAndSeries();
prefs.setBoolean(PREF_AUTHOR_SERIES_FIXUP_REQUIRED, false);
}
}
@Override
public void onFinish() {}
@Override
public boolean requiresOnFinish() { return false; }
}
public static boolean hasBeenCalled() {
return mHasBeenCalled;
}
}