|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Mantas Palaima |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.palaima.debugdrawer.location; |
| 18 | + |
| 19 | +import com.google.android.gms.common.ConnectionResult; |
| 20 | +import com.google.android.gms.common.api.GoogleApiClient; |
| 21 | +import com.google.android.gms.location.LocationListener; |
| 22 | +import com.google.android.gms.location.LocationRequest; |
| 23 | +import com.google.android.gms.location.LocationServices; |
| 24 | + |
| 25 | +import android.content.Context; |
| 26 | +import android.location.Location; |
| 27 | +import android.os.Bundle; |
| 28 | +import android.util.Log; |
| 29 | + |
| 30 | +public class LocationController implements GoogleApiClient.ConnectionCallbacks, |
| 31 | + GoogleApiClient.OnConnectionFailedListener { |
| 32 | + |
| 33 | + private LocationListener mLocationListener; |
| 34 | + |
| 35 | + private static LocationController instance; |
| 36 | + |
| 37 | + |
| 38 | + private GoogleApiClient mGoogleApiClient; |
| 39 | + |
| 40 | + private LocationRequest mLocationRequest; |
| 41 | + |
| 42 | + private boolean mIsStarted; |
| 43 | + |
| 44 | + private boolean mConnected; |
| 45 | + |
| 46 | + public static LocationController newInstance(Context context) { |
| 47 | + if (instance == null) |
| 48 | + instance = new LocationController(context); |
| 49 | + return instance; |
| 50 | + } |
| 51 | + |
| 52 | + private LocationController(Context context) { |
| 53 | + buildGoogleApiClient(context); |
| 54 | + } |
| 55 | + |
| 56 | + public void setLocationListener(LocationListener listener) { |
| 57 | + mLocationListener = listener; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onConnected(Bundle bundle) { |
| 62 | + Log.d("LocationModule", "onConnected "); |
| 63 | + mConnected = true; |
| 64 | + if (!mIsStarted) { |
| 65 | + startLocationUpdates(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onConnectionSuspended(int i) { |
| 71 | + Log.d("LocationModule", "onConnectionSuspended "); |
| 72 | + mConnected = false; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onConnectionFailed(ConnectionResult connectionResult) { |
| 77 | + Log.d("LocationModule", "onConnectionFailed "); |
| 78 | + mConnected = false; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get last known location |
| 83 | + * |
| 84 | + * @return Location |
| 85 | + */ |
| 86 | + public Location getLastLocation() { |
| 87 | + return LocationServices.FusedLocationApi.getLastLocation( |
| 88 | + mGoogleApiClient); |
| 89 | + } |
| 90 | + |
| 91 | + protected synchronized void buildGoogleApiClient(Context context) { |
| 92 | + mGoogleApiClient = new GoogleApiClient.Builder(context) |
| 93 | + .addConnectionCallbacks(this) |
| 94 | + .addOnConnectionFailedListener(this) |
| 95 | + .addApi(LocationServices.API) |
| 96 | + .build(); |
| 97 | + } |
| 98 | + |
| 99 | +/* protected void createLocationRequest() { |
| 100 | + mLocationRequest = new LocationRequest(); |
| 101 | + mLocationRequest.setInterval(TimeUnit.SECONDS.toMillis(10)); |
| 102 | + mLocationRequest.setFastestInterval(TimeUnit.SECONDS.toMillis(5)); |
| 103 | + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); |
| 104 | + }*/ |
| 105 | + |
| 106 | + public void setLocationRequest(LocationRequest locationRequest) { |
| 107 | + mLocationRequest = locationRequest; |
| 108 | + } |
| 109 | + |
| 110 | + public void startLocationUpdates() { |
| 111 | + mGoogleApiClient.connect(); |
| 112 | + if (mConnected && mLocationRequest != null) { |
| 113 | + mIsStarted = true; |
| 114 | + LocationServices.FusedLocationApi.requestLocationUpdates( |
| 115 | + mGoogleApiClient, mLocationRequest, mLocationListener); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public void stopLocationUpdates() { |
| 120 | + if (mConnected && mLocationRequest != null) { |
| 121 | + LocationServices.FusedLocationApi.removeLocationUpdates( |
| 122 | + mGoogleApiClient, mLocationListener); |
| 123 | + mGoogleApiClient.disconnect(); |
| 124 | + mConnected = false; |
| 125 | + mIsStarted = false; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments