Some user-agent manipulation.
package com.github.pockethub.android.accounts;
+import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
+import android.os.Build;
import android.os.Bundle;
+import android.support.v4.content.IntentCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
+import android.webkit.WebResourceRequest;
import android.webkit.WebViewClient;
import com.afollestad.materialdialogs.MaterialDialog;
@@ -34,11 +38,18 @@ public class LoginWebViewActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
- webView.loadUrl(getIntent().getStringExtra(LoginActivity.INTENT_EXTRA_URL));
// Needs the be activated to allow GitHub to perform their requests.
webView.getSettings().setJavaScriptEnabled(true);
+ String userAgent = webView.getSettings().getUserAgentString();
+ // Remove chrome from the user agent since GitHub checks it incorrectly
+ userAgent = userAgent.replaceAll("Chrome/\\d{2}\\.\\d\\.\\d\\.\\d", "");
+ webView.getSettings().setUserAgentString(userAgent);
+
+ String url = getIntent().getStringExtra(LoginActivity.INTENT_EXTRA_URL);
+ webView.loadUrl(url);
+
webView.setWebViewClient(new WebViewClient() {
MaterialDialog dialog = new MaterialDialog.Builder(LoginWebViewActivity.this)
.content(R.string.loading)
@@ -58,6 +69,19 @@ public class LoginWebViewActivity extends AppCompatActivity {
@Override
public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
Uri uri = Uri.parse(url);
+ return overrideOAuth(uri) || super.shouldOverrideUrlLoading(view, url);
+ }
+
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+ @Override
+ public boolean shouldOverrideUrlLoading(android.webkit.WebView view,
+ WebResourceRequest request) {
+
+ return overrideOAuth(request.getUrl())
+ || super.shouldOverrideUrlLoading(view, request);
+ }
+
+ private boolean overrideOAuth(Uri uri) {
if (uri.getScheme().equals(getString(R.string.github_oauth_scheme))) {
Intent data = new Intent();
data.setData(uri);
@@ -65,7 +89,8 @@ public class LoginWebViewActivity extends AppCompatActivity {
finish();
return true;
}
- return super.shouldOverrideUrlLoading(view, url);
+
+ return false;
}
});
