custom tabs in opr login

This commit is contained in:
simon 2020-12-24 16:11:01 +02:00
parent 9ce3816280
commit f2492b2cbe
5 changed files with 71 additions and 14 deletions

View file

@ -475,6 +475,15 @@
<data android:scheme="osmand-oauth" />
</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="opr-oauth" />
</intent-filter>
</activity>
<receiver android:name="net.osmand.plus.audionotes.MediaRemoteControlReceiver">

View file

@ -24,6 +24,7 @@
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<WebView
android:id="@+id/printDialogWebview"
android:layout_width="match_parent"

View file

@ -13,14 +13,15 @@ import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.map.TileSourceManager;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.PluginsFragment;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.mapmarkers.MapMarkersDialogFragment;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.mapsource.EditMapSourceDialogFragment;
import net.osmand.plus.openplacereviews.OPRWebviewActivity;
import net.osmand.plus.search.QuickSearchDialogFragment;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings;
@ -36,7 +37,7 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.*;
import static net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener;
public class IntentHelper {
@ -66,6 +67,9 @@ public class IntentHelper {
if (!applied) {
applied = parseOAuthIntent();
}
if (!applied) {
applied = parseOprOAuthIntent();
}
return applied;
}
@ -306,6 +310,21 @@ public class IntentHelper {
return false;
}
private boolean parseOprOAuthIntent() {
Intent intent = mapActivity.getIntent();
if (intent != null && intent.getData() != null) {
Uri uri = intent.getData();
if (uri.toString().startsWith(OPRWebviewActivity.OPR_OAUTH_PREFIX)) {
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
app.getOsmOAuthHelper().addListener(getOnAuthorizeListener());
app.getOsmOAuthHelper().authorize(oauthVerifier);
mapActivity.setIntent(null);
return true;
}
}
return false;
}
private OsmAuthorizationListener getOnAuthorizeListener() {
return new OsmAuthorizationListener() {
@Override

View file

@ -11,8 +11,10 @@ import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import net.osmand.AndroidUtils;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -25,7 +27,10 @@ import java.util.List;
public class OPRWebviewActivity extends OsmandActionBarActivity {
public static final String KEY_LOGIN = "LOGIN_KEY";
public static final String KEY_TITLE = "TITLE_KEY";
public static final String OPR_OAUTH_PREFIX = "opr-oauth";
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)";
private static final String PURPOSE = "opr-android";
private static final String CALLBACK_URL = OPR_OAUTH_PREFIX + "://osmand_opr_auth";
private WebView webView;
private boolean isLogin = false;
@ -38,11 +43,23 @@ public class OPRWebviewActivity extends OsmandActionBarActivity {
}
public static String getLoginUrl(Context ctx) {
return getBaseUrl(ctx) + "login";
return getBaseUrl(ctx) + "login" + getQueryString(ctx);
}
public static String getRegisterUrl(Context ctx) {
return getBaseUrl(ctx) + "signup";
return getBaseUrl(ctx) + "signup" + getQueryString(ctx);
}
public static String getQueryString(Context ctx) {
return "?" + getPurposeParam(ctx) + "&" + getCallbackParam(ctx);
}
public static String getPurposeParam(Context ctx) {
return "purpose=" + PURPOSE;
}
public static String getCallbackParam(Context ctx) {
return "callback=" + CALLBACK_URL;
}
public static List<String> getFinishUrls(Context ctx) {

View file

@ -1,7 +1,7 @@
package net.osmand.plus.openplacereviews;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.SpannableString;
@ -13,15 +13,18 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import net.osmand.PlatformUtil;
import net.osmand.plus.BuildConfig;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.base.BaseOsmAndFragment;
import org.apache.commons.logging.Log;
public class OprStartFragment extends BaseOsmAndFragment {
@ -50,10 +53,7 @@ public class OprStartFragment extends BaseOsmAndFragment {
createAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(requireContext(), OPRWebviewActivity.class);
i.putExtra(OPRWebviewActivity.KEY_TITLE, getString(R.string.register_opr_create_new_account));
i.putExtra(OPRWebviewActivity.KEY_LOGIN, false);
startActivity(i);
handleCreateAccount();
}
});
View haveAccount = v.findViewById(R.id.register_opr_have_account);
@ -62,16 +62,27 @@ public class OprStartFragment extends BaseOsmAndFragment {
haveAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(requireContext(), OPRWebviewActivity.class);
i.putExtra(OPRWebviewActivity.KEY_TITLE, getString(R.string.user_login));
i.putExtra(OPRWebviewActivity.KEY_LOGIN, true);
startActivity(i);
handleHaveAccount();
}
});
setURLSpan(v);
return v;
}
private void handleHaveAccount() {
String url = OPRWebviewActivity.getLoginUrl(requireContext());
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(requireContext(), Uri.parse(url));
}
private void handleCreateAccount() {
String url = OPRWebviewActivity.getRegisterUrl(requireContext());
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(requireContext(), Uri.parse(url));
}
private void setURLSpan(View v) {
String desc = requireContext().getString(R.string.register_on_openplacereviews_desc);
SpannableString ss = new SpannableString(desc);