Bug 713687 - Part 3 - Network API Android backend: make GeckoNetworkManager a singleton. r=dougt

This commit is contained in:
Mounir Lamouri 2012-01-16 18:17:34 +01:00
Родитель 2c8aad6487
Коммит d722304bf8
6 изменённых файлов: 70 добавлений и 48 удалений

Просмотреть файл

@ -88,8 +88,6 @@ abstract public class GeckoApp
private IntentFilter mConnectivityFilter;
private BroadcastReceiver mConnectivityReceiver;
private BroadcastReceiver mBatteryReceiver;
private IntentFilter mNetworkFilter;
private GeckoNetworkManager mNetworkReceiver;
enum LaunchState {PreLaunch, Launching, WaitForDebugger,
Launched, GeckoRunning, GeckoExiting};
@ -419,10 +417,7 @@ abstract public class GeckoApp
SmsManager.getInstance().init();
}
mNetworkFilter = new IntentFilter();
mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mNetworkReceiver = new GeckoNetworkManager();
mNetworkReceiver.initialize();
GeckoNetworkManager.getInstance().init();
if (!checkAndSetLaunchState(LaunchState.PreLaunch,
LaunchState.Launching))
@ -516,7 +511,7 @@ abstract public class GeckoApp
super.onPause();
unregisterReceiver(mConnectivityReceiver);
unregisterReceiver(mNetworkReceiver);
GeckoNetworkManager.getInstance().stop();
}
@Override
@ -535,8 +530,7 @@ abstract public class GeckoApp
onNewIntent(getIntent());
registerReceiver(mConnectivityReceiver, mConnectivityFilter);
mNetworkReceiver.resume();
registerReceiver(mNetworkReceiver, mNetworkFilter);
GeckoNetworkManager.getInstance().start();
}
@Override
@ -589,10 +583,11 @@ abstract public class GeckoApp
SmsManager.getInstance().shutdown();
}
GeckoNetworkManager.getInstance().stop();
super.onDestroy();
unregisterReceiver(mBatteryReceiver);
unregisterReceiver(mNetworkReceiver);
}
@Override

Просмотреть файл

@ -1778,6 +1778,6 @@ public class GeckoAppShell
}
public static double[] getCurrentNetworkInformation() {
return GeckoNetworkManager.getCurrentInformation();
return GeckoNetworkManager.getInstance().getCurrentInformation();
}
}

Просмотреть файл

@ -44,6 +44,7 @@ import android.util.Log;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
@ -96,6 +97,8 @@ import android.telephony.TelephonyManager;
public class GeckoNetworkManager
extends BroadcastReceiver
{
static private final GeckoNetworkManager sInstance = new GeckoNetworkManager();
static private final double kDefaultBandwidth = -1.0;
static private final boolean kDefaultCanBeMetered = false;
@ -127,36 +130,49 @@ public class GeckoNetworkManager
NETWORK_UNKNOWN
}
static private NetworkType sNetworkType = NetworkType.NETWORK_NONE;
private NetworkType mNetworkType = NetworkType.NETWORK_NONE;
private IntentFilter mNetworkFilter = new IntentFilter();
public static GeckoNetworkManager getInstance() {
return sInstance;
}
@Override
public void onReceive(Context aContext, Intent aIntent) {
updateNetworkType();
}
public static void initialize() {
sNetworkType = getNetworkType();
public void init() {
mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mNetworkType = getNetworkType();
}
public static void resume() {
public void start() {
updateNetworkType();
GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter);
}
private static void updateNetworkType() {
NetworkType previousNetworkType = sNetworkType;
sNetworkType = getNetworkType();
public void stop() {
GeckoApp.mAppContext.unregisterReceiver(sInstance);
}
if (sNetworkType == previousNetworkType) {
private void updateNetworkType() {
NetworkType previousNetworkType = mNetworkType;
mNetworkType = getNetworkType();
if (mNetworkType == previousNetworkType) {
return;
}
GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(sNetworkType),
isNetworkUsuallyMetered(sNetworkType)));
GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(mNetworkType),
isNetworkUsuallyMetered(mNetworkType)));
}
public static double[] getCurrentInformation() {
return new double[] { getNetworkSpeed(sNetworkType),
isNetworkUsuallyMetered(sNetworkType) ? 1.0 : 0.0 };
public double[] getCurrentInformation() {
return new double[] { getNetworkSpeed(mNetworkType),
isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 };
}
private static NetworkType getNetworkType() {

Просмотреть файл

@ -119,11 +119,9 @@ abstract public class GeckoApp
private IntentFilter mConnectivityFilter;
private IntentFilter mBatteryFilter;
private IntentFilter mNetworkFilter;
private BroadcastReceiver mConnectivityReceiver;
private BroadcastReceiver mBatteryReceiver;
private GeckoNetworkManager mNetworkReceiver;
public static BrowserToolbar mBrowserToolbar;
public static DoorHangerPopup mDoorHangerPopup;
@ -1576,10 +1574,7 @@ abstract public class GeckoApp
SmsManager.getInstance().init();
}
mNetworkFilter = new IntentFilter();
mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mNetworkReceiver = new GeckoNetworkManager();
mNetworkReceiver.initialize();
GeckoNetworkManager.getInstance().init();
final GeckoApp self = this;
@ -1711,7 +1706,7 @@ abstract public class GeckoApp
super.onPause();
unregisterReceiver(mConnectivityReceiver);
unregisterReceiver(mNetworkReceiver);
GeckoNetworkManager.getInstance().stop();
}
@Override
@ -1729,8 +1724,7 @@ abstract public class GeckoApp
onNewIntent(getIntent());
registerReceiver(mConnectivityReceiver, mConnectivityFilter);
mNetworkReceiver.resume();
registerReceiver(mNetworkReceiver, mNetworkFilter);
GeckoNetworkManager.getInstance().start();
if (mOwnActivityDepth > 0)
mOwnActivityDepth--;
@ -1813,10 +1807,11 @@ abstract public class GeckoApp
SmsManager.getInstance().shutdown();
}
GeckoNetworkManager.getInstance().stop();
super.onDestroy();
unregisterReceiver(mBatteryReceiver);
unregisterReceiver(mNetworkReceiver);
}
@Override

Просмотреть файл

@ -1749,6 +1749,6 @@ public class GeckoAppShell
}
public static double[] getCurrentNetworkInformation() {
return GeckoNetworkManager.getCurrentInformation();
return GeckoNetworkManager.getInstance().getCurrentInformation();
}
}

Просмотреть файл

@ -44,6 +44,7 @@ import android.util.Log;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
@ -96,6 +97,8 @@ import android.telephony.TelephonyManager;
public class GeckoNetworkManager
extends BroadcastReceiver
{
static private final GeckoNetworkManager sInstance = new GeckoNetworkManager();
static private final double kDefaultBandwidth = -1.0;
static private final boolean kDefaultCanBeMetered = false;
@ -127,36 +130,49 @@ public class GeckoNetworkManager
NETWORK_UNKNOWN
}
static private NetworkType sNetworkType = NetworkType.NETWORK_NONE;
private NetworkType mNetworkType = NetworkType.NETWORK_NONE;
private IntentFilter mNetworkFilter = new IntentFilter();
public static GeckoNetworkManager getInstance() {
return sInstance;
}
@Override
public void onReceive(Context aContext, Intent aIntent) {
updateNetworkType();
}
public static void initialize() {
sNetworkType = getNetworkType();
public void init() {
mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mNetworkType = getNetworkType();
}
public static void resume() {
public void start() {
updateNetworkType();
GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter);
}
private static void updateNetworkType() {
NetworkType previousNetworkType = sNetworkType;
sNetworkType = getNetworkType();
public void stop() {
GeckoApp.mAppContext.unregisterReceiver(sInstance);
}
if (sNetworkType == previousNetworkType) {
private void updateNetworkType() {
NetworkType previousNetworkType = mNetworkType;
mNetworkType = getNetworkType();
if (mNetworkType == previousNetworkType) {
return;
}
GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(sNetworkType),
isNetworkUsuallyMetered(sNetworkType)));
GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(mNetworkType),
isNetworkUsuallyMetered(mNetworkType)));
}
public static double[] getCurrentInformation() {
return new double[] { getNetworkSpeed(sNetworkType),
isNetworkUsuallyMetered(sNetworkType) ? 1.0 : 0.0 };
public double[] getCurrentInformation() {
return new double[] { getNetworkSpeed(mNetworkType),
isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 };
}
private static NetworkType getNetworkType() {