Add OAuth prefs to new osm plugin settings screen

This commit is contained in:
Vitaliy 2020-10-21 01:06:35 +03:00
parent 9b3eeecf89
commit 433b2b8739
4 changed files with 111 additions and 5 deletions

View file

@ -466,6 +466,13 @@
<data android:mimeType="text/plain" /> <data android:mimeType="text/plain" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="osmand-oauth" />
</intent-filter>
</activity> </activity>
<receiver android:name="net.osmand.plus.audionotes.MediaRemoteControlReceiver"> <receiver android:name="net.osmand.plus.audionotes.MediaRemoteControlReceiver">

View file

@ -118,6 +118,7 @@ import net.osmand.plus.measurementtool.GpxData;
import net.osmand.plus.measurementtool.MeasurementEditingContext; import net.osmand.plus.measurementtool.MeasurementEditingContext;
import net.osmand.plus.measurementtool.MeasurementToolFragment; import net.osmand.plus.measurementtool.MeasurementToolFragment;
import net.osmand.plus.measurementtool.SnapTrackWarningFragment; import net.osmand.plus.measurementtool.SnapTrackWarningFragment;
import net.osmand.plus.osmedit.OsmEditingFragment;
import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.routepreparationmenu.ChooseRouteFragment; import net.osmand.plus.routepreparationmenu.ChooseRouteFragment;
@ -2216,6 +2217,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
return getFragment(GpxApproximationFragment.TAG); return getFragment(GpxApproximationFragment.TAG);
} }
public OsmEditingFragment getOsmEditingFragment() {
return getFragment(SettingsScreenType.OPEN_STREET_MAP_EDITING.fragmentName);
}
public SnapTrackWarningFragment getSnapTrackWarningBottomSheet() { public SnapTrackWarningFragment getSnapTrackWarningBottomSheet() {
return getFragment(SnapTrackWarningFragment.TAG); return getFragment(SnapTrackWarningFragment.TAG);
} }

View file

@ -19,6 +19,7 @@ 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.osmedit.OsmEditingFragment;
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;
@ -59,6 +60,9 @@ public class IntentHelper {
if (!applied) { if (!applied) {
applied = parseSendIntent(); applied = parseSendIntent();
} }
if (!applied) {
applied = parseOAuthIntent();
}
return applied; return applied;
} }
@ -284,6 +288,23 @@ public class IntentHelper {
return false; return false;
} }
private boolean parseOAuthIntent() {
Intent intent = mapActivity.getIntent();
if (intent != null && intent.getData() != null) {
Uri uri = intent.getData();
if (uri.toString().startsWith("osmand-oauth")) {
OsmEditingFragment fragment = mapActivity.getOsmEditingFragment();
if (fragment != null) {
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
fragment.authorize(oauthVerifier);
mapActivity.setIntent(null);
return true;
}
}
}
return false;
}
private boolean handleSendText(Intent intent) { private boolean handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (!Algorithms.isEmpty(sharedText)) { if (!Algorithms.isEmpty(sharedText)) {

View file

@ -1,5 +1,6 @@
package net.osmand.plus.osmedit; package net.osmand.plus.osmedit;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -7,31 +8,50 @@ import android.os.Bundle;
import android.text.SpannableString; import android.text.SpannableString;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import net.osmand.plus.settings.backend.OsmAndAppCustomization; import net.osmand.PlatformUtil;
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.osmedit.oauth.OsmOAuthAuthorizationAdapter;
import net.osmand.plus.settings.backend.OsmAndAppCustomization;
import net.osmand.plus.settings.bottomsheets.OsmLoginDataBottomSheet;
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;
import net.osmand.plus.settings.bottomsheets.OsmLoginDataBottomSheet;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx; import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
import net.osmand.plus.widgets.style.CustomTypefaceSpan; import net.osmand.plus.widgets.style.CustomTypefaceSpan;
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 { public class OsmEditingFragment extends BaseSettingsFragment implements OnPreferenceChanged {
private static final Log log = PlatformUtil.getLog(OsmEditingFragment.class);
private static final String OSM_EDITING_INFO = "osm_editing_info"; private static final String OSM_EDITING_INFO = "osm_editing_info";
private static final String OPEN_OSM_EDITS = "open_osm_edits"; 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_LOGIN_DATA = "osm_login_data";
private static final String OSM_OAUTH_SUCCESS = "osm_oauth_success";
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 onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
client = new OsmOAuthAuthorizationAdapter(app);
}
@Override @Override
protected void setupPreferences() { protected void setupPreferences() {
@ -42,6 +62,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
setupOfflineEditingPref(); setupOfflineEditingPref();
setupOsmEditsDescrPref(); setupOsmEditsDescrPref();
setupOsmEditsPref(); setupOsmEditsPref();
setupOAuthPrefs();
} }
@Override @Override
@ -73,7 +94,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
Drawable enabled = getActiveIcon(R.drawable.ic_world_globe_dark); Drawable enabled = getActiveIcon(R.drawable.ic_world_globe_dark);
Drawable icon = getPersistentPrefIcon(enabled, disabled); Drawable icon = getPersistentPrefIcon(enabled, disabled);
SwitchPreferenceEx offlineEditingPref = (SwitchPreferenceEx) findPreference(settings.OFFLINE_EDITION.getId()); SwitchPreferenceEx offlineEditingPref = findPreference(settings.OFFLINE_EDITION.getId());
offlineEditingPref.setDescription(getString(R.string.offline_edition_descr)); offlineEditingPref.setDescription(getString(R.string.offline_edition_descr));
offlineEditingPref.setIcon(icon); offlineEditingPref.setIcon(icon);
} }
@ -101,9 +122,37 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
createProfile.setIcon(getActiveIcon(R.drawable.ic_action_folder)); createProfile.setIcon(getActiveIcon(R.drawable.ic_action_folder));
} }
private void setupOAuthPrefs() {
Context ctx = getContext();
if (ctx != null) {
PreferenceScreen screen = getPreferenceScreen();
if (client.isValidToken()) {
Preference prefOAuth = new Preference(ctx);
prefOAuth.setTitle(R.string.osm_authorization_success);
prefOAuth.setSummary(R.string.osm_authorization_success);
prefOAuth.setKey(OSM_OAUTH_SUCCESS);
Preference prefClearToken = new Preference(ctx);
prefClearToken.setTitle(R.string.shared_string_logoff);
prefClearToken.setSummary(R.string.clear_osm_token);
prefClearToken.setKey(OSM_OAUTH_CLEAR);
screen.addPreference(prefOAuth);
screen.addPreference(prefClearToken);
} else {
Preference prefOAuth = new Preference(ctx);
prefOAuth.setTitle(R.string.perform_oauth_authorization);
prefOAuth.setSummary(R.string.perform_oauth_authorization_description);
prefOAuth.setKey(OSM_OAUTH_LOGIN);
screen.addPreference(prefOAuth);
}
}
}
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
if (OPEN_OSM_EDITS.equals(preference.getKey())) { String prefId = preference.getKey();
if (OPEN_OSM_EDITS.equals(prefId)) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(TAB_ID, OSM_EDIT_TAB); bundle.putInt(TAB_ID, OSM_EDIT_TAB);
@ -113,12 +162,29 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
favorites.putExtra(MapActivity.INTENT_PARAMS, bundle); favorites.putExtra(MapActivity.INTENT_PARAMS, bundle);
startActivity(favorites); startActivity(favorites);
return true; return true;
} else if (OSM_LOGIN_DATA.equals(preference.getKey())) { } else if (OSM_LOGIN_DATA.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager(); FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) { if (fragmentManager != null) {
OsmLoginDataBottomSheet.showInstance(fragmentManager, OSM_LOGIN_DATA, this, false, getSelectedAppMode()); OsmLoginDataBottomSheet.showInstance(fragmentManager, OSM_LOGIN_DATA, this, false, getSelectedAppMode());
return true; 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); return super.onPreferenceClick(preference);
} }
@ -130,4 +196,11 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
nameAndPasswordPref.setSummary(settings.USER_NAME.get()); nameAndPasswordPref.setSummary(settings.USER_NAME.get());
} }
} }
public void authorize(String oauthVerifier) {
if (client != null) {
client.authorize(oauthVerifier);
}
updateAllSettings();
}
} }