platform/android/Rhodes/src/com/rhomobile/rhodes/Rhodes.java in rhodes-1.5.4 vs platform/android/Rhodes/src/com/rhomobile/rhodes/Rhodes.java in rhodes-1.5.5

- old
+ new

@@ -40,10 +40,11 @@ import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.pm.ActivityInfo; import android.content.res.Configuration; +import android.graphics.Bitmap; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Bundle; import android.os.Environment; @@ -57,10 +58,11 @@ import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.ViewGroup.LayoutParams; +import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; import android.os.Process; @@ -76,10 +78,12 @@ public static final int RHO_TOOLBAR_VIEW = 3; public static int WINDOW_FLAGS = WindowManager.LayoutParams.FLAG_FULLSCREEN; public static int WINDOW_MASK = WindowManager.LayoutParams.FLAG_FULLSCREEN; + private static int MAX_PROGRESS = 10000; + private long uiThreadId; public long getUiThreadId() { return uiThreadId; } @@ -94,11 +98,12 @@ private MainView mainView; private SplashScreen splashScreen = null; private Hashtable<String, UriHandler> uriHandlers = new Hashtable<String, UriHandler>(); - + private Boolean contentChanged = null; + private String sdCardError = "Application can not access the SD card while it's mounted. Please unmount the device and stop the adb server before launching the app."; private String rootPath = null; public native void createRhodesApp(String path); @@ -181,42 +186,57 @@ phPath.getParentFile().mkdirs(); makeLink(sdPath.getAbsolutePath(), phPath.getAbsolutePath()); */ } - private void copyFilesFromBundle() { + public boolean isNameChanged() { try { - //String phRootPath = phoneMemoryRootPath(); - String sdRootPath = sdcardRootPath(); - - boolean nameChanged = false; - boolean contentChanged = true; - FileSource as = new AssetsSource(getResources().getAssets()); FileSource fs = new FileSource(); - - nameChanged = !Utils.isContentsEquals(as, "name", fs, new File(sdRootPath, "name").getPath()); - if (nameChanged) - contentChanged = true; - else - contentChanged = !Utils.isContentsEquals(as, "hash", fs, new File(sdRootPath, "hash").getPath()); - - if (contentChanged) { - Logger.D(TAG, "Copying required files from bundle to sdcard"); + return !Utils.isContentsEquals(as, "name", fs, new File(getRootPath(), "name").getPath()); + } + catch (IOException e) { + return true; + } + } + + public boolean isBundleChanged() { + if (contentChanged == null) { + try { + String rp = getRootPath(); - /* - File phrf = new File(phRootPath); - if (!phrf.exists()) - phrf.mkdirs(); - phrf = null; - */ + FileSource as = new AssetsSource(getResources().getAssets()); + FileSource fs = new FileSource(); + + if (isNameChanged()) + contentChanged = new Boolean(true); + else + contentChanged = new Boolean(!Utils.isContentsEquals(as, "hash", fs, new File(rp, "hash").getPath())); + } + catch (IOException e) { + contentChanged = new Boolean(true); + } + } + return contentChanged.booleanValue(); + } + + private void copyFilesFromBundle() { + try { + if (isBundleChanged()) { + Logger.D(TAG, "Copying required files from bundle"); + + boolean nameChanged = isNameChanged(); + + String rp = getRootPath(); + + FileSource as = new AssetsSource(getResources().getAssets()); String items[] = {"apps", "lib", "db", "hash", "name"}; for (int i = 0; i != items.length; ++i) { String item = items[i]; //File phf = new File(phRootPath, item); - File sdf = new File(sdRootPath, item); + File sdf = new File(rp, item); Logger.D(TAG, "Copy '" + item + "' to '" + sdf + "'"); Utils.copyRecursively(as, item, sdf, nameChanged); /* String src = sdf.getAbsolutePath(); String dst = phf.getAbsolutePath(); @@ -228,10 +248,11 @@ File dbfiles = new File(rootPath + "apps/public/db-files"); if (!dbfiles.exists()) dbfiles.mkdirs(); dbfiles = null; + contentChanged = new Boolean(true); Logger.D(TAG, "All files copied"); } else Logger.D(TAG, "No need to copy files to SD card"); } catch (IOException e) { @@ -251,17 +272,10 @@ } public WebView createWebView() { WebView w = new WebView(this); - try { - w.loadUrl("file:///android_asset/apps/app/loading.html"); - } - catch (Exception e) { - // Ignore - } - WebSettings webSettings = w.getSettings(); webSettings.setSavePassword(false); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(false); @@ -272,23 +286,48 @@ w.setVerticalScrollBarEnabled(true); w.setHorizontalScrollBarEnabled(true); w.setWebViewClient(new WebViewClient() { + + private boolean splashHidden = false; @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return handleUrlLoading(url); } @Override + public void onPageStarted(WebView view, String url, Bitmap favicon) { + getWindow().setFeatureInt(Window.FEATURE_PROGRESS, 0); + super.onPageStarted(view, url, favicon); + } + + @Override public void onPageFinished(WebView view, String url) { - if (url.startsWith("http://")) + if (!splashHidden && url.startsWith("http://")) { hideSplashScreen(); + splashHidden = true; + } + getWindow().setFeatureInt(Window.FEATURE_PROGRESS, MAX_PROGRESS); + super.onPageFinished(view, url); } }); + + w.setWebChromeClient(new WebChromeClient() { + @Override + public void onProgressChanged(WebView view, int newProgress) { + newProgress *= 100; + if (newProgress < 0) + newProgress = 0; + if (newProgress > MAX_PROGRESS) + newProgress = MAX_PROGRESS; + getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress); + super.onProgressChanged(view, newProgress); + } + }); return w; } public void setMainView(MainView v) { @@ -353,12 +392,12 @@ catch (Exception e) { Logger.E("Rhodes", "performOnUiThread failed: " + e.getMessage()); } } - private void showSplashScreen(String file) throws IOException { - splashScreen = new SplashScreen(this, file); + private void showSplashScreen() { + splashScreen = new SplashScreen(this, "apps/app/loading.png"); splashScreen.start(outerFrame); } public void hideSplashScreen() { if (splashScreen != null) { @@ -378,11 +417,13 @@ if (!checkSDCard()) { finish(); return; } - uiThreadId = Thread.currentThread().getId(); + Thread ct = Thread.currentThread(); + ct.setPriority(Thread.MAX_PRIORITY); + uiThreadId = ct.getId(); RhodesInstance.setInstance(this); initRootPath(); try { copyFromBundle("apps/rhoconfig.txt"); @@ -410,23 +451,23 @@ outerFrame = new FrameLayout(this); this.setContentView(outerFrame, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); Logger.I("Rhodes", "Loading..."); + showSplashScreen(); + // Increase WebView rendering priority + WebView w = new WebView(this); + WebSettings webSettings = w.getSettings(); + webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); + + // Get screen width/height WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); Display d = wm.getDefaultDisplay(); screenHeight = d.getHeight(); screenWidth = d.getWidth(); - try { - showSplashScreen("apps/app/loading.png"); - } - catch (Exception e) { - setMainView(new SimpleMainView()); - } - // TODO: detect camera availability isCameraAvailable = true; // Register custom uri handlers here UriHandler[] handlers = { @@ -641,9 +682,13 @@ } public void stopSelf() { stopRhodesApp(); Process.killProcess(Process.myPid()); + } + + public static void exit() { + RhodesInstance.getInstance().stopSelf(); } static { // Load native implementation of rhodes System.loadLibrary("rhodes");