зеркало из https://github.com/mozilla/gecko-dev.git
bug 700354 - Use of AsyncTask needs some cleanup r=mfinkle
This commit is contained in:
Родитель
a0790afc4b
Коммит
728afb5089
|
@ -170,7 +170,7 @@ public class Favicons {
|
||||||
mDbHelper.close();
|
mDbHelper.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class LoadFaviconTask extends AsyncTask<Void, Void, BitmapDrawable> {
|
private class LoadFaviconTask extends GeckoAsyncTask<Void, Void, BitmapDrawable> {
|
||||||
private String mPageUrl;
|
private String mPageUrl;
|
||||||
private String mFaviconUrl;
|
private String mFaviconUrl;
|
||||||
private OnFaviconLoadedListener mListener;
|
private OnFaviconLoadedListener mListener;
|
||||||
|
|
|
@ -341,81 +341,14 @@ abstract public class GeckoApp
|
||||||
if (!checkAndSetLaunchState(LaunchState.Launching, LaunchState.Launched))
|
if (!checkAndSetLaunchState(LaunchState.Launching, LaunchState.Launched))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
class GeckoTask extends AsyncTask<Intent, Void, Void> {
|
|
||||||
protected Void doInBackground(Intent... intents) {
|
|
||||||
Intent intent = intents[0];
|
|
||||||
File cacheFile = GeckoAppShell.getCacheDir();
|
|
||||||
File libxulFile = new File(cacheFile, "libxul.so");
|
|
||||||
|
|
||||||
if ((!libxulFile.exists() ||
|
|
||||||
new File(getApplication().getPackageResourcePath()).lastModified() >= libxulFile.lastModified())) {
|
|
||||||
File[] libs = cacheFile.listFiles(new FilenameFilter() {
|
|
||||||
public boolean accept(File dir, String name) {
|
|
||||||
return name.endsWith(".so");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (libs != null) {
|
|
||||||
for (int i = 0; i < libs.length; i++) {
|
|
||||||
libs[i].delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
|
|
||||||
Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - runGecko");
|
|
||||||
String args = intent.getStringExtra("args");
|
String args = intent.getStringExtra("args");
|
||||||
if (args != null && args.contains("-profile"))
|
if (args != null && args.contains("-profile"))
|
||||||
mUserDefinedProfile = true;
|
mUserDefinedProfile = true;
|
||||||
|
|
||||||
// and then fire us up
|
|
||||||
try {
|
|
||||||
String uri = intent.getDataString();
|
|
||||||
String title = uri;
|
|
||||||
if (!mUserDefinedProfile &&
|
|
||||||
(uri == null || uri.length() == 0)) {
|
|
||||||
SharedPreferences prefs = getSharedPreferences("GeckoApp", MODE_PRIVATE);
|
|
||||||
uri = prefs.getString("last-uri", "");
|
|
||||||
title = prefs.getString("last-title", uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
final String awesomeTitle = title;
|
|
||||||
mMainHandler.post(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
mBrowserToolbar.setTitle(awesomeTitle);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Log.w(LOGTAG, "RunGecko - URI = " + uri);
|
|
||||||
|
|
||||||
GeckoAppShell.runGecko(getApplication().getPackageResourcePath(),
|
|
||||||
args,
|
|
||||||
uri);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e(LOG_NAME, "top level exception", e);
|
|
||||||
StringWriter sw = new StringWriter();
|
|
||||||
PrintWriter pw = new PrintWriter(sw);
|
|
||||||
e.printStackTrace(pw);
|
|
||||||
pw.flush();
|
|
||||||
GeckoAppShell.reportJavaCrash(sw.toString());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (intent == null)
|
if (intent == null)
|
||||||
intent = getIntent();
|
intent = getIntent();
|
||||||
|
|
||||||
new GeckoTask().execute(intent);
|
new GeckoThread(intent).start();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1726,20 +1659,23 @@ abstract public class GeckoApp
|
||||||
GeckoAppShell.sendEventToGecko(new GeckoEvent(event));
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
private class GeocoderTask extends AsyncTask<Location, Void, Void> {
|
private class GeocoderRunnable implements Runnable {
|
||||||
protected Void doInBackground(Location... location) {
|
Location mLocation;
|
||||||
|
GeocoderRunnable (Location location) {
|
||||||
|
mLocation = location;
|
||||||
|
}
|
||||||
|
public void run() {
|
||||||
try {
|
try {
|
||||||
List<Address> addresses = mGeocoder.getFromLocation(location[0].getLatitude(),
|
List<Address> addresses = mGeocoder.getFromLocation(mLocation.getLatitude(),
|
||||||
location[0].getLongitude(), 1);
|
mLocation.getLongitude(), 1);
|
||||||
// grab the first address. in the future,
|
// grab the first address. in the future,
|
||||||
// may want to expose multiple, or filter
|
// may want to expose multiple, or filter
|
||||||
// for best.
|
// for best.
|
||||||
mLastGeoAddress = addresses.get(0);
|
mLastGeoAddress = addresses.get(0);
|
||||||
GeckoAppShell.sendEventToGecko(new GeckoEvent(location[0], mLastGeoAddress));
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(mLocation, mLastGeoAddress));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.w(LOGTAG, "GeocoderTask "+e);
|
Log.w(LOGTAG, "GeocoderTask "+e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1751,7 +1687,7 @@ abstract public class GeckoApp
|
||||||
mGeocoder = new Geocoder(mLayerController.getView().getContext(), Locale.getDefault());
|
mGeocoder = new Geocoder(mLayerController.getView().getContext(), Locale.getDefault());
|
||||||
|
|
||||||
if (mLastGeoAddress == null) {
|
if (mLastGeoAddress == null) {
|
||||||
new GeocoderTask().execute(location);
|
GeckoAppShell.getHandler().post(new GeocoderRunnable(location));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
float[] results = new float[1];
|
float[] results = new float[1];
|
||||||
|
@ -1764,7 +1700,7 @@ abstract public class GeckoApp
|
||||||
// geocoder with very similar values, so
|
// geocoder with very similar values, so
|
||||||
// only call after about 100m
|
// only call after about 100m
|
||||||
if (results[0] > 100)
|
if (results[0] > 100)
|
||||||
new GeocoderTask().execute(location);
|
GeckoAppShell.getHandler().post(new GeocoderRunnable(location));
|
||||||
}
|
}
|
||||||
|
|
||||||
GeckoAppShell.sendEventToGecko(new GeckoEvent(location, mLastGeoAddress));
|
GeckoAppShell.sendEventToGecko(new GeckoEvent(location, mLastGeoAddress));
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
||||||
|
* ***** BEGIN LICENSE BLOCK *****
|
||||||
|
* 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) 2011
|
||||||
|
* the Initial Developer. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Contributor(s):
|
||||||
|
* Brad Lassey <blassey@mozilla.com>
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
// AsyncTask runs onPostExecute on the thread it is constructed on
|
||||||
|
// We construct these off of the main thread, and we want that to run
|
||||||
|
// on the main UI thread, so this is a convenience class to do that
|
||||||
|
public abstract class GeckoAsyncTask<Params, Progress, Result> {
|
||||||
|
public void execute(final Params... params) {
|
||||||
|
GeckoAppShell.getHandler().post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
final Result result = doInBackground(params);
|
||||||
|
GeckoApp.mAppContext.runOnUiThread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
onPostExecute(result);
|
||||||
|
}});
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Result doInBackground(Params... params);
|
||||||
|
protected abstract void onPostExecute(Result result);
|
||||||
|
}
|
|
@ -0,0 +1,128 @@
|
||||||
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
||||||
|
* ***** BEGIN LICENSE BLOCK *****
|
||||||
|
* 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) 2011
|
||||||
|
* the Initial Developer. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Contributor(s):
|
||||||
|
* Brad Lassey <blassey@mozilla.com>
|
||||||
|
*
|
||||||
|
* 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 android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.content.res.Configuration;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.AbsoluteLayout;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class GeckoThread extends Thread {
|
||||||
|
|
||||||
|
private static final String LOGTAG = "GeckoThread";
|
||||||
|
|
||||||
|
Intent mIntent;
|
||||||
|
|
||||||
|
GeckoThread (Intent intent) {
|
||||||
|
mIntent = intent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
final GeckoApp app = GeckoApp.mAppContext;
|
||||||
|
Intent intent = mIntent;
|
||||||
|
File cacheFile = GeckoAppShell.getCacheDir();
|
||||||
|
File libxulFile = new File(cacheFile, "libxul.so");
|
||||||
|
|
||||||
|
if ((!libxulFile.exists() ||
|
||||||
|
new File(app.getApplication().getPackageResourcePath()).lastModified() >= libxulFile.lastModified())) {
|
||||||
|
File[] libs = cacheFile.listFiles(new FilenameFilter() {
|
||||||
|
public boolean accept(File dir, String name) {
|
||||||
|
return name.endsWith(".so");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (libs != null) {
|
||||||
|
for (int i = 0; i < libs.length; i++) {
|
||||||
|
libs[i].delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(
|
||||||
|
app.getApplication().getPackageResourcePath());
|
||||||
|
Locale.setDefault(locale);
|
||||||
|
Resources res = app.getBaseContext().getResources();
|
||||||
|
Configuration config = res.getConfiguration();
|
||||||
|
config.locale = locale;
|
||||||
|
res.updateConfiguration(config, res.getDisplayMetrics());
|
||||||
|
|
||||||
|
Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - runGecko");
|
||||||
|
|
||||||
|
// and then fire us up
|
||||||
|
try {
|
||||||
|
String uri = intent.getDataString();
|
||||||
|
String title = uri;
|
||||||
|
if (!app.mUserDefinedProfile &&
|
||||||
|
(uri == null || uri.length() == 0)) {
|
||||||
|
SharedPreferences prefs = app.getSharedPreferences("GeckoApp", app.MODE_PRIVATE);
|
||||||
|
uri = prefs.getString("last-uri", "");
|
||||||
|
title = prefs.getString("last-title", uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
final String awesomeTitle = title;
|
||||||
|
app.mMainHandler.post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
app.mBrowserToolbar.setTitle(awesomeTitle);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Log.w(LOGTAG, "RunGecko - URI = " + uri);
|
||||||
|
|
||||||
|
GeckoAppShell.runGecko(app.getApplication().getPackageResourcePath(),
|
||||||
|
intent.getStringExtra("args"),
|
||||||
|
uri);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(LOGTAG, "top level exception", e);
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
PrintWriter pw = new PrintWriter(sw);
|
||||||
|
e.printStackTrace(pw);
|
||||||
|
pw.flush();
|
||||||
|
GeckoAppShell.reportJavaCrash(sw.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -68,6 +68,8 @@ JAVAFILES = \
|
||||||
Tabs.java \
|
Tabs.java \
|
||||||
TabsTray.java \
|
TabsTray.java \
|
||||||
GeckoBatteryManager.java \
|
GeckoBatteryManager.java \
|
||||||
|
GeckoThread.java \
|
||||||
|
GeckoAsyncTask.java \
|
||||||
gfx/BufferedCairoImage.java \
|
gfx/BufferedCairoImage.java \
|
||||||
gfx/CairoImage.java \
|
gfx/CairoImage.java \
|
||||||
gfx/CairoUtils.java \
|
gfx/CairoUtils.java \
|
||||||
|
|
|
@ -42,7 +42,6 @@ import android.content.ContentValues;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.provider.Browser;
|
import android.provider.Browser;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
@ -162,10 +161,10 @@ public class Tab {
|
||||||
if (he != null) {
|
if (he != null) {
|
||||||
he.mTitle = mTitle;
|
he.mTitle = mTitle;
|
||||||
GeckoAppShell.getHandler().post(new Runnable() {
|
GeckoAppShell.getHandler().post(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
GlobalHistory.getInstance().update(he.mUri, he.mTitle);
|
GlobalHistory.getInstance().update(he.mUri, he.mTitle);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Log.e(LOG_NAME, "Requested title update on empty history stack");
|
Log.e(LOG_NAME, "Requested title update on empty history stack");
|
||||||
}
|
}
|
||||||
|
@ -280,10 +279,10 @@ public class Tab {
|
||||||
HistoryEntry he = new HistoryEntry(uri, null);
|
HistoryEntry he = new HistoryEntry(uri, null);
|
||||||
mHistory.add(he);
|
mHistory.add(he);
|
||||||
GeckoAppShell.getHandler().post(new Runnable() {
|
GeckoAppShell.getHandler().post(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
GlobalHistory.getInstance().add(uri);
|
GlobalHistory.getInstance().add(uri);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (event.equals("Back")) {
|
} else if (event.equals("Back")) {
|
||||||
if (mHistoryIndex - 1 < 0) {
|
if (mHistoryIndex - 1 < 0) {
|
||||||
Log.e(LOG_NAME, "Received unexpected back notification");
|
Log.e(LOG_NAME, "Received unexpected back notification");
|
||||||
|
@ -309,7 +308,7 @@ public class Tab {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class CheckBookmarkTask extends AsyncTask<Void, Void, Boolean> {
|
private class CheckBookmarkTask extends GeckoAsyncTask<Void, Void, Boolean> {
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(Void... unused) {
|
protected Boolean doInBackground(Void... unused) {
|
||||||
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
||||||
|
@ -332,7 +331,7 @@ public class Tab {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AddBookmarkTask extends AsyncTask<Void, Void, Void> {
|
private class AddBookmarkTask extends GeckoAsyncTask<Void, Void, Void> {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... unused) {
|
protected Void doInBackground(Void... unused) {
|
||||||
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
||||||
|
@ -370,7 +369,7 @@ public class Tab {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class RemoveBookmarkTask extends AsyncTask<Void, Void, Void> {
|
private class RemoveBookmarkTask extends GeckoAsyncTask<Void, Void, Void> {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... unused) {
|
protected Void doInBackground(Void... unused) {
|
||||||
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
||||||
|
|
Загрузка…
Ссылка в новой задаче