Catch bad token exceptions in the broadcast receivers that show dialogs

pull/14/head
Sam Bosley 14 years ago
parent a9fc5b8d5c
commit a790be4079

@ -11,6 +11,7 @@ import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.view.View; import android.view.View;
import android.view.WindowManager.BadTokenException;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
@ -324,13 +325,24 @@ public class AstridActivity extends FragmentActivity
private class ReminderReceiver extends BroadcastReceiver { private class ReminderReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, final Intent intent) {
// Process in app notification // Process in app notification
Intent customIntent = intent.getExtras().getParcelable(Notifications.EXTRAS_CUSTOM_INTENT); Intent customIntent = intent.getExtras().getParcelable(Notifications.EXTRAS_CUSTOM_INTENT);
long taskId = customIntent.getLongExtra(NotificationFragment.TOKEN_ID, 0); long taskId = customIntent.getLongExtra(NotificationFragment.TOKEN_ID, 0);
if (taskId > 0) { if (taskId > 0) {
String text = intent.getStringExtra(Notifications.EXTRAS_TEXT); String text = intent.getStringExtra(Notifications.EXTRAS_TEXT);
new ReminderDialog(AstridActivity.this, taskId, text).show(); try {
new ReminderDialog(AstridActivity.this, taskId, text).show();
} catch (BadTokenException e) { // Activity not running when tried to show dialog--rebroadcast
new Thread() {
@Override
public void run() {
AndroidUtilities.sleepDeep(500L);
sendBroadcast(intent);
}
}.start();
return;
}
} }
// Remove broadcast // Remove broadcast
@ -341,7 +353,7 @@ public class AstridActivity extends FragmentActivity
private class RepeatConfirmationReceiver extends BroadcastReceiver { private class RepeatConfirmationReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, final Intent intent) {
long taskId = intent.getLongExtra( long taskId = intent.getLongExtra(
AstridApiConstants.EXTRAS_TASK_ID, 0); AstridApiConstants.EXTRAS_TASK_ID, 0);
if (taskId > 0) { if (taskId > 0) {
@ -352,8 +364,17 @@ public class AstridActivity extends FragmentActivity
Task task = PluginServices.getTaskService().fetchById(taskId, Task task = PluginServices.getTaskService().fetchById(taskId,
DateChangedAlerts.REPEAT_RESCHEDULED_PROPERTIES); DateChangedAlerts.REPEAT_RESCHEDULED_PROPERTIES);
DateChangedAlerts.showRepeatTaskRescheduledDialog( try {
AstridActivity.this, task, oldDueDate, newDueDate); DateChangedAlerts.showRepeatTaskRescheduledDialog(
AstridActivity.this, task, oldDueDate, newDueDate);
} catch (BadTokenException e) { // Activity not running when tried to show dialog--rebroadcast
new Thread() {
@Override
public void run() {
sendBroadcast(intent);
}
}.start();
}
} }
} }
} }

Loading…
Cancel
Save