Track deletion UI: code cleanup.

This commit is contained in:
Dennis Guse
2020-07-03 23:33:16 +02:00
parent ff65a4d04f
commit 98c3169929
4 changed files with 46 additions and 115 deletions
@@ -62,17 +62,15 @@ public abstract class AbstractListActivity extends AbstractActivity implements C
@Override @Override
public void onConfirmDeleteDone(long[] trackIds) { public void onConfirmDeleteDone(long[] trackIds) {
boolean stopRecording = false; boolean stopRecording = false;
if (trackIds.length == 1 && trackIds[0] == -1L) {
stopRecording = true; long recordingTrackId = PreferencesUtils.getRecordingTrackId(this);
} else { for (long trackId : trackIds) {
long recordingTrackId = PreferencesUtils.getRecordingTrackId(this); if (trackId == recordingTrackId) {
for (long trackId : trackIds) { stopRecording = true;
if (trackId == recordingTrackId) { break;
stopRecording = true;
break;
}
} }
} }
if (stopRecording) { if (stopRecording) {
getTrackRecordingServiceConnection().stopRecording(this, false); getTrackRecordingServiceConnection().stopRecording(this, false);
} }
@@ -1,87 +0,0 @@
package de.dennisguse.opentracks;
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import android.content.Context;
import android.os.AsyncTask;
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
/**
* Async Task to delete tracks.
*
* @author Jimmy Shih
*/
class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
private final long[] trackIds;
private final Context context;
private TrackDeleteActivity deleteActivity;
// true if the AsyncTask has completed
private boolean completed;
/**
* Creates an AsyncTask.
*
* @param deleteActivity the activity currently associated with this task
* @param trackIds the track ids to delete. To delete all, set to size 1 with
* trackIds[0] == -1L
*/
public DeleteAsyncTask(TrackDeleteActivity deleteActivity, long[] trackIds) {
this.deleteActivity = deleteActivity;
this.trackIds = trackIds;
context = deleteActivity.getApplicationContext();
completed = false;
}
/**
* Sets the current activity associated with this AyncTask.
*
* @param deleteActivity the current activity, can be null
*/
public void setActivity(TrackDeleteActivity deleteActivity) {
this.deleteActivity = deleteActivity;
if (completed && deleteActivity != null) {
deleteActivity.onAsyncTaskCompleted();
}
}
@Override
protected Boolean doInBackground(Void... params) {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
if (trackIds.length == 1 && trackIds[0] == -1L) {
contentProviderUtils.deleteAllTracks(context);
} else {
for (long id : trackIds) {
if (isCancelled()) {
return false;
}
contentProviderUtils.deleteTrack(context, id);
}
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
completed = true;
if (deleteActivity != null) {
deleteActivity.onAsyncTaskCompleted();
}
}
}
@@ -16,40 +16,41 @@
package de.dennisguse.opentracks; package de.dennisguse.opentracks;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
/** /**
* An activity for deleting tracks. * An activity for deleting tracks.
* *
* @author Jimmy Shih * @author Jimmy Shih
*/ */
public class TrackDeleteActivity extends Activity { public class TrackDeleteActivity extends AbstractActivity {
public static final String EXTRA_TRACK_IDS = "track_ids"; public static final String EXTRA_TRACK_IDS = "track_ids";
private long[] trackIds;
private DeleteAsyncTask deleteAsyncTask; private DeleteAsyncTask deleteAsyncTask;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.track_delete);
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
Intent intent = getIntent(); Intent intent = getIntent();
long[] trackIds = intent.getLongArrayExtra(EXTRA_TRACK_IDS); trackIds = intent.getLongArrayExtra(EXTRA_TRACK_IDS);
Object retained = getLastNonConfigurationInstance(); deleteAsyncTask = new DeleteAsyncTask();
if (retained instanceof DeleteAsyncTask) { }
deleteAsyncTask = (DeleteAsyncTask) retained;
deleteAsyncTask.setActivity(this); @Override
} else { protected void onStart() {
deleteAsyncTask = new DeleteAsyncTask(this, trackIds); super.onStart();
deleteAsyncTask.execute(); deleteAsyncTask.execute();
}
} }
@Override @Override
@@ -59,13 +60,34 @@ public class TrackDeleteActivity extends Activity {
} }
@Override @Override
public Object onRetainNonConfigurationInstance() { protected int getLayoutResId() {
deleteAsyncTask.setActivity(null); return R.layout.track_delete;
return deleteAsyncTask;
} }
public void onAsyncTaskCompleted() { public void onAsyncTaskCompleted() {
setResult(RESULT_OK); setResult(RESULT_OK);
finish(); finish();
} }
class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(TrackDeleteActivity.this);
for (long id : trackIds) {
if (isCancelled()) {
return false;
}
contentProviderUtils.deleteTrack(TrackDeleteActivity.this, id);
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
TrackDeleteActivity.this.onAsyncTaskCompleted();
}
}
} }
@@ -165,9 +165,7 @@ public class ContentProviderUtils {
return track; return track;
} }
/** @VisibleForTesting
* Deletes all tracks (including waypoints and trackPoints).
*/
public void deleteAllTracks(Context context) { public void deleteAllTracks(Context context) {
contentResolver.delete(TrackPointsColumns.CONTENT_URI_BY_ID, null, null); contentResolver.delete(TrackPointsColumns.CONTENT_URI_BY_ID, null, null);
contentResolver.delete(WaypointsColumns.CONTENT_URI, null, null); contentResolver.delete(WaypointsColumns.CONTENT_URI, null, null);