Load new urls in new DroidGap activity - not same webview as initial url.

This commit is contained in:
Bryce Curtis 2011-06-29 18:23:20 -05:00
parent fc1bea4947
commit 1e3422ae70
3 changed files with 97 additions and 28 deletions

View File

@ -37,8 +37,11 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name="com.phonegap.DroidGap" android:label="@string/app_name">
<intent-filter>
</intent-filter>
</activity>
</application> </application>
<uses-sdk android:minSdkVersion="2" /> <uses-sdk android:minSdkVersion="2" />
</manifest> </manifest>

View File

@ -12,6 +12,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.phonegap.api.Plugin; import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult; import com.phonegap.api.PluginResult;
import java.util.HashMap;
/** /**
* This class exposes methods in DroidGap that can be called from JavaScript. * This class exposes methods in DroidGap that can be called from JavaScript.
@ -83,8 +84,11 @@ public class App extends Plugin {
public void loadUrl(String url, JSONObject props) throws JSONException { public void loadUrl(String url, JSONObject props) throws JSONException {
System.out.println("App.loadUrl("+url+","+props+")"); System.out.println("App.loadUrl("+url+","+props+")");
int wait = 0; int wait = 0;
boolean usePhoneGap = true;
boolean clearPrev = false;
// If there are properties, then set them on the Activity // If there are properties, then set them on the Activity
HashMap<String, Object> params = new HashMap<String, Object>();
if (props != null) { if (props != null) {
JSONArray keys = props.names(); JSONArray keys = props.names();
for (int i=0; i<keys.length(); i++) { for (int i=0; i<keys.length(); i++) {
@ -92,32 +96,43 @@ public class App extends Plugin {
if (key.equals("wait")) { if (key.equals("wait")) {
wait = props.getInt(key); wait = props.getInt(key);
} }
else if (key.equalsIgnoreCase("usephonegap")) {
usePhoneGap = props.getBoolean(key);
}
else if (key.equalsIgnoreCase("clearprev")) {
clearPrev = props.getBoolean(key);
}
else { else {
Object value = props.get(key); Object value = props.get(key);
if (value == null) { if (value == null) {
} }
else if (value.getClass().equals(String.class)) { else if (value.getClass().equals(String.class)) {
this.ctx.getIntent().putExtra(key, (String)value); params.put(key, (String)value);
} }
else if (value.getClass().equals(Boolean.class)) { else if (value.getClass().equals(Boolean.class)) {
this.ctx.getIntent().putExtra(key, (Boolean)value); params.put(key, (Boolean)value);
} }
else if (value.getClass().equals(Integer.class)) { else if (value.getClass().equals(Integer.class)) {
this.ctx.getIntent().putExtra(key, (Integer)value); params.put(key, (Integer)value);
} }
} }
} }
} }
// If wait property, then delay loading // If wait property, then delay loading
if (wait > 0) { if (wait > 0) {
((DroidGap)this.ctx).loadUrl(url, wait); try {
synchronized(this) {
this.wait(wait);
} }
else { } catch (InterruptedException e) {
((DroidGap)this.ctx).loadUrl(url); e.printStackTrace();
} }
} }
((DroidGap)this.ctx).showWebPage(url, usePhoneGap, clearPrev, params);
}
/** /**
* Cancel loadUrl before it has been loaded. * Cancel loadUrl before it has been loaded.

View File

@ -7,6 +7,8 @@
*/ */
package com.phonegap; package com.phonegap;
import java.util.HashMap;
import java.util.Map.Entry;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -136,7 +138,7 @@ public class DroidGap extends PhonegapActivity {
private String urlFile; private String urlFile;
// The base of the initial URL for our app // The base of the initial URL for our app
private String baseUrl; private String baseUrl = null;
// Plugin to call when activity result is received // Plugin to call when activity result is received
protected Plugin activityResultCallback = null; protected Plugin activityResultCallback = null;
@ -350,12 +352,14 @@ public class DroidGap extends PhonegapActivity {
System.out.println("loadUrl("+url+")"); System.out.println("loadUrl("+url+")");
this.urlFile = this.getUrlFile(url); this.urlFile = this.getUrlFile(url);
this.url = url; this.url = url;
if (this.baseUrl == null) {
int i = url.lastIndexOf('/'); int i = url.lastIndexOf('/');
if (i > 0) { if (i > 0) {
this.baseUrl = url.substring(0, i); this.baseUrl = url.substring(0, i+1);
} }
else { else {
this.baseUrl = this.url; this.baseUrl = this.url + "/";
}
} }
System.out.println("url="+url+" baseUrl="+baseUrl); System.out.println("url="+url+" baseUrl="+baseUrl);
@ -739,6 +743,56 @@ public class DroidGap extends PhonegapActivity {
return url.substring(0, p3); return url.substring(0, p3);
} }
/**
* Display a new browser with the specified URL.
*
* NOTE: If usePhoneGap is set, only trusted PhoneGap URLs should be loaded,
* since any PhoneGap API can be called by the loaded HTML page.
*
* @param url The url to load.
* @param usePhoneGap Load url in PhoneGap webview.
* @return "" if ok, or error message.
*/
public void showWebPage(String url, boolean usePhoneGap, HashMap<String, Object> params) {
try {
Intent intent = null;
if (usePhoneGap) {
intent = new Intent().setClass(this, com.phonegap.DroidGap.class);
intent.putExtra("url", url);
// Add parameters
if (params != null) {
java.util.Set<Entry<String,Object>> s = params.entrySet();
java.util.Iterator<Entry<String,Object>> it = s.iterator();
while(it.hasNext()) {
Entry<String,Object> entry = it.next();
String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
}
else if (value.getClass().equals(String.class)) {
intent.putExtra(key, (String)value);
}
else if (value.getClass().equals(Boolean.class)) {
intent.putExtra(key, (Boolean)value);
}
else if (value.getClass().equals(Integer.class)) {
intent.putExtra(key, (Integer)value);
}
}
}
}
else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
}
this.startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
System.out.println("DroidGap.showWebPage: Error loading url "+url+":"+ e.toString());
}
}
/** /**
* Provides a hook for calling "alert" from javascript. Useful for * Provides a hook for calling "alert" from javascript. Useful for
* debugging your javascript. * debugging your javascript.
@ -826,7 +880,7 @@ public class DroidGap extends PhonegapActivity {
@Override @Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
boolean reqOk = false; boolean reqOk = false;
if (this.ctx.urlFile.equals(this.ctx.getUrlFile(url))) { if (url.indexOf(this.ctx.baseUrl) == 0) {
reqOk = true; reqOk = true;
} }
@ -1069,17 +1123,14 @@ public class DroidGap extends PhonegapActivity {
// All else // All else
else { else {
int i = url.lastIndexOf('/');
String newBaseUrl = url;
if (i > 0) {
newBaseUrl = url.substring(0, i);
}
// If our app or file:, then load into our webview // If our app or file:, then load into our webview
// NOTE: This replaces our app with new URL. When BACK is pressed, // NOTE: This replaces our app with new URL. When BACK is pressed,
// our app is reloaded and restarted. All state is lost. // our app is reloaded and restarted. All state is lost.
if (this.ctx.loadInWebView || url.startsWith("file://") || this.ctx.baseUrl.equals(newBaseUrl)) { if (this.ctx.loadInWebView || url.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0) {
this.ctx.appView.loadUrl(url); HashMap<String, Object> params = new HashMap<String, Object>();
params.put("loadingDialog", "");
params.put("hideLoadingDialogOnPageLoad", true);
this.ctx.showWebPage(url, true, params);
} }
// If not our application, let default viewer handle // If not our application, let default viewer handle