Skip to content

Commit

Permalink
Upgrade to Android S, bug fixes and dependency upgrade
Browse files Browse the repository at this point in the history
Changes:
- dependencies upgrade
- permission changes to ask for coarse and fine GPS at the same time
- permissions for the Android S - schedule exact alarm, sampling frequency and GPS
- notifications updated for the Android S with PendingIntent.FLAG_MUTABLE
- wear permission update for Android R
- wear adapters updated with new positioning
- wear synchronization is being done by coroutines, not services
- wear updated UI - titles and BoxLayouts
  • Loading branch information
Foxpace committed Oct 3, 2021
1 parent 87a4cf9 commit 5ed880e
Show file tree
Hide file tree
Showing 31 changed files with 351 additions and 271 deletions.
6 changes: 3 additions & 3 deletions CountDownDialog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.2"

defaultConfig {
minSdkVersion 19
targetSdkVersion 30
targetSdkVersion 31
versionCode 1
versionName "1.0"

Expand All @@ -34,7 +34,7 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
Expand Down
8 changes: 4 additions & 4 deletions WearOsLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.2"

defaultConfig {
minSdkVersion 19
targetSdkVersion 30
targetSdkVersion 31
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -38,14 +38,14 @@ dependencies {
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'com.google.android.gms:play-services-wearable:17.1.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2-native-mt'

implementation 'com.github.GrenderG:Toasty:1.5.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ object WearOsConstants {
const val SAMPLE_PATH_TIME = "/com.motionapps.sensorbox.sample.time"
const val FILE_TO_TRANSFER_PATH = "/com.motionapps.sensorbox.file_to_transfer"


}
27 changes: 20 additions & 7 deletions WearOsLib/src/main/java/com/motionapps/wearoslib/WearOsNotify.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.motionapps.wearoslib

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
Expand Down Expand Up @@ -29,6 +30,7 @@ object WearOsNotify {
* @param importance - importance
* @return - built notification
*/
@SuppressLint("UnspecifiedImmutableFlag")
fun createProgressNotification(
context: Context,
count: Int,
Expand Down Expand Up @@ -58,14 +60,25 @@ object WearOsNotify {
builder.setSmallIcon(R.drawable.ic_graph)
builder.color = ContextCompat.getColor(context, R.color.black_color)
val stopIntent = Intent(STOP_SYNC)
builder.addAction(
R.drawable.ic_baseline_stop,
context.getString(R.string.text_stop),
PendingIntent.getBroadcast(
context,
25, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.addAction(
R.drawable.ic_baseline_stop,
context.getString(R.string.text_stop),
PendingIntent.getBroadcast(
context,
25, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
)
)
}else{
builder.addAction(
R.drawable.ic_baseline_stop,
context.getString(R.string.text_stop),
PendingIntent.getBroadcast(
context,
25, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
)
}
}
return builder.build()
}
Expand Down
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.2"


defaultConfig {
applicationId "motionapps.sensorbox"
minSdkVersion 19
targetSdkVersion 30
targetSdkVersion 31
versionCode 62
versionName "4.2.2"
multiDexEnabled true
Expand Down Expand Up @@ -64,7 +64,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
Expand All @@ -79,8 +79,8 @@ dependencies {
implementation 'com.google.firebase:firebase-analytics-ktx:19.0.1'
implementation 'com.google.firebase:firebase-crashlytics-ktx:18.2.1'

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2-native-mt'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt'

implementation 'androidx.fragment:fragment-ktx:1.3.6'

Expand All @@ -99,8 +99,8 @@ dependencies {
implementation 'io.github.ShawnLin013:number-picker:2.4.13'
implementation 'com.jaredrummler:material-spinner:1.3.1'
implementation 'io.github.medyo:android-about-page:2.0.0'
implementation 'de.psdev.licensesdialog:licensesdialog:2.1.0'
implementation 'com.github.GrenderG:Toasty:1.5.0'
implementation 'de.psdev.licensesdialog:licensesdialog:2.2.0'
implementation 'com.github.GrenderG:Toasty:1.5.2'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package="com.motionapps.sensorbox" >

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Expand Down Expand Up @@ -121,14 +122,16 @@
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/AppTheme" >
android:theme="@style/AppTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".MsgListener" >
<service android:name=".MsgListener"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ open class HomeFragment : Fragment() {

infoButton.setOnClickListener {

if(permissionHandler.checkGPSPermission(this)){
if(!permissionHandler.checkGPSPermission(this)){
return@setOnClickListener
}
val action: NavDirections = HomeFragmentDirections.homeInfoAction(SensorNeeds.GPS)
Expand All @@ -233,7 +233,7 @@ open class HomeFragment : Fragment() {

imageButton.setOnClickListener {

if(permissionHandler.checkGPSPermission(this)){
if(!permissionHandler.checkGPSPermission(this)){
return@setOnClickListener
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.motionapps.sensorbox.permissions


import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Color
import android.hardware.Sensor
Expand Down Expand Up @@ -62,6 +63,33 @@ class PermissionHandler(fragment: Fragment) {
}
}

@RequiresApi(Build.VERSION_CODES.S)
val gpsCallbackS = fragment.registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
when {
permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
// Precise location access granted.
val action: NavDirections = HomeFragmentDirections.homeInfoAction(SensorNeeds.GPS)
Navigation.findNavController(fragment.requireView()).navigate(action)
}
permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
// Only approximate location access granted.
val action: NavDirections = HomeFragmentDirections.homeInfoAction(SensorNeeds.GPS)
Navigation.findNavController(fragment.requireView()).navigate(action)
}
else -> {
// permission is denied
if (permissions.keys.any { fragment.shouldShowRequestPermissionRationale(it) }) {
showDialogFineLocation(fragment)
} else {
// R.string.permission_gps_show,
PermissionSettingsDialog.showSettings(fragment.requireContext())
}
}
}
}

@RequiresApi(Build.VERSION_CODES.M)
val bodySensors = fragment.registerForActivityResult(
ActivityResultContracts.RequestPermission()
Expand All @@ -87,54 +115,61 @@ class PermissionHandler(fragment: Fragment) {

fun checkGPSPermission(fragment: Fragment): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(
fragment.requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
if (!isAnyGpsPermission(fragment.requireContext())) {
showDialogFineLocation(fragment)
return true
return false
}
return true
}
return false
return true
}

private fun isAnyGpsPermission(context: Context): Boolean {
val coarse = ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
val fine = ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED

return coarse || fine
}

/**
* Creates prominent disclosure for GPS for apps under Android Q
* Creates prominent disclosure for GPS for apps - modified for Android S
* Asks for fine location for Android below SDK 31
*
* @return material dialog
*/
@RequiresApi(Build.VERSION_CODES.M)
@ExperimentalCoroutinesApi
@InternalCoroutinesApi
fun showDialogFineLocation(fragment: Fragment) {
dialog = PermissionSettingsDialog.showPermissionRational(
permissionCallback = gpsCallback,
fragment = fragment,
messageText = R.string.permission_gps,
permission = Manifest.permission.ACCESS_FINE_LOCATION,
icon = R.drawable.ic_baseline_location,
)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
dialog = PermissionSettingsDialog.showPermissionRational(
permissionCallback = gpsCallback,
fragment = fragment,
messageText = R.string.permission_gps,
permission = Manifest.permission.ACCESS_FINE_LOCATION,
icon = R.drawable.ic_baseline_location,
)
} else {
dialog = PermissionSettingsDialog.showPermissionRational(
permissionCallback = gpsCallbackS,
fragment = fragment,
messageText = R.string.permission_gps,
permissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
),
icon = R.drawable.ic_baseline_location,
)
}

}

// /**
// * Creates prominent disclosure for GPS for apps for Android Q and above
// *
// * @return material dialog
// */
// @RequiresApi(Build.VERSION_CODES.Q)
// @ExperimentalCoroutinesApi
// @InternalCoroutinesApi
// fun showDialogBackgroundLocation(
// fragment: Fragment,
// ) {
// dialog = PermissionSettingsDialog.showPermissionSettings(
// fragment = fragment,
// messageText = R.string.permission_gps_android_Q,
// icon = R.drawable.ic_baseline_location,
// )
// }

fun checkHeartRateSensor(fragment: Fragment, sensorId: Int): Boolean {
if (sensorId == Sensor.TYPE_HEART_RATE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH && ActivityCompat.checkSelfPermission(
Expand Down
Loading

0 comments on commit 5ed880e

Please sign in to comment.