2010-08-27 13:22:57 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
2010-06-02 14:55:28 -07:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009-2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com>
|
2010-11-12 15:58:29 -08:00
|
|
|
* Matt Brubeck <mbrubeck@mozilla.com>
|
|
|
|
* Vivien Nicolas <vnicolas@mozilla.com>
|
2010-06-02 14:55:28 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.zip.*;
|
|
|
|
import java.nio.*;
|
2010-09-13 18:16:24 -07:00
|
|
|
import java.nio.channels.FileChannel;
|
2010-09-30 08:37:36 -07:00
|
|
|
import java.util.concurrent.*;
|
2010-06-02 14:55:28 -07:00
|
|
|
|
|
|
|
import android.os.*;
|
|
|
|
import android.app.*;
|
|
|
|
import android.text.*;
|
|
|
|
import android.view.*;
|
|
|
|
import android.view.inputmethod.*;
|
|
|
|
import android.content.*;
|
2011-02-27 07:50:56 -08:00
|
|
|
import android.content.res.*;
|
2010-06-02 14:55:28 -07:00
|
|
|
import android.graphics.*;
|
|
|
|
import android.widget.*;
|
|
|
|
import android.hardware.*;
|
|
|
|
|
|
|
|
import android.util.*;
|
2010-09-30 08:37:36 -07:00
|
|
|
import android.net.*;
|
2010-06-02 14:55:28 -07:00
|
|
|
|
|
|
|
abstract public class GeckoApp
|
|
|
|
extends Activity
|
|
|
|
{
|
2010-06-14 19:17:37 -07:00
|
|
|
public static final String ACTION_ALERT_CLICK = "org.mozilla.gecko.ACTION_ALERT_CLICK";
|
|
|
|
public static final String ACTION_ALERT_CLEAR = "org.mozilla.gecko.ACTION_ALERT_CLEAR";
|
|
|
|
|
2010-06-02 14:55:28 -07:00
|
|
|
public static FrameLayout mainLayout;
|
|
|
|
public static GeckoSurfaceView surfaceView;
|
|
|
|
public static GeckoApp mAppContext;
|
2010-11-08 18:11:13 -08:00
|
|
|
public static boolean mFullscreen = false;
|
2011-02-07 20:40:42 -08:00
|
|
|
public static File sGREDir = null;
|
2011-01-10 21:48:35 -08:00
|
|
|
static Thread mLibLoadThread = null;
|
2011-03-30 21:13:35 -07:00
|
|
|
public Handler mMainHandler;
|
2011-05-09 12:58:55 -07:00
|
|
|
private IntentFilter mConnectivityFilter;
|
|
|
|
private BroadcastReceiver mConnectivityReceiver;
|
2010-06-02 14:55:28 -07:00
|
|
|
|
2010-11-24 14:51:41 -08:00
|
|
|
enum LaunchState {PreLaunch, Launching, WaitButton,
|
|
|
|
Launched, GeckoRunning, GeckoExiting};
|
|
|
|
private static LaunchState sLaunchState = LaunchState.PreLaunch;
|
|
|
|
|
2011-02-27 07:50:56 -08:00
|
|
|
|
2010-11-24 14:51:41 -08:00
|
|
|
static boolean checkLaunchState(LaunchState checkState) {
|
|
|
|
synchronized(sLaunchState) {
|
|
|
|
return sLaunchState == checkState;
|
|
|
|
}
|
|
|
|
}
|
2011-02-27 07:50:56 -08:00
|
|
|
|
2010-11-24 14:51:41 -08:00
|
|
|
static void setLaunchState(LaunchState setState) {
|
|
|
|
synchronized(sLaunchState) {
|
|
|
|
sLaunchState = setState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if mLaunchState is equal to checkState this sets mLaunchState to setState
|
|
|
|
// and return true. Otherwise we return false.
|
|
|
|
static boolean checkAndSetLaunchState(LaunchState checkState, LaunchState setState) {
|
|
|
|
synchronized(sLaunchState) {
|
|
|
|
if (sLaunchState != checkState)
|
|
|
|
return false;
|
|
|
|
sLaunchState = setState;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-27 18:08:55 -07:00
|
|
|
void showErrorDialog(String message)
|
|
|
|
{
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setMessage(message)
|
|
|
|
.setCancelable(false)
|
2011-02-11 14:35:50 -08:00
|
|
|
.setPositiveButton(R.string.exit_label,
|
2010-09-27 18:08:55 -07:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog,
|
|
|
|
int id)
|
|
|
|
{
|
|
|
|
GeckoApp.this.finish();
|
2011-02-11 14:35:50 -08:00
|
|
|
System.exit(0);
|
2010-09-27 18:08:55 -07:00
|
|
|
}
|
|
|
|
}).show();
|
|
|
|
}
|
|
|
|
|
2010-11-24 14:51:41 -08:00
|
|
|
// Returns true when the intent is going to be handled by gecko launch
|
2011-01-10 21:48:35 -08:00
|
|
|
boolean launch(Intent intent)
|
2010-06-02 14:55:28 -07:00
|
|
|
{
|
2010-11-24 14:51:41 -08:00
|
|
|
if (!checkAndSetLaunchState(LaunchState.Launching, LaunchState.Launched))
|
|
|
|
return false;
|
|
|
|
|
2011-01-10 21:48:35 -08:00
|
|
|
if (intent == null)
|
|
|
|
intent = getIntent();
|
|
|
|
final Intent i = intent;
|
2011-02-27 07:50:56 -08:00
|
|
|
new Thread() {
|
2011-01-10 21:48:35 -08:00
|
|
|
public void run() {
|
2011-02-09 11:58:17 -08:00
|
|
|
long startup_time = System.currentTimeMillis();
|
2011-01-10 21:48:35 -08:00
|
|
|
try {
|
|
|
|
if (mLibLoadThread != null)
|
|
|
|
mLibLoadThread.join();
|
|
|
|
} catch (InterruptedException ie) {}
|
2011-02-27 07:50:56 -08:00
|
|
|
surfaceView.mSplashStatusMsg =
|
2011-01-10 21:48:35 -08:00
|
|
|
getResources().getString(R.string.splash_screen_label);
|
|
|
|
surfaceView.drawSplashScreen();
|
|
|
|
// unpack files in the components directory
|
|
|
|
try {
|
|
|
|
unpackComponents();
|
|
|
|
} catch (FileNotFoundException fnfe) {
|
|
|
|
Log.e("GeckoApp", "error unpacking components", fnfe);
|
|
|
|
Looper.prepare();
|
|
|
|
showErrorDialog(getString(R.string.error_loading_file));
|
|
|
|
Looper.loop();
|
|
|
|
return;
|
|
|
|
} catch (IOException ie) {
|
|
|
|
Log.e("GeckoApp", "error unpacking components", ie);
|
|
|
|
String msg = ie.getMessage();
|
|
|
|
Looper.prepare();
|
2011-02-28 20:24:42 -08:00
|
|
|
if (msg != null && msg.equalsIgnoreCase("No space left on device"))
|
2011-01-10 21:48:35 -08:00
|
|
|
showErrorDialog(getString(R.string.no_space_to_start_error));
|
|
|
|
else
|
|
|
|
showErrorDialog(getString(R.string.error_loading_file));
|
|
|
|
Looper.loop();
|
|
|
|
return;
|
|
|
|
}
|
2011-02-27 07:50:56 -08:00
|
|
|
|
2011-01-10 21:48:35 -08:00
|
|
|
// and then fire us up
|
|
|
|
String env = i.getStringExtra("env0");
|
|
|
|
GeckoAppShell.runGecko(getApplication().getPackageResourcePath(),
|
|
|
|
i.getStringExtra("args"),
|
|
|
|
i.getDataString());
|
|
|
|
}
|
|
|
|
}.start();
|
2010-11-24 14:51:41 -08:00
|
|
|
return true;
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when the activity is first created. */
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
2011-02-27 07:50:56 -08:00
|
|
|
mAppContext = this;
|
2011-03-30 21:13:35 -07:00
|
|
|
mMainHandler = new Handler();
|
2011-02-27 07:50:56 -08:00
|
|
|
|
|
|
|
SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
|
|
|
|
String localeCode = settings.getString(getPackageName() + ".locale", "");
|
|
|
|
if (localeCode != null && localeCode.length() > 0)
|
|
|
|
GeckoAppShell.setSelectedLocale(localeCode);
|
|
|
|
|
2010-06-24 12:00:42 -07:00
|
|
|
Log.i("GeckoApp", "create");
|
2010-06-02 14:55:28 -07:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2011-02-08 00:39:15 -08:00
|
|
|
if (sGREDir == null)
|
|
|
|
sGREDir = new File(this.getApplicationInfo().dataDir);
|
2011-02-07 20:40:42 -08:00
|
|
|
|
2010-11-08 18:11:13 -08:00
|
|
|
getWindow().setFlags(mFullscreen ?
|
|
|
|
WindowManager.LayoutParams.FLAG_FULLSCREEN : 0,
|
|
|
|
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
2010-06-02 14:55:28 -07:00
|
|
|
|
2010-11-08 18:11:13 -08:00
|
|
|
if (surfaceView == null)
|
|
|
|
surfaceView = new GeckoSurfaceView(this);
|
|
|
|
else
|
|
|
|
mainLayout.removeView(surfaceView);
|
2010-09-13 18:16:24 -07:00
|
|
|
|
2010-06-02 14:55:28 -07:00
|
|
|
mainLayout = new FrameLayout(this);
|
|
|
|
mainLayout.addView(surfaceView,
|
|
|
|
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
|
|
|
|
FrameLayout.LayoutParams.FILL_PARENT));
|
|
|
|
|
|
|
|
setContentView(mainLayout,
|
|
|
|
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT));
|
2010-11-24 14:51:41 -08:00
|
|
|
|
2011-05-10 09:14:05 -07:00
|
|
|
mConnectivityFilter = new IntentFilter();
|
|
|
|
mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
|
|
mConnectivityReceiver = new GeckoConnectivityReceiver();
|
|
|
|
|
2010-11-24 14:51:41 -08:00
|
|
|
if (!checkAndSetLaunchState(LaunchState.PreLaunch,
|
|
|
|
LaunchState.Launching))
|
|
|
|
return;
|
|
|
|
|
|
|
|
checkAndLaunchUpdate();
|
2011-02-27 07:50:56 -08:00
|
|
|
mLibLoadThread = new Thread(new Runnable() {
|
2011-01-10 21:48:35 -08:00
|
|
|
public void run() {
|
2011-02-27 07:50:56 -08:00
|
|
|
// 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();
|
2011-01-10 21:48:35 -08:00
|
|
|
GeckoAppShell.loadGeckoLibs(
|
|
|
|
getApplication().getPackageResourcePath());
|
2011-02-27 07:50:56 -08:00
|
|
|
Locale.setDefault(locale);
|
|
|
|
Resources res = getBaseContext().getResources();
|
|
|
|
Configuration config = res.getConfiguration();
|
|
|
|
config.locale = locale;
|
|
|
|
res.updateConfiguration(config, res.getDisplayMetrics());
|
|
|
|
|
|
|
|
|
2011-01-10 21:48:35 -08:00
|
|
|
}});
|
2011-01-18 16:25:20 -08:00
|
|
|
File cacheFile = GeckoAppShell.getCacheDir();
|
|
|
|
File libxulFile = new File(cacheFile, "libxul.so");
|
|
|
|
|
|
|
|
if (GeckoAppShell.getFreeSpace() > GeckoAppShell.kFreeSpaceThreshold &&
|
2011-02-27 07:50:56 -08:00
|
|
|
(!libxulFile.exists() ||
|
2011-01-18 16:25:20 -08:00
|
|
|
new File(getApplication().getPackageResourcePath()).lastModified()
|
|
|
|
>= libxulFile.lastModified()))
|
2011-01-10 21:48:35 -08:00
|
|
|
surfaceView.mSplashStatusMsg =
|
|
|
|
getResources().getString(R.string.splash_screen_installing);
|
|
|
|
else
|
|
|
|
surfaceView.mSplashStatusMsg =
|
|
|
|
getResources().getString(R.string.splash_screen_label);
|
2011-01-19 22:14:12 -08:00
|
|
|
mLibLoadThread.start();
|
2011-02-13 20:25:22 -08:00
|
|
|
if (IsNewInstall() && IsUnsupportedDevice()) {
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setMessage(R.string.incompatable_device)
|
|
|
|
.setCancelable(false)
|
|
|
|
.setPositiveButton(R.string.continue_label, null)
|
|
|
|
.setNegativeButton(R.string.exit_label,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog,
|
|
|
|
int id)
|
|
|
|
{
|
|
|
|
GeckoApp.this.finish();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2011-02-27 07:50:56 -08:00
|
|
|
}
|
2011-02-13 20:25:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean IsNewInstall() {
|
|
|
|
File appIni = new File(sGREDir, "application.ini");
|
|
|
|
return !appIni.exists();
|
|
|
|
}
|
2011-02-11 14:35:50 -08:00
|
|
|
|
2011-02-13 20:25:22 -08:00
|
|
|
boolean IsUnsupportedDevice() {
|
2011-04-28 12:34:48 -07:00
|
|
|
// We don't currently support devices with less than 512Mb of RAM,
|
|
|
|
// and want to warn if run on such devices. Most 512Mb devices
|
|
|
|
// report about 350Mb available, so we check - somewhat arbitrarily -
|
|
|
|
// for a minimum of 300Mb here.
|
2011-02-13 20:25:22 -08:00
|
|
|
File meminfo = new File("/proc/meminfo");
|
2011-02-27 07:50:56 -08:00
|
|
|
try {
|
2011-02-13 20:25:22 -08:00
|
|
|
BufferedReader br = new BufferedReader(new FileReader(meminfo));
|
|
|
|
String totalMem = "";
|
|
|
|
while(!totalMem.contains("MemTotal:") && totalMem != null)
|
|
|
|
totalMem = br.readLine();
|
|
|
|
StringTokenizer st = new StringTokenizer(totalMem, " ");
|
|
|
|
st.nextToken(); // "MemInfo:"
|
|
|
|
totalMem = st.nextToken();
|
2011-02-27 07:50:56 -08:00
|
|
|
|
2011-02-13 20:25:22 -08:00
|
|
|
Log.i("GeckoMemory", "MemTotal: " + Integer.parseInt(totalMem));
|
2011-04-28 12:34:48 -07:00
|
|
|
return Integer.parseInt(totalMem) < 300000L;
|
2011-02-13 20:25:22 -08:00
|
|
|
} catch (Exception ex) {
|
|
|
|
// Will catch NullPointerException if totalMem isn't found,
|
2011-02-27 07:50:56 -08:00
|
|
|
// a NumberFormatException if the token isn't parsible
|
2011-02-13 20:25:22 -08:00
|
|
|
// IOException from the file reading or NoSuchElementException
|
|
|
|
// if totalMem doesn't have 2 tokens. None of these are fatal,
|
|
|
|
// so log it and move on.
|
|
|
|
Log.w("GeckoMemTest", "Exception when finding total memory", ex);
|
2011-02-11 14:35:50 -08:00
|
|
|
}
|
2011-02-13 20:25:22 -08:00
|
|
|
return false;
|
2010-11-24 14:51:41 -08:00
|
|
|
}
|
|
|
|
|
2010-06-18 10:42:51 -07:00
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
2010-11-24 14:51:41 -08:00
|
|
|
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;
|
|
|
|
}
|
2010-06-18 10:42:51 -07:00
|
|
|
final String action = intent.getAction();
|
2010-12-09 11:01:30 -08:00
|
|
|
if ("org.mozilla.gecko.DEBUG".equals(action) &&
|
2010-11-24 14:51:41 -08:00
|
|
|
checkAndSetLaunchState(LaunchState.Launching, LaunchState.WaitButton)) {
|
|
|
|
final Button launchButton = new Button(this);
|
|
|
|
launchButton.setText("Launch"); // don't need to localize
|
|
|
|
launchButton.setOnClickListener(new Button.OnClickListener() {
|
2011-02-27 07:50:56 -08:00
|
|
|
public void onClick (View v) {
|
|
|
|
// hide the button so we can't be launched again
|
|
|
|
mainLayout.removeView(launchButton);
|
|
|
|
setLaunchState(LaunchState.Launching);
|
|
|
|
launch(null);
|
|
|
|
}
|
|
|
|
});
|
2010-11-24 14:51:41 -08:00
|
|
|
mainLayout.addView(launchButton, 300, 200);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (checkLaunchState(LaunchState.WaitButton) || launch(intent))
|
|
|
|
return;
|
|
|
|
|
2010-06-18 10:42:51 -07:00
|
|
|
if (Intent.ACTION_VIEW.equals(action)) {
|
|
|
|
String uri = intent.getDataString();
|
|
|
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(uri));
|
|
|
|
Log.i("GeckoApp","onNewIntent: "+uri);
|
|
|
|
}
|
2010-10-15 11:16:45 -07:00
|
|
|
else if (Intent.ACTION_MAIN.equals(action)) {
|
|
|
|
Log.i("GeckoApp", "Intent : ACTION_MAIN");
|
|
|
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(""));
|
|
|
|
}
|
|
|
|
else if (action.equals("org.mozilla.fennec.WEBAPP")) {
|
|
|
|
String uri = intent.getStringExtra("args");
|
|
|
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(uri));
|
|
|
|
Log.i("GeckoApp","Intent : WEBAPP - " + uri);
|
|
|
|
}
|
2010-06-18 10:42:51 -07:00
|
|
|
}
|
|
|
|
|
2010-06-02 14:55:28 -07:00
|
|
|
@Override
|
|
|
|
public void onPause()
|
|
|
|
{
|
2010-06-22 08:22:31 -07:00
|
|
|
Log.i("GeckoApp", "pause");
|
2010-06-17 14:33:15 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_PAUSING));
|
2010-06-02 14:55:28 -07:00
|
|
|
// 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();
|
2011-05-09 12:58:55 -07:00
|
|
|
|
|
|
|
unregisterReceiver(mConnectivityReceiver);
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume()
|
|
|
|
{
|
2010-06-22 08:22:31 -07:00
|
|
|
Log.i("GeckoApp", "resume");
|
2010-11-24 14:51:41 -08:00
|
|
|
if (checkLaunchState(LaunchState.GeckoRunning))
|
2011-02-01 22:34:12 -08:00
|
|
|
GeckoAppShell.onResume();
|
2010-06-02 14:55:28 -07:00
|
|
|
// After an onPause, the activity is back in the foreground.
|
|
|
|
// Undo whatever we did in onPause.
|
|
|
|
super.onResume();
|
2010-11-24 14:51:41 -08:00
|
|
|
|
|
|
|
// Just in case. Normally we start in onNewIntent
|
|
|
|
if (checkLaunchState(LaunchState.PreLaunch) ||
|
|
|
|
checkLaunchState(LaunchState.Launching))
|
|
|
|
onNewIntent(getIntent());
|
2011-05-09 12:58:55 -07:00
|
|
|
|
|
|
|
registerReceiver(mConnectivityReceiver, mConnectivityFilter);
|
2011-02-01 22:34:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop()
|
|
|
|
{
|
|
|
|
Log.i("GeckoApp", "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();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRestart()
|
|
|
|
{
|
|
|
|
Log.i("GeckoApp", "restart");
|
|
|
|
super.onRestart();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart()
|
|
|
|
{
|
|
|
|
Log.i("GeckoApp", "start");
|
|
|
|
super.onStart();
|
2010-06-24 12:00:42 -07:00
|
|
|
}
|
|
|
|
|
2010-06-02 14:55:28 -07:00
|
|
|
@Override
|
|
|
|
public void onDestroy()
|
|
|
|
{
|
2010-06-24 12:00:42 -07:00
|
|
|
Log.i("GeckoApp", "destroy");
|
2010-06-02 14:55:28 -07:00
|
|
|
// Tell Gecko to shutting down; we'll end up calling System.exit()
|
|
|
|
// in onXreExit.
|
2010-11-08 18:11:18 -08:00
|
|
|
if (isFinishing())
|
2010-12-11 14:36:11 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_SHUTDOWN));
|
2010-06-02 14:55:28 -07:00
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(android.content.res.Configuration newConfig)
|
|
|
|
{
|
2010-06-24 12:00:42 -07:00
|
|
|
Log.i("GeckoApp", "configuration changed");
|
2010-06-02 14:55:28 -07:00
|
|
|
// nothing, just ignore
|
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLowMemory()
|
|
|
|
{
|
2011-01-19 22:14:12 -08:00
|
|
|
Log.e("GeckoApp", "low memory");
|
2010-11-24 14:51:41 -08:00
|
|
|
if (checkLaunchState(LaunchState.GeckoRunning))
|
2010-08-31 10:37:54 -07:00
|
|
|
GeckoAppShell.onLowMemory();
|
2010-06-02 14:55:28 -07:00
|
|
|
super.onLowMemory();
|
|
|
|
}
|
|
|
|
|
2010-12-21 12:02:14 -08:00
|
|
|
abstract public String getPackageName();
|
2010-06-25 13:12:03 -07:00
|
|
|
abstract public String getContentProcessName();
|
2010-06-02 14:55:28 -07:00
|
|
|
|
|
|
|
protected void unpackComponents()
|
2010-09-30 08:53:42 -07:00
|
|
|
throws IOException, FileNotFoundException
|
2010-06-02 14:55:28 -07:00
|
|
|
{
|
|
|
|
ZipFile zip;
|
|
|
|
InputStream listStream;
|
|
|
|
|
2011-02-07 20:40:42 -08:00
|
|
|
File componentsDir = new File(sGREDir, "components");
|
2010-09-30 08:53:42 -07:00
|
|
|
componentsDir.mkdir();
|
|
|
|
zip = new ZipFile(getApplication().getPackageResourcePath());
|
2010-09-01 18:35:46 -07:00
|
|
|
|
|
|
|
byte[] buf = new byte[8192];
|
2011-03-18 08:50:19 -07:00
|
|
|
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("GeckoApp", "error removing files", ex);
|
|
|
|
}
|
2010-09-01 18:35:46 -07:00
|
|
|
unpackFile(zip, buf, null, "application.ini");
|
|
|
|
unpackFile(zip, buf, null, getContentProcessName());
|
|
|
|
try {
|
2010-09-30 08:53:42 -07:00
|
|
|
unpackFile(zip, buf, null, "update.locale");
|
|
|
|
} catch (Exception e) {/* this is non-fatal */}
|
2010-06-02 14:55:28 -07:00
|
|
|
|
2010-09-30 10:19:05 -07:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
}
|
2011-05-05 08:03:01 -07:00
|
|
|
|
|
|
|
// copy any hyphenation dictionaries file into a hyphenation/ directory
|
|
|
|
Enumeration<? extends ZipEntry> hyphenEntries = zip.entries();
|
|
|
|
while (hyphenEntries.hasMoreElements()) {
|
|
|
|
ZipEntry entry = hyphenEntries.nextElement();
|
|
|
|
if (entry.getName().startsWith("hyphenation/")) {
|
|
|
|
Log.i("GeckoAppJava", "installing hyphenation : " + entry.getName());
|
|
|
|
unpackFile(zip, buf, entry, entry.getName());
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
|
|
|
|
2011-03-18 08:50:19 -07:00
|
|
|
void removeFiles() throws IOException {
|
|
|
|
BufferedReader reader = new BufferedReader(
|
|
|
|
new FileReader(new File(sGREDir, "removed-files")));
|
|
|
|
try {
|
|
|
|
for (String removedFileName = reader.readLine();
|
|
|
|
removedFileName != null; removedFileName = reader.readLine()) {
|
|
|
|
File removedFile = new File(sGREDir, removedFileName);
|
|
|
|
if (removedFile.exists())
|
|
|
|
removedFile.delete();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-04 15:00:52 -08:00
|
|
|
boolean haveKilledZombies = false;
|
|
|
|
|
2011-03-18 08:50:19 -07:00
|
|
|
private boolean unpackFile(ZipFile zip, byte[] buf, ZipEntry fileEntry,
|
2010-09-30 08:53:42 -07:00
|
|
|
String name)
|
|
|
|
throws IOException, FileNotFoundException
|
2010-06-02 14:55:28 -07:00
|
|
|
{
|
|
|
|
if (fileEntry == null)
|
|
|
|
fileEntry = zip.getEntry(name);
|
2010-09-30 08:53:42 -07:00
|
|
|
if (fileEntry == null)
|
|
|
|
throw new FileNotFoundException("Can't find " + name + " in " +
|
|
|
|
zip.getName());
|
2010-06-02 14:55:28 -07:00
|
|
|
|
2011-02-07 20:40:42 -08:00
|
|
|
File outFile = new File(sGREDir, name);
|
2010-06-02 14:55:28 -07:00
|
|
|
if (outFile.exists() &&
|
2010-10-05 16:48:33 -07:00
|
|
|
outFile.lastModified() == fileEntry.getTime() &&
|
2010-06-02 14:55:28 -07:00
|
|
|
outFile.length() == fileEntry.getSize())
|
2011-03-18 08:50:19 -07:00
|
|
|
return false;
|
2010-06-02 14:55:28 -07:00
|
|
|
|
2011-03-04 15:00:52 -08:00
|
|
|
if (!haveKilledZombies) {
|
|
|
|
haveKilledZombies = true;
|
|
|
|
GeckoAppShell.killAnyZombies();
|
|
|
|
}
|
2011-02-22 10:38:13 -08:00
|
|
|
|
2010-09-30 08:53:42 -07:00
|
|
|
File dir = outFile.getParentFile();
|
|
|
|
if (!outFile.exists())
|
|
|
|
dir.mkdirs();
|
2010-06-02 14:55:28 -07:00
|
|
|
|
|
|
|
InputStream fileStream;
|
2010-09-30 08:53:42 -07:00
|
|
|
fileStream = zip.getInputStream(fileEntry);
|
2010-06-02 14:55:28 -07:00
|
|
|
|
2010-09-30 08:53:42 -07:00
|
|
|
OutputStream outStream = new FileOutputStream(outFile);
|
2010-06-02 14:55:28 -07:00
|
|
|
|
2010-09-30 08:53:42 -07:00
|
|
|
while (fileStream.available() > 0) {
|
|
|
|
int read = fileStream.read(buf, 0, buf.length);
|
|
|
|
outStream.write(buf, 0, read);
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
2010-09-17 17:37:28 -07:00
|
|
|
|
2010-09-30 08:53:42 -07:00
|
|
|
fileStream.close();
|
|
|
|
outStream.close();
|
2010-09-17 17:37:28 -07:00
|
|
|
outFile.setLastModified(fileEntry.getTime());
|
2011-03-18 08:50:19 -07:00
|
|
|
return true;
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
2010-09-30 08:53:42 -07:00
|
|
|
|
2010-10-24 13:26:19 -07:00
|
|
|
public void addEnvToIntent(Intent intent) {
|
2010-06-02 14:55:28 -07:00
|
|
|
Map<String,String> envMap = System.getenv();
|
|
|
|
Set<Map.Entry<String,String>> envSet = envMap.entrySet();
|
|
|
|
Iterator<Map.Entry<String,String>> envIter = envSet.iterator();
|
|
|
|
StringBuffer envstr = new StringBuffer();
|
|
|
|
int c = 0;
|
|
|
|
while (envIter.hasNext()) {
|
|
|
|
Map.Entry<String,String> entry = envIter.next();
|
2011-02-27 07:50:56 -08:00
|
|
|
intent.putExtra("env" + c, entry.getKey() + "="
|
2010-10-24 13:26:19 -07:00
|
|
|
+ entry.getValue());
|
|
|
|
c++;
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void doRestart() {
|
|
|
|
try {
|
2010-12-21 12:02:14 -08:00
|
|
|
String action = "org.mozilla.gecko.restart";
|
2010-10-24 13:26:19 -07:00
|
|
|
Intent intent = new Intent(action);
|
2010-12-21 12:02:14 -08:00
|
|
|
intent.setClassName(getPackageName(),
|
|
|
|
getPackageName() + ".Restarter");
|
2010-10-24 13:26:19 -07:00
|
|
|
addEnvToIntent(intent);
|
2010-12-02 08:46:22 -08:00
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
|
|
|
|
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
|
2010-10-24 13:26:19 -07:00
|
|
|
Log.i("GeckoAppJava", intent.toString());
|
2011-03-04 15:00:52 -08:00
|
|
|
GeckoAppShell.killAnyZombies();
|
2010-10-26 18:57:29 -07:00
|
|
|
startActivity(intent);
|
2010-06-02 14:55:28 -07:00
|
|
|
} catch (Exception e) {
|
2011-01-03 14:34:44 -08:00
|
|
|
Log.i("GeckoAppJava", "error doing restart", e);
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
2010-10-24 13:26:19 -07:00
|
|
|
finish();
|
2011-02-28 14:10:40 -08:00
|
|
|
// Give the restart process time to start before we die
|
2011-03-04 15:00:52 -08:00
|
|
|
GeckoAppShell.waitForAnotherGeckoProc();
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|
2010-06-14 19:17:37 -07:00
|
|
|
|
|
|
|
public void handleNotification(String action, String alertName, String alertCookie) {
|
|
|
|
GeckoAppShell.handleNotification(action, alertName, alertCookie);
|
|
|
|
}
|
2010-09-13 18:16:24 -07:00
|
|
|
|
|
|
|
private void checkAndLaunchUpdate() {
|
|
|
|
Log.i("GeckoAppJava", "Checking for an update");
|
|
|
|
|
|
|
|
int statusCode = 8; // UNEXPECTED_ERROR
|
2011-02-09 15:22:35 -08:00
|
|
|
File baseUpdateDir = null;
|
2011-01-03 14:32:57 -08:00
|
|
|
if (Build.VERSION.SDK_INT >= 8)
|
2011-02-09 15:22:35 -08:00
|
|
|
baseUpdateDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
|
2011-01-03 14:32:57 -08:00
|
|
|
else
|
2011-02-09 15:22:35 -08:00
|
|
|
baseUpdateDir = new File(Environment.getExternalStorageDirectory().getPath(), "download");
|
2011-01-03 14:32:57 -08:00
|
|
|
|
2011-02-09 15:22:35 -08:00
|
|
|
File updateDir = new File(new File(baseUpdateDir, "updates"),"0");
|
2010-09-13 18:16:24 -07:00
|
|
|
|
2011-01-03 14:32:57 -08:00
|
|
|
File updateFile = new File(updateDir, "update.apk");
|
|
|
|
File statusFile = new File(updateDir, "update.status");
|
2010-11-18 14:13:31 -08:00
|
|
|
|
|
|
|
if (!statusFile.exists() || !readUpdateStatus(statusFile).equals("pending"))
|
|
|
|
return;
|
2010-09-13 18:16:24 -07:00
|
|
|
|
|
|
|
if (!updateFile.exists())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Log.i("GeckoAppJava", "Update is available!");
|
|
|
|
|
|
|
|
// Launch APK
|
2010-12-21 12:02:14 -08:00
|
|
|
File updateFileToRun = new File(updateDir + getPackageName() + "-update.apk");
|
2010-09-13 18:16:24 -07:00
|
|
|
try {
|
2010-09-17 11:03:43 -07:00
|
|
|
if (updateFile.renameTo(updateFileToRun)) {
|
2010-09-13 18:16:24 -07:00
|
|
|
String amCmd = "/system/bin/am start -a android.intent.action.VIEW " +
|
|
|
|
"-n com.android.packageinstaller/.PackageInstallerActivity -d file://" +
|
|
|
|
updateFileToRun.getPath();
|
|
|
|
Log.i("GeckoAppJava", amCmd);
|
|
|
|
Runtime.getRuntime().exec(amCmd);
|
|
|
|
statusCode = 0; // OK
|
|
|
|
} else {
|
2010-09-17 11:03:43 -07:00
|
|
|
Log.i("GeckoAppJava", "Cannot rename the update file!");
|
2010-09-13 18:16:24 -07:00
|
|
|
statusCode = 7; // WRITE_ERROR
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2011-01-03 14:34:44 -08:00
|
|
|
Log.i("GeckoAppJava", "error launching installer to update", e);
|
2010-09-13 18:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the status file
|
|
|
|
String status = statusCode == 0 ? "succeeded\n" : "failed: "+ statusCode + "\n";
|
|
|
|
|
|
|
|
OutputStream outStream;
|
|
|
|
try {
|
|
|
|
byte[] buf = status.getBytes("UTF-8");
|
|
|
|
outStream = new FileOutputStream(statusFile);
|
|
|
|
outStream.write(buf, 0, buf.length);
|
|
|
|
outStream.close();
|
|
|
|
} catch (Exception e) {
|
2011-01-03 14:34:44 -08:00
|
|
|
Log.i("GeckoAppJava", "error writing status file", e);
|
2010-09-13 18:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (statusCode == 0)
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2010-09-30 08:37:36 -07:00
|
|
|
|
2010-11-18 14:13:31 -08:00
|
|
|
private String readUpdateStatus(File statusFile) {
|
|
|
|
String status = "";
|
|
|
|
try {
|
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(statusFile));
|
|
|
|
status = reader.readLine();
|
|
|
|
reader.close();
|
|
|
|
} catch (Exception e) {
|
2011-01-03 14:34:44 -08:00
|
|
|
Log.i("GeckoAppJava", "error reading update status", e);
|
2010-11-18 14:13:31 -08:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2010-09-30 08:37:36 -07:00
|
|
|
static final int FILE_PICKER_REQUEST = 1;
|
|
|
|
|
|
|
|
private SynchronousQueue<String> mFilePickerResult = new SynchronousQueue();
|
2010-11-05 10:43:12 -07:00
|
|
|
public String showFilePicker(String aMimeType) {
|
2010-09-30 08:37:36 -07:00
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
2010-11-05 10:43:12 -07:00
|
|
|
intent.setType(aMimeType);
|
2010-09-30 08:37:36 -07:00
|
|
|
GeckoApp.this.
|
|
|
|
startActivityForResult(
|
|
|
|
Intent.createChooser(intent,"choose a file"),
|
|
|
|
FILE_PICKER_REQUEST);
|
|
|
|
String filePickerResult = "";
|
|
|
|
try {
|
|
|
|
filePickerResult = mFilePickerResult.take();
|
|
|
|
} catch (InterruptedException e) {
|
2011-01-03 14:34:44 -08:00
|
|
|
Log.i("GeckoApp", "showing file picker ", e);
|
2010-09-30 08:37:36 -07:00
|
|
|
}
|
2011-02-27 07:50:56 -08:00
|
|
|
|
2010-09-30 08:37:36 -07:00
|
|
|
return filePickerResult;
|
|
|
|
}
|
2011-02-27 07:50:56 -08:00
|
|
|
|
2010-09-30 08:37:36 -07:00
|
|
|
@Override
|
2011-02-27 07:50:56 -08:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode,
|
2010-09-30 08:37:36 -07:00
|
|
|
Intent data) {
|
|
|
|
String filePickerResult = "";
|
|
|
|
if (data != null && resultCode == RESULT_OK) {
|
|
|
|
try {
|
|
|
|
ContentResolver cr = getContentResolver();
|
|
|
|
Uri uri = data.getData();
|
|
|
|
String mimeType = cr.getType(uri);
|
2011-02-27 07:50:56 -08:00
|
|
|
String fileExt = "." +
|
2011-03-07 21:45:43 -08:00
|
|
|
GeckoAppShell.getExtensionFromMimeType(mimeType);
|
2011-02-27 07:50:56 -08:00
|
|
|
File file =
|
|
|
|
File.createTempFile("tmp_" +
|
|
|
|
(int)Math.floor(1000 * Math.random()),
|
2011-02-07 20:40:42 -08:00
|
|
|
fileExt, sGREDir);
|
2011-02-27 07:50:56 -08:00
|
|
|
|
2010-09-30 08:37:36 -07:00
|
|
|
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) {
|
2011-01-03 14:34:44 -08:00
|
|
|
Log.e("GeckoApp", "showing file picker", e);
|
2010-09-30 08:37:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
mFilePickerResult.put(filePickerResult);
|
|
|
|
} catch (InterruptedException e) {
|
2011-01-03 14:34:44 -08:00
|
|
|
Log.i("GeckoApp", "error returning file picker result", e);
|
2010-09-30 08:37:36 -07:00
|
|
|
}
|
|
|
|
}
|
2010-06-02 14:55:28 -07:00
|
|
|
}
|