OAuth, refactor, fix icon
This commit is contained in:
parent
220cc0ed46
commit
e14989f464
11 changed files with 128 additions and 59 deletions
|
@ -9,8 +9,12 @@ import com.github.scribejava.core.model.*;
|
|||
import com.github.scribejava.core.oauth.OAuth10aService;
|
||||
import net.osmand.PlatformUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
|
@ -89,6 +93,39 @@ public class OsmOAuthAuthorizationClient {
|
|||
service.execute(req, callback);
|
||||
}
|
||||
|
||||
public String authorizeOsmUserDetails() throws InterruptedException, ExecutionException, IOException, XmlPullParserException {
|
||||
OAuth1RequestToken requestToken = startOAuth();
|
||||
if (requestToken == null) {
|
||||
throw new IllegalArgumentException("Illegal request token: ");
|
||||
}
|
||||
// save for reuse of request ( they usually don't match and should'nt be an issue)
|
||||
String url = "https://api.openstreetmap.org/api/0.6/user/details";
|
||||
OAuthRequest req = new OAuthRequest(Verb.GET, url);
|
||||
service.signRequest(accessToken, req);
|
||||
req.addHeader("Content-Type", "application/xml");
|
||||
Response response = service.execute(req);
|
||||
String user = "";
|
||||
String home = "";
|
||||
String lang = "";
|
||||
XmlPullParser parser = PlatformUtil.newXMLPullParser();
|
||||
parser.setInput(response.getStream(), "UTF-8");
|
||||
List<String> languages = new ArrayList<>();
|
||||
int tok;
|
||||
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
||||
if (tok == XmlPullParser.START_TAG) {
|
||||
String name = parser.getName();
|
||||
if ("user".equals(name)) {
|
||||
user = parser.getAttributeValue("", "display_name");
|
||||
} else if ("home".equals(name)) {
|
||||
} else if ("lang".equals(name)) {
|
||||
languages.add(parser.nextText());
|
||||
}
|
||||
}
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
public Response performRequest(String url, String method, String body)
|
||||
throws InterruptedException, ExecutionException, IOException {
|
||||
service.getApi().getSignatureType();
|
||||
|
@ -103,6 +140,8 @@ public class OsmOAuthAuthorizationClient {
|
|||
return service.execute(req);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public OAuth1RequestToken startOAuth() {
|
||||
try {
|
||||
requestToken = service.getRequestToken();
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
android:layout_height="@dimen/dialog_button_height"
|
||||
android:layout_weight="1">
|
||||
|
||||
<LinearLayout
|
||||
<FrameLayout
|
||||
android:id="@+id/button_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
|
@ -16,11 +16,12 @@
|
|||
|
||||
<net.osmand.plus.widgets.TextViewEx
|
||||
android:id="@+id/button_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingLeft="@dimen/content_padding_small"
|
||||
android:paddingRight="@dimen/content_padding_small"
|
||||
android:gravity="center"
|
||||
android:layout_gravity="center"
|
||||
android:textSize="@dimen/default_desc_text_size"
|
||||
osmand:typeface="@string/font_roboto_medium"
|
||||
android:duplicateParentState="true"
|
||||
|
@ -28,6 +29,6 @@
|
|||
android:paddingStart="@dimen/content_padding_small"
|
||||
android:paddingEnd="@dimen/content_padding_small" />
|
||||
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
||||
|
||||
</LinearLayout>
|
|
@ -17,7 +17,7 @@
|
|||
<string name="use_login_password">Use login and password</string>
|
||||
<string name="open_street_map_login_mode">You need to login to upload new or modified changes. \n\nYou can log in using the safe OAuth method or use your login and password.</string>
|
||||
<string name="osm_edits_view_descr">You can view all your unloaded edits or OSM bugs in %1$s. Uploaded points don’t show in OsmAnd.</string>
|
||||
<string name="sing_in_with_open_street_map">Sing in with OpenStreetMap</string>
|
||||
<string name="sing_in_with_open_street_map">Sign in with OpenStreetMap</string>
|
||||
<string name="login_open_street_map_org">Login to OpenStreetMap.org</string>
|
||||
<string name="login_open_street_map">Login to OpenStreetMap</string>
|
||||
<string name="plugin_global_prefs_info">These plugin setting are global, and apply to all profiles</string>
|
||||
|
|
|
@ -115,6 +115,7 @@ import net.osmand.plus.mapcontextmenu.other.TrackDetailsMenu;
|
|||
import net.osmand.plus.mapmarkers.PlanRouteFragment;
|
||||
import net.osmand.plus.measurementtool.GpxApproximationFragment;
|
||||
import net.osmand.plus.measurementtool.GpxData;
|
||||
import net.osmand.plus.measurementtool.LoginBottomSheetFragment;
|
||||
import net.osmand.plus.measurementtool.MeasurementEditingContext;
|
||||
import net.osmand.plus.measurementtool.MeasurementToolFragment;
|
||||
import net.osmand.plus.measurementtool.SnapTrackWarningFragment;
|
||||
|
@ -2217,8 +2218,8 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
return getFragment(GpxApproximationFragment.TAG);
|
||||
}
|
||||
|
||||
public OsmEditingFragment getOsmEditingFragment() {
|
||||
return getFragment(SettingsScreenType.OPEN_STREET_MAP_EDITING.fragmentName);
|
||||
public LoginBottomSheetFragment getLoginBottomSheetFragment() {
|
||||
return getFragment(LoginBottomSheetFragment.TAG);
|
||||
}
|
||||
|
||||
public SnapTrackWarningFragment getSnapTrackWarningBottomSheet() {
|
||||
|
|
|
@ -19,6 +19,7 @@ import net.osmand.plus.activities.PluginsFragment;
|
|||
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
|
||||
import net.osmand.plus.mapmarkers.MapMarkersDialogFragment;
|
||||
import net.osmand.plus.mapsource.EditMapSourceDialogFragment;
|
||||
import net.osmand.plus.measurementtool.LoginBottomSheetFragment;
|
||||
import net.osmand.plus.osmedit.OsmEditingFragment;
|
||||
import net.osmand.plus.search.QuickSearchDialogFragment;
|
||||
import net.osmand.plus.settings.backend.ApplicationMode;
|
||||
|
@ -293,7 +294,7 @@ public class IntentHelper {
|
|||
if (intent != null && intent.getData() != null) {
|
||||
Uri uri = intent.getData();
|
||||
if (uri.toString().startsWith("osmand-oauth")) {
|
||||
OsmEditingFragment fragment = mapActivity.getOsmEditingFragment();
|
||||
LoginBottomSheetFragment fragment = mapActivity.getLoginBottomSheetFragment();
|
||||
if (fragment != null) {
|
||||
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
|
||||
fragment.authorize(oauthVerifier);
|
||||
|
|
|
@ -29,11 +29,10 @@ public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment {
|
|||
private OsmOAuthAuthorizationAdapter client;
|
||||
private static final String OSM_LOGIN_DATA = "osm_login_data";
|
||||
|
||||
public static final String TAG = ExitBottomSheetDialogFragment.class.getSimpleName();
|
||||
public static final String TAG = LoginBottomSheetFragment.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
|
||||
items.add(new ShortDescriptionItem.Builder()
|
||||
.setDescription(getString(R.string.open_street_map_login_mode))
|
||||
.setTitle(getString(R.string.login_open_street_map_org))
|
||||
|
@ -50,7 +49,6 @@ public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment {
|
|||
return R.string.shared_string_cancel;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected int getRightBottomButtonTextId() {
|
||||
return R.string.use_login_password;
|
||||
|
@ -131,5 +129,21 @@ public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment {
|
|||
fragment.show(fragmentManager, TAG);
|
||||
}
|
||||
}
|
||||
|
||||
public interface LoginOsmAutorizationListener {
|
||||
void informAutorizationPrefsUpdate();
|
||||
}
|
||||
|
||||
public void authorize(String oauthVerifier) {
|
||||
if (client != null) {
|
||||
client.authorize(oauthVerifier);
|
||||
}
|
||||
Fragment target = getTargetFragment();
|
||||
if (target instanceof LoginOsmAutorizationListener) {
|
||||
((LoginOsmAutorizationListener) target).informAutorizationPrefsUpdate();
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import android.os.Bundle;
|
|||
import android.text.SpannableString;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
@ -33,7 +32,7 @@ import org.apache.commons.logging.Log;
|
|||
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
|
||||
import static net.osmand.plus.osmedit.OsmEditingPlugin.OSM_EDIT_TAB;
|
||||
|
||||
public class OsmEditingFragment extends BaseSettingsFragment implements OnPreferenceChanged {
|
||||
public class OsmEditingFragment extends BaseSettingsFragment implements OnPreferenceChanged, ValidateOsmLoginDetailsTask.ValidateOsmLoginListener, LoginBottomSheetFragment.LoginOsmAutorizationListener {
|
||||
|
||||
private static final Log log = PlatformUtil.getLog(OsmEditingFragment.class);
|
||||
|
||||
|
@ -41,11 +40,14 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
private static final String OPEN_OSM_EDITS = "open_osm_edits";
|
||||
private static final String OSM_LOGIN_DATA = "osm_login_data";
|
||||
private static final String OSM_LOGOUT = "osm_logout";
|
||||
private static final String OSM_OAUTH_CLEAR = "osm_oauth_clear";
|
||||
private static final String OSM_OAUTH_LOGIN = "osm_oauth_login";
|
||||
|
||||
private OsmOAuthAuthorizationAdapter client;
|
||||
|
||||
@Override
|
||||
public void loginValidationFinished(String error) {
|
||||
updateAllSettings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -57,8 +59,8 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
Preference osmEditingInfo = findPreference(OSM_EDITING_INFO);
|
||||
osmEditingInfo.setIcon(getContentIcon(R.drawable.ic_action_info_dark));
|
||||
|
||||
setupNameAndPasswordPref();
|
||||
setupExitPref();
|
||||
setupLoginPref();
|
||||
setupLogoutPref();
|
||||
|
||||
setupOfflineEditingPref();
|
||||
setupOsmEditsDescrPref();
|
||||
|
@ -83,7 +85,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
}
|
||||
}
|
||||
|
||||
private void setupNameAndPasswordPref() {
|
||||
private void setupLoginPref() {
|
||||
Preference nameAndPasswordPref = findPreference(OSM_LOGIN_DATA);
|
||||
nameAndPasswordPref.setTitle(R.string.login_open_street_map);
|
||||
nameAndPasswordPref.setIcon(getContentIcon(R.drawable.ic_action_user_account));
|
||||
|
@ -94,12 +96,17 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
nameAndPasswordPref.setVisible(visible);
|
||||
}
|
||||
|
||||
private void setupExitPref() {
|
||||
private void setupLogoutPref() {
|
||||
Preference nameAndPasswordPref = findPreference(OSM_LOGOUT);
|
||||
nameAndPasswordPref.setSummary(settings.USER_NAME.get());
|
||||
boolean validToken = client.isValidToken();
|
||||
if (validToken){
|
||||
nameAndPasswordPref.setSummary(settings.USER_DISPLAY_NAME.get());
|
||||
|
||||
} else {
|
||||
nameAndPasswordPref.setSummary(settings.USER_NAME.get());
|
||||
}
|
||||
nameAndPasswordPref.setIcon(getContentIcon(R.drawable.ic_action_user_account));
|
||||
|
||||
boolean validToken = client.isValidToken();
|
||||
boolean loginExists = !Algorithms.isEmpty(settings.USER_NAME.get()) && !Algorithms.isEmpty(settings.USER_PASSWORD.get());
|
||||
boolean visible = validToken || loginExists;
|
||||
nameAndPasswordPref.setVisible(visible);
|
||||
|
@ -171,23 +178,6 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
app.showShortToastMessage(R.string.osm_edit_logout_success);
|
||||
updateAllSettings();
|
||||
return true;
|
||||
} else if (OSM_OAUTH_CLEAR.equals(prefId)) {
|
||||
settings.USER_ACCESS_TOKEN.set("");
|
||||
settings.USER_ACCESS_TOKEN_SECRET.set("");
|
||||
|
||||
client.resetToken();
|
||||
client = new OsmOAuthAuthorizationAdapter(app);
|
||||
|
||||
app.showShortToastMessage(R.string.osm_edit_logout_success);
|
||||
updateAllSettings();
|
||||
return true;
|
||||
} else if (OSM_OAUTH_LOGIN.equals(prefId)) {
|
||||
View view = getView();
|
||||
if (view != null) {
|
||||
ViewGroup appBarLayout = view.findViewById(R.id.appbar);
|
||||
client.startOAuth(appBarLayout);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return super.onPreferenceClick(preference);
|
||||
}
|
||||
|
@ -197,17 +187,9 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
updateAllSettings();
|
||||
}
|
||||
|
||||
public void authorize(String oauthVerifier) {
|
||||
FragmentManager fragmentManager = getMapActivity().getSupportFragmentManager();
|
||||
LoginBottomSheetFragment fragment = (LoginBottomSheetFragment) fragmentManager.findFragmentByTag(LoginBottomSheetFragment.TAG);
|
||||
if (fragment != null) {
|
||||
OsmOAuthAuthorizationAdapter authorizationAdapter = fragment.getClient();
|
||||
if (authorizationAdapter != null) {
|
||||
authorizationAdapter.authorize(oauthVerifier);
|
||||
}
|
||||
fragment.dismiss();
|
||||
client = new OsmOAuthAuthorizationAdapter(app);
|
||||
}
|
||||
@Override
|
||||
public void informAutorizationPrefsUpdate() {
|
||||
client = new OsmOAuthAuthorizationAdapter(app);
|
||||
updateAllSettings();
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package net.osmand.plus.osmedit;
|
|||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import net.osmand.plus.GpxSelectionHelper;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandPlugin;
|
||||
import net.osmand.plus.R;
|
||||
|
@ -12,13 +13,21 @@ import java.lang.ref.WeakReference;
|
|||
public class ValidateOsmLoginDetailsTask extends AsyncTask<Void, Void, OsmBugResult> {
|
||||
|
||||
private OsmandApplication app;
|
||||
private final WeakReference<OsmEditingFragment> fragmentRef;
|
||||
private ValidateOsmLoginListener validateOsmLoginListener;
|
||||
|
||||
public interface ValidateOsmLoginListener {
|
||||
|
||||
void loginValidationFinished(String error);
|
||||
|
||||
public ValidateOsmLoginDetailsTask(OsmandApplication app, OsmEditingFragment targetfragment) {
|
||||
this.app = app;
|
||||
this.fragmentRef = new WeakReference<>(targetfragment);
|
||||
}
|
||||
|
||||
public ValidateOsmLoginDetailsTask(OsmandApplication app, ValidateOsmLoginListener validateTargetOsmLoginDetailsTask ) {
|
||||
this.app = app;
|
||||
this.validateOsmLoginListener = validateTargetOsmLoginDetailsTask;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected OsmBugResult doInBackground(Void... params) {
|
||||
OsmEditingPlugin plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class);
|
||||
|
@ -36,9 +45,8 @@ public class ValidateOsmLoginDetailsTask extends AsyncTask<Void, Void, OsmBugRes
|
|||
} else {
|
||||
app.showToastMessage(R.string.osm_authorization_success);
|
||||
}
|
||||
OsmEditingFragment targetfragment = fragmentRef.get();
|
||||
if (targetfragment != null) {
|
||||
targetfragment.updateAllSettings();
|
||||
if (validateOsmLoginListener != null) {
|
||||
validateOsmLoginListener.loginValidationFinished(osmBugResult.warning);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,11 +7,17 @@ import android.webkit.WebView;
|
|||
import com.github.scribejava.core.model.OAuth1AccessToken;
|
||||
import com.github.scribejava.core.model.OAuth1RequestToken;
|
||||
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
|
||||
import com.github.scribejava.core.model.OAuthRequest;
|
||||
import com.github.scribejava.core.model.Response;
|
||||
import com.github.scribejava.core.model.Verb;
|
||||
|
||||
import net.osmand.osm.oauth.OsmOAuthAuthorizationClient;
|
||||
import net.osmand.plus.BuildConfig;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
|
@ -71,6 +77,10 @@ public class OsmOAuthAuthorizationAdapter {
|
|||
client.performGetRequest(url, callback);
|
||||
}
|
||||
|
||||
public String authorizeOsmUserDetails () throws InterruptedException, ExecutionException, IOException, XmlPullParserException {
|
||||
return client.authorizeOsmUserDetails();
|
||||
}
|
||||
|
||||
public Response performRequest(String url, String method, String body)
|
||||
throws InterruptedException, ExecutionException, IOException {
|
||||
return client.performRequest(url, method, body);
|
||||
|
@ -83,6 +93,20 @@ public class OsmOAuthAuthorizationAdapter {
|
|||
|
||||
public void authorize(String oauthVerifier) {
|
||||
client.authorize(oauthVerifier);
|
||||
String response = "";
|
||||
try {
|
||||
response= client.authorizeOsmUserDetails();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (XmlPullParserException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
saveToken();
|
||||
application.getSettings().USER_DISPLAY_NAME.set(response);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1088,6 +1088,7 @@ public class OsmandSettings {
|
|||
|
||||
// this value string is synchronized with settings_pref.xml preference name
|
||||
public final OsmandPreference<String> USER_NAME = new StringPreference(this, "user_name", "").makeGlobal().makeShared();
|
||||
public final OsmandPreference<String> USER_DISPLAY_NAME = new StringPreference(this, "user_display_name", "").makeGlobal().makeShared();
|
||||
|
||||
public static final String BILLING_USER_DONATION_WORLD_PARAMETER = "";
|
||||
public static final String BILLING_USER_DONATION_NONE_PARAMETER = "none";
|
||||
|
|
|
@ -32,8 +32,6 @@ public class OsmLoginDataBottomSheet extends BasePreferenceBottomSheet {
|
|||
|
||||
private EditText userNameEditText;
|
||||
private EditText passwordEditText;
|
||||
private TextInputLayout passwordBox;
|
||||
private TextInputLayout loginBox;
|
||||
|
||||
|
||||
@Override
|
||||
|
@ -48,8 +46,8 @@ public class OsmLoginDataBottomSheet extends BasePreferenceBottomSheet {
|
|||
|
||||
userNameEditText = view.findViewById(R.id.name_edit_text);
|
||||
passwordEditText = view.findViewById(R.id.password_edit_text);
|
||||
passwordBox = view.findViewById(R.id.password_text_box);
|
||||
loginBox = view.findViewById(R.id.name_text_box);
|
||||
TextInputLayout passwordBox = view.findViewById(R.id.password_text_box);
|
||||
TextInputLayout loginBox = view.findViewById(R.id.name_text_box);
|
||||
|
||||
String name = app.getSettings().USER_NAME.get();
|
||||
String password = app.getSettings().USER_PASSWORD.get();
|
||||
|
|
Loading…
Reference in a new issue