Permalink
Browse files

Merge pull request #202 from rksh/registration_activity

ENH - using a webview to integrate registration into app
  • Loading branch information...
2 parents a8065c3 + de5481b commit bea171ce4bcc8e0c73a12f136f14ebfa27500523 @rksh rksh committed Jan 9, 2018
@@ -128,6 +128,16 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
+ <activity
+ android:name=".RegistrationActivity"
+ android:parentActivityName=".MainActivity"
+ android:configChanges="keyboard|keyboardHidden|orientation|locale|screenSize" >
+ <intent-filter>
+ <action android:name="android.intent.action.VIEW" />
+ <category android:name="android.intent.category.DEFAULT" />
+ <data android:scheme="net.wigle.wigleandroid.register" />
+ </intent-filter>
+ </activity>
<activity
android:name=".FilterActivity"
android:configChanges="keyboard|keyboardHidden|orientation|locale|screenSize"
@@ -145,6 +145,8 @@
public static final String OBSERVED_URL = "https://api.wigle.net/api/v2/network/mine";
public static final String FILE_POST_URL = "https://api.wigle.net/api/v2/file/upload";
public static final String KML_TRANSID_URL_STEM = "https://api.wigle.net/api/v2/file/kml/";
+ // registration web view
+ public static final String REG_URL = "https://wigle.net/register";
private static final String LOG_TAG = "wigle";
public static final String ENCODING = "ISO-8859-1";
@@ -0,0 +1,61 @@
+package net.wigle.wigleandroid;
+
+import android.content.Context;
+import android.os.Build;
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.webkit.CookieManager;
+import android.webkit.CookieSyncManager;
+import android.webkit.WebChromeClient;
+import android.webkit.WebSettings;
+import android.webkit.WebView;
+
+import net.wigle.wigleandroid.background.WiGLERegistrationInterface;
+
+/**
+ * Created by arkasha on 1/8/18.
+ */
+
+public class RegistrationActivity extends AppCompatActivity {
+
+ public static final String AGENT = "WiGLE WiFi Registrant";
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_registration);
+ WebView regWebView = (WebView) findViewById(R.id.wigle_registration_container);
+ WebSettings webSettings = regWebView.getSettings();
+ webSettings.setJavaScriptEnabled(true);
+ regWebView.clearCache(true);
+ clearCookies(MainActivity.getMainActivity().getApplicationContext());
+ regWebView.setWebChromeClient(new WebChromeClient());
+ regWebView.getSettings().setUserAgentString(AGENT);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ WebView.setWebContentsDebuggingEnabled(true);
+ }
+ regWebView.addJavascriptInterface(new WiGLERegistrationInterface(this),
+ "WiGLEWiFi");
+ regWebView.loadUrl(MainActivity.REG_URL);
+ }
+
+ /**
+ * dammit, android. stolen from:
+ * https://stackoverflow.com/questions/28998241/how-to-clear-cookies-and-cache-of-webview-on-android-when-not-in-webview
+ */
+ @SuppressWarnings("deprecation")
+ protected void clearCookies(Context context) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
+ CookieManager.getInstance().removeAllCookies(null);
+ CookieManager.getInstance().flush();
+ } else {
+ CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
+ cookieSyncMngr.startSync();
+ CookieManager cookieManager=CookieManager.getInstance();
+ cookieManager.removeAllCookie();
+ cookieManager.removeSessionCookie();
+ cookieSyncMngr.stopSync();
+ cookieSyncMngr.sync();
+ }
+ }
+}
@@ -325,7 +325,7 @@ public void onCheckedChanged( final CompoundButton buttonView, final boolean isC
final TextView register = (TextView) view.findViewById(R.id.register);
final String registerString = getString(R.string.register);
final String activateString = getString(R.string.activate);
- String registerBlurb = "<a href='https://wigle.net/register'>" + registerString +
+ String registerBlurb = "<a href='net.wigle.wigleandroid.register://register'>" + registerString +
"</a> @WiGLE.net";
// ALIBI: vision APIs started in 4.2.2; JB2 4.3 = 18 is safe. 17 might work...
@@ -0,0 +1,52 @@
+package net.wigle.wigleandroid.background;
+
+import android.app.Activity;
+import android.content.SharedPreferences;
+import android.webkit.JavascriptInterface;
+
+import net.wigle.wigleandroid.ListFragment;
+import net.wigle.wigleandroid.MainActivity;
+import net.wigle.wigleandroid.TokenAccess;
+
+/**
+ * Created by arkasha on 1/8/18.
+ */
+
+public class WiGLERegistrationInterface {
+ private final Activity activity;
+
+ public WiGLERegistrationInterface(Activity activity) {
+ this.activity = activity;
+ }
+
+
+ /**
+ * update the users authentication preferences from JS running in the page
+ *
+ * @param userName
+ * @param userId
+ * @param token
+ */
+ @JavascriptInterface
+ public void registrationComplete(final String userName, final String userId, final String token) {
+
+ MainActivity.info("Successful registration for "+userName+ " auth ID: "+userId);
+ final SharedPreferences prefs = MainActivity.getMainActivity().
+ getSharedPreferences(ListFragment.SHARED_PREFS, 0);
+ final SharedPreferences.Editor editor = prefs.edit();
+ editor.putString(ListFragment.PREF_USERNAME, userName);
+ editor.putString(ListFragment.PREF_AUTHNAME, userId);
+ editor.putBoolean(ListFragment.PREF_BE_ANONYMOUS, false);
+ editor.apply();
+ TokenAccess.setApiToken(prefs, token);
+ activity.finish();
+ }
+
+ /**
+ * exit webview activity on reg cancel
+ */
+ @JavascriptInterface
+ public void registrationCancelled() {
+ activity.finish();
+ }
+}
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+ <WebView
+ android:id="@+id/wigle_registration_container"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+ </WebView>
+</RelativeLayout>

0 comments on commit bea171c

Please sign in to comment.