Skip to content

Commit

Permalink
- init LocationHelper only "once", in Application()
Browse files Browse the repository at this point in the history
 -- LocationUpdates only get started, then ends when app ends. Basically no calls made to LocationHelper.stopLocationUpdates()

- only request AccessFineLocation permission, no need for backgroundLocation permission

- added app launcher screen (because fusedLocationClient.requestLocationUpdates needs time to init)

- version bump v1.1.2
  • Loading branch information
lmj0011 committed Apr 27, 2020
1 parent e5d3b0b commit 4543b47
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 67 deletions.
20 changes: 13 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
applicationId "name.lmj0011.courierlocker"
minSdkVersion 19
targetSdkVersion 29
versionCode 45
versionCode 46
versionName "1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down Expand Up @@ -50,35 +50,41 @@ android {
productFlavors {
dev { // active development/testing
applicationIdSuffix ".dev"
versionNameSuffix ".1.0-dev"
versionNameSuffix ".1.2-dev"
resValue("string", "app_name", "Courier Locker (dev)")
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher_dev",
appRoundIcon: "@mipmap/ic_launcher_dev_round"
appRoundIcon: "@mipmap/ic_launcher_dev_round",
appThemeLauncher: "@style/AppTheme.Launcher"
]
}
beta { // public testing
applicationIdSuffix ".beta"
versionNameSuffix ".1.0-beta"
versionNameSuffix ".1.2-beta"
resValue("string", "app_name", "Courier Locker (beta)")
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher_beta",
appRoundIcon: "@mipmap/ic_launcher_beta_round"
appRoundIcon: "@mipmap/ic_launcher_beta_round",
appThemeLauncher: "@style/AppTheme.Launcher"
]
}
prod { // official release
applicationIdSuffix ".prod"
versionNameSuffix ".1.0"
versionNameSuffix ".1.2"
resValue("string", "app_name", "Courier Locker")
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher",
appRoundIcon: "@mipmap/ic_launcher_round"
appRoundIcon: "@mipmap/ic_launcher_round",
appThemeLauncher: "@style/AppTheme.Launcher"
]
}
}
dataBinding {
enabled = true
}
lintOptions {
checkReleaseBuilds false
}
}

dependencies {
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package="name.lmj0011.courierlocker">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
Expand All @@ -14,7 +14,7 @@
android:label="@string/app_name"
android:roundIcon="${appRoundIcon}"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:theme="@style/AppTheme.Launcher"
android:usesCleartextTraffic="${usesCleartextTraffic}"
>

Expand All @@ -33,14 +33,18 @@
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:theme="${appThemeLauncher}">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"/>

<activity
android:name=".SettingsActivity"
android:theme="@style/AppTheme"
/>

<service
android:name=".services.CurrentStatusForegroundService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package name.lmj0011.courierlocker
import android.app.Application
import com.jakewharton.threetenabp.AndroidThreeTen
import com.mooveit.library.Fakeit
import name.lmj0011.courierlocker.helpers.LocationHelper
import name.lmj0011.courierlocker.helpers.NotificationHelper
import shortbread.Shortbread
import timber.log.Timber

class CourierLockerApplication : Application() {
override fun onCreate() {
super.onCreate()
LocationHelper.setFusedLocationClient(this)
Timber.plant(Timber.DebugTree())
Fakeit.init()
AndroidThreeTen.init(this)
Expand Down
33 changes: 13 additions & 20 deletions app/src/main/java/name/lmj0011/courierlocker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import name.lmj0011.courierlocker.fragments.TripsFragmentDirections
import name.lmj0011.courierlocker.fragments.dialogs.ImportedAppDataDialogFragment
import name.lmj0011.courierlocker.helpers.LocationHelper
import name.lmj0011.courierlocker.helpers.PermissionHelper
import name.lmj0011.courierlocker.helpers.PlexmapsXmlParser
import name.lmj0011.courierlocker.services.CurrentStatusForegroundService
import shortbread.Shortcut

Expand All @@ -44,6 +43,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme_NoActionBar)
super.onCreate(savedInstanceState)
Timber.i("onCreate Called")

Expand All @@ -69,20 +69,24 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
setupActionBarWithNavController(navController, appBarConfiguration)
binding.navView.setNavigationItemSelectedListener(this::onNavigationItemSelected)

LocationHelper.setFusedLocationClient(this)

// hide the fab initially
binding.drawerLayout.fab.hide()
}

override fun onRestart() {
super.onRestart()
Timber.i("onRestart Called")
CurrentStatusForegroundService.stopService(this)
}

override fun onStart() {
super.onStart()
Timber.i("onStart Called")

if(!PermissionHelper.permissionAccessFineLocationApproved &&
!PermissionHelper.backgroundLocationPermissionApproved) {

PermissionHelper.requestBackgroundLocationAccess(this)
if(!PermissionHelper.permissionAccessFineLocationApproved) {
PermissionHelper.requestFineLocationAccess(this)
} else {
LocationHelper.startLocationUpdates()
}
}

Expand Down Expand Up @@ -112,13 +116,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
dialog.show(supportFragmentManager, "ImportedAppDataDialogFragment")
}


CurrentStatusForegroundService.stopService(this)

if(PermissionHelper.permissionAccessFineLocationApproved) {
LocationHelper.startLocationUpdates()
} else {
this.showToastMessage("App has no Location permissions.")
if(!PermissionHelper.permissionAccessFineLocationApproved) {
this.showToastMessage("Location permission is required for some features to work.", Toast.LENGTH_LONG)
}
}

Expand All @@ -127,7 +126,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
Timber.i("onPause Called")

if(PermissionHelper.permissionAccessFineLocationApproved) {
LocationHelper.stopLocationUpdates()
CurrentStatusForegroundService.startService(this)
}
}
Expand All @@ -143,11 +141,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
CurrentStatusForegroundService.stopService(this)
}

override fun onRestart() {
super.onRestart()
Timber.i("onRestart Called")
}

override fun onSupportNavigateUp(): Boolean {
this.onBackPressed()
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class MapsFragment : Fragment() {
super.onStart()

if(!PermissionHelper.permissionAccessFineLocationApproved) {
PermissionHelper.requestBackgroundLocationAccess(mainActivity)
PermissionHelper.requestFineLocationAccess(mainActivity)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ object LocationHelper {
)
}

fun stopLocationUpdates() {
isFusedLocationClientSet()
fusedLocationClient.removeLocationUpdates(locationCallback)
}

/**
* ref: https://stackoverflow.com/a/12600225/2445763
*/
Expand Down Expand Up @@ -153,8 +148,8 @@ object LocationHelper {

if (locationResult.locations.isNotEmpty()) {
// get latest location info
lastLatitude.value = locationResult.lastLocation.latitude
lastLongitude.value = locationResult.lastLocation.longitude
lastLatitude.postValue(locationResult.lastLocation.latitude)
lastLongitude.postValue(locationResult.lastLocation.longitude)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat

object PermissionHelper {

const val FINE_LOCATION_ACCESS_RQUEST_CODE = 100
const val BACKGROUND_LOCATION_ACCESS_RQUEST_CODE = 101

var backgroundLocationPermissionApproved = false
private set

var permissionAccessFineLocationApproved = false
private set
Expand All @@ -23,10 +18,6 @@ object PermissionHelper {
permissionAccessFineLocationApproved = ActivityCompat
.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED

backgroundLocationPermissionApproved = ActivityCompat
.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
PackageManager.PERMISSION_GRANTED
}

fun requestFineLocationAccess (activity: Activity) {
Expand All @@ -35,17 +26,4 @@ object PermissionHelper {
FINE_LOCATION_ACCESS_RQUEST_CODE
)
}

fun requestBackgroundLocationAccess (activity: Activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
this.requestFineLocationAccess(activity)
return
}

ActivityCompat.requestPermissions(activity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION),
BACKGROUND_LOCATION_ACCESS_RQUEST_CODE
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ class CurrentStatusForegroundService : LifecycleService() {

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
LocationHelper.setFusedLocationClient(this)
LocationHelper.startLocationUpdates()

//do heavy work on a background thread

// add category to Intent to make it unique
val notificationIntent = Intent(this, MainActivity::class.java).addCategory("Running Foreground service.")
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-hdpi/launch_screen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">

<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="?attr/colorPrimary"/>

<!-- Your product logo - 144dp color version of your app icon -->
<item android:drawable="@drawable/ic_launcher_icon" android:gravity="center"/>

</layer-list>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-hdpi/launch_screen_beta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">

<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="?attr/colorPrimary"/>

<!-- Your product logo - 144dp color version of your app icon -->
<item android:drawable="@drawable/ic_launcher_icon_beta" android:gravity="center"/>

</layer-list>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-hdpi/launch_screen_dev.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">

<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="?attr/colorPrimary"/>

<!-- Your product logo - 144dp color version of your app icon -->
<item android:drawable="@drawable/ic_launcher_icon_dev" android:gravity="center"/>

</layer-list>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_file_download.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#000000" android:pathData="M19,9h-4V3H9v6H5l7,7 7,-7zM5,18v2h14v-2H5z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_file_upload.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#000000" android:pathData="M9,16h6v-6h4l-7,-7 -7,7h4zM5,18h14v2L5,20z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_launcher_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="144dp"
android:height="144dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M328,941c-145,-47 -269,-174 -312,-320 -23,-80 -21,-217 5,-294 68,-204 241,-327 459,-327 177,0 328,83 417,230 65,106 83,281 42,403 -50,150 -171,268 -318,311 -75,22 -219,20 -293,-3zM430,819c0,-16 -5,-63 -11,-102 -10,-62 -17,-78 -50,-112 -21,-22 -41,-52 -44,-68 -6,-21 -28,-40 -96,-82 -49,-30 -93,-55 -99,-55 -15,0 -23,45 -18,113 9,143 111,271 256,323 55,19 62,17 62,-17zM653,807c119,-62 189,-175 195,-314 3,-67 1,-84 -11,-89 -9,-3 -54,19 -105,50 -73,45 -92,62 -97,86 -3,16 -23,44 -44,62 -34,31 -38,41 -53,128 -24,133 -10,142 115,77zM517,540c-8,-42 -7,-56 8,-84 21,-39 9,-72 -29,-82 -54,-13 -89,36 -60,85 14,24 15,37 7,80l-10,51 47,0 47,0 -10,-50zM404,335c61,-31 90,-31 149,-1l47,23 90,-21c116,-28 122,-35 83,-83 -153,-191 -433,-191 -586,0 -22,26 -28,40 -20,48 6,6 51,21 99,34 49,13 89,24 89,24 0,1 22,-10 49,-24z"
android:fillColor="#ffffff"
android:strokeColor="#00000000"/>
</vector>
14 changes: 14 additions & 0 deletions app/src/main/res/drawable/ic_launcher_icon_beta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="96dp"
android:height="96dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M328,941c-145,-47 -269,-174 -312,-320 -23,-80 -21,-217 5,-294 68,-204 241,-327 459,-327 177,0 328,83 417,230 65,106 83,281 42,403 -50,150 -171,268 -318,311 -75,22 -219,20 -293,-3zM430,819c0,-16 -5,-63 -11,-102 -10,-62 -17,-78 -50,-112 -21,-22 -41,-52 -44,-68 -6,-21 -28,-40 -96,-82 -49,-30 -93,-55 -99,-55 -15,0 -23,45 -18,113 9,143 111,271 256,323 55,19 62,17 62,-17zM653,807c119,-62 189,-175 195,-314 3,-67 1,-84 -11,-89 -9,-3 -54,19 -105,50 -73,45 -92,62 -97,86 -3,16 -23,44 -44,62 -34,31 -38,41 -53,128 -24,133 -10,142 115,77zM517,540c-8,-42 -7,-56 8,-84 21,-39 9,-72 -29,-82 -54,-13 -89,36 -60,85 14,24 15,37 7,80l-10,51 47,0 47,0 -10,-50zM404,335c48,-24 49,-25 18,-25l-32,0 0,-75 0,-75 45,0c38,0 45,3 45,20 0,16 -7,20 -30,20 -16,0 -30,5 -30,10 0,6 11,10 25,10 16,0 25,6 25,15 0,9 -9,15 -25,15 -14,0 -25,5 -25,10 0,6 14,10 30,10 23,0 30,4 30,20 0,11 6,20 13,20 8,0 35,11 60,24l47,23 90,-21c111,-27 120,-34 91,-75 -24,-33 -102,-103 -108,-97 -3,2 3,28 11,57 9,29 16,61 16,71 0,24 -27,23 -35,-2 -3,-11 -11,-20 -17,-20 -5,0 -12,9 -15,20 -3,11 -14,20 -24,20 -23,0 -23,1 0,-79 12,-44 24,-67 37,-71 14,-5 5,-12 -39,-28 -77,-29 -177,-29 -253,-1l-58,22 35,11c33,11 34,13 37,70 4,67 -3,76 -60,76l-38,0 0,-65c0,-36 -4,-65 -9,-65 -11,0 -101,99 -101,110 0,14 19,22 109,46 47,12 86,23 86,23 0,1 22,-10 49,-24zM340,259c0,-5 -7,-9 -15,-9 -8,0 -15,7 -15,15 0,9 6,12 15,9 8,-4 15,-10 15,-15z"
android:fillColor="#ffffff"
android:strokeColor="#00000000"/>
<path
android:pathData="M520,255c0,-42 -3,-55 -15,-55 -8,0 -15,-9 -15,-20 0,-17 7,-20 50,-20 43,0 50,3 50,20 0,11 -7,20 -15,20 -12,0 -15,13 -15,55 0,48 -2,55 -20,55 -18,0 -20,-7 -20,-55z"
android:fillColor="#ffffff"
android:strokeColor="#00000000"/>
</vector>
22 changes: 22 additions & 0 deletions app/src/main/res/drawable/ic_launcher_icon_dev.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="96dp"
android:height="96dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M328,941c-145,-47 -269,-174 -312,-320 -23,-80 -21,-217 5,-294 68,-204 241,-327 459,-327 177,0 328,83 417,230 65,106 83,281 42,403 -50,150 -171,268 -318,311 -75,22 -219,20 -293,-3zM430,819c0,-16 -5,-63 -11,-102 -10,-62 -17,-78 -50,-112 -21,-22 -41,-52 -44,-68 -6,-21 -28,-40 -96,-82 -49,-30 -93,-55 -99,-55 -15,0 -23,45 -18,113 9,143 111,271 256,323 55,19 62,17 62,-17zM653,807c119,-62 189,-175 195,-314 3,-67 1,-84 -11,-89 -9,-3 -54,19 -105,50 -73,45 -92,62 -97,86 -3,16 -23,44 -44,62 -34,31 -38,41 -53,128 -24,133 -10,142 115,77zM517,540c-8,-42 -7,-56 8,-84 21,-39 9,-72 -29,-82 -54,-13 -89,36 -60,85 14,24 15,37 7,80l-10,51 47,0 47,0 -10,-50zM404,335c61,-31 90,-31 149,-1l47,23 90,-21c116,-28 122,-35 83,-83 -153,-191 -433,-191 -586,0 -22,26 -28,40 -20,48 6,6 51,21 99,34 49,13 89,24 89,24 0,1 22,-10 49,-24z"
android:fillColor="#ffffff"
android:strokeColor="#00000000"/>
<path
android:pathData="M330,220l0,-70 33,0c45,0 57,15 57,72 0,52 -15,68 -64,68 -25,0 -26,-2 -26,-70zM380,240c12,-22 5,-60 -11,-60 -5,0 -9,18 -9,40 0,45 4,49 20,20z"
android:fillColor="#ffffff"
android:strokeColor="#00000000"/>
<path
android:pathData="M440,220l0,-70 45,0c33,0 45,4 45,15 0,10 -10,15 -30,15 -16,0 -30,5 -30,10 0,6 11,10 25,10 16,0 25,6 25,15 0,9 -9,15 -25,15 -16,0 -25,6 -25,15 0,10 10,15 30,15 20,0 30,5 30,15 0,11 -12,15 -45,15l-45,0 0,-70z"
android:fillColor="#ffffff"
android:strokeColor="#00000000"/>
<path
android:pathData="M567,283c-10,-17 -30,-123 -24,-129 13,-13 37,7 37,31 0,34 18,32 22,-2 2,-17 10,-29 21,-31 21,-4 22,14 4,83 -10,38 -18,51 -34,53 -12,2 -23,0 -26,-5z"
android:fillColor="#ffffff"
android:strokeColor="#00000000"/>
</vector>
Loading

0 comments on commit 4543b47

Please sign in to comment.