Skip to content

Commit

Permalink
Merge branch 'release/4.7' into feature/site-settings-review
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyr59h committed Oct 23, 2015
2 parents be9d2c6 + 0939a67 commit def84af
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.wordpress.android.util;

import android.Manifest.permission;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtils {
/**
* Check for permissions, request them if they're not granted.
*
* @return true if permissions are already granted, else request them and return false.
*/
private static boolean checkAndRequestPermissions(Activity activity, int requestCode, String[] permissionList) {
List<String> toRequest = new ArrayList<>();
for (String permission : permissionList) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
toRequest.add(permission);
}
}
if (toRequest.size() > 0) {
String[] requestedPermissions = toRequest.toArray(new String[toRequest.size()]);
ActivityCompat.requestPermissions(activity, requestedPermissions, requestCode);
return false;
}
return true;
}

public static boolean checkAndRequestCameraAndStoragePermissions(Activity activity, int requestCode) {
return checkAndRequestPermissions(activity, requestCode, new String[]{
permission.WRITE_EXTERNAL_STORAGE,
permission.CAMERA
});
}

public static boolean checkAndRequestStoragePermission(Activity activity, int requestCode) {
return checkAndRequestPermissions(activity, requestCode, new String[]{
permission.WRITE_EXTERNAL_STORAGE
});
}

public static boolean checkLocationPermissions(Activity activity, int requestCode) {
return checkAndRequestPermissions(activity, requestCode, new String[]{
permission.ACCESS_FINE_LOCATION,
permission.ACCESS_COARSE_LOCATION
});
}
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,65 @@
//This Handy-Dandy class acquired and tweaked from http://stackoverflow.com/a/3145655/309558
package org.wordpress.android.util.helpers;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import java.util.Timer;
import java.util.TimerTask;

public class LocationHelper {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled = false;
boolean network_enabled = false;

public boolean getLocation(Context context, LocationResult result) {
locationResult = result;
if (lm == null)
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Timer mTimer;
LocationManager mLocationManager;
LocationResult mLocationResult;
boolean mGpsEnabled = false;
boolean mNetworkEnabled = false;

public boolean getLocation(Activity activity, LocationResult result) {


mLocationResult = result;
if (mLocationManager == null) {
mLocationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
}

// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
mGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
mNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}

// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled)
if (!mGpsEnabled && !mNetworkEnabled) {
return false;
}

if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if (mGpsEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
}

if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
if (mNetworkEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
}

timer1 = new Timer();
timer1.schedule(new GetLastLocation(), 30000);
mTimer = new Timer();
mTimer.schedule(new GetLastLocation(), 30000);
return true;
}

LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
mTimer.cancel();
mLocationResult.gotLocation(location);
mLocationManager.removeUpdates(this);
mLocationManager.removeUpdates(locationListenerNetwork);
}

public void onProviderDisabled(String provider) {
Expand All @@ -68,10 +74,10 @@ public void onStatusChanged(String provider, int status, Bundle extras) {

LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
mTimer.cancel();
mLocationResult.gotLocation(location);
mLocationManager.removeUpdates(this);
mLocationManager.removeUpdates(locationListenerGps);
}

public void onProviderDisabled(String provider) {
Expand All @@ -87,34 +93,36 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
mLocationManager.removeUpdates(locationListenerGps);
mLocationManager.removeUpdates(locationListenerNetwork);

Location net_loc = null, gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mGpsEnabled) {
gps_loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (mNetworkEnabled) {
net_loc = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

// if there are both values use the latest one
if (gps_loc != null && net_loc != null) {
if (gps_loc.getTime() > net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
if (gps_loc.getTime() > net_loc.getTime()) {
mLocationResult.gotLocation(gps_loc);
} else {
mLocationResult.gotLocation(net_loc);
}
return;
}

if (gps_loc != null) {
locationResult.gotLocation(gps_loc);
mLocationResult.gotLocation(gps_loc);
return;
}
if (net_loc != null) {
locationResult.gotLocation(net_loc);
mLocationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
mLocationResult.gotLocation(null);
}
}

Expand All @@ -123,10 +131,10 @@ public static abstract class LocationResult {
}

public void cancelTimer() {
if (timer1 != null) {
timer1.cancel();
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
if (mTimer != null) {
mTimer.cancel();
mLocationManager.removeUpdates(locationListenerGps);
mLocationManager.removeUpdates(locationListenerNetwork);
}
}
}

0 comments on commit def84af

Please sign in to comment.