developer options: added possibility to use external script in app

This commit is contained in:
Philipp Schaefer 2013-03-31 18:12:14 +02:00
parent 551f0608fd
commit af4ee26575
5 changed files with 82 additions and 32 deletions

View File

@ -7,9 +7,12 @@
<string name="version">Print Version</string> <string name="version">Print Version</string>
<string name="cache_clear">Clear Cache</string> <string name="cache_clear">Clear Cache</string>
<string name="locate">Get Location</string> <string name="locate">Get Location</string>
<string name="local">local</string>
<string name="build_version">Build Version</string> <string name="build_version">Build Version</string>
<string name="pref_force_desktop">Force desktop mode</string> <string name="pref_force_desktop">Force desktop mode</string>
<string name="pref_force_desktop_sum">Nice for tablets, looks awful on smartphones</string> <string name="pref_force_desktop_sum">Nice for tablets, looks awful on smartphones</string>
<string name="pref_developer_options">Developer options</string>
<string name="pref_select_iitc">IITC source</string>
</resources> </resources>

View File

@ -5,9 +5,21 @@
android:title="@string/pref_force_desktop" android:title="@string/pref_force_desktop"
android:summary="@string/pref_force_desktop_sum" android:summary="@string/pref_force_desktop_sum"
android:defaultValue="false" /> android:defaultValue="false" />
<ListPreference
android:key="pref_build_version" <PreferenceCategory
android:title="@string/build_version" android:key="pref_developer_options"
android:enabled="false" android:title="@string/pref_developer_options">
android:selectable="false" />
<EditTextPreference
android:key="pref_iitc_source"
android:title="@string/pref_select_iitc"
android:summary=""
android:defaultValue="local"/>
<ListPreference
android:key="pref_build_version"
android:title="@string/build_version"
android:enabled="false"
android:selectable="false" />
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@ -7,10 +7,12 @@ import com.cradle.iitc_mobile.R;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.StrictMode;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -21,13 +23,30 @@ public class IITC_Mobile extends Activity {
private IITC_WebView iitc_view; private IITC_WebView iitc_view;
private boolean back_button_pressed = false; private boolean back_button_pressed = false;
private boolean desktop = false; private boolean desktop = false;
private OnSharedPreferenceChangeListener listener;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// TODO build an async task for url.openStream() in IITC_WebViewClient
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
iitc_view = (IITC_WebView) findViewById(R.id.iitc_webview); iitc_view = (IITC_WebView) findViewById(R.id.iitc_webview);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
listener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key == "pref_force_desktop")
desktop = sharedPreferences.getBoolean("pref_force_desktop", false);
// reload intel map
iitc_view.loadUrl(addUrlParam("https://www.ingress.com/intel"));
injectJS();
}
};
sharedPref.registerOnSharedPreferenceChangeListener(listener);
// we do not want to reload our page every time we switch orientations... // we do not want to reload our page every time we switch orientations...
// so restore state if activity was already created // so restore state if activity was already created
if(savedInstanceState != null) { if(savedInstanceState != null) {
@ -56,20 +75,6 @@ public class IITC_Mobile extends Activity {
} }
} }
@Override
protected void onResume() {
super.onResume();
// reload page if, the desktop/mobile pref has changed
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
if (desktop != sharedPref.getBoolean("pref_force_desktop", false)) {
Log.d("pref changed", "force Desktop/Mobile changed...reloading");
desktop = sharedPref.getBoolean("pref_force_desktop", false);
iitc_view.loadUrl(addUrlParam("https://www.ingress.com/intel"));
injectJS();
}
}
// save instance state to avoid reloading on orientation change // save instance state to avoid reloading on orientation change
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {

View File

@ -4,7 +4,10 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle; import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
public class IITC_SettingsFragment extends PreferenceFragment { public class IITC_SettingsFragment extends PreferenceFragment {
@ -27,5 +30,18 @@ public class IITC_SettingsFragment extends PreferenceFragment {
catch (NameNotFoundException e) { catch (NameNotFoundException e) {
} }
pref_build_version.setSummary(version); pref_build_version.setSummary(version);
// set iitc source
EditTextPreference pref_iitc_source = (EditTextPreference) findPreference("pref_iitc_source");
pref_iitc_source.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((CharSequence) newValue);
return true;
}
});
// first init of summary
String pref_iitc_source_sum = (String) pref_iitc_source.getSummary() + pref_iitc_source.getText();
pref_iitc_source.setSummary(pref_iitc_source_sum);
} }
} }

View File

@ -1,7 +1,9 @@
package com.cradle.iitc_mobile; package com.cradle.iitc_mobile;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.net.http.SslError; import android.net.http.SslError;
import android.preference.PreferenceManager;
import android.webkit.SslErrorHandler; import android.webkit.SslErrorHandler;
import android.webkit.WebResourceResponse; import android.webkit.WebResourceResponse;
import android.webkit.WebView; import android.webkit.WebView;
@ -10,6 +12,8 @@ import android.webkit.WebViewClient;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class IITC_WebViewClient extends WebViewClient { public class IITC_WebViewClient extends WebViewClient {
private static final ByteArrayInputStream style = new ByteArrayInputStream( private static final ByteArrayInputStream style = new ByteArrayInputStream(
@ -27,26 +31,36 @@ public class IITC_WebViewClient extends WebViewClient {
} }
public void loadIITC_JS(Context c) throws java.io.IOException { public void loadIITC_JS(Context c) throws java.io.IOException {
// in developer options, you are able to load the script from external source
// if a http address is given, use script from this address. else use the local script
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(c);
String iitc_source = sharedPref.getString("pref_iitc_source", "local");
String js = "";
if (iitc_source.contains("http")) {
URL url = new URL(iitc_source);
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next();
} else {
InputStream input; InputStream input;
input = c.getAssets().open("iitc.js"); input = c.getAssets().open("iitc.js");
int size = input.available(); int size = input.available();
byte[] buffer = new byte[size]; byte[] buffer = new byte[size];
input.read(buffer); input.read(buffer);
input.close(); input.close();
String js = new String(buffer); 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( // need to wrap the mobile iitc.js version in a document ready. IITC
"text/javascript", // expects to be injected after the DOM has been loaded completely.
"UTF-8", // Since the mobile client injects IITC by replacing the gen_dashboard
new ByteArrayInputStream(js.getBytes()) // 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 ByteArrayInputStream(js.getBytes())
);
}; };
// enable https // enable https