new tile management

This commit is contained in:
Philipp Schaefer 2013-12-21 17:09:34 +01:00
parent 1e0e687b71
commit fa89fc67d8
6 changed files with 144 additions and 13 deletions

View File

@ -9,7 +9,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

View File

@ -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();
}
}
}

View File

@ -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;
}
}
}

View File

@ -118,7 +118,7 @@ public class IITC_WebView extends WebView {
}
});
mIitcWebViewClient = new IITC_WebViewClient(c);
mIitcWebViewClient = new IITC_WebViewClient(mIitc);
setWebViewClient(mIitcWebViewClient);
}

View File

@ -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;
}
}

View File

@ -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<String, Void, Boolean> {
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;
}
}