This commit is contained in:
Jon Atkins 2013-12-21 19:10:24 +00:00
commit 81cb7e60c4
10 changed files with 216 additions and 129 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

@ -97,8 +97,8 @@
<string name="pref_force_desktop_sum">Nice for tablets, looks awful on smartphones</string>
<string name="pref_force_https">Force https</string>
<string name="pref_force_https_sum">Disabling may improve performance</string>
<string name="pref_external_storage">Move cache to external storage</string>
<string name="pref_external_storage_sum">Restart required! Write cache to sdCard. Saves internal storage.
<string name="pref_external_storage">Move tiles cache to external storage</string>
<string name="pref_external_storage_sum">Restart required! Write cache to sdCard.
External storage has to be mounted.</string>
<string name="pref_press_twice_to_exit">Press back button twice to exit</string>
<string name="pref_press_twice_to_exit_sum">Avoids accidental exits</string>
@ -116,10 +116,6 @@
<string name="pref_select_iitc">IITC source</string>
<string name="pref_select_iitc_sum">Load IITC main script from url or use local script. Currently used source:</string>
<string name="pref_caching">Aggressive Caching</string>
<string name="pref_caching_sum">Prefer cached values even if they are expired…saves traffic but
may result in login issues</string>
<string-array name="pref_hide_fullscreen">
<item>System Bar</item>
<item>Action Bar</item>
@ -133,17 +129,6 @@
<item>16</item>
</string-array>
<string-array name="pref_caching_array">
<item>Always</item>
<item>Mobile data only (default)</item>
<item>Never</item>
</string-array>
<string-array name="pref_caching_array_vals">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string name="menu_clear_cookies">Clear Cookies</string>
<string name="menu_search">Search</string>
<string name="choose_account_to_login">Choose account to login</string>

View File

@ -112,14 +112,6 @@
<PreferenceCategory
android:key="pref_advanced_tweaks_cat"
android:title="@string/pref_tweaks_cat">
<ListPreference
android:defaultValue="1"
android:entries="@array/pref_caching_array"
android:entryValues="@array/pref_caching_array_vals"
android:key="pref_caching"
android:summary="@string/pref_caching_sum"
android:title="@string/pref_caching"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_fake_user_agent"

View File

@ -13,11 +13,11 @@ import java.io.File;
*/
public class IITC_Application extends Application {
@Override
public File getCacheDir() {
public File getFilesDir() {
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_external_storage", false)) {
return (getExternalCacheDir() != null) ? getExternalCacheDir() : super.getCacheDir();
return (getExternalFilesDir(null) != null) ? getExternalFilesDir(null) : super.getFilesDir();
} else {
return super.getCacheDir();
return super.getFilesDir();
}
}
}

View File

@ -13,7 +13,6 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
@ -128,8 +127,6 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
return;
} else if (key.equals("pref_fake_user_agent")) {
mIitcWebView.setUserAgent();
} else if (key.equals("pref_caching")) {
mIitcWebView.updateCaching(false);
} else if (key.equals("pref_press_twice_to_exit")
|| key.equals("pref_share_selected_tab")
|| key.equals("pref_messages")
@ -252,8 +249,6 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
// enough idle...let's do some work
Log.d("iitcm", "resuming...reset idleTimer");
mIitcWebView.updateCaching(false);
mUserLocation.onStart();
if (mReloadNeeded) {
@ -267,6 +262,20 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
}
}
@Override
protected void onResume() {
mIitcWebView.resumeTimers();
mIitcWebView.onResume();
super.onResume();
}
@Override
protected void onPause() {
mIitcWebView.pauseTimers();
mIitcWebView.onPause();
super.onPause();
}
@Override
protected void onStop() {
Log.d("iitcm", "stopping iitcm");

View File

@ -0,0 +1,42 @@
package com.cradle.iitc_mobile;
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.InputStream;
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 {
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()) {
// asynchronously download tile if outdated, ignore date if not connected to wifi
if (mIitc.getWebView().isConnectedToWifi()) new DownloadTile(path, fileName).execute(url);
// return tile from storage
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

@ -0,0 +1,51 @@
package com.cradle.iitc_mobile;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
/**
* Created by cradle on 12/21/13.
*/
public class IITC_WebChromeClient extends WebChromeClient {
private IITC_Mobile mIitcm;
public IITC_WebChromeClient(IITC_Mobile iitcm) {
mIitcm = iitcm;
}
/**
* our webchromeclient should share geolocation with the iitc script
*
* allow access by default
*/
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
/**
* display progress bar in activity
*/
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
// maximum for newProgress is 100
// maximum for setProgress is 10,000
mIitcm.setProgress(newProgress * 100);
}
/**
* remove splash screen if any JS error occurs
*/
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
if (consoleMessage.messageLevel() == ConsoleMessage.MessageLevel.ERROR) {
mIitcm.setLoadingState(false);
}
return super.onConsoleMessage(consoleMessage);
}
}

View File

@ -37,6 +37,7 @@ public class IITC_WebView extends WebView {
private WebSettings mSettings;
private IITC_WebViewClient mIitcWebViewClient;
private IITC_WebChromeClient mIitcWebChromeClient;
private IITC_JSInterface mJsInterface;
private IITC_Mobile mIitc;
private SharedPreferences mSharedPrefs;
@ -56,9 +57,7 @@ public class IITC_WebView extends WebView {
mSettings.setDomStorageEnabled(true);
mSettings.setAllowFileAccess(true);
mSettings.setGeolocationEnabled(true);
mSettings.setAppCacheEnabled(true);
mSettings.setDatabasePath(getContext().getApplicationInfo().dataDir + "/databases/");
mSettings.setAppCachePath(getContext().getCacheDir().getAbsolutePath());
mJsInterface = new IITC_JSInterface(mIitc);
addJavascriptInterface(mJsInterface, "android");
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(mIitc);
@ -82,43 +81,9 @@ public class IITC_WebView extends WebView {
}
};
setWebChromeClient(new WebChromeClient() {
/**
* our webchromeclient should share geolocation with the iitc script
*
* allow access by default
*/
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
/**
* display progress bar in activity
*/
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
// maximum for newProgress is 100
// maximum for setProgress is 10,000
((Activity) getContext()).setProgress(newProgress * 100);
}
/**
* remove splash screen if any JS error occurs
*/
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
if (consoleMessage.messageLevel() == ConsoleMessage.MessageLevel.ERROR) {
mIitc.setLoadingState(false);
}
return super.onConsoleMessage(consoleMessage);
}
});
mIitcWebViewClient = new IITC_WebViewClient(c);
mIitcWebChromeClient = new IITC_WebChromeClient(mIitc);
setWebChromeClient(mIitcWebChromeClient);
mIitcWebViewClient = new IITC_WebViewClient(mIitc);
setWebViewClient(mIitcWebViewClient);
}
@ -271,36 +236,8 @@ public class IITC_WebView extends WebView {
return mJsInterface;
}
public void updateCaching(boolean login) {
switch (Integer.parseInt(mSharedPrefs.getString("pref_caching", "1"))) {
case 0:
mSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
break;
case 2:
mSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
break;
default:
if (getUrl() != null) {
login |= getUrl().contains("accounts.google.com");
}
// use cache if on mobile network...saves traffic
if (!isConnectedToWifi() && !login) {
Log.d("iitcm", "not connected to wifi...load tiles from cache");
mSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
} else {
if (login) {
Log.d("iitcm", "login...load tiles from network");
} else {
Log.d("iitcm", "connected to wifi...load tiles from network");
}
mSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
}
break;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private boolean isConnectedToWifi() {
public boolean isConnectedToWifi() {
ConnectivityManager conMan = (ConnectivityManager) getContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

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);
@ -193,8 +195,6 @@ public class IITC_WebViewClient extends WebViewClient {
@Override
public void onReceivedLoginRequest(WebView view, String realm, String account, String args) {
Log.d("iitcm", "Login requested: " + realm + " " + account + " " + args);
Log.d("iitcm", "logging in...updating caching mode");
((IITC_WebView) view).updateCaching(true);
mIitcInjected = false;
//((IITC_Mobile) mContext).onReceivedLoginRequest(this, view, realm, account, args);
}
@ -202,7 +202,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 +261,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 +285,20 @@ public class IITC_WebViewClient extends WebViewClient {
@Override
public WebResourceResponse shouldInterceptRequest(final WebView view,
String url) {
if (url.contains("/css/common.css")) {
// if any tiles are requested, handle it with IITC_TileManager
if (url.matches(".*tile.*jpg.*") // mapquest tiles | ovi tiles
|| url.matches(".*tile.*png.*") // cloudmade tiles
|| url.matches(".*mts.*googleapis.*smartmaps") // google tiles
|| url.matches(".*tile.*jpeg.*") // bing tiles
|| url.matches(".*maps.*yandex.*tiles.*") // yandex maps
) {
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,11 +331,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);
}
if (url.contains("logout")) {
Log.d("iitcm", "logging out...updating caching mode");
((IITC_WebView) view).updateCaching(true);
mIitc.loadUrl(url);
}
return false;
} else {
@ -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,62 @@
package com.cradle.iitc_mobile.async;
import android.os.AsyncTask;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
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();
File file = new File(mFilePath, mFileName);
// update tile if needed, else return
if (conn.getLastModified() < file.lastModified()) return true;
InputStream is = null;
is = conn.getInputStream();
Log.d("iitcm", "writing to file: " + file.toString());
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;
}
}