logout added

This commit is contained in:
simon 2020-10-02 16:00:03 +03:00
parent 689ebf5b75
commit 33f933e89d
4 changed files with 61 additions and 26 deletions

View file

@ -53,9 +53,13 @@ public class OsmOAuthAuthorizationClient {
return service;
}
public void setAccessToken(OAuth1AccessToken accessToken) { this.accessToken = accessToken; }
public void setAccessToken(OAuth1AccessToken accessToken) {
this.accessToken = accessToken;
}
public OAuth1AccessToken getAccessToken() { return this.accessToken; }
public OAuth1AccessToken getAccessToken() {
return this.accessToken;
}
public Response performRequestWithoutAuth(String url, String requestMethod, String requestBody)
throws InterruptedException, ExecutionException, IOException {
@ -88,20 +92,6 @@ public class OsmOAuthAuthorizationClient {
return service.execute(req);
}
private Verb parseRequestMethod(String method) {
Verb m = Verb.GET;
if (method.equals("POST")) {
m = Verb.POST;
}
if (method.equals("PUT")) {
m = Verb.PUT;
}
if (method.equals("DELETE")) {
m = Verb.DELETE;
}
return m;
}
public OAuth1RequestToken startOAuth() {
try {
requestToken = service.getRequestToken();
@ -131,4 +121,18 @@ public class OsmOAuthAuthorizationClient {
public boolean isValidToken() {
return !(accessToken == null);
}
private Verb parseRequestMethod(String method) {
Verb m = Verb.GET;
if (method.equals("POST")) {
m = Verb.POST;
}
if (method.equals("PUT")) {
m = Verb.PUT;
}
if (method.equals("DELETE")) {
m = Verb.DELETE;
}
return m;
}
}

View file

@ -11,6 +11,12 @@
Thx - Hardy
-->
<string name="osm_edit_logout_success">Logout successful</string>
<string name="clear_osm_token">Clear OpenStreetMap OAuth token</string>
<string name="test_user_request_description">Test user request</string>
<string name="test_user_request">Retrieve current user info</string>
<string name="perform_oauth_authorization">Log in via OAuth</string>
<string name="perform_oauth_authorization_description">Perform an OAuth Login to use osmedit features</string>
<string name="sort_name_ascending">Name: A Z</string>
<string name="sort_name_descending">Name: Z A</string>
<string name="sort_last_modified">Last modified</string>

View file

@ -81,8 +81,8 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
prefOAuth.setKey("local_openstreetmap_token");
final Preference prefTestUser = new Preference(this);
prefTestUser.setTitle("Test user request");
prefTestUser.setSummary("Test user request");
prefTestUser.setTitle(R.string.test_user_request);
prefTestUser.setSummary(R.string.test_user_request_description);
prefTestUser.setKey("local_openstreetmap_token");
prefTestUser.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
@ -92,8 +92,7 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
@Override
public void onCompleted(Response response) {
try {
Toast.makeText(SettingsOsmEditingActivity.this,
"DATA RETRIEVED: " + response.getBody().toString(),Toast.LENGTH_SHORT).show();
Toast.makeText(SettingsOsmEditingActivity.this,response.getBody(),Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
@ -108,11 +107,27 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
return true;
}
});
grp.addPreference(prefTestUser);
final Preference prefClearToken = new Preference(this);
prefClearToken.setTitle(R.string.shared_string_logoff);
prefClearToken.setSummary(R.string.clear_osm_token);
prefClearToken.setKey("local_openstreetmap_token");
prefClearToken.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
settings.USER_ACCESS_TOKEN.set("");
settings.USER_ACCESS_TOKEN_SECRET.set("");
client.resetToken();
Toast.makeText(SettingsOsmEditingActivity.this, R.string.osm_edit_logout_success, Toast.LENGTH_SHORT).show();
finish();
startActivity(getIntent());
return true;
}
});
grp.addPreference(prefClearToken);
}
else {
prefOAuth.setTitle(R.string.osb_author_dialog_password);
prefOAuth.setSummary(R.string.osb_author_dialog_password);
prefOAuth.setTitle(R.string.perform_oauth_authorization);
prefOAuth.setSummary(R.string.perform_oauth_authorization_description);
prefOAuth.setKey("local_openstreetmap_token");
prefOAuth.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
@ -193,6 +208,8 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
if (uri != null && uri.toString().startsWith("osmand-oauth")) {
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
client.authorize(oauthVerifier);
finish();
startActivity(getIntent());
}
}
}

View file

@ -34,10 +34,18 @@ public class OsmOAuthAuthorizationAdapter {
return client.isValidToken();
}
public void resetToken() {
client.setAccessToken(null);
}
public void restoreToken() {
String token = application.getSettings().USER_ACCESS_TOKEN.get();
String tokenSecret = application.getSettings().USER_ACCESS_TOKEN_SECRET.get();
if (!(token.isEmpty() || tokenSecret.isEmpty())) {
client.setAccessToken(new OAuth1AccessToken(token, tokenSecret));
} else {
client.setAccessToken(null);
}
}
public void startOAuth(ViewGroup rootLayout) {