OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.device.geolocation; | |
6 | |
7 import android.content.Context; | |
8 import android.location.Location; | |
9 import android.os.Bundle; | |
10 | |
11 import com.google.android.gms.common.ConnectionResult; | |
12 import com.google.android.gms.common.GoogleApiAvailability; | |
13 import com.google.android.gms.common.api.GoogleApiClient; | |
14 import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; | |
15 import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListe
ner; | |
16 import com.google.android.gms.location.FusedLocationProviderApi; | |
17 import com.google.android.gms.location.LocationListener; | |
18 import com.google.android.gms.location.LocationRequest; | |
19 import com.google.android.gms.location.LocationServices; | |
20 | |
21 import org.chromium.base.Log; | |
22 import org.chromium.base.ThreadUtils; | |
23 | |
24 /** | |
25 * This is a LocationProvider using Google Play Services. | |
26 * | |
27 * https://developers.google.com/android/reference/com/google/android/gms/locati
on/package-summary | |
28 */ | |
29 public class LocationProviderGmsCore implements ConnectionCallbacks, OnConnectio
nFailedListener, | |
30 LocationListener, | |
31 LocationProviderFactory.Location
Provider { | |
32 private static final String TAG = "cr_LocationProvider"; | |
33 | |
34 // Values for the LocationRequest's setInterval for normal and high accuracy
, respectively. | |
35 private static final long UPDATE_INTERVAL_MS = 1000; | |
36 private static final long UPDATE_INTERVAL_FAST_MS = 500; | |
37 | |
38 private final GoogleApiClient mGoogleApiClient; | |
39 private FusedLocationProviderApi mLocationProviderApi = LocationServices.Fus
edLocationApi; | |
40 | |
41 private boolean mEnablehighAccuracy; | |
42 private LocationRequest mLocationRequest; | |
43 | |
44 public static boolean isGooglePlayServicesAvailable(Context context) { | |
45 return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable
(context) | |
46 == ConnectionResult.SUCCESS; | |
47 } | |
48 | |
49 LocationProviderGmsCore(Context context) { | |
50 Log.i(TAG, "Google Play Services"); | |
51 mGoogleApiClient = new GoogleApiClient.Builder(context) | |
52 .addApi(LocationServices.API) | |
53 .addConnectionCallbacks(this) | |
54 .addOnConnectionFailedListener(this) | |
55 .build(); | |
56 assert mGoogleApiClient != null; | |
57 } | |
58 | |
59 LocationProviderGmsCore(GoogleApiClient client, FusedLocationProviderApi loc
ationApi) { | |
60 mGoogleApiClient = client; | |
61 mLocationProviderApi = locationApi; | |
62 } | |
63 | |
64 // ConnectionCallbacks implementation | |
65 @Override | |
66 public void onConnected(Bundle connectionHint) { | |
67 ThreadUtils.assertOnUiThread(); | |
68 | |
69 mLocationRequest = LocationRequest.create(); | |
70 if (mEnablehighAccuracy) { | |
71 mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) | |
72 .setInterval(UPDATE_INTERVAL_FAST_MS); | |
73 } else { | |
74 mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER
_ACCURACY) | |
75 .setInterval(UPDATE_INTERVAL_MS); | |
76 } | |
77 | |
78 final Location location = mLocationProviderApi.getLastLocation(mGoogleAp
iClient); | |
79 if (location != null) { | |
80 LocationProviderAdapter.onNewLocationAvailable(location); | |
81 } | |
82 | |
83 try { | |
84 // Request updates on UI Thread replicating LocationProviderAndroid'
s behaviour. | |
85 mLocationProviderApi.requestLocationUpdates( | |
86 mGoogleApiClient, mLocationRequest, this, ThreadUtils.getUiT
hreadLooper()); | |
87 } catch (IllegalStateException e) { | |
88 // Happens "If this method is executed in a thread that has not call
ed Looper.prepare()" | |
89 Log.e(TAG, "Unexpected exception " + e); | |
90 assert false; | |
91 } | |
92 } | |
93 | |
94 @Override | |
95 public void onConnectionSuspended(int cause) {} | |
96 | |
97 // OnConnectionFailedListener implementation | |
98 @Override | |
99 public void onConnectionFailed(ConnectionResult result) { | |
100 LocationProviderAdapter.newErrorAvailable( | |
101 "Failed to connect to Google Play Services: " + result.toString(
)); | |
102 } | |
103 | |
104 // LocationProviderFactory.LocationProvider implementation | |
105 @Override | |
106 public void start(boolean enableHighAccuracy) { | |
107 ThreadUtils.assertOnUiThread(); | |
108 if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect(); | |
109 | |
110 mEnablehighAccuracy = enableHighAccuracy; | |
111 mGoogleApiClient.connect(); // Should return via onConnected(). | |
112 } | |
113 | |
114 @Override | |
115 public void stop() { | |
116 ThreadUtils.assertOnUiThread(); | |
117 if (!mGoogleApiClient.isConnected()) return; | |
118 | |
119 mLocationProviderApi.removeLocationUpdates(mGoogleApiClient, this); | |
120 | |
121 mGoogleApiClient.disconnect(); | |
122 } | |
123 | |
124 @Override | |
125 public boolean isRunning() { | |
126 assert ThreadUtils.runningOnUiThread(); | |
127 if (mGoogleApiClient == null) return false; | |
128 return mGoogleApiClient.isConnecting() || mGoogleApiClient.isConnected()
; | |
129 } | |
130 | |
131 // LocationListener implementation | |
132 @Override | |
133 public void onLocationChanged(Location location) { | |
134 LocationProviderAdapter.onNewLocationAvailable(location); | |
135 } | |
136 } | |
OLD | NEW |