Search in sources :

Example 1 with LocationPackage

use of com.facebook.places.internal.LocationPackage in project facebook-android-sdk by facebook.

the class PlaceManager method getCurrentPlaceParameters.

private static Bundle getCurrentPlaceParameters(CurrentPlaceRequestParams request, LocationPackage locationPackage) throws FacebookException {
    if (request == null) {
        throw new FacebookException("Request and location must be specified.");
    }
    if (locationPackage == null) {
        locationPackage = new LocationPackage();
    }
    if (locationPackage.location == null) {
        locationPackage.location = request.getLocation();
    }
    if (locationPackage.location == null) {
        throw new FacebookException("A location must be specified");
    }
    try {
        Bundle parameters = new Bundle(6);
        parameters.putString(PARAM_SUMMARY, PARAM_TRACKING);
        int limit = request.getLimit();
        if (limit > 0) {
            parameters.putInt(PARAM_LIMIT, limit);
        }
        Set<String> fields = request.getFields();
        if (fields != null && !fields.isEmpty()) {
            parameters.putString(PARAM_FIELDS, TextUtils.join(",", fields));
        }
        // Coordinates.
        Location location = locationPackage.location;
        JSONObject coordinates = new JSONObject();
        coordinates.put(PARAM_LATITUDE, location.getLatitude());
        coordinates.put(PARAM_LONGITUDE, location.getLongitude());
        if (location.hasAccuracy()) {
            coordinates.put(PARAM_ACCURACY, location.getAccuracy());
        }
        if (location.hasAltitude()) {
            coordinates.put(PARAM_ALTITUDE, location.getAltitude());
        }
        if (location.hasBearing()) {
            coordinates.put(PARAM_HEADING, location.getBearing());
        }
        if (location.hasSpeed()) {
            coordinates.put(PARAM_SPEED, location.getSpeed());
        }
        parameters.putString(PARAM_COORDINATES, coordinates.toString());
        // min confidence level
        ConfidenceLevel minConfidenceLevel = request.getMinConfidenceLevel();
        if (minConfidenceLevel == ConfidenceLevel.LOW || minConfidenceLevel == ConfidenceLevel.MEDIUM || minConfidenceLevel == ConfidenceLevel.HIGH) {
            String minConfidenceLevelString = minConfidenceLevel.toString().toLowerCase(Locale.US);
            parameters.putString(PARAM_MIN_CONFIDENCE_LEVEL, minConfidenceLevelString);
        }
        if (locationPackage != null) {
            // wifi
            JSONObject wifi = new JSONObject();
            wifi.put(PARAM_ENABLED, locationPackage.isWifiScanningEnabled);
            WifiScanResult connectedWifi = locationPackage.connectedWifi;
            if (connectedWifi != null) {
                wifi.put(PARAM_CURRENT_CONNECTION, getWifiScanJson(connectedWifi));
            }
            List<WifiScanResult> ambientWifi = locationPackage.ambientWifi;
            if (ambientWifi != null) {
                JSONArray array = new JSONArray();
                for (WifiScanResult wifiScanResult : ambientWifi) {
                    array.put(getWifiScanJson(wifiScanResult));
                }
                wifi.put(PARAM_ACCESS_POINTS, array);
            }
            parameters.putString(PARAM_WIFI, wifi.toString());
            // bluetooth
            JSONObject bluetooth = new JSONObject();
            bluetooth.put(PARAM_ENABLED, locationPackage.isBluetoothScanningEnabled);
            List<BluetoothScanResult> bluetoothScanResults = locationPackage.ambientBluetoothLe;
            if (bluetoothScanResults != null) {
                JSONArray array = new JSONArray();
                for (BluetoothScanResult bluetoothScanResult : bluetoothScanResults) {
                    JSONObject bluetoothData = new JSONObject();
                    bluetoothData.put(PARAM_PAYLOAD, bluetoothScanResult.payload);
                    bluetoothData.put(PARAM_RSSI, bluetoothScanResult.rssi);
                    array.put(bluetoothData);
                }
                bluetooth.put(PARAM_SCANS, array);
            }
            parameters.putString(PARAM_BLUETOOTH, bluetooth.toString());
        }
        return parameters;
    } catch (JSONException ex) {
        throw new FacebookException(ex);
    }
}
Also used : WifiScanResult(com.facebook.places.internal.WifiScanResult) LocationPackage(com.facebook.places.internal.LocationPackage) Bundle(android.os.Bundle) BluetoothScanResult(com.facebook.places.internal.BluetoothScanResult) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ConfidenceLevel(com.facebook.places.model.CurrentPlaceRequestParams.ConfidenceLevel) JSONObject(org.json.JSONObject) FacebookException(com.facebook.FacebookException) Location(android.location.Location)

Example 2 with LocationPackage

use of com.facebook.places.internal.LocationPackage in project facebook-android-sdk by facebook.

the class PlaceManager method newCurrentPlaceRequest.

/**
 * Creates a new current place request.
 * <p>
 * The current place request estimates the place where the user is currently located.
 * The response contains a list of places and their associated confidence levels.
 * <p>
 * If a location is not specified in {@link CurrentPlaceRequestParams}, then the SDK
 * retrieves the current location using {@link android.location.LocationManager}.
 *
 * @param requestParams the request parameters. See {@link CurrentPlaceRequestParams}
 * @param callback a {@link OnRequestReadyCallback} that is invoked when the
 * {@link GraphRequest} has been created and is ready to be executed.
 */
public static void newCurrentPlaceRequest(final CurrentPlaceRequestParams requestParams, final OnRequestReadyCallback callback) {
    Location location = requestParams.getLocation();
    CurrentPlaceRequestParams.ScanMode scanMode = requestParams.getScanMode();
    LocationPackageRequestParams.Builder builder = new LocationPackageRequestParams.Builder();
    // Don't scan for a location if one is provided.
    builder.setLocationScanEnabled(location == null);
    if (scanMode != null && scanMode == CurrentPlaceRequestParams.ScanMode.LOW_LATENCY) {
        // In low-latency mode, avoid active Wi-Fi scanning which can takes
        // several seconds.
        builder.setWifiActiveScanAllowed(false);
    }
    LocationPackageManager.requestLocationPackage(builder.build(), new LocationPackageManager.Listener() {

        @Override
        public void onLocationPackage(LocationPackage locationPackage) {
            if (locationPackage.locationError != null) {
                callback.onLocationError(getLocationError(locationPackage.locationError));
            } else {
                Bundle parameters = getCurrentPlaceParameters(requestParams, locationPackage);
                GraphRequest graphRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), CURRENT_PLACE_RESULTS, parameters, HttpMethod.GET);
                callback.onRequestReady(graphRequest);
            }
        }
    });
}
Also used : LocationPackageRequestParams(com.facebook.places.internal.LocationPackageRequestParams) GraphRequest(com.facebook.GraphRequest) LocationPackage(com.facebook.places.internal.LocationPackage) Bundle(android.os.Bundle) CurrentPlaceRequestParams(com.facebook.places.model.CurrentPlaceRequestParams) LocationPackageManager(com.facebook.places.internal.LocationPackageManager) Location(android.location.Location)

Aggregations

Location (android.location.Location)2 Bundle (android.os.Bundle)2 LocationPackage (com.facebook.places.internal.LocationPackage)2 FacebookException (com.facebook.FacebookException)1 GraphRequest (com.facebook.GraphRequest)1 BluetoothScanResult (com.facebook.places.internal.BluetoothScanResult)1 LocationPackageManager (com.facebook.places.internal.LocationPackageManager)1 LocationPackageRequestParams (com.facebook.places.internal.LocationPackageRequestParams)1 WifiScanResult (com.facebook.places.internal.WifiScanResult)1 CurrentPlaceRequestParams (com.facebook.places.model.CurrentPlaceRequestParams)1 ConfidenceLevel (com.facebook.places.model.CurrentPlaceRequestParams.ConfidenceLevel)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1