From ed5566d4ea3372218c11e8d0d651bbcde5464314 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Tue, 24 Nov 2020 14:58:44 +0200 Subject: [PATCH] Fix "Use dev.openstreetmap.org" should be visible only if "OsmAnd Development" is enabled. --- .../development/OsmandDevelopmentPlugin.java | 9 ++++ .../LoginBottomSheetFragment.java | 3 +- .../plus/osmedit/OsmEditingFragment.java | 41 +++++++++---------- .../plus/osmedit/oauth/OsmOAuthHelper.java | 24 +++++++++++ 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java b/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java index d7798a646b..e5d7b3e11d 100644 --- a/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java +++ b/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java @@ -140,4 +140,13 @@ public class OsmandDevelopmentPlugin extends OsmandPlugin { public DashFragmentData getCardFragment() { return DashSimulateFragment.FRAGMENT_DATA; } + + @Override + public void disable(OsmandApplication app) { + if (app.getSettings().USE_DEV_URL.get()) { + app.getSettings().USE_DEV_URL.set(false); + app.getOsmOAuthHelper().resetAuthorization(); + } + super.disable(app); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java index 2b50ea6b2f..94ef66beab 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/LoginBottomSheetFragment.java @@ -16,6 +16,7 @@ import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities.DialogButtonType; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.osmedit.oauth.OsmOAuthHelper; @@ -90,7 +91,7 @@ public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment impl View view = getView(); if (view != null) { Fragment fragment = getTargetFragment(); - if (fragment instanceof OsmAuthorizationListener) { + if (!(getActivity() instanceof MapActivity) && fragment instanceof OsmAuthorizationListener) { osmOAuthHelper.addListener((OsmAuthorizationListener) fragment); } osmOAuthHelper.startOAuth((ViewGroup) view); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java index 991073c435..81cebe393d 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingFragment.java @@ -15,13 +15,14 @@ import androidx.fragment.app.FragmentManager; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.FontCache; import net.osmand.plus.measurementtool.LoginBottomSheetFragment; import net.osmand.plus.osmedit.ValidateOsmLoginDetailsTask.ValidateOsmLoginListener; -import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationAdapter; +import net.osmand.plus.osmedit.oauth.OsmOAuthHelper; import net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener; import net.osmand.plus.settings.backend.OsmAndAppCustomization; import net.osmand.plus.settings.fragments.BaseSettingsFragment; @@ -41,12 +42,12 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer public static final String OSM_LOGIN_DATA = "osm_login_data"; private static final String OSM_EDITING_INFO = "osm_editing_info"; - private OsmOAuthAuthorizationAdapter authorizationAdapter; + private OsmOAuthHelper authHelper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - authorizationAdapter = app.getOsmOAuthHelper().getAuthorizationAdapter(); + authHelper = app.getOsmOAuthHelper(); FragmentActivity activity = requireMyActivity(); activity.getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) { @@ -121,7 +122,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer } private boolean isValidToken() { - return authorizationAdapter.isValidToken(); + return authHelper.isValidToken(); } private boolean isLoginExists() { @@ -139,10 +140,14 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer } private void setupUseDevUrlPref() { - Drawable icon = getPersistentPrefIcon(R.drawable.ic_action_laptop); SwitchPreferenceEx useDevUrlPref = findPreference(settings.USE_DEV_URL.getId()); - useDevUrlPref.setDescription(getString(R.string.use_dev_url_descr)); - useDevUrlPref.setIcon(icon); + if (OsmandPlugin.isDevelopment()) { + Drawable icon = getPersistentPrefIcon(R.drawable.ic_action_laptop); + useDevUrlPref.setDescription(getString(R.string.use_dev_url_descr)); + useDevUrlPref.setIcon(icon); + } else { + useDevUrlPref.setVisible(false); + } } private void setupOsmEditsDescrPref() { @@ -174,7 +179,6 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer if (settings.USE_DEV_URL.getId().equals(prefId) && newValue instanceof Boolean) { settings.USE_DEV_URL.set((Boolean) newValue); osmLogout(); - authorizationAdapter = app.getOsmOAuthHelper().updateAdapter(); return true; } return super.onPreferenceChange(preference, newValue); @@ -206,33 +210,26 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer return super.onPreferenceClick(preference); } - private void osmLogout() { - boolean validToken = isValidToken(); - if (validToken || isLoginExists()) { - if (validToken) { - settings.USER_ACCESS_TOKEN.resetToDefault(); - settings.USER_ACCESS_TOKEN_SECRET.resetToDefault(); - authorizationAdapter.resetToken(); - } else { - settings.USER_NAME.resetToDefault(); - settings.USER_PASSWORD.resetToDefault(); - } + public void osmLogout() { + if (authHelper.isValidToken() || isLoginExists()) { app.showShortToastMessage(R.string.osm_edit_logout_success); - updateAllSettings(); } + authHelper.resetAuthorization(); + updateAllSettings(); } @Override public void onPreferenceChanged(String prefId) { if (settings.USE_DEV_URL.getId().equals(prefId)) { osmLogout(); - authorizationAdapter = app.getOsmOAuthHelper().updateAdapter(); } updateAllSettings(); } @Override public void authorizationCompleted() { - updateAllSettings(); + if (getContext() != null) { + updateAllSettings(); + } } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java index d7ef523d8c..acda0e0361 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/oauth/OsmOAuthHelper.java @@ -5,6 +5,8 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.util.Algorithms; import java.util.HashSet; import java.util.Set; @@ -14,9 +16,11 @@ public class OsmOAuthHelper { private final OsmandApplication app; private OsmOAuthAuthorizationAdapter authorizationAdapter; private final Set listeners = new HashSet<>(); + private final OsmandSettings settings; public OsmOAuthHelper(@NonNull OsmandApplication app) { this.app = app; + settings = app.getSettings(); authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); } @@ -45,6 +49,22 @@ public class OsmOAuthHelper { authorizationAdapter.authorize(oauthVerifier, this); } + public void resetAuthorization() { + if (isValidToken()) { + settings.USER_ACCESS_TOKEN.resetToDefault(); + settings.USER_ACCESS_TOKEN_SECRET.resetToDefault(); + authorizationAdapter.resetToken(); + } else if (isLoginExists()) { + settings.USER_NAME.resetToDefault(); + settings.USER_PASSWORD.resetToDefault(); + } + updateAdapter(); + } + + private boolean isLoginExists() { + return !Algorithms.isEmpty(settings.USER_NAME.get()) && !Algorithms.isEmpty(settings.USER_PASSWORD.get()); + } + public void notifyAndRemoveListeners() { for (OsmAuthorizationListener listener : listeners) { listener.authorizationCompleted(); @@ -52,6 +72,10 @@ public class OsmOAuthHelper { listeners.clear(); } + public boolean isValidToken() { + return authorizationAdapter.isValidToken(); + } + public interface OsmAuthorizationListener { void authorizationCompleted(); }