From 77a7b03144743b1468803e134d7bc5300604e060 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Mon, 16 Nov 2020 16:55:06 +0200 Subject: [PATCH 1/3] Fix crash on taping "Upload" in My places. --- .../SendOsmNoteBottomSheetFragment.java | 15 ++++++++-- .../dialogs/SendPoiBottomSheetFragment.java | 28 ++++++++++++------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java index 407e8c0b5f..8cd61909e4 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.osmedit.dialogs; +import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.ContextThemeWrapper; @@ -24,6 +25,7 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.osmedit.OsmNotesPoint; import net.osmand.plus.osmedit.OsmPoint; import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationAdapter; @@ -171,9 +173,16 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen @Override protected void onRightBottomButtonClick() { - ProgressDialogPoiUploader progressDialogPoiUploader; - progressDialogPoiUploader = new SimpleProgressDialogPoiUploader((MapActivity) getActivity()); - progressDialogPoiUploader.showProgressDialog(poi, false, uploadAnonymously.isChecked()); + ProgressDialogPoiUploader progressDialogPoiUploader = null; + Activity activity = getActivity(); + if (activity instanceof MapActivity) { + progressDialogPoiUploader = new SimpleProgressDialogPoiUploader((MapActivity) activity); + } else if (getParentFragment() instanceof ProgressDialogFragment) { + progressDialogPoiUploader = (ProgressDialogPoiUploader) getParentFragment(); + } + if (progressDialogPoiUploader != null) { + progressDialogPoiUploader.showProgressDialog(poi, false, uploadAnonymously.isChecked()); + } dismiss(); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java index 7755568e43..dd862d0608 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.osmedit.dialogs; +import android.app.Activity; import android.os.Bundle; import android.view.ContextThemeWrapper; import android.view.View; @@ -20,6 +21,7 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; +import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.osmedit.OpenstreetmapPoint; import net.osmand.plus.osmedit.OsmPoint; import net.osmand.plus.settings.backend.OsmandSettings; @@ -109,19 +111,25 @@ public class SendPoiBottomSheetFragment extends MenuBottomSheetDialogFragment { @Override protected void onRightBottomButtonClick() { - final ProgressDialogPoiUploader progressDialogPoiUploader; - progressDialogPoiUploader = new SimpleProgressDialogPoiUploader((MapActivity) getActivity()); - - String comment = messageEditText.getText().toString(); - if (comment.length() > 0) { - for (OsmPoint osmPoint : poi) { - if (osmPoint.getGroup() == OsmPoint.Group.POI) { - ((OpenstreetmapPoint) osmPoint).setComment(comment); - break; + ProgressDialogPoiUploader progressDialogPoiUploader = null; + Activity activity = getActivity(); + if (activity instanceof MapActivity) { + progressDialogPoiUploader = new SimpleProgressDialogPoiUploader((MapActivity) activity); + } else if (getParentFragment() instanceof ProgressDialogFragment) { + progressDialogPoiUploader = (ProgressDialogPoiUploader) getParentFragment(); + } + if (progressDialogPoiUploader != null) { + String comment = messageEditText.getText().toString(); + if (comment.length() > 0) { + for (OsmPoint osmPoint : poi) { + if (osmPoint.getGroup() == OsmPoint.Group.POI) { + ((OpenstreetmapPoint) osmPoint).setComment(comment); + break; + } } } + progressDialogPoiUploader.showProgressDialog(poi, closeChangeSet.isChecked(), false); } - progressDialogPoiUploader.showProgressDialog(poi, closeChangeSet.isChecked(), false); dismiss(); } From 49c0e998bf69134eecfd81ef3f7513bea86f4864 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Mon, 16 Nov 2020 23:56:35 +0200 Subject: [PATCH 2/3] Fix crash: landscape tap on "Sign in..." --- .../net/osmand/plus/helpers/IntentHelper.java | 20 +++- .../oauth/OsmOAuthAuthorizationAdapter.java | 109 ++++++++++++++---- .../plus/osmedit/oauth/OsmOAuthHelper.java | 33 +----- 3 files changed, 104 insertions(+), 58 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java b/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java index 5fa07a91e3..25cc6dcc30 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java @@ -297,12 +297,7 @@ public class IntentHelper { Uri uri = intent.getData(); if (uri.toString().startsWith("osmand-oauth")) { String oauthVerifier = uri.getQueryParameter("oauth_verifier"); - app.getOsmOAuthHelper().authorize(oauthVerifier); - for (Fragment fragment : mapActivity.getSupportFragmentManager().getFragments()) { - if (fragment instanceof OsmAuthorizationListener) { - ((OsmAuthorizationListener) fragment).authorizationCompleted(); - } - } + app.getOsmOAuthHelper().authorize(oauthVerifier, getOnAuthorizeListener()); mapActivity.setIntent(null); return true; } @@ -310,6 +305,19 @@ public class IntentHelper { return false; } + private OsmAuthorizationListener getOnAuthorizeListener() { + return new OsmAuthorizationListener() { + @Override + public void authorizationCompleted() { + for (Fragment fragment : mapActivity.getSupportFragmentManager().getFragments()) { + if (fragment instanceof OsmAuthorizationListener) { + ((OsmAuthorizationListener) fragment).authorizationCompleted(); + } + } + } + }; + } + private boolean handleSendText(Intent intent) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); if (!Algorithms.isEmpty(sharedText)) { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java index 7c2562b580..726a1e1b37 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthAuthorizationAdapter.java @@ -1,10 +1,13 @@ package net.osmand.plus.osmedit.oauth; import android.net.TrafficStats; +import android.os.AsyncTask; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; +import androidx.annotation.NonNull; + import com.github.scribejava.core.model.OAuth1AccessToken; import com.github.scribejava.core.model.OAuth1RequestToken; import com.github.scribejava.core.model.OAuthAsyncRequestCallback; @@ -16,18 +19,22 @@ import net.osmand.osm.oauth.OsmOAuthAuthorizationClient; import net.osmand.plus.BuildConfig; import net.osmand.plus.OsmandApplication; +import org.apache.commons.logging.Log; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.concurrent.ExecutionException; +import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.*; + public class OsmOAuthAuthorizationAdapter { private static final int THREAD_ID = 10101; private static final String OSM_USER = "user"; private static final String DISPLAY_NAME = "display_name"; private static final String OSM_USER_DETAILS_URL = "https://api.openstreetmap.org/api/0.6/user/details"; + public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationAdapter.class); private OsmandApplication app; private OsmOAuthAuthorizationClient client = @@ -61,9 +68,8 @@ public class OsmOAuthAuthorizationAdapter { } } - public void startOAuth(ViewGroup rootLayout) { - OAuth1RequestToken requestToken = client.startOAuth(); - loadWebView(rootLayout, client.getService().getAuthorizationUrl(requestToken)); + public void startOAuth(final ViewGroup rootLayout) { + new StartOAuthAsyncTask(rootLayout).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); } private void saveToken() { @@ -93,30 +99,91 @@ public class OsmOAuthAuthorizationAdapter { return client.performRequestWithoutAuth(url, method, body); } - public void authorize(String oauthVerifier) { - client.authorize(oauthVerifier); - saveToken(); + public void authorize(String oauthVerifier, final OsmOAuthHelper helper, final OsmAuthorizationListener listener) { + new AuthorizeAsyncTask(helper, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, oauthVerifier); } - public String getUserName() throws InterruptedException, ExecutionException, IOException, XmlPullParserException { - Response response = getOsmUserDetails(); - return parseUserName(response); + private class StartOAuthAsyncTask extends AsyncTask { + + private final ViewGroup rootLayout; + + public StartOAuthAsyncTask(ViewGroup rootLayout) { + this.rootLayout = rootLayout; + } + + @Override + protected OAuth1RequestToken doInBackground(Void... params) { + return client.startOAuth(); + } + + @Override + protected void onPostExecute(@NonNull OAuth1RequestToken requestToken) { + loadWebView(rootLayout, client.getService().getAuthorizationUrl(requestToken)); + } + } - public Response getOsmUserDetails() throws InterruptedException, ExecutionException, IOException { - return performRequest(OSM_USER_DETAILS_URL, Verb.GET.name(), null); - } + private class AuthorizeAsyncTask extends AsyncTask { - public String parseUserName(Response response) throws XmlPullParserException, IOException { - String userName = null; - XmlPullParser parser = PlatformUtil.newXMLPullParser(); - parser.setInput(response.getStream(), "UTF-8"); - int tok; - while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) { - if (tok == XmlPullParser.START_TAG && OSM_USER.equals(parser.getName())) { - userName = parser.getAttributeValue("", DISPLAY_NAME); + private final OsmOAuthHelper helper; + private final OsmAuthorizationListener listener; + + public AuthorizeAsyncTask(OsmOAuthHelper helper, OsmAuthorizationListener listener) { + this.helper = helper; + this.listener = listener; + } + + @Override + protected Void doInBackground(String... oauthVerifier) { + client.authorize(oauthVerifier[0]); + saveToken(); + updateUserName(); + return null; + } + + @Override + protected void onPostExecute(Void result) { + if (listener != null) { + listener.authorizationCompleted(); } } - return userName; + + public void updateUserName() { + String userName = ""; + try { + userName = getUserName(); + } catch (InterruptedException e) { + log.error(e); + } catch (ExecutionException e) { + log.error(e); + } catch (IOException e) { + log.error(e); + } catch (XmlPullParserException e) { + log.error(e); + } + app.getSettings().USER_DISPLAY_NAME.set(userName); + } + + public String getUserName() throws InterruptedException, ExecutionException, IOException, XmlPullParserException { + Response response = getOsmUserDetails(); + return parseUserName(response); + } + + public Response getOsmUserDetails() throws InterruptedException, ExecutionException, IOException { + return performRequest(OSM_USER_DETAILS_URL, Verb.GET.name(), null); + } + + public String parseUserName(Response response) throws XmlPullParserException, IOException { + String userName = null; + XmlPullParser parser = PlatformUtil.newXMLPullParser(); + parser.setInput(response.getStream(), "UTF-8"); + int tok; + while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) { + if (tok == XmlPullParser.START_TAG && OSM_USER.equals(parser.getName())) { + userName = parser.getAttributeValue("", DISPLAY_NAME); + } + } + return userName; + } } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java index d13bf624f2..a9ff75c6b5 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java @@ -4,25 +4,13 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; -import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; -import org.apache.commons.logging.Log; -import org.xmlpull.v1.XmlPullParserException; - -import java.io.IOException; -import java.util.concurrent.ExecutionException; - public class OsmOAuthHelper { - private static final Log log = PlatformUtil.getLog(OsmOAuthHelper.class); - - private final OsmandApplication app; - private final OsmOAuthAuthorizationAdapter authorizationAdapter; public OsmOAuthHelper(@NonNull OsmandApplication app) { - this.app = app; authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); } @@ -30,31 +18,14 @@ public class OsmOAuthHelper { authorizationAdapter.startOAuth(view); } - public void authorize(String oauthVerifier) { - authorizationAdapter.authorize(oauthVerifier); - updateUserName(); + public void authorize(String oauthVerifier, OsmAuthorizationListener listener) { + authorizationAdapter.authorize(oauthVerifier, this, listener); } public OsmOAuthAuthorizationAdapter getAuthorizationAdapter() { return authorizationAdapter; } - private void updateUserName() { - String userName = ""; - try { - userName = authorizationAdapter.getUserName(); - } catch (InterruptedException e) { - log.error(e); - } catch (ExecutionException e) { - log.error(e); - } catch (IOException e) { - log.error(e); - } catch (XmlPullParserException e) { - log.error(e); - } - app.getSettings().USER_DISPLAY_NAME.set(userName); - } - public interface OsmAuthorizationListener { void authorizationCompleted(); } From fe375c91f1e759f88da49a0cc495d9bcf8298689 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Wed, 18 Nov 2020 12:08:15 +0200 Subject: [PATCH 3/3] Fix progress dialog from my places --- OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java | 5 ++--- .../plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java | 3 +-- .../plus/osmedit/dialogs/SendPoiBottomSheetFragment.java | 3 +-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 7aff0a604f..dff223e4d7 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -62,7 +62,6 @@ import net.osmand.plus.osmedit.FileTypeBottomSheetDialogFragment.FileTypeFragmen import net.osmand.plus.osmedit.OsmEditOptionsBottomSheetDialogFragment.OsmEditOptionsFragmentListener; import net.osmand.plus.osmedit.OsmPoint.Group; import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment; -import net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.PoiUploaderType; import net.osmand.util.Algorithms; import org.xmlpull.v1.XmlSerializer; @@ -638,12 +637,12 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo && !Algorithms.isEmpty(settings.USER_PASSWORD.get()); if (hasPoiGroup(points)) { if (isLogged) { - SendPoiBottomSheetFragment.showInstance(activity.getSupportFragmentManager(), points); + SendPoiBottomSheetFragment.showInstance(getChildFragmentManager(), points); } else { LoginBottomSheetFragment.showInstance(activity.getSupportFragmentManager(), this); } } else { - SendOsmNoteBottomSheetFragment.showInstance(activity.getSupportFragmentManager(), points); + SendOsmNoteBottomSheetFragment.showInstance(getChildFragmentManager(), points); } } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java index 8cd61909e4..c488ad9149 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java @@ -25,7 +25,6 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; -import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.osmedit.OsmNotesPoint; import net.osmand.plus.osmedit.OsmPoint; import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationAdapter; @@ -177,7 +176,7 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen Activity activity = getActivity(); if (activity instanceof MapActivity) { progressDialogPoiUploader = new SimpleProgressDialogPoiUploader((MapActivity) activity); - } else if (getParentFragment() instanceof ProgressDialogFragment) { + } else if (getParentFragment() instanceof ProgressDialogPoiUploader) { progressDialogPoiUploader = (ProgressDialogPoiUploader) getParentFragment(); } if (progressDialogPoiUploader != null) { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java index dd862d0608..676a1063ff 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java @@ -21,7 +21,6 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; -import net.osmand.plus.dialogs.ProgressDialogFragment; import net.osmand.plus.osmedit.OpenstreetmapPoint; import net.osmand.plus.osmedit.OsmPoint; import net.osmand.plus.settings.backend.OsmandSettings; @@ -115,7 +114,7 @@ public class SendPoiBottomSheetFragment extends MenuBottomSheetDialogFragment { Activity activity = getActivity(); if (activity instanceof MapActivity) { progressDialogPoiUploader = new SimpleProgressDialogPoiUploader((MapActivity) activity); - } else if (getParentFragment() instanceof ProgressDialogFragment) { + } else if (getParentFragment() instanceof ProgressDialogPoiUploader) { progressDialogPoiUploader = (ProgressDialogPoiUploader) getParentFragment(); } if (progressDialogPoiUploader != null) {