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 private final GoogleApiClient mGoogleApiClient; | |
35 private FusedLocationProviderApi mLocationProviderApi = LocationServices.Fus edLocationApi; | |
36 | |
37 private boolean mEnablehighAccuracy; | |
38 private LocationRequest mLocationRequest; | |
39 | |
40 public static boolean isGooglePlayServicesAvailable(Context context) { | |
41 return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable (context) | |
42 == ConnectionResult.SUCCESS; | |
43 } | |
44 | |
45 LocationProviderGmsCore(Context context) { | |
46 Log.i(TAG, "Google Play Services"); | |
47 mGoogleApiClient = new GoogleApiClient.Builder(context) | |
48 .addApi(LocationServices.API) | |
49 .addConnectionCallbacks(this) | |
50 .addOnConnectionFailedListener(this) | |
51 .build(); | |
52 assert mGoogleApiClient != null; | |
53 } | |
54 | |
55 LocationProviderGmsCore(GoogleApiClient client, FusedLocationProviderApi loc ationApi) { | |
56 mGoogleApiClient = client; | |
57 mLocationProviderApi = locationApi; | |
58 } | |
59 | |
60 // ConnectionCallbacks implementation | |
61 @Override | |
62 public void onConnected(Bundle connectionHint) { | |
63 ThreadUtils.assertOnUiThread(); | |
64 | |
65 mLocationRequest = LocationRequest.create(); | |
66 if (mEnablehighAccuracy) { | |
67 mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) ; | |
68 } else { | |
69 mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER _ACCURACY); | |
70 } | |
71 | |
72 final Location location = mLocationProviderApi.getLastLocation(mGoogleAp iClient); | |
73 if (location != null) { | |
74 LocationProviderAdapter.onNewLocationAvailable(location); | |
75 } | |
76 | |
77 try { | |
78 // Request updates on UI Thread replicating LocationProviderAndroid' s behaviour. | |
79 mLocationProviderApi.requestLocationUpdates( | |
80 mGoogleApiClient, mLocationRequest, this, ThreadUtils.getUiT hreadLooper()); | |
81 } catch (IllegalStateException e) { | |
82 // Happens "If this method is executed in a thread that has not call ed Looper.prepare()" | |
83 Log.e(TAG, "Unexpected exception " + e); | |
84 assert false; | |
85 } | |
86 } | |
87 | |
88 @Override | |
89 public void onConnectionSuspended(int cause) {} | |
90 | |
91 // OnConnectionFailedListener implementation | |
92 @Override | |
93 public void onConnectionFailed(ConnectionResult result) { | |
94 LocationProviderAdapter.newErrorAvailable( | |
95 "Failed to connect to Google Play Services :" + result.toString( )); | |
agrieve
2017/04/20 20:39:43
nit: remove space before :
mcasas
2017/04/20 21:50:27
Done.
| |
96 } | |
97 | |
98 // LocationProviderFactory.LocationProvider implementation | |
99 @Override | |
100 public void start(boolean enableHighAccuracy) { | |
101 ThreadUtils.assertOnUiThread(); | |
102 if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect(); | |
103 | |
104 mEnablehighAccuracy = enableHighAccuracy; | |
105 mGoogleApiClient.connect(); // Should return via onConnected(). | |
106 } | |
107 | |
108 @Override | |
109 public void stop() { | |
110 ThreadUtils.assertOnUiThread(); | |
111 if (!mGoogleApiClient.isConnected()) return; | |
112 | |
113 mLocationProviderApi.removeLocationUpdates(mGoogleApiClient, this); | |
114 | |
115 mGoogleApiClient.disconnect(); | |
116 } | |
117 | |
118 @Override | |
119 public boolean isRunning() { | |
120 assert ThreadUtils.runningOnUiThread(); | |
121 if (mGoogleApiClient == null) return false; | |
122 return mGoogleApiClient.isConnecting() || mGoogleApiClient.isConnected() ; | |
123 } | |
124 | |
125 // LocationListener implementation | |
126 @Override | |
127 public void onLocationChanged(Location location) { | |
128 LocationProviderAdapter.onNewLocationAvailable(location); | |
129 } | |
130 } | |
OLD | NEW |