Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated gradle 3.0.1, dependancies and introduced FusedLocationProviderClient #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions android-easylocation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ ext {
}
//noinspection GroovyMissingReturnStatement
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
compileSdkVersion rootProject.ext.compile_sdk_version
buildToolsVersion rootProject.ext.build_tools_version

defaultConfig {
minSdkVersion 15
targetSdkVersion 24
minSdkVersion rootProject.ext.min_sdk_version
targetSdkVersion rootProject.ext.target_sdk_version
versionCode 1
versionName "1.0.2"

Expand All @@ -46,12 +46,15 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided "com.google.android.gms:play-services-location:9.2.0"
provided ("com.google.android.gms:play-services-location:$play_services_version") {
exclude group: "com.android.support", module: "support-v4"
exclude group: "com.android.support", module: "support-media-compat"
}

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
implementation "com.android.support:appcompat-v7:$support_version"
testCompile 'junit:junit:4.12'
}
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationAvailability;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;


public class LocationBgService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,LocationListener {
public class LocationBgService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final long NO_FALLBACK = 0;
private final String TAG = LocationBgService.class.getSimpleName();
private GoogleApiClient googleApiClient;
private int mLocationMode;
private LocationRequest mLocationRequest;
private Handler handler;
private long fallBackToLastLocationTime;
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;


@Nullable
Expand All @@ -38,30 +47,48 @@ public IBinder onBind(Intent intent) {
public void onCreate() {
super.onCreate();
handler = new Handler();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
onLocationUpdate(locationResult.getLastLocation());
}

@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
Log.d(TAG, "googleApiClient location availability is " + locationAvailability.isLocationAvailable());
if (!locationAvailability.isLocationAvailable()) {
Intent intent = new Intent();
intent.setAction(AppConstants.INTENT_NO_LOCATION_RECEIVED);
LocalBroadcastManager.getInstance(LocationBgService.this).sendBroadcast(intent);
}
}
};
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Log.d(TAG,"googleApiClient created");
Log.d(TAG, "googleApiClient created");
googleApiClient.connect();
}

@SuppressWarnings("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
Log.d(TAG,"googleApiClient start command "+ intent.getAction());
if(intent.getAction().equals(AppConstants.ACTION_LOCATION_FETCH_START)) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "googleApiClient start command " + intent.getAction());
if (intent.getAction().equals(AppConstants.ACTION_LOCATION_FETCH_START)) {
mLocationMode = intent.getIntExtra(IntentKey.LOCATION_FETCH_MODE, AppConstants.SINGLE_FIX);
mLocationRequest = intent.getParcelableExtra(IntentKey.LOCATION_REQUEST);
fallBackToLastLocationTime = intent.getLongExtra(IntentKey.FALLBACK_TO_LAST_LOCATION_TIME,NO_FALLBACK);
fallBackToLastLocationTime = intent.getLongExtra(IntentKey.FALLBACK_TO_LAST_LOCATION_TIME, NO_FALLBACK);
if (mLocationRequest == null)
throw new IllegalStateException("Location request can't be null");
if(googleApiClient.isConnected())
if (googleApiClient.isConnected())
requestLocationUpdates();
}
else if(intent.getAction().equals(AppConstants.ACTION_LOCATION_FETCH_STOP)) {
} else if (intent.getAction().equals(AppConstants.ACTION_LOCATION_FETCH_STOP)) {
stopLocationService();
}
return START_NOT_STICKY;
Expand All @@ -71,18 +98,23 @@ else if(intent.getAction().equals(AppConstants.ACTION_LOCATION_FETCH_STOP)) {
private void requestLocationUpdates() {
if (mLocationRequest != null) {
startFallbackToLastLocationTimer();
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, this);
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
}

@SuppressWarnings("MissingPermission")
private void startFallbackToLastLocationTimer() {
if(fallBackToLastLocationTime!=NO_FALLBACK) {
if (fallBackToLastLocationTime != NO_FALLBACK) {
handler.removeCallbacksAndMessages(null);
handler.postDelayed(new Runnable() {
@Override
public void run() {
onLocationChanged(LocationServices.FusedLocationApi.getLastLocation(googleApiClient));
mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
onLocationUpdate(task.isSuccessful() ? task.getResult() : null);
}
});
}
}, fallBackToLastLocationTime);
}
Expand All @@ -91,51 +123,53 @@ public void run() {
@SuppressWarnings("MissingPermission")
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG,"googleApiClient connected");
Log.d(TAG, "googleApiClient connected");
requestLocationUpdates();
}

@Override
public void onConnectionSuspended(int i) {
Log.d(TAG,"googleApiClient connection suspended");
Log.d(TAG, "googleApiClient connection suspended");
stopLocationService();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG,"googleApiClient connection failed");
Log.d(TAG, "googleApiClient connection failed");
stopLocationService();
}

private void stopLocationService() {
if(handler!=null)
if (handler != null)
handler.removeCallbacksAndMessages(null);

Log.d(TAG,"googleApiClient removing location updates");
if(googleApiClient!=null && googleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
Log.d(TAG,"googleApiClient disconnect");
Log.d(TAG, "googleApiClient removing location updates");
if (googleApiClient != null && googleApiClient.isConnected()) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
Log.d(TAG, "googleApiClient disconnect");
googleApiClient.disconnect();
}
Log.d(TAG,"googleApiClient stop service");
Log.d(TAG, "googleApiClient stop service");
stopSelf();
}

@Override
public void onLocationChanged(Location location) {
Log.d(TAG,"googleApiClient location received");
if(location!=null) {

public void onLocationUpdate(Location location) {
Log.d(TAG, "googleApiClient location received");
if (location != null) {
PreferenceUtil.getInstance(this).saveLastKnownLocation(location);
Intent intent = new Intent();
intent.setAction(AppConstants.INTENT_LOCATION_RECEIVED);
intent.putExtra(IntentKey.LOCATION,location);
intent.putExtra(IntentKey.LOCATION, location);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
} else {
Intent intent = new Intent();
intent.setAction(AppConstants.INTENT_NO_LOCATION_RECEIVED);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
if(mLocationMode == AppConstants.SINGLE_FIX)
if (mLocationMode == AppConstants.SINGLE_FIX)
stopLocationService();
}


}
23 changes: 13 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'
apply plugin: 'android-apt'

//noinspection GroovyMissingReturnStatement,GroovyMissingReturnStatement
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
compileSdkVersion rootProject.ext.compile_sdk_version
buildToolsVersion rootProject.ext.build_tools_version

defaultConfig {
applicationId "com.akhgupta.easylocation.demo"
minSdkVersion 15
targetSdkVersion 24
minSdkVersion rootProject.ext.min_sdk_version
targetSdkVersion rootProject.ext.target_sdk_version
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -23,16 +23,19 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
compile "com.google.android.gms:play-services-location:9.6.0"
compile "com.jakewharton:butterknife:$butterknife_version"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknife_version"
implementation ("com.google.android.gms:play-services-location:$play_services_version") {
exclude group: "com.android.support", module: "support-v4"
exclude group: "com.android.support", module: "support-media-compat"
}



androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
implementation "com.android.support:appcompat-v7:$support_version"
testCompile 'junit:junit:4.12'
compile project(':android-easylocation')
}
}
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.compile_sdk_version = 27
ext.build_tools_version = '27.0.3'
ext.min_sdk_version = 15
ext.target_sdk_version = 27
ext.support_version = '27.0.2'
ext.play_services_version = '11.8.0'
ext.butterknife_version = '8.8.1'

repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
Expand All @@ -19,10 +28,11 @@ buildscript {

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Wed Jan 17 22:28:55 IST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip