From fa89fc67d85f94a7e7c5c79e4b9e7223621f1dac Mon Sep 17 00:00:00 2001 From: Philipp Schaefer Date: Sat, 21 Dec 2013 17:09:34 +0100 Subject: [PATCH] new tile management --- mobile/IITC_Mobile.iml | 2 +- .../cradle/iitc_mobile/IITC_Application.java | 9 +++ .../cradle/iitc_mobile/IITC_TileManager.java | 44 ++++++++++++ .../com/cradle/iitc_mobile/IITC_WebView.java | 2 +- .../iitc_mobile/IITC_WebViewClient.java | 31 ++++++--- .../iitc_mobile/async/DownloadTile.java | 69 +++++++++++++++++++ 6 files changed, 144 insertions(+), 13 deletions(-) create mode 100644 mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java create mode 100644 mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java diff --git a/mobile/IITC_Mobile.iml b/mobile/IITC_Mobile.iml index 7647439e..66d0a847 100644 --- a/mobile/IITC_Mobile.iml +++ b/mobile/IITC_Mobile.iml @@ -9,7 +9,7 @@ - + diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Application.java b/mobile/src/com/cradle/iitc_mobile/IITC_Application.java index 23327f58..1f203448 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Application.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Application.java @@ -20,4 +20,13 @@ public class IITC_Application extends Application { return super.getCacheDir(); } } + + @Override + public File getFilesDir() { + if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_external_storage", false)) { + return (getExternalFilesDir(null) != null) ? getExternalFilesDir(null) : super.getFilesDir(); + } else { + return super.getFilesDir(); + } + } } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java b/mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java new file mode 100644 index 00000000..7acf1ee3 --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java @@ -0,0 +1,44 @@ +package com.cradle.iitc_mobile; + +import android.util.Log; +import android.webkit.WebResourceResponse; + +import com.cradle.iitc_mobile.async.DownloadTile; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +public class IITC_TileManager { + + private final IITC_Mobile mIitc; + private static final String TYPE = "image/*"; + private static final String ENCODING = null; + + IITC_TileManager(IITC_Mobile iitc) { + mIitc = iitc; + } + + public WebResourceResponse getTile(String url) throws Exception { + Log.d("iitcm", "checking for tile: " + url); + String path = mIitc.getApplication().getFilesDir().toString() + "/" + url; + path = path.replace("http://", ""); + path = path.replace("https://", ""); + String[] split = path.split("/"); + String fileName = split[split.length - 1]; + path = path.replace(fileName, ""); + File file = new File(path, fileName); + if (file.exists()) { + InputStream in = new BufferedInputStream(new FileInputStream(file)); + return new WebResourceResponse(TYPE, ENCODING, in); + } else { + // asynchronously download tile to cache and let webviewclient load the resource + new DownloadTile(path, fileName).execute(url); + return null; + } + } +} diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java index 206513d6..5503db5c 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java @@ -118,7 +118,7 @@ public class IITC_WebView extends WebView { } }); - mIitcWebViewClient = new IITC_WebViewClient(c); + mIitcWebViewClient = new IITC_WebViewClient(mIitc); setWebViewClient(mIitcWebViewClient); } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java index f1e8af47..d28f080b 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java @@ -43,10 +43,12 @@ public class IITC_WebViewClient extends WebViewClient { private String mIitcScript = null; private String mIitcPath = null; private boolean mIitcInjected = false; - private final Context mContext; + private final IITC_Mobile mIitc; + private final IITC_TileManager mTileManager; - public IITC_WebViewClient(Context c) { - this.mContext = c; + public IITC_WebViewClient(IITC_Mobile iitc) { + this.mIitc = iitc; + this.mTileManager = new IITC_TileManager(mIitc); this.mIitcPath = Environment.getExternalStorageDirectory().getPath() + "/IITC_Mobile/"; } @@ -113,13 +115,13 @@ public class IITC_WebViewClient extends WebViewClient { js = this.fileToString(mIitcPath + "dev/total-conversion-build.user.js", false); if (js.equals("false")) { - Toast.makeText(mContext, "File " + mIitcPath + + Toast.makeText(mIitc, "File " + mIitcPath + "dev/total-conversion-build.user.js not found. " + "Disable developer mode or add iitc files to the dev folder.", Toast.LENGTH_LONG).show(); return; } else { - Toast.makeText(mContext, "Developer mode enabled", + Toast.makeText(mIitc, "Developer mode enabled", Toast.LENGTH_SHORT).show(); } } else { @@ -145,7 +147,7 @@ public class IITC_WebViewClient extends WebViewClient { mIitcInjected = false; } - PackageManager pm = mContext.getPackageManager(); + PackageManager pm = mIitc.getPackageManager(); boolean hasMultitouch = pm .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH); boolean forcedZoom = sharedPref.getBoolean("pref_user_zoom", false); @@ -202,7 +204,7 @@ public class IITC_WebViewClient extends WebViewClient { public void loadPlugins(WebView view) { // get the plugin preferences SharedPreferences sharedPref = PreferenceManager - .getDefaultSharedPreferences(mContext); + .getDefaultSharedPreferences(mIitc); boolean dev_enabled = sharedPref.getBoolean("pref_dev_checkbox", false); String path = (dev_enabled) ? mIitcPath + "dev/plugins/" : "plugins/"; @@ -261,7 +263,7 @@ public class IITC_WebViewClient extends WebViewClient { } } else { // load plugins from asset folder - AssetManager am = mContext.getAssets(); + AssetManager am = mIitc.getAssets(); try { s = new Scanner(am.open(file)).useDelimiter("\\A"); } catch (IOException e) { @@ -285,7 +287,14 @@ public class IITC_WebViewClient extends WebViewClient { @Override public WebResourceResponse shouldInterceptRequest(final WebView view, String url) { - if (url.contains("/css/common.css")) { + if (url.matches(".*tile.*jpg") || url.matches(".*tile.*png") || url.matches(".*mts.*googleapis.*")) { + try { + return mTileManager.getTile(url); + } catch (Exception e) { + e.printStackTrace(); + return super.shouldInterceptRequest(view, url); + } + } else if (url.contains("/css/common.css")) { return new WebResourceResponse("text/css", "UTF-8", STYLE); // } else if (url.contains("gen_dashboard.js")) { // // define initialize function to get rid of JS ReferenceError on intel page's 'onLoad' @@ -318,7 +327,7 @@ public class IITC_WebViewClient extends WebViewClient { Log.d("iitcm", "should be an internal clicked position link...reload script for: " + url); - ((IITC_Mobile) mContext).loadUrl(url); + mIitc.loadUrl(url); } if (url.contains("logout")) { Log.d("iitcm", "logging out...updating caching mode"); @@ -330,7 +339,7 @@ public class IITC_WebViewClient extends WebViewClient { "no ingress intel link, start external app to load url: " + url); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); - mContext.startActivity(intent); + mIitc.startActivity(intent); return true; } } diff --git a/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java b/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java new file mode 100644 index 00000000..49e6fbb0 --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java @@ -0,0 +1,69 @@ +package com.cradle.iitc_mobile.async; + +import android.app.AlertDialog; +import android.os.AsyncTask; +import android.util.Log; + +import com.cradle.iitc_mobile.IITC_Mobile; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +/* + * this class parses the http response of a web page. + * since network operations shouldn't be done on main UI thread + * (NetworkOnMainThread exception is thrown) we use an async task for this. + */ +public class DownloadTile extends AsyncTask { + + private String mFilePath; + private String mFileName; + + public DownloadTile(String path, String fileName) { + mFilePath = path; + mFileName = fileName; + + } + + @Override + protected Boolean doInBackground(String... urls) { + URL tileUrl = null; + URLConnection conn = null; + try { + tileUrl = new URL(urls[0]); + conn = tileUrl.openConnection(); + InputStream is = null; + is = conn.getInputStream(); + Log.d("iitcm", "writing to path: " + mFilePath); + File file = new File(mFilePath, mFileName); + File output = writeTileToFile(is, file, mFilePath); + } catch (IOException e) { + e.printStackTrace(); + return false; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + private File writeTileToFile(InputStream inStream, File file, String path) throws Exception { + File filePath = new File(path); + filePath.mkdirs(); + FileOutputStream outStream = new FileOutputStream(file); + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + int len = 0; + while ((len = inStream.read(buffer)) != -1) { + outStream.write(buffer, 0, len); + } + if(outStream!=null) outStream.close(); + return file; + } + +}