From e14989f46420c521db1d3ea6c759d5a27ecb6e40 Mon Sep 17 00:00:00 2001 From: androiddevkkotlin Date: Tue, 3 Nov 2020 17:27:19 +0200 Subject: [PATCH] OAuth, refactor, fix icon --- .../oauth/OsmOAuthAuthorizationClient.java | 39 +++++++++++++ .../res/layout/bottom_sheet_dialog_button.xml | 7 ++- OsmAnd/res/values/strings.xml | 2 +- .../osmand/plus/activities/MapActivity.java | 5 +- .../net/osmand/plus/helpers/IntentHelper.java | 3 +- .../LoginBottomSheetFragment.java | 20 ++++++- .../plus/osmedit/OsmEditingFragment.java | 58 +++++++------------ .../osmedit/ValidateOsmLoginDetailsTask.java | 22 ++++--- .../oauth/OsmOAuthAuthorizationAdapter.java | 24 ++++++++ .../plus/settings/backend/OsmandSettings.java | 1 + .../bottomsheets/OsmLoginDataBottomSheet.java | 6 +- 11 files changed, 128 insertions(+), 59 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java index f1c718465e..f973e134a2 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java @@ -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 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(); diff --git a/OsmAnd/res/layout/bottom_sheet_dialog_button.xml b/OsmAnd/res/layout/bottom_sheet_dialog_button.xml index ee757eba8f..697075cba5 100644 --- a/OsmAnd/res/layout/bottom_sheet_dialog_button.xml +++ b/OsmAnd/res/layout/bottom_sheet_dialog_button.xml @@ -7,7 +7,7 @@ android:layout_height="@dimen/dialog_button_height" android:layout_weight="1"> - - + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index ab75bf8fc1..61b5aade88 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -17,7 +17,7 @@ Use login and password 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. You can view all your unloaded edits or OSM bugs in %1$s. Uploaded points don’t show in OsmAnd. - Sing in with OpenStreetMap + Sign in with OpenStreetMap Login to OpenStreetMap.org Login to OpenStreetMap These plugin setting are global, and apply to all profiles diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 8cb6e832c5..6d8c5ad4d5 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -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() { diff --git a/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java b/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java index 19748b100c..3efb1f6aac 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java @@ -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); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java index a34de00fdf..bcdc7aa357 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java @@ -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(); + } } + diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java index 37101874e8..75f6c66d9c 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java @@ -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(); } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/osmedit/ValidateOsmLoginDetailsTask.java b/OsmAnd/src/net/osmand/plus/osmedit/ValidateOsmLoginDetailsTask.java index dc6803c5bd..3fc06f1b8b 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/ValidateOsmLoginDetailsTask.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/ValidateOsmLoginDetailsTask.java @@ -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 { private OsmandApplication app; - private final WeakReference 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 USER_NAME = new StringPreference(this, "user_name", "").makeGlobal().makeShared(); + public final OsmandPreference 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"; diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/OsmLoginDataBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/OsmLoginDataBottomSheet.java index 8971fe8bcd..cca766bf9c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/OsmLoginDataBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/OsmLoginDataBottomSheet.java @@ -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();