19
19
import android .app .Activity ;
20
20
import android .app .AlertDialog ;
21
21
import android .content .Context ;
22
+ import android .content .DialogInterface ;
22
23
import android .os .AsyncTask ;
23
24
import android .util .Log ;
24
25
import android .widget .ProgressBar ;
@@ -88,7 +89,7 @@ public static void initialize(Context context) {
88
89
* <p>
89
90
* If this returns false, call createAll.
90
91
*/
91
- public boolean isContentCreated (Context unused ) {
92
+ public boolean isContentCreated (@ SuppressWarnings ( "unused" ) Context unused ) {
92
93
// Ideally this would probe each individual item to see if anything needs to be done,
93
94
// and a subsequent "prepare" call would generate only the necessary items. This
94
95
// takes a much simpler approach and just checks to see if the files exist. If the
@@ -117,7 +118,8 @@ public void createAll(Activity caller) {
117
118
* Prepares the specified content. For example, if the caller requires a movie that doesn't
118
119
* exist, this will post a progress dialog and generate the movie.
119
120
* <p>
120
- * Call from main UI thread.
121
+ * Call from main UI thread. This returns immediately. Content generation continues
122
+ * on a background thread.
121
123
*/
122
124
public void prepareContent (Activity caller , int [] tags ) {
123
125
// Put up the progress dialog.
@@ -126,7 +128,7 @@ public void prepareContent(Activity caller, int[] tags) {
126
128
AlertDialog dialog = builder .show ();
127
129
128
130
// Generate content in async task.
129
- GenerateTask genTask = new GenerateTask (dialog , tags );
131
+ GenerateTask genTask = new GenerateTask (caller , dialog , tags );
130
132
genTask .execute ();
131
133
}
132
134
@@ -201,6 +203,7 @@ public interface ProgressUpdater {
201
203
private static class GenerateTask extends AsyncTask <Void , Integer , Integer >
202
204
implements ProgressUpdater {
203
205
// ----- accessed from UI thread -----
206
+ private final Context mContext ;
204
207
private final AlertDialog mPrepDialog ;
205
208
private final ProgressBar mProgressBar ;
206
209
@@ -209,9 +212,11 @@ private static class GenerateTask extends AsyncTask<Void, Integer, Integer>
209
212
210
213
// ----- accessed from both -----
211
214
private final int [] mTags ;
215
+ private volatile RuntimeException mFailure ;
212
216
213
217
214
- public GenerateTask (AlertDialog dialog , int [] tags ) {
218
+ public GenerateTask (Context context , AlertDialog dialog , int [] tags ) {
219
+ mContext = context ;
215
220
mPrepDialog = dialog ;
216
221
mTags = tags ;
217
222
mProgressBar = (ProgressBar ) mPrepDialog .findViewById (R .id .work_progress );
@@ -226,10 +231,20 @@ protected Integer doInBackground(Void... params) {
226
231
for (int i = 0 ; i < mTags .length ; i ++) {
227
232
mCurrentIndex = i ;
228
233
updateProgress (0 );
229
- contentManager .prepare (this , mTags [i ]);
234
+ try {
235
+ contentManager .prepare (this , mTags [i ]);
236
+ } catch (RuntimeException re ) {
237
+ mFailure = re ;
238
+ break ;
239
+ }
230
240
updateProgress (100 );
231
241
}
232
- Log .d (TAG , "done" );
242
+
243
+ if (mFailure != null ) {
244
+ Log .w (TAG , "Failed while generating content" , mFailure );
245
+ } else {
246
+ Log .d (TAG , "generation complete" );
247
+ }
233
248
return 0 ;
234
249
}
235
250
@@ -254,6 +269,30 @@ protected void onProgressUpdate(Integer... progressArray) {
254
269
protected void onPostExecute (Integer result ) {
255
270
Log .d (TAG , "onPostExecute -- dismss" );
256
271
mPrepDialog .dismiss ();
272
+
273
+ if (mFailure != null ) {
274
+ showFailureDialog (mContext , mFailure );
275
+ }
276
+ }
277
+
278
+ /**
279
+ * Posts an error dialog, including the message from the failure exception.
280
+ */
281
+ private void showFailureDialog (Context context , RuntimeException failure ) {
282
+ AlertDialog .Builder builder = new AlertDialog .Builder (context );
283
+ builder .setTitle (R .string .content_generation_failed_title );
284
+ String msg = context .getString (R .string .content_generation_failed_msg ,
285
+ failure .getMessage ());
286
+ builder .setMessage (msg );
287
+ builder .setPositiveButton (R .string .ok , new DialogInterface .OnClickListener () {
288
+ @ Override
289
+ public void onClick (DialogInterface dialog , int id ) {
290
+ dialog .dismiss ();
291
+ }
292
+ });
293
+ builder .setCancelable (false );
294
+ AlertDialog dialog = builder .create ();
295
+ dialog .show ();
257
296
}
258
297
}
259
298
}
0 commit comments