Fix "Use dev.openstreetmap.org" should be visible only if "OsmAnd Development" is enabled.

This commit is contained in:
Dima-1 2020-11-24 14:58:44 +02:00
parent 923278a046
commit ed5566d4ea
4 changed files with 54 additions and 23 deletions

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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();
}
}
}

View file

@ -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<OsmAuthorizationListener> 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();
}