Example usage for android.location Location setExtras

List of usage examples for android.location Location setExtras

Introduction

In this page you can find the example usage for android.location Location setExtras.

Prototype

public void setExtras(Bundle extras) 

Source Link

Document

Sets the extra information associated with this fix to the given Bundle.

Usage

From source file:org.fitchfamily.android.wifi_backend.backend.BackendService.java

private Location wiFiBasedLocation(@NonNull List<WifiAccessPoint> accessPoints) {
    if (accessPoints.isEmpty()) {
        return null;
    } else {/*from w w w .  j a  va  2  s  . c o  m*/
        Set<Location> locations = new HashSet<>(accessPoints.size());

        for (WifiAccessPoint accessPoint : accessPoints) {
            SimpleLocation result = database.getLocation(accessPoint.rfId());

            if (result != null) {
                Bundle extras = new Bundle();
                extras.putInt(Configuration.EXTRA_SIGNAL_LEVEL, accessPoint.level());

                Location location = result.toAndroidLocation();
                location.setExtras(extras);
                locations.add(location);
            }
        }

        if (locations.isEmpty()) {
            if (DEBUG) {
                Log.i(TAG, "WifiDBResolver.process(): No APs with known locations");
            }
            return null;
        } else {
            // Find largest group of AP locations. If we don't have at
            // least two near each other then we don't have enough
            // information to get a good location.
            locations = LocationUtil.culledAPs(locations, BackendService.this);

            if (locations == null || locations.size() < 2) {
                if (DEBUG) {
                    Log.i(TAG,
                            "WifiDBResolver.process(): Insufficient number of WiFi hotspots to resolve location");
                }
                return null;
            } else {
                Location avgLoc = LocationUtil.weightedAverage("wifi", locations);

                if (avgLoc == null) {
                    if (DEBUG) {
                        Log.e(TAG, "Averaging locations did not work.");
                    }
                    return null;
                } else {
                    avgLoc.setTime(System.currentTimeMillis());
                    return avgLoc;
                }
            }
        }
    }
}

From source file:org.openbmap.unifiedNlp.Geocoder.OnlineProvider.java

/**
 * Queries location for list of wifis/*ww w  . j  a va2  s .co m*/
 */
@SuppressWarnings("unchecked")
@Override
public void getLocation(List<ScanResult> wifisList, List<Cell> cellsList) {
    ArrayList<String> wifis = new ArrayList<>();

    if (wifisList != null) {
        // Generates a list of wifis from scan results
        for (ScanResult r : wifisList) {
            if ((r.BSSID != null) & !(r.SSID.endsWith("_nomap"))) {
                wifis.add(r.BSSID);
            }
        }
        Log.i(TAG, "Using " + wifis.size() + " wifis for geolocation");
    } else
        Log.i(TAG, "No wifis supplied for geolocation");

    new AsyncTask<Object, Void, JSONObject>() {

        @Override
        protected JSONObject doInBackground(Object... params) {
            if (params == null) {
                throw new IllegalArgumentException("Wifi list was null");
            }
            mWifiQuery = (ArrayList<String>) params[0];
            mCellQuery = new ArrayList<>();
            for (Cell temp : (List<Cell>) params[1]) {
                mCellQuery.add(temp.toString());
            }

            Random r = new Random();
            int idx = r.nextInt(3);

            final String balancer = String.format(REQUEST_URL, new String[] { "a", "b", "c" }[idx]);
            if (mDebug) {
                Log.v(TAG, "Using balancer " + balancer);
            }
            return loadJSON(balancer, (ArrayList<String>) params[0], (List<Cell>) params[1]);
        }

        @Override
        protected void onPostExecute(JSONObject jsonData) {
            if (jsonData == null) {
                Log.e(TAG, "JSON data was null");
                return;
            }

            try {
                Log.i(TAG, "JSON response: " + jsonData.toString());
                String source = jsonData.getString("source");
                JSONObject location = jsonData.getJSONObject("location");
                Double lat = location.getDouble("lat");
                Double lon = location.getDouble("lng");
                Long acc = jsonData.getLong("accuracy");
                Location result = new Location(TAG);
                result.setLatitude(lat);
                result.setLongitude(lon);
                result.setAccuracy(acc);
                result.setTime(System.currentTimeMillis());

                Bundle b = new Bundle();
                b.putString("source", source);
                b.putStringArrayList("bssids", mWifiQuery);
                b.putStringArrayList("cells", mCellQuery);
                result.setExtras(b);

                if (plausibleLocationUpdate(result)) {
                    setLastLocation(result);
                    setLastFix(System.currentTimeMillis());
                    mListener.onLocationReceived(result);
                } else {
                    Log.i(TAG, "Strange location, ignoring");
                }
            } catch (JSONException e) {
                Log.e(TAG, "Error parsing JSON:" + e.getMessage());
            }
        }

        public JSONObject loadJSON(String url, ArrayList<String> wifiParams, List<Cell> cellParams) {
            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();
            JSONObject params = buildParams(wifiParams, cellParams);
            return jParser.getJSONFromUrl(url, params);
        }

        /**
         * Builds a JSON array with cell and wifi query
         * @param wifis ArrayList containing bssids
         * @param cells
         * @return JSON object
         */
        public JSONObject buildParams(ArrayList<String> wifis, List<Cell> cells) {
            JSONObject root = new JSONObject();
            try {
                // add wifi objects
                JSONArray jsonArray = new JSONArray();
                if (mDebug) {
                    JSONObject object = new JSONObject();
                    object.put("debug", "1");
                    jsonArray.put(object);
                }

                for (String s : wifis) {
                    JSONObject object = new JSONObject();
                    object.put("macAddress", s);
                    object.put("signalStrength", "-54");
                    jsonArray.put(object);
                }
                if (jsonArray.length() > 0) {
                    root.put("wifiAccessPoints", jsonArray);
                }

                // add cell objects
                jsonArray = new JSONArray();
                for (Cell s : cells) {
                    JSONObject object = new JSONObject();
                    object.put("cellId", s.cellId);
                    object.put("locationAreaCode", s.area);
                    object.put("mobileCountryCode", s.mcc);
                    object.put("mobileNetworkCode", s.mnc);
                    jsonArray.put(object);
                }
                if (jsonArray.length() > 0) {
                    root.put("cellTowers", jsonArray);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.v(TAG, "Query param: " + root.toString());
            return root;
        }
    }.execute(wifis, cellsList);
}

From source file:com.vonglasow.michael.satstat.MainActivity.java

public static void markLocationAsStale(Location location) {
    if (location.getExtras() == null)
        location.setExtras(new Bundle());
    location.getExtras().putBoolean(KEY_LOCATION_STALE, true);
}