Merge pull request #10263 from osmandapp/fix_osmedit_setting_dev
Fix "Use dev.openstreetmap.org" visible
This commit is contained in:
commit
1a4b546351
4 changed files with 54 additions and 23 deletions
|
@ -140,4 +140,13 @@ public class OsmandDevelopmentPlugin extends OsmandPlugin {
|
||||||
public DashFragmentData getCardFragment() {
|
public DashFragmentData getCardFragment() {
|
||||||
return DashSimulateFragment.FRAGMENT_DATA;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -16,6 +16,7 @@ import net.osmand.PlatformUtil;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
import net.osmand.plus.UiUtilities.DialogButtonType;
|
import net.osmand.plus.UiUtilities.DialogButtonType;
|
||||||
|
import net.osmand.plus.activities.MapActivity;
|
||||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||||
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
||||||
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper;
|
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper;
|
||||||
|
@ -90,7 +91,7 @@ public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment impl
|
||||||
View view = getView();
|
View view = getView();
|
||||||
if (view != null) {
|
if (view != null) {
|
||||||
Fragment fragment = getTargetFragment();
|
Fragment fragment = getTargetFragment();
|
||||||
if (fragment instanceof OsmAuthorizationListener) {
|
if (!(getActivity() instanceof MapActivity) && fragment instanceof OsmAuthorizationListener) {
|
||||||
osmOAuthHelper.addListener((OsmAuthorizationListener) fragment);
|
osmOAuthHelper.addListener((OsmAuthorizationListener) fragment);
|
||||||
}
|
}
|
||||||
osmOAuthHelper.startOAuth((ViewGroup) view);
|
osmOAuthHelper.startOAuth((ViewGroup) view);
|
||||||
|
|
|
@ -15,13 +15,14 @@ import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
|
import net.osmand.plus.OsmandPlugin;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
import net.osmand.plus.activities.MapActivity;
|
import net.osmand.plus.activities.MapActivity;
|
||||||
import net.osmand.plus.helpers.AndroidUiHelper;
|
import net.osmand.plus.helpers.AndroidUiHelper;
|
||||||
import net.osmand.plus.helpers.FontCache;
|
import net.osmand.plus.helpers.FontCache;
|
||||||
import net.osmand.plus.measurementtool.LoginBottomSheetFragment;
|
import net.osmand.plus.measurementtool.LoginBottomSheetFragment;
|
||||||
import net.osmand.plus.osmedit.ValidateOsmLoginDetailsTask.ValidateOsmLoginListener;
|
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.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener;
|
||||||
import net.osmand.plus.settings.backend.OsmAndAppCustomization;
|
import net.osmand.plus.settings.backend.OsmAndAppCustomization;
|
||||||
import net.osmand.plus.settings.fragments.BaseSettingsFragment;
|
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";
|
public static final String OSM_LOGIN_DATA = "osm_login_data";
|
||||||
private static final String OSM_EDITING_INFO = "osm_editing_info";
|
private static final String OSM_EDITING_INFO = "osm_editing_info";
|
||||||
|
|
||||||
private OsmOAuthAuthorizationAdapter authorizationAdapter;
|
private OsmOAuthHelper authHelper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
authorizationAdapter = app.getOsmOAuthHelper().getAuthorizationAdapter();
|
authHelper = app.getOsmOAuthHelper();
|
||||||
|
|
||||||
FragmentActivity activity = requireMyActivity();
|
FragmentActivity activity = requireMyActivity();
|
||||||
activity.getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
|
activity.getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
|
||||||
|
@ -121,7 +122,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidToken() {
|
private boolean isValidToken() {
|
||||||
return authorizationAdapter.isValidToken();
|
return authHelper.isValidToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isLoginExists() {
|
private boolean isLoginExists() {
|
||||||
|
@ -139,10 +140,14 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupUseDevUrlPref() {
|
private void setupUseDevUrlPref() {
|
||||||
Drawable icon = getPersistentPrefIcon(R.drawable.ic_action_laptop);
|
|
||||||
SwitchPreferenceEx useDevUrlPref = findPreference(settings.USE_DEV_URL.getId());
|
SwitchPreferenceEx useDevUrlPref = findPreference(settings.USE_DEV_URL.getId());
|
||||||
|
if (OsmandPlugin.isDevelopment()) {
|
||||||
|
Drawable icon = getPersistentPrefIcon(R.drawable.ic_action_laptop);
|
||||||
useDevUrlPref.setDescription(getString(R.string.use_dev_url_descr));
|
useDevUrlPref.setDescription(getString(R.string.use_dev_url_descr));
|
||||||
useDevUrlPref.setIcon(icon);
|
useDevUrlPref.setIcon(icon);
|
||||||
|
} else {
|
||||||
|
useDevUrlPref.setVisible(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupOsmEditsDescrPref() {
|
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) {
|
if (settings.USE_DEV_URL.getId().equals(prefId) && newValue instanceof Boolean) {
|
||||||
settings.USE_DEV_URL.set((Boolean) newValue);
|
settings.USE_DEV_URL.set((Boolean) newValue);
|
||||||
osmLogout();
|
osmLogout();
|
||||||
authorizationAdapter = app.getOsmOAuthHelper().updateAdapter();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return super.onPreferenceChange(preference, newValue);
|
return super.onPreferenceChange(preference, newValue);
|
||||||
|
@ -206,33 +210,26 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
||||||
return super.onPreferenceClick(preference);
|
return super.onPreferenceClick(preference);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void osmLogout() {
|
public void osmLogout() {
|
||||||
boolean validToken = isValidToken();
|
if (authHelper.isValidToken() || isLoginExists()) {
|
||||||
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();
|
|
||||||
}
|
|
||||||
app.showShortToastMessage(R.string.osm_edit_logout_success);
|
app.showShortToastMessage(R.string.osm_edit_logout_success);
|
||||||
updateAllSettings();
|
|
||||||
}
|
}
|
||||||
|
authHelper.resetAuthorization();
|
||||||
|
updateAllSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPreferenceChanged(String prefId) {
|
public void onPreferenceChanged(String prefId) {
|
||||||
if (settings.USE_DEV_URL.getId().equals(prefId)) {
|
if (settings.USE_DEV_URL.getId().equals(prefId)) {
|
||||||
osmLogout();
|
osmLogout();
|
||||||
authorizationAdapter = app.getOsmOAuthHelper().updateAdapter();
|
|
||||||
}
|
}
|
||||||
updateAllSettings();
|
updateAllSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void authorizationCompleted() {
|
public void authorizationCompleted() {
|
||||||
|
if (getContext() != null) {
|
||||||
updateAllSettings();
|
updateAllSettings();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -5,6 +5,8 @@ import android.view.ViewGroup;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
|
import net.osmand.plus.settings.backend.OsmandSettings;
|
||||||
|
import net.osmand.util.Algorithms;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -14,9 +16,11 @@ public class OsmOAuthHelper {
|
||||||
private final OsmandApplication app;
|
private final OsmandApplication app;
|
||||||
private OsmOAuthAuthorizationAdapter authorizationAdapter;
|
private OsmOAuthAuthorizationAdapter authorizationAdapter;
|
||||||
private final Set<OsmAuthorizationListener> listeners = new HashSet<>();
|
private final Set<OsmAuthorizationListener> listeners = new HashSet<>();
|
||||||
|
private final OsmandSettings settings;
|
||||||
|
|
||||||
public OsmOAuthHelper(@NonNull OsmandApplication app) {
|
public OsmOAuthHelper(@NonNull OsmandApplication app) {
|
||||||
this.app = app;
|
this.app = app;
|
||||||
|
settings = app.getSettings();
|
||||||
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
|
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +49,22 @@ public class OsmOAuthHelper {
|
||||||
authorizationAdapter.authorize(oauthVerifier, this);
|
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() {
|
public void notifyAndRemoveListeners() {
|
||||||
for (OsmAuthorizationListener listener : listeners) {
|
for (OsmAuthorizationListener listener : listeners) {
|
||||||
listener.authorizationCompleted();
|
listener.authorizationCompleted();
|
||||||
|
@ -52,6 +72,10 @@ public class OsmOAuthHelper {
|
||||||
listeners.clear();
|
listeners.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isValidToken() {
|
||||||
|
return authorizationAdapter.isValidToken();
|
||||||
|
}
|
||||||
|
|
||||||
public interface OsmAuthorizationListener {
|
public interface OsmAuthorizationListener {
|
||||||
void authorizationCompleted();
|
void authorizationCompleted();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue