Ejemplo n.º 1
0
  private void copyResourcesToLocal() {
    String name, sFileName;
    InputStream content;

    R.raw a = new R.raw();
    java.lang.reflect.Field[] t = R.raw.class.getFields();
    Resources resources = getResources();

    boolean succeed = true;

    for (int i = 0; i < t.length; i++) {
      try {
        name = resources.getText(t[i].getInt(a)).toString();
        sFileName = name.substring(name.lastIndexOf('/') + 1, name.length());
        content = getResources().openRawResource(t[i].getInt(a));
        content.reset();

        // perl project
        if (sFileName.endsWith(GlobalConstants.PERL_PROJECT_ZIP_NAME)) {
          succeed &= Utils.unzip(content, this.getFilesDir().getAbsolutePath() + "/", true);
        }
        // perl -> /data/data/com.android.perl/files/perl
        else if (sFileName.endsWith(GlobalConstants.PERL_ZIP_NAME)) {
          succeed &= Utils.unzip(content, this.getFilesDir().getAbsolutePath() + "/", true);
          FileUtils.chmod(new File(this.getFilesDir().getAbsolutePath() + "/perl/perl"), 0755);
        }
        // perl extras -> /sdcard/com.android.perl/perl/site-perl
        else if (sFileName.endsWith(GlobalConstants.PERL_EXTRAS_ZIP_NAME)) {
          Utils.createDirectoryOnExternalStorage(this.getPackageName() + "/" + "extras");
          Utils.createDirectoryOnExternalStorage(
              this.getPackageName() + "/" + "extras" + "/" + "tmp");
          succeed &=
              Utils.unzip(
                  content,
                  Environment.getExternalStorageDirectory().getAbsolutePath()
                      + "/"
                      + this.getPackageName()
                      + "/extras/",
                  true);
        }

      } catch (Exception e) {
        Log.e(GlobalConstants.LOG_TAG, "Failed to copyResourcesToLocal", e);
        succeed = false;
      }
    } // end for all files in res/raw
  }
  // Unpack and set file permission for python files located in res/raw based
  // off of Anthony Prieur & Daniel Oppenheim work
  // https://code.google.com/p/android-python27/
  private void copyPythonToLocal() {
    String zipPath, zipName;
    InputStream content;
    R.raw a = new R.raw();
    java.lang.reflect.Field[] t = R.raw.class.getFields();
    Resources resources = getResources();
    Log.i(Common.LOG_TAG, Common.LOG_INFO_PYTHON_UNZIP_STARTED);
    for (int i = 0; i < t.length; i++) {
      try {
        // Get path of zip to unpack
        zipPath = resources.getText(t[i].getInt(a)).toString();
        // Extract the zipName from zipPath
        zipName = zipPath.substring(zipPath.lastIndexOf('/') + 1, zipPath.length());
        content = getResources().openRawResource(t[i].getInt(a));
        content.reset();

        // Python_27 we unpack to ->
        // /data/data/com.sensibility_testbed/files/python
        if (zipName.endsWith(Common.PYTHON_ZIP_NAME)) {
          // This is the Python binary. It needs to live in /data/data/....,
          // as it can't be made executable on the SDcard due to VFAT.
          // Thus, we must not use seattleInstallDirectory for this.
          Utils.unzip(content, this.getFilesDir().getAbsolutePath() + "/", true);
          // set file permissions
          FileUtils.chmod(
              new File(this.getFilesDir().getAbsolutePath() + "/python/bin/python"), 0755);
        }
        // Python_extras_27 we unpack to ->
        // /sdcard/com.sensibility_testbed/extras/python
        else if (zipName.endsWith(Common.PYTHON_EXTRAS_ZIP_NAME)) {
          Utils.unzip(content, seattleInstallDirectory.getAbsolutePath() + "/extras/", true);
        }
      } catch (Exception e) {
        Log.e(Common.LOG_TAG, Common.LOG_EXCEPTION_PYTHON_UNZIPPING, e);
      }
    }
    Log.i(Common.LOG_TAG, Common.LOG_INFO_PYTHON_UNZIP_COMPLETED);
  }
  @Override
  protected void onStart() {
    super.onStart();

    try {
      jsonSrc = FileUtils.readFromAssetsFile(that, "json2.js");
      androidJsSrc = FileUtils.readFromAssetsFile(that, "android.js");
    } catch (IOException e1) {
      throw new RuntimeException(e1);
    }

    mObserver = new HtmlEventObserver();

    // TODO
    // RpcReceiverManager mFacadeManager = getRpcReceiverManager();
    // mFacadeManager.getReceiver(EventFacade.class).addGlobalEventObserver(mObserver);
    // mAPIWrapperSource = generateAPIWrapper();

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, config.getLOG_TAG());
    mWebView.setKeepScreenOn(true);

    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.setScrollbarFadingEnabled(false);
    mWebView.setVerticalScrollBarEnabled(false);
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.setScrollContainer(false);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setPluginsEnabled(true);

    webSettings.setSupportZoom(false);
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);

    mWebView.addJavascriptInterface(mWrapper, "_rpc_wrapper");
    mWebView.addJavascriptInterface(
        new Object() {

          @SuppressWarnings("unused")
          public void register(String event, int id) {
            mObserver.register(event, id);
          }
        },
        "_callback_wrapper");

    if (Build.VERSION.SDK_INT >= 5) {
      try {
        Method m1 = WebSettings.class.getMethod("setDomStorageEnabled", new Class[] {Boolean.TYPE});
        m1.invoke(webSettings, Boolean.TRUE);

        Method m2 = WebSettings.class.getMethod("setDatabaseEnabled", new Class[] {Boolean.TYPE});
        m2.invoke(webSettings, Boolean.TRUE);

        Method m3 = WebSettings.class.getMethod("setDatabasePath", new Class[] {String.class});
        m3.invoke(webSettings, this.getFilesDir().getParent() + "/databases/");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    mWebView.setWebChromeClient(new WebChromeClient() {});

    mWebView.setWebViewClient(
        new WebViewClient() {
          //        	public void onReceivedError(WebView view, int errorCode, String description,
          // String failingUrl) {
          //        		Toast.makeText(that, "Error: " + description, Toast.LENGTH_SHORT).show();
          //        	}
        });

    webSettings.setCacheMode(WebSettings.LOAD_NORMAL);

    if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 14) {
      mWebView.setSystemUiVisibility(WebView.STATUS_BAR_HIDDEN);
    } else if (Build.VERSION.SDK_INT >= 14) {
      mWebView.setSystemUiVisibility(WebView.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

    mWebView.loadUrl("javascript:" + jsonSrc);
    mWebView.loadUrl("javascript:" + androidJsSrc);
    mWebView.loadUrl("javascript:" + mAPIWrapperSource);
    Log.d(
        config.getLOG_TAG(),
        "Launch Webview: "
            + "file://"
            + this.getFilesDir().getAbsolutePath()
            + "/"
            + config.getMAIN_SCRIPT_NAME());
    mWebView.loadUrl(
        "file://" + this.getFilesDir().getAbsolutePath() + "/" + config.getMAIN_SCRIPT_NAME());
  }