Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Code updated to run on SDK 33 and Gradle 7.5 #13

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
79 changes: 38 additions & 41 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
/*
* Copyright (C) 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
compileSdkVersion 29
namespace 'com.example.android.treasureHunt'
compileSdk 33

defaultConfig {
applicationId "com.example.android.treasureHunt"
minSdkVersion 19
targetSdkVersion 29
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

dataBinding {
enabled true
// Enables data binding.
buildFeatures {
dataBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-beta01'
}

// Layout
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

// Google Play Services
implementation 'com.google.android.gms:play-services-maps:18.1.0'
implementation 'com.google.android.gms:play-services-location:21.0.1'

// ViewModel and Lifecycle
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.0"
}
17 changes: 10 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2019 Google Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,10 +16,13 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.treasureHunt">

<!--
The FINE_LOCATION permission is required for geofencing functionality to work.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!--
The BACKGROUND_LOCATION permission is required for geofencing functionality to work on Q+
-->
Expand All @@ -34,16 +36,17 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".HuntMainActivity"
<activity
android:name=".HuntMainActivity"
android:exported="true"
android:label="@string/title_activity_hunt"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".GeofenceBroadcastReceiver"/>
</application>

</manifest>
<receiver android:name=".GeofenceBroadcastReceiver" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,47 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
if (intent.action == ACTION_GEOFENCE_EVENT) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)

if (geofencingEvent.hasError()) {
val errorMessage = errorMessage(context, geofencingEvent.errorCode)
Log.e(TAG, errorMessage)
return
if (geofencingEvent != null) {
if (geofencingEvent.hasError()) {
val errorMessage = errorMessage(context, geofencingEvent.errorCode)
Log.e(TAG, errorMessage)
return
}
}

if (geofencingEvent.geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
Log.v(TAG, context.getString(R.string.geofence_entered))
if (geofencingEvent != null) {
if (geofencingEvent.geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
Log.v(TAG, context.getString(R.string.geofence_entered))

val fenceId = when {
geofencingEvent.triggeringGeofences.isNotEmpty() ->
geofencingEvent.triggeringGeofences[0].requestId
else -> {
Log.e(TAG, "No Geofence Trigger Found! Abort mission!")
return
val fenceId = when {
geofencingEvent.triggeringGeofences!!.isNotEmpty() ->
geofencingEvent.triggeringGeofences!![0].requestId
else -> {
Log.e(TAG, "No Geofence Trigger Found! Abort mission!")
return
}
}
// Check geofence against the constants listed in GeofenceUtil.kt to see if the
// user has entered any of the locations we track for geofences.
val foundIndex = GeofencingConstants.LANDMARK_DATA.indexOfFirst {
it.id == fenceId
}
}
// Check geofence against the constants listed in GeofenceUtil.kt to see if the
// user has entered any of the locations we track for geofences.
val foundIndex = GeofencingConstants.LANDMARK_DATA.indexOfFirst {
it.id == fenceId
}

// Unknown Geofences aren't helpful to us
if ( -1 == foundIndex ) {
Log.e(TAG, "Unknown Geofence: Abort Mission")
return
}
// Unknown Geofences aren't helpful to us
if ( -1 == foundIndex ) {
Log.e(TAG, "Unknown Geofence: Abort Mission")
return
}

val notificationManager = ContextCompat.getSystemService(
context,
NotificationManager::class.java
) as NotificationManager
val notificationManager = ContextCompat.getSystemService(
context,
NotificationManager::class.java
) as NotificationManager

notificationManager.sendGeofenceEnteredNotification(
context, foundIndex
)
notificationManager.sendGeofenceEnteredNotification(
context, foundIndex
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.example.android.treasureHunt

import androidx.lifecycle.LiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.lifecycle.*

/*
* This class contains the state of the game. The two important pieces of state are the index
Expand All @@ -32,22 +29,26 @@ import androidx.lifecycle.ViewModel
* the Home action will cause the state to be saved, even if the game is terminated by Android in
* the background.
*/
/**
* Transformations are deprecated now.
* https://stackoverflow.com/a/75465436
*/
class GeofenceViewModel(state: SavedStateHandle) : ViewModel() {
private val _geofenceIndex = state.getLiveData(GEOFENCE_INDEX_KEY, -1)
private val _hintIndex = state.getLiveData(HINT_INDEX_KEY, 0)
val geofenceIndex: LiveData<Int>
get() = _geofenceIndex

val geofenceHintResourceId = Transformations.map(geofenceIndex) {
val index = geofenceIndex?.value ?: -1
val geofenceHintResourceId = geofenceIndex.map {
val index = geofenceIndex.value ?: -1
when {
index < 0 -> R.string.not_started_hint
index < GeofencingConstants.NUM_LANDMARKS -> GeofencingConstants.LANDMARK_DATA[geofenceIndex.value!!].hint
else -> R.string.geofence_over
}
}

val geofenceImageResourceId = Transformations.map(geofenceIndex) {
val geofenceImageResourceId = geofenceIndex.map {
val index = geofenceIndex.value ?: -1
when {
index < GeofencingConstants.NUM_LANDMARKS -> R.drawable.android_map
Expand Down
Loading