include IITC in the app’s resources and load it from there

IITC can only be updated with the app from now on, as the remote-loading
code has been removed. The new injection method intercepts all resources
being requested by the web page and either injects our code or and empty
string. This should stop IITCM from requesting them at all, saving band-
width and reducing the boot up time dramatically.
This commit is contained in:
Stefan Breunig 2013-03-10 17:10:31 +01:00
parent 70211b700c
commit 6f2f1302be
4 changed files with 63 additions and 22 deletions

1
mobile/.gitignore vendored
View File

@ -1,7 +1,6 @@
.classpath .classpath
.project .project
.settings/ .settings/
assets/
bin/ bin/
gen/ gen/
libs/ libs/

1
mobile/assets/iitc.js Symbolic link
View File

@ -0,0 +1 @@
../../iitc-debug.user.js

View File

@ -15,7 +15,7 @@ public class IITC_WebView extends WebView {
private IITC_WebViewClient webclient; private IITC_WebViewClient webclient;
// init web view // init web view
private void iitc_init() { private void iitc_init(Context c) {
settings = this.getSettings(); settings = this.getSettings();
settings.setJavaScriptEnabled(true); settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true); settings.setDomStorageEnabled(true);
@ -31,7 +31,7 @@ public class IITC_WebView extends WebView {
} }
}); });
webclient = new IITC_WebViewClient(); webclient = new IITC_WebViewClient(c);
this.setWebViewClient(webclient); this.setWebViewClient(webclient);
} }
@ -39,19 +39,19 @@ public class IITC_WebView extends WebView {
public IITC_WebView(Context context) { public IITC_WebView(Context context) {
super(context); super(context);
iitc_init(); iitc_init(context);
} }
public IITC_WebView(Context context, AttributeSet attrs) { public IITC_WebView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
iitc_init(); iitc_init(context);
} }
public IITC_WebView(Context context, AttributeSet attrs, int defStyle) { public IITC_WebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
iitc_init(); iitc_init(context);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -1,13 +1,50 @@
package com.cradle.iitc_mobile; package com.cradle.iitc_mobile;
import android.content.Context;
import android.net.http.SslError; import android.net.http.SslError;
import android.util.Log; import android.util.Log;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.SslErrorHandler; import android.webkit.SslErrorHandler;
import android.webkit.WebResourceResponse;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import java.io.InputStream;
import java.io.StringBufferInputStream;
public class IITC_WebViewClient extends WebViewClient { public class IITC_WebViewClient extends WebViewClient {
private static final StringBufferInputStream style = new StringBufferInputStream(
"body, #dashboard_container, #map_canvas { background: #000 !important; }");
private static final StringBufferInputStream empty = new StringBufferInputStream("");
private static WebResourceResponse iitcjs;
public IITC_WebViewClient(Context c) throws java.io.IOException {
loadIITC_JS(c);
}
private static void loadIITC_JS(Context c) throws java.io.IOException {
InputStream input;
input = c.getAssets().open("iitc.js");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
String js = new String(buffer);
// need to wrap the mobile iitc.js version in a document ready. IITC
// expects to be injected after the DOM has been loaded completely.
// Since the mobile client injects IITC by replacing the gen_dashboard
// file, IITC runs to early. The document.ready delays IITC long enough
// so it boots correctly.
js = "$(document).ready(function(){" + js + "});";
iitcjs = new WebResourceResponse(
"text/javascript",
"UTF-8",
new StringBufferInputStream(js)
);
};
// enable https // enable https
@Override @Override
@ -15,23 +52,27 @@ public class IITC_WebViewClient extends WebViewClient {
handler.proceed() ; handler.proceed() ;
}; };
// injecting IITC when page is loaded // Check every external resource if its okay to load it and maybe replace it
// with our own content. This is used to block loading Niantic resources
// which arent required and to inject IITC early into the site.
// via http://stackoverflow.com/a/8274881/1684530
@Override @Override
public void onPageFinished(WebView web, String Url) { public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
Log.d("loading finish", web.getUrl()); if(url.contains("/css/common.css")) {
if (web.getUrl().contains("ingress.com/intel") && !web.getUrl().contains("accounts")) { return new WebResourceResponse("text/css", "UTF-8", style);
// first check for cookies, than inject javascript } else if(url.contains("gen_dashboard.js")) {
// this enables the user to login if necessary return this.iitcjs;
CookieManager cm = CookieManager.getInstance(); } else if(url.contains("/css/ap_icons.css")
final String cookie = cm.getCookie("https://www.ingress.com/intel"); || url.contains("/css/map_icons.css")
if(cookie != null) { || url.contains("/css/misc_icons.css")
web.loadUrl("javascript: (function() { " || url.contains("/css/style_full.css")
+ "var script=document.createElement('script');" || url.contains("/css/style_mobile.css")
+ "script.type='text/javascript';" || url.contains("/css/portalrender.css")
+ "script.src='https://iitcserv.appspot.com/iitc-nightly/iitc-nightly-latest.user.js';" || url.contains("js/analytics.js")
+ "document.getElementsByTagName('head').item(0).appendChild(script);" || url.contains("google-analytics.com/ga.js")) {
+ "})()"); return new WebResourceResponse("text/plain", "UTF-8", empty);
} } else {
return super.shouldInterceptRequest(view, url);
} }
} }
} }