protected void unpackComponents() throws IOException, FileNotFoundException {
    File applicationPackage = new File(getApplication().getPackageResourcePath());
    File componentsDir = new File(sGREDir, "components");
    if (componentsDir.lastModified() == applicationPackage.lastModified()) return;

    componentsDir.mkdir();
    componentsDir.setLastModified(applicationPackage.lastModified());

    GeckoAppShell.killAnyZombies();

    ZipFile zip = new ZipFile(applicationPackage);

    byte[] buf = new byte[32768];
    try {
      if (unpackFile(zip, buf, null, "removed-files")) removeFiles();
    } catch (Exception ex) {
      // This file may not be there, so just log any errors and move on
      Log.w(LOG_FILE_NAME, "error removing files", ex);
    }

    // copy any .xpi file into an extensions/ directory
    Enumeration<? extends ZipEntry> zipEntries = zip.entries();
    while (zipEntries.hasMoreElements()) {
      ZipEntry entry = zipEntries.nextElement();
      if (entry.getName().startsWith("extensions/") && entry.getName().endsWith(".xpi")) {
        Log.i("GeckoAppJava", "installing extension : " + entry.getName());
        unpackFile(zip, buf, entry, entry.getName());
      }
    }
  }
 public void doRestart() {
   try {
     String action = "org.mozilla.gecko.restart";
     Intent intent = new Intent(action);
     intent.setClassName(getPackageName(), getPackageName() + ".Restarter");
     addEnvToIntent(intent);
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
     Log.i(LOG_FILE_NAME, intent.toString());
     GeckoAppShell.killAnyZombies();
     startActivity(intent);
   } catch (Exception e) {
     Log.i(LOG_FILE_NAME, "error doing restart", e);
   }
   finish();
   // Give the restart process time to start before we die
   GeckoAppShell.waitForAnotherGeckoProc();
 }
  @Override
  public void onStop() {
    Log.i(LOG_FILE_NAME, "stop");
    // We're about to be stopped, potentially in preparation for
    // being destroyed.  We're killable after this point -- as I
    // understand it, in extreme cases the process can be terminated
    // without going through onDestroy.
    //
    // We might also get an onRestart after this; not sure what
    // that would mean for Gecko if we were to kill it here.
    // Instead, what we should do here is save prefs, session,
    // etc., and generally mark the profile as 'clean', and then
    // dirty it again if we get an onResume.

    GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_STOPPING));
    super.onStop();
    GeckoAppShell.putChildInBackground();
  }
  @Override
  protected void onNewIntent(Intent intent) {
    if (checkLaunchState(LaunchState.GeckoExiting)) {
      // We're exiting and shouldn't try to do anything else just incase
      // we're hung for some reason we'll force the process to exit
      System.exit(0);
      return;
    }
    final String action = intent.getAction();
    if (ACTION_DEBUG.equals(action)
        && checkAndSetLaunchState(LaunchState.Launching, LaunchState.WaitForDebugger)) {

      mMainHandler.postDelayed(
          new Runnable() {
            public void run() {
              Log.i(LOG_FILE_NAME, "Launching from debug intent after 5s wait");
              setLaunchState(LaunchState.Launching);
              launch(null);
            }
          },
          1000 * 5 /* 5 seconds */);
      Log.i(LOG_FILE_NAME, "Intent : ACTION_DEBUG - waiting 5s before launching");
      return;
    }
    if (checkLaunchState(LaunchState.WaitForDebugger) || launch(intent)) return;

    if (Intent.ACTION_MAIN.equals(action)) {
      Log.i(LOG_FILE_NAME, "Intent : ACTION_MAIN");
      GeckoAppShell.sendEventToGecko(new GeckoEvent(""));
    } else if (Intent.ACTION_VIEW.equals(action)) {
      String uri = intent.getDataString();
      GeckoAppShell.sendEventToGecko(new GeckoEvent(uri));
      Log.i(LOG_FILE_NAME, "onNewIntent: " + uri);
    } else if (ACTION_WEBAPP.equals(action)) {
      String uri = intent.getStringExtra("args");
      GeckoAppShell.sendEventToGecko(new GeckoEvent(uri));
      Log.i(LOG_FILE_NAME, "Intent : WEBAPP - " + uri);
    } else if (ACTION_BOOKMARK.equals(action)) {
      String args = intent.getStringExtra("args");
      GeckoAppShell.sendEventToGecko(new GeckoEvent(args));
      Log.i(LOG_FILE_NAME, "Intent : BOOKMARK - " + args);
    }
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String filePickerResult = "";
    if (data != null && resultCode == RESULT_OK) {
      try {
        ContentResolver cr = getContentResolver();
        Uri uri = data.getData();
        Cursor cursor =
            GeckoApp.mAppContext
                .getContentResolver()
                .query(uri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null);
        String name = null;
        if (cursor != null) {
          try {
            if (cursor.moveToNext()) {
              name = cursor.getString(0);
            }
          } finally {
            cursor.close();
          }
        }
        String fileName = "tmp_";
        String fileExt = null;
        int period;
        if (name == null || (period = name.lastIndexOf('.')) == -1) {
          String mimeType = cr.getType(uri);
          fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType);
        } else {
          fileExt = name.substring(period);
          fileName = name.substring(0, period);
        }
        File file = File.createTempFile(fileName, fileExt, sGREDir);

        FileOutputStream fos = new FileOutputStream(file);
        InputStream is = cr.openInputStream(uri);
        byte[] buf = new byte[4096];
        int len = is.read(buf);
        while (len != -1) {
          fos.write(buf, 0, len);
          len = is.read(buf);
        }
        fos.close();
        filePickerResult = file.getAbsolutePath();
      } catch (Exception e) {
        Log.e(LOG_FILE_NAME, "showing file picker", e);
      }
    }
    try {
      mFilePickerResult.put(filePickerResult);
    } catch (InterruptedException e) {
      Log.i(LOG_FILE_NAME, "error returning file picker result", e);
    }
  }
  @Override
  public void onResume() {
    Log.i(LOG_FILE_NAME, "resume");
    if (checkLaunchState(LaunchState.GeckoRunning)) GeckoAppShell.onResume();
    // After an onPause, the activity is back in the foreground.
    // Undo whatever we did in onPause.
    super.onResume();

    // Just in case. Normally we start in onNewIntent
    if (checkLaunchState(LaunchState.PreLaunch) || checkLaunchState(LaunchState.Launching))
      onNewIntent(getIntent());

    registerReceiver(mConnectivityReceiver, mConnectivityFilter);
    GeckoNetworkManager.getInstance().start();
    GeckoScreenOrientationListener.getInstance().start();
  }
  @Override
  public void onPause() {
    Log.i(LOG_FILE_NAME, "pause");
    GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_PAUSING));
    // The user is navigating away from this activity, but nothing
    // has come to the foreground yet; for Gecko, we may want to
    // stop repainting, for example.

    // Whatever we do here should be fast, because we're blocking
    // the next activity from showing up until we finish.

    // onPause will be followed by either onResume or onStop.
    super.onPause();

    unregisterReceiver(mConnectivityReceiver);
    GeckoNetworkManager.getInstance().stop();
    GeckoScreenOrientationListener.getInstance().stop();
  }
  public String showFilePicker(String aMimeType) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType(aMimeType);
    GeckoApp.this.startActivityForResult(
        Intent.createChooser(intent, getString(R.string.choose_file)), FILE_PICKER_REQUEST);
    String filePickerResult = "";

    try {
      while (null == (filePickerResult = mFilePickerResult.poll(1, TimeUnit.MILLISECONDS))) {
        Log.i("GeckoApp", "processing events from showFilePicker ");
        GeckoAppShell.processNextNativeEvent();
      }
    } catch (InterruptedException e) {
      Log.i(LOG_FILE_NAME, "showing file picker ", e);
    }

    return filePickerResult;
  }
  @Override
  public void onDestroy() {
    Log.i(LOG_FILE_NAME, "destroy");

    // Tell Gecko to shutting down; we'll end up calling System.exit()
    // in onXreExit.
    if (isFinishing()) GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_SHUTDOWN));

    if (SmsManager.getInstance() != null) {
      SmsManager.getInstance().stop();
      if (isFinishing()) SmsManager.getInstance().shutdown();
    }

    GeckoNetworkManager.getInstance().stop();
    GeckoScreenOrientationListener.getInstance().stop();

    super.onDestroy();

    unregisterReceiver(mBatteryReceiver);
  }
 public void handleNotification(String action, String alertName, String alertCookie) {
   GeckoAppShell.handleNotification(action, alertName, alertCookie);
 }
 @Override
 public void onLowMemory() {
   Log.e(LOG_FILE_NAME, "low memory");
   if (checkLaunchState(LaunchState.GeckoRunning)) GeckoAppShell.onLowMemory();
   super.onLowMemory();
 }
 @Override
 public void onStart() {
   Log.i(LOG_FILE_NAME, "start");
   GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_START));
   super.onStart();
 }
 @Override
 public void onRestart() {
   Log.i(LOG_FILE_NAME, "restart");
   GeckoAppShell.putChildInForeground();
   super.onRestart();
 }
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    mAppContext = this;
    mMainHandler = new Handler();

    if (!sTryCatchAttached) {
      sTryCatchAttached = true;
      mMainHandler.post(
          new Runnable() {
            public void run() {
              try {
                Looper.loop();
              } catch (Exception e) {
                Log.e(LOG_FILE_NAME, "top level exception", e);
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                pw.flush();
                GeckoAppShell.reportJavaCrash(sw.toString());
              }
              // resetting this is kinda pointless, but oh well
              sTryCatchAttached = false;
            }
          });
    }

    SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
    String localeCode = settings.getString(getPackageName() + ".locale", "");
    if (localeCode != null && localeCode.length() > 0) GeckoAppShell.setSelectedLocale(localeCode);

    Log.i(LOG_FILE_NAME, "create");
    super.onCreate(savedInstanceState);

    if (sGREDir == null) sGREDir = new File(this.getApplicationInfo().dataDir);

    getWindow()
        .setFlags(
            mFullscreen ? WindowManager.LayoutParams.FLAG_FULLSCREEN : 0,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    if (cameraView == null) {
      cameraView = new SurfaceView(this);
      cameraView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    if (surfaceView == null) surfaceView = new GeckoSurfaceView(this);
    else mainLayout.removeAllViews();

    mainLayout = new AbsoluteLayout(this);
    mainLayout.addView(
        surfaceView,
        new AbsoluteLayout.LayoutParams(
            AbsoluteLayout.LayoutParams.MATCH_PARENT, // level 8
            AbsoluteLayout.LayoutParams.MATCH_PARENT,
            0,
            0));

    setContentView(
        mainLayout,
        new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    mConnectivityFilter = new IntentFilter();
    mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    mConnectivityReceiver = new GeckoConnectivityReceiver();

    IntentFilter batteryFilter = new IntentFilter();
    batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
    mBatteryReceiver = new GeckoBatteryManager();
    registerReceiver(mBatteryReceiver, batteryFilter);

    if (SmsManager.getInstance() != null) {
      SmsManager.getInstance().start();
    }

    GeckoNetworkManager.getInstance().init();

    if (!checkAndSetLaunchState(LaunchState.PreLaunch, LaunchState.Launching)) return;

    checkAndLaunchUpdate();
    mLibLoadThread =
        new Thread(
            new Runnable() {
              public void run() {
                // At some point while loading the gecko libs our default locale gets set
                // so just save it to locale here and reset it as default after the join
                Locale locale = Locale.getDefault();
                GeckoAppShell.loadGeckoLibs(getApplication().getPackageResourcePath());
                Locale.setDefault(locale);
                Resources res = getBaseContext().getResources();
                Configuration config = res.getConfiguration();
                config.locale = locale;
                res.updateConfiguration(config, res.getDisplayMetrics());
              }
            });
    mLibLoadThread.start();
  }