Fix crash: landscape tap on "Sign in..."

This commit is contained in:
Dima-1 2020-11-16 23:56:35 +02:00
parent 77a7b03144
commit 49c0e998bf
3 changed files with 104 additions and 58 deletions

View file

@ -297,12 +297,7 @@ 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); app.getOsmOAuthHelper().authorize(oauthVerifier, getOnAuthorizeListener());
for (Fragment fragment : mapActivity.getSupportFragmentManager().getFragments()) {
if (fragment instanceof OsmAuthorizationListener) {
((OsmAuthorizationListener) fragment).authorizationCompleted();
}
}
mapActivity.setIntent(null); mapActivity.setIntent(null);
return true; return true;
} }
@ -310,6 +305,19 @@ public class IntentHelper {
return false; 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) { private boolean handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (!Algorithms.isEmpty(sharedText)) { if (!Algorithms.isEmpty(sharedText)) {

View file

@ -1,10 +1,13 @@
package net.osmand.plus.osmedit.oauth; package net.osmand.plus.osmedit.oauth;
import android.net.TrafficStats; import android.net.TrafficStats;
import android.os.AsyncTask;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.WebView; import android.webkit.WebView;
import androidx.annotation.NonNull;
import com.github.scribejava.core.model.OAuth1AccessToken; import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken; import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.model.OAuthAsyncRequestCallback; 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.BuildConfig;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; 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;
private static final String OSM_USER = "user"; private static final String OSM_USER = "user";
private static final String DISPLAY_NAME = "display_name"; 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"; 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 OsmandApplication app;
private OsmOAuthAuthorizationClient client = private OsmOAuthAuthorizationClient client =
@ -61,9 +68,8 @@ public class OsmOAuthAuthorizationAdapter {
} }
} }
public void startOAuth(ViewGroup rootLayout) { public void startOAuth(final ViewGroup rootLayout) {
OAuth1RequestToken requestToken = client.startOAuth(); new StartOAuthAsyncTask(rootLayout).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
loadWebView(rootLayout, client.getService().getAuthorizationUrl(requestToken));
} }
private void saveToken() { private void saveToken() {
@ -93,30 +99,91 @@ public class OsmOAuthAuthorizationAdapter {
return client.performRequestWithoutAuth(url, method, body); return client.performRequestWithoutAuth(url, method, body);
} }
public void authorize(String oauthVerifier) { public void authorize(String oauthVerifier, final OsmOAuthHelper helper, final OsmAuthorizationListener listener) {
client.authorize(oauthVerifier); new AuthorizeAsyncTask(helper, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, oauthVerifier);
saveToken();
} }
public String getUserName() throws InterruptedException, ExecutionException, IOException, XmlPullParserException { private class StartOAuthAsyncTask extends AsyncTask<Void, Void, OAuth1RequestToken> {
Response response = getOsmUserDetails();
return parseUserName(response); 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 { private class AuthorizeAsyncTask extends AsyncTask<String, Void, Void> {
return performRequest(OSM_USER_DETAILS_URL, Verb.GET.name(), null);
}
public String parseUserName(Response response) throws XmlPullParserException, IOException { private final OsmOAuthHelper helper;
String userName = null; private final OsmAuthorizationListener listener;
XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(response.getStream(), "UTF-8"); public AuthorizeAsyncTask(OsmOAuthHelper helper, OsmAuthorizationListener listener) {
int tok; this.helper = helper;
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) { this.listener = listener;
if (tok == XmlPullParser.START_TAG && OSM_USER.equals(parser.getName())) { }
userName = parser.getAttributeValue("", DISPLAY_NAME);
@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;
}
} }
} }

View file

@ -4,25 +4,13 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; 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 { public class OsmOAuthHelper {
private static final Log log = PlatformUtil.getLog(OsmOAuthHelper.class);
private final OsmandApplication app;
private final OsmOAuthAuthorizationAdapter authorizationAdapter; private final OsmOAuthAuthorizationAdapter authorizationAdapter;
public OsmOAuthHelper(@NonNull OsmandApplication app) { public OsmOAuthHelper(@NonNull OsmandApplication app) {
this.app = app;
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
} }
@ -30,31 +18,14 @@ public class OsmOAuthHelper {
authorizationAdapter.startOAuth(view); authorizationAdapter.startOAuth(view);
} }
public void authorize(String oauthVerifier) { public void authorize(String oauthVerifier, OsmAuthorizationListener listener) {
authorizationAdapter.authorize(oauthVerifier); authorizationAdapter.authorize(oauthVerifier, this, listener);
updateUserName();
} }
public OsmOAuthAuthorizationAdapter getAuthorizationAdapter() { public OsmOAuthAuthorizationAdapter getAuthorizationAdapter() {
return authorizationAdapter; 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 { public interface OsmAuthorizationListener {
void authorizationCompleted(); void authorizationCompleted();
} }