Add OsmOAuthHelper

This commit is contained in:
Dima-1 2020-11-12 16:43:33 +02:00
parent f4edc8dc83
commit 291bed500b
7 changed files with 167 additions and 93 deletions

View file

@ -43,6 +43,7 @@ import net.osmand.plus.liveupdates.LiveUpdatesHelper;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.monitoring.LiveMonitoringHelper; import net.osmand.plus.monitoring.LiveMonitoringHelper;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper;
import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiFiltersHelper;
import net.osmand.plus.quickaction.QuickActionRegistry; import net.osmand.plus.quickaction.QuickActionRegistry;
import net.osmand.plus.render.MapRenderRepositories; import net.osmand.plus.render.MapRenderRepositories;
@ -466,7 +467,7 @@ public class AppInitializer implements IProgress {
app.lockHelper = startupInit(new LockHelper(app), LockHelper.class); app.lockHelper = startupInit(new LockHelper(app), LockHelper.class);
app.settingsHelper = startupInit(new SettingsHelper(app), SettingsHelper.class); app.settingsHelper = startupInit(new SettingsHelper(app), SettingsHelper.class);
app.quickActionRegistry = startupInit(new QuickActionRegistry(app.getSettings()), QuickActionRegistry.class); app.quickActionRegistry = startupInit(new QuickActionRegistry(app.getSettings()), QuickActionRegistry.class);
app.osmOAuthHelper = startupInit(new OsmOAuthHelper(app), OsmOAuthHelper.class);
initOpeningHoursParser(); initOpeningHoursParser();
} }

View file

@ -43,7 +43,6 @@ import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.io.NetworkUtils; import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.AppInitializer.AppInitializeListener; import net.osmand.plus.AppInitializer.AppInitializeListener;
import net.osmand.plus.access.AccessibilityMode; import net.osmand.plus.access.AccessibilityMode;
import net.osmand.plus.helpers.DayNightHelper;
import net.osmand.plus.activities.ExitActivity; import net.osmand.plus.activities.ExitActivity;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.activities.SavingTrackHelper;
@ -57,13 +56,15 @@ import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.DownloadService; import net.osmand.plus.download.DownloadService;
import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.IndexItem;
import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.AvoidSpecificRoads;
import net.osmand.plus.helpers.enums.DrivingRegion; import net.osmand.plus.helpers.DayNightHelper;
import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.enums.MetricsConstants;
import net.osmand.plus.helpers.WaypointHelper; import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.helpers.enums.DrivingRegion;
import net.osmand.plus.helpers.enums.MetricsConstants;
import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.monitoring.LiveMonitoringHelper; import net.osmand.plus.monitoring.LiveMonitoringHelper;
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper;
import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiFiltersHelper;
import net.osmand.plus.quickaction.QuickActionRegistry; import net.osmand.plus.quickaction.QuickActionRegistry;
import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.render.RendererRegistry;
@ -154,6 +155,7 @@ public class OsmandApplication extends MultiDexApplication {
SettingsHelper settingsHelper; SettingsHelper settingsHelper;
GpxDbHelper gpxDbHelper; GpxDbHelper gpxDbHelper;
QuickActionRegistry quickActionRegistry; QuickActionRegistry quickActionRegistry;
OsmOAuthHelper osmOAuthHelper;
private Resources localizedResources; private Resources localizedResources;
@ -368,6 +370,10 @@ public class OsmandApplication extends MultiDexApplication {
return settingsHelper; return settingsHelper;
} }
public OsmOAuthHelper getOsmOAuthHelper() {
return osmOAuthHelper;
}
public synchronized DownloadIndexesThread getDownloadThread() { public synchronized DownloadIndexesThread getDownloadThread() {
if (downloadIndexesThread == null) { if (downloadIndexesThread == null) {
downloadIndexesThread = new DownloadIndexesThread(this); downloadIndexesThread = new DownloadIndexesThread(this);

View file

@ -4,6 +4,8 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import androidx.fragment.app.Fragment;
import net.osmand.AndroidNetworkUtils; import net.osmand.AndroidNetworkUtils;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
@ -19,7 +21,6 @@ import net.osmand.plus.activities.PluginsFragment;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType; import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.mapmarkers.MapMarkersDialogFragment; import net.osmand.plus.mapmarkers.MapMarkersDialogFragment;
import net.osmand.plus.mapsource.EditMapSourceDialogFragment; import net.osmand.plus.mapsource.EditMapSourceDialogFragment;
import net.osmand.plus.measurementtool.LoginBottomSheetFragment;
import net.osmand.plus.search.QuickSearchDialogFragment; import net.osmand.plus.search.QuickSearchDialogFragment;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
@ -35,6 +36,8 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.*;
public class IntentHelper { public class IntentHelper {
private static final Log LOG = PlatformUtil.getLog(IntentHelper.class); private static final Log LOG = PlatformUtil.getLog(IntentHelper.class);
@ -293,15 +296,17 @@ public class IntentHelper {
if (intent != null && intent.getData() != null) { if (intent != null && intent.getData() != null) {
Uri uri = intent.getData(); Uri uri = intent.getData();
if (uri.toString().startsWith("osmand-oauth")) { if (uri.toString().startsWith("osmand-oauth")) {
LoginBottomSheetFragment fragment = mapActivity.getLoginBottomSheetFragment();
if (fragment != null) {
String oauthVerifier = uri.getQueryParameter("oauth_verifier"); String oauthVerifier = uri.getQueryParameter("oauth_verifier");
fragment.authorize(oauthVerifier); app.getOsmOAuthHelper().authorize(oauthVerifier);
for (Fragment fragment : mapActivity.getSupportFragmentManager().getFragments()) {
if (fragment instanceof OsmAuthorizationListener) {
((OsmAuthorizationListener) fragment).authorizationCompleted();
}
}
mapActivity.setIntent(null); mapActivity.setIntent(null);
return true; return true;
} }
} }
}
return false; return false;
} }

View file

@ -18,28 +18,30 @@ import net.osmand.plus.R;
import net.osmand.plus.UiUtilities.DialogButtonType; import net.osmand.plus.UiUtilities.DialogButtonType;
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.OsmOAuthAuthorizationAdapter; import net.osmand.plus.osmedit.oauth.OsmOAuthHelper;
import net.osmand.plus.settings.bottomsheets.OsmLoginDataBottomSheet; import net.osmand.plus.settings.bottomsheets.OsmLoginDataBottomSheet;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA; import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA;
import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.*;
public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment { public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment implements OsmAuthorizationListener {
public static final String TAG = LoginBottomSheetFragment.class.getSimpleName(); public static final String TAG = LoginBottomSheetFragment.class.getSimpleName();
private static final Log log = PlatformUtil.getLog(LoginBottomSheetFragment.class); private static final Log LOG = PlatformUtil.getLog(LoginBottomSheetFragment.class);
private OsmOAuthAuthorizationAdapter authorizationAdapter; private OsmOAuthHelper osmOAuthHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OsmandApplication app = requiredMyApplication();
osmOAuthHelper = app.getOsmOAuthHelper();
}
@Override @Override
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
OsmandApplication app = requiredMyApplication();
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
items.add(new SimpleBottomSheetItem.Builder().setLayoutId(R.layout.bottom_sheet_login).create()); items.add(new SimpleBottomSheetItem.Builder().setLayoutId(R.layout.bottom_sheet_login).create());
} }
@ -87,59 +89,29 @@ public class LoginBottomSheetFragment extends MenuBottomSheetDialogFragment {
protected void onThirdBottomButtonClick() { protected void onThirdBottomButtonClick() {
View view = getView(); View view = getView();
if (view != null) { if (view != null) {
authorizationAdapter.startOAuth((ViewGroup) view); osmOAuthHelper.startOAuth((ViewGroup) view);
} }
} }
private boolean isValidToken() {
return authorizationAdapter.isValidToken();
}
@Override @Override
protected DialogButtonType getRightBottomButtonType() { protected DialogButtonType getRightBottomButtonType() {
return (DialogButtonType.SECONDARY); return (DialogButtonType.SECONDARY);
} }
public static void showInstance(@NonNull FragmentManager fragmentManager, @Nullable Fragment targetFragment) { public static void showInstance(@NonNull FragmentManager fragmentManager, @Nullable Fragment targetFragment) {
try {
if (!fragmentManager.isStateSaved()) { if (!fragmentManager.isStateSaved()) {
LoginBottomSheetFragment fragment = new LoginBottomSheetFragment(); LoginBottomSheetFragment fragment = new LoginBottomSheetFragment();
fragment.setTargetFragment(targetFragment, 0); fragment.setTargetFragment(targetFragment, 0);
fragment.show(fragmentManager, TAG); fragment.show(fragmentManager, TAG);
} }
} catch (RuntimeException e) {
LOG.error("showInstance", e);
}
} }
public void authorize(String oauthVerifier) { @Override
if (authorizationAdapter != null) { public void authorizationCompleted() {
authorizationAdapter.authorize(oauthVerifier);
updateUserName();
}
Fragment target = getTargetFragment();
if (target instanceof OsmAuthorizationListener) {
((OsmAuthorizationListener) target).authorizationCompleted();
}
dismiss(); dismiss();
} }
private void updateUserName() {
OsmandApplication app = getMyApplication();
if (app != null) {
String userName = "";
try {
userName = authorizationAdapter.getUserName();
} catch (InterruptedException e) {
log.error(e);
} catch (ExecutionException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
} catch (XmlPullParserException e) {
log.error(e);
}
app.getSettings().USER_DISPLAY_NAME.set(userName);
}
}
public interface OsmAuthorizationListener {
void authorizationCompleted();
}
} }

View file

@ -19,9 +19,9 @@ 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.measurementtool.LoginBottomSheetFragment.OsmAuthorizationListener;
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.OsmOAuthAuthorizationAdapter;
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;
import net.osmand.plus.settings.fragments.OnPreferenceChanged; import net.osmand.plus.settings.fragments.OnPreferenceChanged;
@ -34,7 +34,8 @@ import org.apache.commons.logging.Log;
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
import static net.osmand.plus.osmedit.OsmEditingPlugin.OSM_EDIT_TAB; import static net.osmand.plus.osmedit.OsmEditingPlugin.OSM_EDIT_TAB;
public class OsmEditingFragment extends BaseSettingsFragment implements OnPreferenceChanged, ValidateOsmLoginListener, OsmAuthorizationListener { public class OsmEditingFragment extends BaseSettingsFragment implements OnPreferenceChanged, ValidateOsmLoginListener,
OsmAuthorizationListener {
private static final Log log = PlatformUtil.getLog(OsmEditingFragment.class); private static final Log log = PlatformUtil.getLog(OsmEditingFragment.class);
@ -48,7 +49,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); authorizationAdapter = app.getOsmOAuthHelper().getAuthorizationAdapter();
} }
@Override @Override
@ -176,7 +177,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
settings.USER_ACCESS_TOKEN_SECRET.resetToDefault(); settings.USER_ACCESS_TOKEN_SECRET.resetToDefault();
authorizationAdapter.resetToken(); authorizationAdapter.resetToken();
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); authorizationAdapter = app.getOsmOAuthHelper().getAuthorizationAdapter();
} else { } else {
settings.USER_NAME.resetToDefault(); settings.USER_NAME.resetToDefault();
settings.USER_PASSWORD.resetToDefault(); settings.USER_PASSWORD.resetToDefault();
@ -195,7 +196,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
@Override @Override
public void authorizationCompleted() { public void authorizationCompleted() {
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); authorizationAdapter = app.getOsmOAuthHelper().getAuthorizationAdapter();
updateAllSettings(); updateAllSettings();
} }
} }

View file

@ -4,6 +4,7 @@ import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -28,6 +29,7 @@ import net.osmand.plus.osmedit.OpenstreetmapPoint;
import net.osmand.plus.osmedit.OsmNotesPoint; import net.osmand.plus.osmedit.OsmNotesPoint;
import net.osmand.plus.osmedit.OsmPoint; import net.osmand.plus.osmedit.OsmPoint;
import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationAdapter; import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationAdapter;
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.bottomsheets.OsmLoginDataBottomSheet; import net.osmand.plus.settings.bottomsheets.OsmLoginDataBottomSheet;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -36,18 +38,23 @@ import org.apache.commons.logging.Log;
import static net.osmand.plus.UiUtilities.setupDialogButton; import static net.osmand.plus.UiUtilities.setupDialogButton;
import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA; import static net.osmand.plus.osmedit.OsmEditingFragment.OSM_LOGIN_DATA;
import static net.osmand.plus.osmedit.ValidateOsmLoginDetailsTask.*;
import static net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.*; import static net.osmand.plus.osmedit.dialogs.SendPoiDialogFragment.*;
public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragment { public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragment implements ValidateOsmLoginListener,
OsmAuthorizationListener {
public static final String TAG = SendOsmNoteBottomSheetFragment.class.getSimpleName(); public static final String TAG = SendOsmNoteBottomSheetFragment.class.getSimpleName();
private static final Log LOG = PlatformUtil.getLog(SendOsmNoteBottomSheetFragment.class); private static final Log LOG = PlatformUtil.getLog(SendOsmNoteBottomSheetFragment.class);
public static final String OPENSTREETMAP_POINT = "openstreetmap_point"; public static final String OPENSTREETMAP_POINT = "openstreetmap_point";
public static final String POI_UPLOADER_TYPE = "poi_uploader_type"; public static final String POI_UPLOADER_TYPE = "poi_uploader_type";
private OsmPoint[] poi; private OsmPoint[] poi;
private boolean isLogin;
protected OsmandSettings settings; protected OsmandSettings settings;
private TextView accountName;
private LinearLayout accountBlockView;
private LinearLayout signInView;
private SwitchCompat uploadAnonymously;
public enum PoiUploaderType { public enum PoiUploaderType {
SIMPLE, SIMPLE,
@ -64,7 +71,6 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen
@Override @Override
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
isLogin = isLogin();
poi = (OsmPoint[]) getArguments().getSerializable(OPENSTREETMAP_POINT); poi = (OsmPoint[]) getArguments().getSerializable(OPENSTREETMAP_POINT);
OsmandApplication app = getMyApplication(); OsmandApplication app = getMyApplication();
@ -77,23 +83,21 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen
noteText.setText(((OsmNotesPoint) poi[0]).getText()); noteText.setText(((OsmNotesPoint) poi[0]).getText());
TextInputLayout noteHint = sendOsmNoteView.findViewById(R.id.note_hint); TextInputLayout noteHint = sendOsmNoteView.findViewById(R.id.note_hint);
noteHint.setHint(AndroidUtils.addColon(app, R.string.osn_bug_name)); noteHint.setHint(AndroidUtils.addColon(app, R.string.osn_bug_name));
final LinearLayout accountBlockView = sendOsmNoteView.findViewById(R.id.account_container); accountBlockView = sendOsmNoteView.findViewById(R.id.account_container);
final LinearLayout signInView = sendOsmNoteView.findViewById(R.id.sign_in_container); signInView = sendOsmNoteView.findViewById(R.id.sign_in_container);
final SwitchCompat uploadAnonymously = sendOsmNoteView.findViewById(R.id.upload_anonymously_switch); uploadAnonymously = sendOsmNoteView.findViewById(R.id.upload_anonymously_switch);
final TextView accountName = sendOsmNoteView.findViewById(R.id.user_name); accountName = sendOsmNoteView.findViewById(R.id.user_name);
settings = app.getSettings(); settings = app.getSettings();
String userNameOAuth = settings.USER_DISPLAY_NAME.get(); updateAccountName();
String userNameOpenID = settings.USER_NAME.get();
String userName = isLoginOAuth() ? userNameOAuth : userNameOpenID;
accountName.setText(userName);
View signInButton = sendOsmNoteView.findViewById(R.id.sign_in_button); View signInButton = sendOsmNoteView.findViewById(R.id.sign_in_button);
setupButton(signInButton, R.string.sing_in_with_open_street_map, DialogButtonType.PRIMARY, setupButton(signInButton, R.string.sing_in_with_open_street_map, DialogButtonType.PRIMARY,
R.drawable.ic_action_openstreetmap_logo); R.drawable.ic_action_openstreetmap_logo);
signInButton.setOnClickListener(new View.OnClickListener() { signInButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
OsmandApplication app = requiredMyApplication();
app.getOsmOAuthHelper().startOAuth((ViewGroup) v);
} }
}); });
View loginButton = sendOsmNoteView.findViewById(R.id.login_button); View loginButton = sendOsmNoteView.findViewById(R.id.login_button);
@ -101,20 +105,18 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen
loginButton.setOnClickListener(new View.OnClickListener() { loginButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
OsmLoginDataBottomSheet.showInstance(getFragmentManager(), OSM_LOGIN_DATA, getTargetFragment(), OsmLoginDataBottomSheet.showInstance(getFragmentManager(), OSM_LOGIN_DATA,
usedOnMap, null); SendOsmNoteBottomSheetFragment.this, usedOnMap, null);
} }
}); });
accountBlockView.setVisibility(!isLogin ? View.GONE : View.VISIBLE); updateSignIn(uploadAnonymously.isChecked());
signInView.setVisibility(isLogin ? View.GONE : View.VISIBLE);
uploadAnonymously.setBackgroundResource(nightMode ? R.drawable.layout_bg_dark : R.drawable.layout_bg); uploadAnonymously.setBackgroundResource(nightMode ? R.drawable.layout_bg_dark : R.drawable.layout_bg);
final int paddingSmall = app.getResources().getDimensionPixelSize(R.dimen.content_padding_small); final int paddingSmall = app.getResources().getDimensionPixelSize(R.dimen.content_padding_small);
uploadAnonymously.setPadding(paddingSmall, 0, paddingSmall, 0); uploadAnonymously.setPadding(paddingSmall, 0, paddingSmall, 0);
uploadAnonymously.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { uploadAnonymously.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
accountBlockView.setVisibility(isChecked || !isLogin ? View.GONE : View.VISIBLE); updateSignIn(isChecked);
signInView.setVisibility(isChecked || isLogin ? View.GONE : View.VISIBLE);
if (nightMode) { if (nightMode) {
uploadAnonymously.setBackgroundResource( uploadAnonymously.setBackgroundResource(
isChecked ? R.drawable.layout_bg_dark_solid : R.drawable.layout_bg_dark); isChecked ? R.drawable.layout_bg_dark_solid : R.drawable.layout_bg_dark);
@ -131,6 +133,20 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen
items.add(bottomSheetItem); items.add(bottomSheetItem);
} }
private void updateAccountName() {
String userNameOAuth = settings.USER_DISPLAY_NAME.get();
String userNameOpenID = settings.USER_NAME.get();
String userName = isLoginOAuth() ? userNameOAuth : userNameOpenID;
accountName.setText(userName);
updateSignIn(uploadAnonymously.isChecked());
}
private void updateSignIn(boolean isChecked) {
boolean isLogin = isLogin();
accountBlockView.setVisibility(isChecked || !isLogin ? View.GONE : View.VISIBLE);
signInView.setVisibility(isChecked || isLogin ? View.GONE : View.VISIBLE);
}
private void setupButton(View buttonView, int buttonTextId, DialogButtonType buttonType, int drawableId) { private void setupButton(View buttonView, int buttonTextId, DialogButtonType buttonType, int drawableId) {
Drawable icon = null; Drawable icon = null;
if (drawableId != -1) { if (drawableId != -1) {
@ -205,11 +221,21 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen
return R.string.shared_string_upload; return R.string.shared_string_upload;
} }
@Override
public void authorizationCompleted() {
updateAccountName();
}
@Override
public void loginValidationFinished(String warning) {
updateAccountName();
}
private boolean isLogin() { private boolean isLogin() {
OsmandApplication app = getMyApplication(); OsmandApplication app = getMyApplication();
OsmandSettings settings = app.getSettings(); OsmandSettings settings = app.getSettings();
OsmOAuthAuthorizationAdapter client = new OsmOAuthAuthorizationAdapter(app); OsmOAuthAuthorizationAdapter adapter = app.getOsmOAuthHelper().getAuthorizationAdapter();
return client.isValidToken() return adapter.isValidToken()
|| !Algorithms.isEmpty(settings.USER_NAME.get()) || !Algorithms.isEmpty(settings.USER_NAME.get())
&& !Algorithms.isEmpty(settings.USER_PASSWORD.get()); && !Algorithms.isEmpty(settings.USER_PASSWORD.get());
} }

View file

@ -0,0 +1,63 @@
package net.osmand.plus.osmedit.oauth;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
public class OsmOAuthHelper {
private static final Log log = PlatformUtil.getLog(OsmOAuthHelper.class);
private final OsmandApplication app;
private final OsmOAuthAuthorizationAdapter authorizationAdapter;
public OsmOAuthHelper(@NonNull OsmandApplication app) {
this.app = app;
authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
}
public void startOAuth(ViewGroup view) {
authorizationAdapter.startOAuth(view);
}
public void authorize(String oauthVerifier) {
authorizationAdapter.authorize(oauthVerifier);
updateUserName();
}
public OsmOAuthAuthorizationAdapter getAuthorizationAdapter() {
return authorizationAdapter;
}
private void updateUserName() {
if (app != null) {
String userName = "";
try {
userName = authorizationAdapter.getUserName();
} catch (InterruptedException e) {
log.error(e);
} catch (ExecutionException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
} catch (XmlPullParserException e) {
log.error(e);
}
app.getSettings().USER_DISPLAY_NAME.set(userName);
}
}
public interface OsmAuthorizationListener {
void authorizationCompleted();
}
}