From ab2ca2a2a48606e665feedd062f42423679bb961 Mon Sep 17 00:00:00 2001 From: sai Date: Thu, 18 Jan 2018 14:54:06 +0530 Subject: [PATCH] Gradle update to 3.0.1, updated dependency versions and replaced LocationServices.FusedLocationApi with FusedLocationProviderClient as it was deprecated in newer android version --- android-easylocation/build.gradle | 15 ++-- .../easylocation/LocationBgService.java | 88 +++++++++++++------ app/build.gradle | 23 ++--- build.gradle | 14 ++- gradle/wrapper/gradle-wrapper.properties | 4 +- 5 files changed, 97 insertions(+), 47 deletions(-) diff --git a/android-easylocation/build.gradle b/android-easylocation/build.gradle index 4b4aa0e..fcba0f9 100644 --- a/android-easylocation/build.gradle +++ b/android-easylocation/build.gradle @@ -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" @@ -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' diff --git a/android-easylocation/src/main/java/com/akhgupta/easylocation/LocationBgService.java b/android-easylocation/src/main/java/com/akhgupta/easylocation/LocationBgService.java index c9f8b4f..31864cf 100644 --- a/android-easylocation/src/main/java/com/akhgupta/easylocation/LocationBgService.java +++ b/android-easylocation/src/main/java/com/akhgupta/easylocation/LocationBgService.java @@ -6,6 +6,7 @@ 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; @@ -13,12 +14,18 @@ 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; @@ -26,6 +33,8 @@ public class LocationBgService extends Service implements GoogleApiClient.Connec private LocationRequest mLocationRequest; private Handler handler; private long fallBackToLastLocationTime; + private FusedLocationProviderClient mFusedLocationClient; + private LocationCallback mLocationCallback; @Nullable @@ -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; @@ -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() { + @Override + public void onComplete(@NonNull Task task) { + onLocationUpdate(task.isSuccessful() ? task.getResult() : null); + } + }); } }, fallBackToLastLocationTime); } @@ -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(); } + + } \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 1510768..1dc0835 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" @@ -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') -} +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index c42821b..8ef7e15 100644 --- a/build.gradle +++ b/build.gradle @@ -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' @@ -19,10 +28,11 @@ buildscript { allprojects { repositories { + google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir -} +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 04e285f..561a967 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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