Use application context for all background tasks.

This commit is contained in:
akwizgran
2014-03-06 18:25:37 +00:00
parent b4c9f278f9
commit b5429f121a
2 changed files with 13 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ import org.briarproject.plugins.droidtooth.DroidtoothPluginFactory;
import org.briarproject.plugins.tcp.LanTcpPluginFactory; import org.briarproject.plugins.tcp.LanTcpPluginFactory;
import org.briarproject.plugins.tor.TorPluginFactory; import org.briarproject.plugins.tor.TorPluginFactory;
import android.app.Application;
import android.content.Context; import android.content.Context;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
@@ -39,14 +40,14 @@ public class AndroidPluginsModule extends AbstractModule {
@Provides @Provides
DuplexPluginConfig getDuplexPluginConfig( DuplexPluginConfig getDuplexPluginConfig(
@PluginExecutor Executor pluginExecutor, @PluginExecutor Executor pluginExecutor,
AndroidExecutor androidExecutor, Context appContext, AndroidExecutor androidExecutor, Application app,
CryptoComponent crypto, LocationUtils locationUtils, CryptoComponent crypto, LocationUtils locationUtils,
ShutdownManager shutdownManager) { ShutdownManager shutdownManager) {
Context ctx = app.getApplicationContext();
DuplexPluginFactory droidtooth = new DroidtoothPluginFactory( DuplexPluginFactory droidtooth = new DroidtoothPluginFactory(
pluginExecutor, androidExecutor, appContext, pluginExecutor, androidExecutor, ctx, crypto.getSecureRandom());
crypto.getSecureRandom());
DuplexPluginFactory tor = new TorPluginFactory(pluginExecutor, DuplexPluginFactory tor = new TorPluginFactory(pluginExecutor,
appContext, locationUtils, shutdownManager); ctx, locationUtils, shutdownManager);
DuplexPluginFactory lan = new LanTcpPluginFactory(pluginExecutor); DuplexPluginFactory lan = new LanTcpPluginFactory(pluginExecutor);
final Collection<DuplexPluginFactory> factories = final Collection<DuplexPluginFactory> factories =
Arrays.asList(droidtooth, tor, lan); Arrays.asList(droidtooth, tor, lan);

View File

@@ -11,6 +11,7 @@ import java.util.logging.Logger;
import org.briarproject.api.system.LocationUtils; import org.briarproject.api.system.LocationUtils;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context; import android.content.Context;
import android.location.Address; import android.location.Address;
import android.location.Geocoder; import android.location.Geocoder;
@@ -26,11 +27,11 @@ class AndroidLocationUtils implements LocationUtils {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(AndroidLocationUtils.class.getName()); Logger.getLogger(AndroidLocationUtils.class.getName());
private final Context ctx; private final Context appContext;
@Inject @Inject
public AndroidLocationUtils(Context ctx) { public AndroidLocationUtils(Application app) {
this.ctx = ctx; appContext = app.getApplicationContext();
} }
/** /**
@@ -69,13 +70,13 @@ class AndroidLocationUtils implements LocationUtils {
} }
private String getCountryFromPhoneNetwork() { private String getCountryFromPhoneNetwork() {
Object o = ctx.getSystemService(TELEPHONY_SERVICE); Object o = appContext.getSystemService(TELEPHONY_SERVICE);
TelephonyManager tm = (TelephonyManager) o; TelephonyManager tm = (TelephonyManager) o;
return tm.getNetworkCountryIso(); return tm.getNetworkCountryIso();
} }
private String getCountryFromSimCard() { private String getCountryFromSimCard() {
Object o = ctx.getSystemService(TELEPHONY_SERVICE); Object o = appContext.getSystemService(TELEPHONY_SERVICE);
TelephonyManager tm = (TelephonyManager) o; TelephonyManager tm = (TelephonyManager) o;
return tm.getSimCountryIso(); return tm.getSimCountryIso();
} }
@@ -86,7 +87,7 @@ class AndroidLocationUtils implements LocationUtils {
private String getCountryFromLocation() { private String getCountryFromLocation() {
Location location = getLastKnownLocation(); Location location = getLastKnownLocation();
if(location == null) return null; if(location == null) return null;
Geocoder code = new Geocoder(ctx); Geocoder code = new Geocoder(appContext);
try { try {
double lat = location.getLatitude(); double lat = location.getLatitude();
double lon = location.getLongitude(); double lon = location.getLongitude();
@@ -106,7 +107,7 @@ class AndroidLocationUtils implements LocationUtils {
* this</a>. * this</a>.
*/ */
private Location getLastKnownLocation() { private Location getLastKnownLocation() {
Object o = ctx.getSystemService(LOCATION_SERVICE); Object o = appContext.getSystemService(LOCATION_SERVICE);
LocationManager locationManager = (LocationManager) o; LocationManager locationManager = (LocationManager) o;
Location bestResult = null; Location bestResult = null;
long bestTime = Long.MIN_VALUE; long bestTime = Long.MIN_VALUE;