Skip to content

Commit

Permalink
add treasure hunt setup
Browse files Browse the repository at this point in the history
  • Loading branch information
solygambas committed Apr 18, 2022
1 parent 519dd64 commit 34d225d
Show file tree
Hide file tree
Showing 48 changed files with 1,981 additions and 1 deletion.
9 changes: 9 additions & 0 deletions 19-treasure-hunt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.idea
*.iml
local.properties
build
.gradle
# Eclipse project files
.project
.settings/
.classpath
36 changes: 36 additions & 0 deletions 19-treasure-hunt/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# How to become a contributor and submit your own code

## Contributor License Agreements

We'd love to accept your sample apps and patches! Before we can take them, we
have to jump a couple of legal hurdles.

Please fill out either the individual or corporate Contributor License Agreement
(CLA).

* If you are an individual writing original source code and you're sure you
own the intellectual property, then you'll need to sign an [individual CLA]
(https://developers.google.com/open-source/cla/individual).
* If you work for a company that wants to allow you to contribute your work,
then you'll need to sign a [corporate CLA]
(https://developers.google.com/open-source/cla/corporate).

Follow either of the two links above to access the appropriate CLA and
instructions for how to sign and return it. Once we receive it, we'll be able to
accept your pull requests.

## Contributing A Patch

1. Submit an issue describing your proposed change to the repo in question.
1. The repo owner will respond to your issue promptly.
1. If your proposed change is accepted, and you haven't already done so, sign a
Contributor License Agreement (see details above).
1. Fork the desired repo, develop and test your code changes.
1. Ensure that your code adheres to the existing style in the sample to which
you are contributing. Refer to the
[Google Cloud Platform Samples Style Guide]
(https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the
recommended coding standards for this organization.
1. Ensure that your code has an appropriate set of unit tests which all pass.
1. Submit a pull request.

614 changes: 614 additions & 0 deletions 19-treasure-hunt/LICENSE

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions 19-treasure-hunt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Treasure Hunt

A real-world scavenger hunt using geofencing.

<!-- <p align="center">
<img src="screenshot.png" style="width:528px;max-width: 100%;">
</p> -->

## Features

- checking user permissions and device settings.
- adding broadcast receivers.
- adding and removing geofences.
- handling geofence transitions.
- mocking locations in the emulator.
- navigating to the winning location.

Based on [Adding Geofencing to Your Map](https://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-geofencing) by Google Codelabs (2022).
36 changes: 36 additions & 0 deletions 19-treasure-hunt/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# built application files
*.apk
*.ap_

# Mac files
.DS_Store

# files for the dex VM
*.dex

# Java class files
*.class

# generated files
bin/
gen/

# Ignore gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/
proguard-project.txt

# Eclipse files
.project
.classpath
.settings/

# Android Studio/IDEA
*.iml
.idea
60 changes: 60 additions & 0 deletions 19-treasure-hunt/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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'

android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.android.treasureHunt"
minSdkVersion 19
targetSdkVersion 29
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
}
}

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'
}
21 changes: 21 additions & 0 deletions 19-treasure-hunt/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
43 changes: 43 additions & 0 deletions 19-treasure-hunt/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->

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

<!-- TODO: Step 1 add in permissions for fine location and background-->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".HuntMainActivity"
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>
Binary file added 19-treasure-hunt/app/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/

package com.example.android.treasureHunt

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.content.ContextCompat
import com.example.android.treasureHunt.HuntMainActivity.Companion.ACTION_GEOFENCE_EVENT
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingEvent

/*
* Triggered by the Geofence. Since we only have one active Geofence at once, we pull the request
* ID from the first Geofence, and locate it within the registered landmark data in our
* GeofencingConstants within GeofenceUtils, which is a linear string search. If we had very large
* numbers of Geofence possibilities, it might make sense to use a different data structure. We
* then pass the Geofence index into the notification, which allows us to have a custom "found"
* message associated with each Geofence.
*/
class GeofenceBroadcastReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
// TODO: Step 11 implement the onReceive method
}
}

private const val TAG = "GeofenceReceiver"
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.
*/

package com.example.android.treasureHunt

import android.content.Context
import com.google.android.gms.location.GeofenceStatusCodes
import com.google.android.gms.maps.model.LatLng
import java.util.concurrent.TimeUnit

/**
* Returns the error string for a geofencing error code.
*/
fun errorMessage(context: Context, errorCode: Int): String {
val resources = context.resources
return when (errorCode) {
GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE -> resources.getString(
R.string.geofence_not_available
)
GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES -> resources.getString(
R.string.geofence_too_many_geofences
)
GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS -> resources.getString(
R.string.geofence_too_many_pending_intents
)
else -> resources.getString(R.string.unknown_geofence_error)
}
}

/**
* Stores latitude and longitude information along with a hint to help user find the location.
*/
data class LandmarkDataObject(val id: String, val hint: Int, val name: Int, val latLong: LatLng)

internal object GeofencingConstants {

/**
* Used to set an expiration time for a geofence. After this amount of time, Location services
* stops tracking the geofence. For this sample, geofences expire after one hour.
*/
val GEOFENCE_EXPIRATION_IN_MILLISECONDS: Long = TimeUnit.HOURS.toMillis(1)

val LANDMARK_DATA = arrayOf(
LandmarkDataObject(
"golden_gate_bridge",
R.string.golden_gate_bridge_hint,
R.string.golden_gate_bridge_location,
LatLng(37.819927, -122.478256)),

LandmarkDataObject(
"ferry_building",
R.string.ferry_building_hint,
R.string.ferry_building_location,
LatLng(37.795490, -122.394276)),

LandmarkDataObject(
"pier_39",
R.string.pier_39_hint,
R.string.pier_39_location,
LatLng(37.808674, -122.409821)),

LandmarkDataObject(
"union_square",
R.string.union_square_hint,
R.string.union_square_location,
LatLng(37.788151, -122.407570))
)

val NUM_LANDMARKS = LANDMARK_DATA.size
const val GEOFENCE_RADIUS_IN_METERS = 100f
const val EXTRA_GEOFENCE_INDEX = "GEOFENCE_INDEX"
}
Loading

0 comments on commit 34d225d

Please sign in to comment.