Fix my places send gpx OAuth

This commit is contained in:
androiddevkkotlin 2020-11-18 17:14:42 +02:00
parent 99ad1f2aeb
commit 9405c7c898
8 changed files with 86 additions and 32 deletions

View file

@ -297,7 +297,8 @@ public class IntentHelper {
Uri uri = intent.getData(); Uri uri = intent.getData();
if (uri.toString().startsWith("osmand-oauth")) { if (uri.toString().startsWith("osmand-oauth")) {
String oauthVerifier = uri.getQueryParameter("oauth_verifier"); String oauthVerifier = uri.getQueryParameter("oauth_verifier");
app.getOsmOAuthHelper().authorize(oauthVerifier, getOnAuthorizeListener()); app.getOsmOAuthHelper().addListener(getOnAuthorizeListener());
app.getOsmOAuthHelper().authorize(oauthVerifier);
mapActivity.setIntent(null); mapActivity.setIntent(null);
return true; return true;
} }

View file

@ -24,7 +24,7 @@ import net.osmand.plus.settings.bottomsheets.OsmLoginDataBottomSheet;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA; import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA;
import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.*; import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener;
public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment implements OsmAuthorizationListener { public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment implements OsmAuthorizationListener {
@ -89,6 +89,10 @@ public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment impl
protected void onThirdBottomButtonClick() { protected void onThirdBottomButtonClick() {
View view = getView(); View view = getView();
if (view != null) { if (view != null) {
Fragment fragment = getTargetFragment();
if (fragment instanceof OsmAuthorizationListener) {
osmOAuthHelper.addListener((OsmAuthorizationListener) fragment);
}
osmOAuthHelper.startOAuth((ViewGroup) view); osmOAuthHelper.startOAuth((ViewGroup) view);
} }
} }

View file

@ -80,11 +80,12 @@ import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.OsmandExpandableListFragment; import net.osmand.plus.base.OsmandExpandableListFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType;
import net.osmand.plus.helpers.enums.TracksSortByMode;
import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment; import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.plus.osmedit.OsmEditingPlugin;
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.helpers.enums.TracksSortByMode;
import java.io.File; import java.io.File;
import java.text.DateFormat; import java.text.DateFormat;
@ -112,7 +113,7 @@ import static net.osmand.util.Algorithms.objectEquals;
import static net.osmand.util.Algorithms.removeAllFiles; import static net.osmand.util.Algorithms.removeAllFiles;
public class AvailableGPXFragment extends OsmandExpandableListFragment implements public class AvailableGPXFragment extends OsmandExpandableListFragment implements
FavoritesFragmentStateHolder { FavoritesFragmentStateHolder, OsmAuthorizationListener {
public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]");
public static final int SEARCH_ID = -1; public static final int SEARCH_ID = -1;
@ -922,6 +923,18 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
public void restoreState(Bundle bundle) { public void restoreState(Bundle bundle) {
} }
@Override
public void authorizationCompleted() {
Bundle bundle = new Bundle();
bundle.putInt(TAB_ID, GPX_TAB);
Intent intent = new Intent(app, app.getAppCustomization().getFavoritesActivity());
intent.putExtra(MapActivity.INTENT_PARAMS, bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
app.startActivity(intent);
}
public class LoadGpxTask extends AsyncTask<Activity, GpxInfo, List<GpxInfo>> { public class LoadGpxTask extends AsyncTask<Activity, GpxInfo, List<GpxInfo>> {
private List<GpxInfo> result; private List<GpxInfo> result;

View file

@ -461,10 +461,10 @@ public class OsmEditingPlugin extends OsmandPlugin {
String pwd = settings.USER_PASSWORD.get(); String pwd = settings.USER_PASSWORD.get();
String authToken = settings.USER_ACCESS_TOKEN.get(); String authToken = settings.USER_ACCESS_TOKEN.get();
if ((Algorithms.isEmpty(name) || Algorithms.isEmpty(pwd)) && Algorithms.isEmpty(authToken)) { if ((Algorithms.isEmpty(name) || Algorithms.isEmpty(pwd)) && Algorithms.isEmpty(authToken)) {
LoginBottomSheetFragment.showInstance(activity.getSupportFragmentManager(), fragment.getTargetFragment()); LoginBottomSheetFragment.showInstance(activity.getSupportFragmentManager(), fragment);
return false; return false;
} else { } else {
SendGpxBottomSheetFragment.showInstance(activity.getSupportFragmentManager(), fragment.getTargetFragment(), info); SendGpxBottomSheetFragment.showInstance(activity.getSupportFragmentManager(), fragment, info);
return true; return true;
} }
} }

View file

@ -42,11 +42,6 @@ import net.osmand.osm.edit.Entity;
import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Node;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.measurementtool.LoginBottomSheetFragment;
import net.osmand.plus.osmedit.dialogs.SendOsmNoteBottomSheetFragment;
import net.osmand.plus.osmedit.dialogs.SendPoiBottomSheetFragment;
import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationAdapter;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.activities.ActionBarProgressActivity; import net.osmand.plus.activities.ActionBarProgressActivity;
@ -55,13 +50,20 @@ import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.base.OsmAndListFragment;
import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.dialogs.ProgressDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.measurementtool.LoginBottomSheetFragment;
import net.osmand.plus.myplaces.FavoritesActivity; import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.myplaces.FavoritesFragmentStateHolder; import net.osmand.plus.myplaces.FavoritesFragmentStateHolder;
import net.osmand.plus.osmedit.ExportOptionsBottomSheetDialogFragment.ExportOptionsFragmentListener; import net.osmand.plus.osmedit.ExportOptionsBottomSheetDialogFragment.ExportOptionsFragmentListener;
import net.osmand.plus.osmedit.FileTypeBottomSheetDialogFragment.FileTypeFragmentListener; import net.osmand.plus.osmedit.FileTypeBottomSheetDialogFragment.FileTypeFragmentListener;
import net.osmand.plus.osmedit.OpenstreetmapLocalUtil.OnNodeCommittedListener;
import net.osmand.plus.osmedit.OsmEditOptionsBottomSheetDialogFragment.OsmEditOptionsFragmentListener; import net.osmand.plus.osmedit.OsmEditOptionsBottomSheetDialogFragment.OsmEditOptionsFragmentListener;
import net.osmand.plus.osmedit.OsmPoint.Group; import net.osmand.plus.osmedit.OsmPoint.Group;
import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment; import net.osmand.plus.osmedit.dialogs.SendOsmNoteBottomSheetFragment;
import net.osmand.plus.osmedit.dialogs.SendPoiBottomSheetFragment;
import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.ProgressDialogPoiUploader;
import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationAdapter;
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlSerializer;
@ -79,8 +81,8 @@ import java.util.Map;
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
import static net.osmand.plus.osmedit.OsmEditingPlugin.OSM_EDIT_TAB; import static net.osmand.plus.osmedit.OsmEditingPlugin.OSM_EDIT_TAB;
public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialogFragment.ProgressDialogPoiUploader, OpenstreetmapLocalUtil.OnNodeCommittedListener, public class OsmEditsFragment extends OsmAndListFragment implements ProgressDialogPoiUploader,
FavoritesFragmentStateHolder { OnNodeCommittedListener, FavoritesFragmentStateHolder, OsmAuthorizationListener {
public static final int EXPORT_TYPE_ALL = 0; public static final int EXPORT_TYPE_ALL = 0;
public static final int EXPORT_TYPE_POI = 1; public static final int EXPORT_TYPE_POI = 1;
@ -104,6 +106,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
private final static int MODE_DELETE = 100; private final static int MODE_DELETE = 100;
private final static int MODE_UPLOAD = 101; private final static int MODE_UPLOAD = 101;
private OsmandApplication app;
private OsmEditingPlugin plugin; private OsmEditingPlugin plugin;
private View footerView; private View footerView;
@ -144,6 +147,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
app = getMyApplication();
if (savedInstanceState != null) { if (savedInstanceState != null) {
exportType = savedInstanceState.getInt(EXPORT_TYPE_KEY); exportType = savedInstanceState.getInt(EXPORT_TYPE_KEY);
} }
@ -647,6 +651,18 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
} }
} }
@Override
public void authorizationCompleted() {
Bundle bundle = new Bundle();
bundle.putInt(TAB_ID, OSM_EDIT_TAB);
Intent intent = new Intent(app, app.getAppCustomization().getFavoritesActivity());
intent.putExtra(MapActivity.INTENT_PARAMS, bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
app.startActivity(intent);
}
boolean hasPoiGroup(OsmPoint[] points) { boolean hasPoiGroup(OsmPoint[] points) {
boolean hasPoiGroup = false; boolean hasPoiGroup = false;
for (OsmPoint p : points) { for (OsmPoint p : points) {

View file

@ -13,6 +13,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat; import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
@ -38,8 +39,10 @@ import org.apache.commons.logging.Log;
import static net.osmand.plus.UiUtilities.setupDialogButton; import static net.osmand.plus.UiUtilities.setupDialogButton;
import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA; import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA;
import static net.osmand.plus.osmedit.ValidateOsmLoginDetailsTask.*; import static net.osmand.plus.osmedit.ValidateOsmLoginDetailsTask.ValidateOsmLoginListener;
import static net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.*; import static net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.OPENSTREETMAP_POINT;
import static net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.ProgressDialogPoiUploader;
import static net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.SimpleProgressDialogPoiUploader;
public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragment implements ValidateOsmLoginListener, public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragment implements ValidateOsmLoginListener,
OsmAuthorizationListener { OsmAuthorizationListener {
@ -89,7 +92,10 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen
signInButton.setOnClickListener(new View.OnClickListener() { signInButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
OsmandApplication app = requiredMyApplication(); Fragment fragment = getParentFragment();
if (fragment instanceof OsmAuthorizationListener) {
app.getOsmOAuthHelper().addListener((OsmAuthorizationListener) fragment);
}
app.getOsmOAuthHelper().startOAuth((ViewGroup) v); app.getOsmOAuthHelper().startOAuth((ViewGroup) v);
} }
}); });

View file

@ -26,8 +26,6 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.*;
public class OsmOAuthAuthorizationAdapter { public class OsmOAuthAuthorizationAdapter {
private static final int THREAD_ID = 10101; private static final int THREAD_ID = 10101;
@ -99,8 +97,8 @@ public class OsmOAuthAuthorizationAdapter {
return client.performRequestWithoutAuth(url, method, body); return client.performRequestWithoutAuth(url, method, body);
} }
public void authorize(String oauthVerifier, final OsmOAuthHelper helper, final OsmAuthorizationListener listener) { public void authorize(String oauthVerifier, OsmOAuthHelper helper) {
new AuthorizeAsyncTask(helper, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, oauthVerifier); new AuthorizeAsyncTask(helper).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, oauthVerifier);
} }
private class StartOAuthAsyncTask extends AsyncTask<Void, Void, OAuth1RequestToken> { private class StartOAuthAsyncTask extends AsyncTask<Void, Void, OAuth1RequestToken> {
@ -120,17 +118,14 @@ public class OsmOAuthAuthorizationAdapter {
protected void onPostExecute(@NonNull OAuth1RequestToken requestToken) { protected void onPostExecute(@NonNull OAuth1RequestToken requestToken) {
loadWebView(rootLayout, client.getService().getAuthorizationUrl(requestToken)); loadWebView(rootLayout, client.getService().getAuthorizationUrl(requestToken));
} }
} }
private class AuthorizeAsyncTask extends AsyncTask<String, Void, Void> { private class AuthorizeAsyncTask extends AsyncTask<String, Void, Void> {
private final OsmOAuthHelper helper; private final OsmOAuthHelper helper;
private final OsmAuthorizationListener listener;
public AuthorizeAsyncTask(OsmOAuthHelper helper, OsmAuthorizationListener listener) { public AuthorizeAsyncTask(OsmOAuthHelper helper) {
this.helper = helper; this.helper = helper;
this.listener = listener;
} }
@Override @Override
@ -143,9 +138,7 @@ public class OsmOAuthAuthorizationAdapter {
@Override @Override
protected void onPostExecute(Void result) { protected void onPostExecute(Void result) {
if (listener != null) { helper.notifyAndRemoveListeners();
listener.authorizationCompleted();
}
} }
public void updateUserName() { public void updateUserName() {

View file

@ -6,26 +6,47 @@ import androidx.annotation.NonNull;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import java.util.HashSet;
import java.util.Set;
public class OsmOAuthHelper { public class OsmOAuthHelper {
private final OsmandApplication app;
private final OsmOAuthAuthorizationAdapter authorizationAdapter; private final OsmOAuthAuthorizationAdapter authorizationAdapter;
private final Set<OsmAuthorizationListener> listeners = new HashSet<>();
public OsmOAuthHelper(@NonNull OsmandApplication app) { public OsmOAuthHelper(@NonNull OsmandApplication app) {
this.app = app;
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
} }
public void startOAuth(ViewGroup view) { public void addListener(OsmAuthorizationListener listener) {
authorizationAdapter.startOAuth(view); listeners.add(listener);
} }
public void authorize(String oauthVerifier, OsmAuthorizationListener listener) { public void removeListener(OsmAuthorizationListener listener) {
authorizationAdapter.authorize(oauthVerifier, this, listener); listeners.remove(listener);
} }
public OsmOAuthAuthorizationAdapter getAuthorizationAdapter() { public OsmOAuthAuthorizationAdapter getAuthorizationAdapter() {
return authorizationAdapter; return authorizationAdapter;
} }
public void startOAuth(@NonNull ViewGroup view) {
authorizationAdapter.startOAuth(view);
}
public void authorize(@NonNull String oauthVerifier) {
authorizationAdapter.authorize(oauthVerifier, this);
}
public void notifyAndRemoveListeners() {
for (OsmAuthorizationListener listener : listeners) {
listener.authorizationCompleted();
}
listeners.clear();
}
public interface OsmAuthorizationListener { public interface OsmAuthorizationListener {
void authorizationCompleted(); void authorizationCompleted();
} }