Skip to content

Instantly share code, notes, and snippets.

@danielsmykowski1
Last active November 1, 2019 21:23
Show Gist options
  • Save danielsmykowski1/ea0dc779d0ad6aed873ecb6a0ffdf48d to your computer and use it in GitHub Desktop.
Save danielsmykowski1/ea0dc779d0ad6aed873ecb6a0ffdf48d to your computer and use it in GitHub Desktop.
Kotlin MainActivity to start walkbase SDK (android project in a flutter project)
package com.dansmy.walkbase_start
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.support.annotation.RequiresApi
import android.support.design.widget.Snackbar
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat;
import android.view.View
import com.walkbase.sdk.WalkbaseManager
import io.flutter.Log
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity(), ActivityCompat.OnRequestPermissionsResultCallback {
private val PERMISSIONS_REQUEST_CODE = 1234 // Arbitrary Number
private val apiKey = "your apiKey"
private val TAG = "MainActivity"
// Create an instance of WalkbaseManager
private lateinit var walkbaseManager: WalkbaseManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
// Initialize the WalkbaseManager
walkbaseManager = WalkbaseManager(this)
// Verify that we have the required fine location permissions before proceeding.
// It's the developers responsibility to confirm this
if (!havePermissions()) {
Log.i(TAG, "Requesting permissions needed for this app.")
requestPermissions()
} else {
walkbaseManager.startWithApiKey(apiKey)
}
}
override fun onResume() {
super.onResume()
// Call this when the Activity Resumes
walkbaseManager.onResume()
}
override fun onStop() {
super.onStop()
// Call this when the Activity Stops
walkbaseManager.onStop()
}
/**
* havePermissions checks if the user has granted permissions to ACCESS FINE LOCATION
* Returns a boolean
*/
private fun havePermissions(): Boolean {
return ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}
// Request location permissions
private fun requestPermissions() {
ActivityCompat.requestPermissions(this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), PERMISSIONS_REQUEST_CODE)
}
private fun showLinkToSettingsSnackbar() {
Snackbar.make(this.flutterView,
"Permission denied",
Snackbar.LENGTH_INDEFINITE)
.setAction("Permission settings", View.OnClickListener {
// Build intent that displays the App settings screen.
val intent = Intent()
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
val uri = Uri.fromParts("package",
BuildConfig.APPLICATION_ID, null)
intent.data = uri
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}).show()
}
// Show a dialog to request location permissionse
private fun showRequestPermissionsSnackbar() {
Snackbar.make(this.flutterView, "We need permissions to access your location",
Snackbar.LENGTH_INDEFINITE)
.setAction("Ok", View.OnClickListener {
// Request permission.
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSIONS_REQUEST_CODE)
}).show()
}
@RequiresApi(api = Build.VERSION_CODES.M)
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
if (requestCode != PERMISSIONS_REQUEST_CODE) {
return
}
for (i in permissions.indices) {
val permission = permissions[i]
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
if (shouldShowRequestPermissionRationale(permission)) {
Log.i(TAG, "Permission denied without 'NEVER ASK AGAIN': " + permission)
showRequestPermissionsSnackbar()
} else {
Log.i(TAG, "Permission denied with 'NEVER ASK AGAIN': " + permission)
showLinkToSettingsSnackbar()
}
} else {
Log.i(TAG, "Permission granted, starting the Walkbase SDK")
// When we have verified the user has granted permission we can start the SDK.
walkbaseManager.startWithApiKey(apiKey)
}
}
}
}
@danielsmykowski1
Copy link
Author

The following dependencies must be added to make the Walkbase SDK work properly:

org.jetbrains.anko:anko-commons:0.10.4
com.squareup.okhttp3:okhttp:4.2.2
com.squareup.okhttp3:logging-interceptor:4.2.2
com.squareup.retrofit2:retrofit:2.6.2
com.squareup.retrofit2:converter-gson:2.6.2
com.google.code.gson:gson:2.8.0
com.google.android.gms:play-services-nearby:12.0.1
com.google.android.gms:play-services-location:12.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment