Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a223b247 authored by Fan Zhang's avatar Fan Zhang
Browse files

Fix a bug where delete alarm toast sometimes doesn't show.

Bug: 22932712

There is a race condition The show toast logic is in a DataSetObserver
and it checks mDeletedAlarm must be present. Sometimes loader's
onLoadFinish() happens before DataSetObserver's onChanged(), and it
wipes mDeletedAlarm, so DataSetObserver doesn't have a chance to show
the toast.

I moved show toast logic to onPostExecute section of AsycnDeleteAlarm.
This is the same mechanism for displaying the toast for adding alarm.

Change-Id: I452b03ea26e22f0638c1223a0a0c64c2ec82707d
parent 366be583
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -255,9 +255,6 @@ public abstract class AlarmClockFragment extends DeskClockFragment implements
            public void onChanged() {

                final int count = mAdapter.getCount();
                if (mDeletedAlarm != null && prevAdapterCount > count) {
                    showUndoBar();
                }

                if (USE_TRANSITION_FRAMEWORK &&
                    ((count == 0 && prevAdapterCount > 0) ||  /* should fade  in */
@@ -1508,22 +1505,29 @@ public abstract class AlarmClockFragment extends DeskClockFragment implements
    }

    private void asyncDeleteAlarm(final Alarm alarm) {
        final Context context = AlarmClockFragment.this.getActivity().getApplicationContext();
        final AsyncTask<Void, Void, Void> deleteTask = new AsyncTask<Void, Void, Void>() {
        final Context context = getActivity().getApplicationContext();
        final AsyncTask<Void, Void, Boolean> deleteTask = new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Void doInBackground(Void... parameters) {
            protected Boolean doInBackground(Void... parameters) {
                // Activity may be closed at this point , make sure data is still valid
                if (context != null && alarm != null) {
                if (context == null || alarm == null) {
                    // Nothing to do here, just return.
                    return false;
                }
                Events.sendAlarmEvent(R.string.action_delete, R.string.label_deskclock);

                    ContentResolver cr = context.getContentResolver();
                AlarmStateManager.deleteAllInstances(context, alarm.id);
                    Alarm.deleteAlarm(cr, alarm.id);
                return Alarm.deleteAlarm(context.getContentResolver(), alarm.id);
            }

            @Override
            protected void onPostExecute(Boolean deleted) {
                if (deleted) {
                    mUndoShowing = true;
                    mDeletedAlarm = alarm;
                    showUndoBar();
                }
                return null;
            }
        };
        mUndoShowing = true;
        deleteTask.execute();
    }