Refactoring async task

This commit is contained in:
Dima-1 2020-11-10 20:28:42 +02:00
parent bcafe632c3
commit a33b703cf7
5 changed files with 164 additions and 112 deletions

View file

@ -0,0 +1,54 @@
package net.osmand.plus.osmedit;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class HandleOsmNoteAsyncTask extends AsyncTask<Void, Void, OsmBugsUtil.OsmBugResult> {
private OsmBugsUtil osmbugsUtil;
private final OsmBugsUtil local;
private final OsmBugsLayer.OpenStreetNote bug;
private final OsmNotesPoint point;
private final String text;
private final OsmPoint.Action action;
private final HandleBugListener handleBugListener;
public HandleOsmNoteAsyncTask(@NonNull OsmBugsUtil osmbugsUtil, @NonNull OsmBugsUtil local,
@Nullable OsmBugsLayer.OpenStreetNote bug, @Nullable OsmNotesPoint point,
String text, OsmPoint.Action action,
@Nullable HandleBugListener handleBugListener) {
this.osmbugsUtil = osmbugsUtil;
this.local = local;
this.bug = bug;
this.point = point;
this.text = text;
this.action = action;
this.handleBugListener = handleBugListener;
}
@Override
protected OsmBugsUtil.OsmBugResult doInBackground(Void... params) {
if (bug != null) {
OsmNotesPoint pnt = new OsmNotesPoint();
pnt.setId(bug.getId());
pnt.setLatitude(bug.getLatitude());
pnt.setLongitude(bug.getLongitude());
return osmbugsUtil.commit(pnt, text, action);
} else if (point != null) {
osmbugsUtil = local;
return osmbugsUtil.modify(point, text);
}
return null;
}
protected void onPostExecute(OsmBugsUtil.OsmBugResult obj) {
handleBugListener.onOsmBugHandled(obj, action, bug, point, text);
}
public interface HandleBugListener {
void onOsmBugHandled(OsmBugsUtil.OsmBugResult obj, OsmPoint.Action action, OsmBugsLayer.OpenStreetNote bug,
OsmNotesPoint point, String text);
}
}

View file

@ -4,7 +4,6 @@ import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.os.AsyncTask;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
@ -29,7 +28,6 @@ import net.osmand.plus.osmedit.dialogs.BugBottomSheetDialog;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.osmedit.OsmBugsUtil.OsmBugResult;
import net.osmand.plus.osmedit.OsmPoint.Action;
import net.osmand.plus.views.layers.ContextMenuLayer.IContextMenuProvider;
import net.osmand.plus.views.OsmandMapLayer;
@ -48,6 +46,8 @@ import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import static net.osmand.plus.osmedit.OsmBugsUtil.*;
public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider {
private static final Log log = PlatformUtil.getLog(OsmBugsLayer.class);
@ -246,7 +246,6 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
return text;
}
protected List<OpenStreetNote> loadingBugs(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude) {
final int deviceApiVersion = android.os.Build.VERSION.SDK_INT;
@ -311,89 +310,16 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
return bugs;
}
private void asyncActionTask(final OpenStreetNote bug, final OsmNotesPoint point, final String text, final Action action) {
AsyncTask<Void, Void, OsmBugResult> task = new AsyncTask<Void, Void, OsmBugResult>() {
private OsmBugsUtil osmbugsUtil;
@Override
protected OsmBugResult doInBackground(Void... params) {
if (bug != null) {
osmbugsUtil = getOsmbugsUtil(bug);
OsmNotesPoint pnt = new OsmNotesPoint();
pnt.setId(bug.getId());
pnt.setLatitude(bug.getLatitude());
pnt.setLongitude(bug.getLongitude());
return osmbugsUtil.commit(pnt, text, action);
} else if (point != null) {
osmbugsUtil = local;
return osmbugsUtil.modify(point, text);
}
return null;
}
protected void onPostExecute(OsmBugResult obj) {
if (activity == null || activity.isFinishing() || activity.isActivityDestroyed()) {
return;
}
if (obj != null && obj.warning == null) {
if (local == osmbugsUtil) {
Toast.makeText(activity, R.string.osm_changes_added_to_local_edits, Toast.LENGTH_LONG).show();
if (obj.local != null) {
PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_OSM_BUG, obj.local.getText());
activity.getContextMenu().show(new LatLon(obj.local.getLatitude(), obj.local.getLongitude()), pd, obj.local);
}
} else {
if (action == Action.REOPEN) {
Toast.makeText(activity, R.string.osn_add_dialog_success, Toast.LENGTH_LONG).show();
} else if (action == Action.MODIFY) {
Toast.makeText(activity, R.string.osb_comment_dialog_success, Toast.LENGTH_LONG).show();
} else if (action == Action.DELETE) {
Toast.makeText(activity, R.string.osn_close_dialog_success, Toast.LENGTH_LONG).show();
} else if (action == Action.CREATE) {
Toast.makeText(activity, R.string.osn_add_dialog_success, Toast.LENGTH_LONG).show();
}
}
clearCache();
} else {
int r = R.string.osb_comment_dialog_error;
if (action == Action.REOPEN) {
r = R.string.osn_add_dialog_error;
reopenBug(bug, text);
} else if (action == Action.DELETE) {
r = R.string.osn_close_dialog_error;
closeBug(bug, text);
} else if (action == Action.CREATE) {
r = R.string.osn_add_dialog_error;
openBug(bug.getLatitude(), bug.getLongitude(), text);
} else if (action == null) {
r = R.string.osn_modify_dialog_error;
modifyBug(point);
} else {
commentBug(bug, text);
}
Toast.makeText(activity, activity.getResources().getString(r) + "\n" + obj, Toast.LENGTH_LONG).show();
}
}
};
executeTaskInBackground(task);
}
public void openBug(final double latitude, final double longitude, String message) {
OpenStreetNote bug = new OpenStreetNote();
bug.setLatitude(latitude);
bug.setLongitude(longitude);
showBugDialog(bug, Action.CREATE, message);
}
public void openBug(final double latitude, final double longitude, String message, boolean autofill) {
OpenStreetNote bug = new OpenStreetNote();
bug.setLatitude(latitude);
bug.setLongitude(longitude);
if (autofill) asyncActionTask(bug, null, message, Action.CREATE);
else showBugDialog(bug, Action.CREATE, message);
if (autofill) {
executeTaskInBackground(new HandleOsmNoteAsyncTask(getOsmbugsUtil(bug), local, bug, null, message,
Action.CREATE, getHandleBugListener()));
} else {
showBugDialog(bug, Action.CREATE, message);
}
}
public void closeBug(final OpenStreetNote bug, String txt) {
@ -443,8 +369,12 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
private void createBugDialog(final boolean offline, String text, int titleTextId, int posButtonTextId,
final Action action, final OpenStreetNote bug, final OsmNotesPoint point) {
BugBottomSheetDialog.showInstance(activity.getSupportFragmentManager(), offline, text, titleTextId,
posButtonTextId, action, bug, null);
if (offline) {
activity.getContextMenu().close();
BugBottomSheetDialog.showInstance(activity.getSupportFragmentManager(), getOsmbugsUtil(bug), local, text,
titleTextId, posButtonTextId, action, bug, point, getHandleBugListener());
return;
}
@SuppressLint("InflateParams") final View view = LayoutInflater.from(activity).inflate(R.layout.open_bug, null);
if (offline) {
view.findViewById(R.id.user_name_field).setVisibility(View.GONE);
@ -453,7 +383,8 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
view.findViewById(R.id.passwordEditTextLabel).setVisibility(View.GONE);
} else {
((EditText) view.findViewById(R.id.user_name_field)).setText(getUserName());
((EditText) view.findViewById(R.id.password_field)).setText(((OsmandApplication) activity.getApplication()).getSettings().USER_PASSWORD.get());
((EditText) view.findViewById(R.id.password_field)).setText(
activity.getMyApplication().getSettings().USER_PASSWORD.get());
}
if (!Algorithms.isEmpty(text)) {
((EditText) view.findViewById(R.id.message_field)).setText(text);
@ -469,17 +400,71 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
public void onClick(DialogInterface dialog, int which) {
String text = offline ? getMessageText(view) : getTextAndUpdateUserPwd(view);
activity.getContextMenu().close();
if (bug != null) {
asyncActionTask(bug, null, text, action);
} else if (point != null) {
asyncActionTask(null, point, text, null);
}
handleBug(text, bug, action, point);
}
});
builder.setNegativeButton(R.string.shared_string_cancel, null);
builder.create().show();
}
private void handleBug(String text, OpenStreetNote bug, Action action, OsmNotesPoint point) {
if (bug != null || point != null) {
executeTaskInBackground(new HandleOsmNoteAsyncTask(getOsmbugsUtil(bug), local, bug, point, text, action,
getHandleBugListener()));
}
}
HandleOsmNoteAsyncTask.HandleBugListener getHandleBugListener() {
return new HandleOsmNoteAsyncTask.HandleBugListener() {
@Override
public void onOsmBugHandled(OsmBugResult obj, Action action, OpenStreetNote bug,
OsmNotesPoint point, String text) {
if (activity == null || activity.isFinishing()) {
return;
}
if (obj != null && obj.warning == null) {
if (local == getOsmbugsUtil(bug)) {
Toast.makeText(activity, R.string.osm_changes_added_to_local_edits, Toast.LENGTH_LONG).show();
if (obj.local != null) {
PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_OSM_BUG, obj.local.getText());
activity.getContextMenu().show(new LatLon(obj.local.getLatitude(), obj.local.getLongitude()),
pd, obj.local);
}
} else {
if (action == Action.REOPEN) {
Toast.makeText(activity, R.string.osn_add_dialog_success, Toast.LENGTH_LONG).show();
} else if (action == Action.MODIFY) {
Toast.makeText(activity, R.string.osb_comment_dialog_success, Toast.LENGTH_LONG).show();
} else if (action == Action.DELETE) {
Toast.makeText(activity, R.string.osn_close_dialog_success, Toast.LENGTH_LONG).show();
} else if (action == Action.CREATE) {
Toast.makeText(activity, R.string.osn_add_dialog_success, Toast.LENGTH_LONG).show();
}
}
clearCache();
} else {
int r = R.string.osb_comment_dialog_error;
if (action == Action.REOPEN) {
r = R.string.osn_add_dialog_error;
reopenBug(bug, text);
} else if (action == Action.DELETE) {
r = R.string.osn_close_dialog_error;
closeBug(bug, text);
} else if (action == Action.CREATE) {
r = R.string.osn_add_dialog_error;
openBug(bug.getLatitude(), bug.getLongitude(), text, false);
} else if (action == null) {
r = R.string.osn_modify_dialog_error;
modifyBug(point);
} else {
commentBug(bug, text);
}
Toast.makeText(activity, activity.getResources().getString(r) + "\n" + obj, Toast.LENGTH_LONG).show();
}
}
};
}
private String getUserName() {
return ((OsmandApplication) activity.getApplication()).getSettings().USER_NAME.get();
}
@ -503,8 +488,6 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
}
}
@Override
public PointDescription getObjectName(Object o) {
if (o instanceof OpenStreetNote) {

View file

@ -229,7 +229,7 @@ public class OsmEditingPlugin extends OsmandPlugin {
editPoiDialogFragment.show(mapActivity.getSupportFragmentManager(),
EditPoiDialogFragment.TAG);
} else if (resId == R.string.context_menu_item_open_note) {
openOsmNote(mapActivity, latitude, longitude);
openOsmNote(mapActivity, latitude, longitude, "", false);
} else if (resId == R.string.context_menu_item_modify_note) {
modifyOsmNote(mapActivity, (OsmNotesPoint) selectedObj);
} else if (resId == R.string.poi_context_menu_modify) {
@ -300,13 +300,6 @@ public class OsmEditingPlugin extends OsmandPlugin {
}
}
public void openOsmNote(MapActivity mapActivity, double latitude, double longitude) {
if (osmBugsLayer == null) {
registerLayers(mapActivity);
}
osmBugsLayer.openBug(latitude, longitude, "");
}
public void openOsmNote(MapActivity mapActivity, double latitude, double longitude, String message, boolean autofill) {
if (osmBugsLayer == null) {
registerLayers(mapActivity);

View file

@ -1,6 +1,7 @@
package net.osmand.plus.osmedit.dialogs;
import android.content.res.ColorStateList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
@ -20,7 +21,9 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.osmedit.HandleOsmNoteAsyncTask;
import net.osmand.plus.osmedit.OsmBugsLayer;
import net.osmand.plus.osmedit.OsmBugsUtil;
import net.osmand.plus.osmedit.OsmNotesPoint;
import net.osmand.plus.osmedit.OsmPoint;
@ -31,14 +34,16 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment {
public static final String TAG = BugBottomSheetDialog.class.getSimpleName();
private static final Log LOG = PlatformUtil.getLog(BugBottomSheetDialog.class);
boolean offline;
OsmBugsUtil osmBugsUtil;
OsmBugsUtil local;
String text;
int titleTextId;
int posButtonTextId;
OsmPoint.Action action;
OsmBugsLayer.OpenStreetNote bug;
OsmNotesPoint point;
private TextInputLayout textBox;
HandleOsmNoteAsyncTask.HandleBugListener handleBugListener;
private TextInputEditText noteText;
@Override
public void createMenuItems(Bundle savedInstanceState) {
@ -51,14 +56,14 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment {
View osmNoteView = View.inflate(UiUtilities.getThemedContext(app, nightMode),
R.layout.track_name_edit_text, null);
textBox = osmNoteView.findViewById(R.id.name_text_box);
TextInputLayout textBox = osmNoteView.findViewById(R.id.name_text_box);
int highlightColorId = nightMode ? R.color.list_background_color_dark : R.color.activity_background_color_light;
textBox.setBoxBackgroundColorResource(highlightColorId);
textBox.setHint(AndroidUtils.addColon(app, R.string.osn_bug_name));
ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat
.getColor(app, nightMode ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light));
textBox.setDefaultHintTextColor(colorStateList);
TextInputEditText noteText = osmNoteView.findViewById(R.id.name_edit_text);
noteText = osmNoteView.findViewById(R.id.name_edit_text);
noteText.setText(text);
BaseBottomSheetItem editOsmNote = new BaseBottomSheetItem.Builder()
@ -74,19 +79,35 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment {
return posButtonTextId;
}
public static void showInstance(@NonNull FragmentManager fm, final boolean offline,
@Override
protected void onRightBottomButtonClick() {
new HandleOsmNoteAsyncTask(osmBugsUtil, local, bug, point, noteText.getText().toString(), action,
handleBugListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
dismiss();
}
@Override
protected int getBgColorId() {
return nightMode ? R.color.activity_background_color_dark : R.color.list_background_color_light;
}
public static void showInstance(@NonNull FragmentManager fm, OsmBugsUtil osmBugsUtil, OsmBugsUtil local,
String text, int titleTextId, int posButtonTextId, final OsmPoint.Action action,
final OsmBugsLayer.OpenStreetNote bug, final OsmNotesPoint point) {
final OsmBugsLayer.OpenStreetNote bug, final OsmNotesPoint point,
HandleOsmNoteAsyncTask.HandleBugListener handleBugListener) {
try {
if (!fm.isStateSaved()) {
BugBottomSheetDialog fragment = new BugBottomSheetDialog();
fragment.offline = offline;
fragment.setRetainInstance(true);
fragment.osmBugsUtil = osmBugsUtil;
fragment.local = local;
fragment.text = text;
fragment.titleTextId = titleTextId;
fragment.posButtonTextId = posButtonTextId;
fragment.action = action;
fragment.bug = bug;
fragment.point = point;
fragment.handleBugListener = handleBugListener;
fragment.show(fm, TAG);
}
} catch (RuntimeException e) {

View file

@ -22,9 +22,11 @@ import net.osmand.plus.osmedit.OsmPoint;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.Algorithms;
import static net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.*;
public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragment {
public static final String TAG = "SendPoiBottomSheetFragment";
public static final String TAG = SendOsmNoteBottomSheetFragment.class.getSimpleName();
public static final String OPENSTREETMAP_POINT = "openstreetmap_point";
public static final String POI_UPLOADER_TYPE = "poi_uploader_type";
private OsmPoint[] poi;
@ -107,12 +109,11 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen
final SwitchCompat uploadAnonymously = (SwitchCompat) view.findViewById(R.id.upload_anonymously_switch);
final EditText messageEditText = (EditText) view.findViewById(R.id.message_field);
final SendPoiDialogFragment.PoiUploaderType poiUploaderType = SendPoiDialogFragment.PoiUploaderType.valueOf(getArguments().getString(POI_UPLOADER_TYPE, SendPoiDialogFragment.PoiUploaderType.SIMPLE.name()));
final SendPoiDialogFragment.ProgressDialogPoiUploader progressDialogPoiUploader;
final ProgressDialogPoiUploader progressDialogPoiUploader;
if (poiUploaderType == SendPoiDialogFragment.PoiUploaderType.SIMPLE && getActivity() instanceof MapActivity) {
progressDialogPoiUploader =
new SendPoiDialogFragment.SimpleProgressDialogPoiUploader((MapActivity) getActivity());
progressDialogPoiUploader = new SimpleProgressDialogPoiUploader((MapActivity) getActivity());
} else {
progressDialogPoiUploader = (SendPoiDialogFragment.ProgressDialogPoiUploader) getParentFragment();
progressDialogPoiUploader = (ProgressDialogPoiUploader) getParentFragment();
}
if (progressDialogPoiUploader != null) {
String comment = messageEditText.getText().toString();