1
class UpdateConfiguration : AppCompatActivity() {
2
private lateinit var updateLocationConfigButton: Button
3
private lateinit var updateNotificationConfigButton: Button
4
private lateinit var updateDataTrackingConfigButton: Button
5
private lateinit var configurationDetailView: TextView
7
private lateinit var defaultConfig: DefaultConfig
8
private lateinit var locationConfig: LocationConfig
9
private lateinit var notificationConfig: NotificationConfig
10
private lateinit var dataTrackingConfig: DataTrackingConfig
12
var permissionsManager: LocationPermissionsManager? = null
13
private var assetId = ""
15
override fun onCreate(savedInstanceState: Bundle?) {
16
super.onCreate(savedInstanceState)
17
setContentView(R.layout.activity_update_configuration)
26
override fun onDestroy() {
32
@SuppressLint("SetTextI18n")
33
private fun initView() {
34
configurationDetailView = findViewById(R.id.configuration_detail)
36
updateLocationConfigButton = findViewById(R.id.update_location_config)
37
updateLocationConfigButton.setOnClickListener {
38
val locationConfigToChange = LocationConfig(
40
smallestDisplacement = 20.0f,
42
fastestInterval = 1500,
43
enableStationaryCheck = true
45
assetTrackingUpdateLocationConfig(locationConfigToChange)
47
locationConfig = assetTrackingGetLocationConfig()
49
configurationDetailView.text = "location config: " + Gson().toJson(locationConfig)
53
updateNotificationConfigButton = findViewById(R.id.update_notification_config)
54
updateNotificationConfigButton.setOnClickListener {
55
val notificationConfigToChange = NotificationConfig(
56
channelId = "NextBillion.ai",
57
channelName = "NextBillion.ai",
59
assetTrackingUpdateNotificationConfig(notificationConfigToChange)
61
notificationConfig = assetTrackingGetNotificationConfig()
63
configurationDetailView.text = "notification config: " + Gson().toJson(notificationConfig)
66
updateDataTrackingConfigButton = findViewById(R.id.update_data_tracking_config)
67
updateDataTrackingConfigButton.setOnClickListener {
69
val dataTrackingConfigToChange = DataTrackingConfig(
70
baseUrl = "https://api.nextbillion.io",
71
dataUploadingBatchSize = 15,
72
dataUploadingBatchWindow = 30,
73
dataStorageSize = 5000,
74
shouldClearLocalDataWhenCollision = false
76
assetTrackingUpdateDataTrackingConfig(dataTrackingConfigToChange)
78
dataTrackingConfig = assetTrackingGetDataTrackingConfig()
79
configurationDetailView.text = "dataTracking config: " + Gson().toJson(dataTrackingConfig)
84
private fun initConfigurations() {
85
defaultConfig = DefaultConfig(
87
enhanceService = true,
90
crashRestartEnabled = true,
91
workOnMainThread = true,
96
locationConfig = LocationConfig(
97
trackingMode = TrackingMode.ACTIVE,
99
fastestInterval = 1000,
100
enableStationaryCheck = false
103
notificationConfig = NotificationConfig(channelId = "Custom.ID", channelName = "Custom.Name")
105
dataTrackingConfig = DataTrackingConfig(
106
baseUrl = "https://api.nextbillion.io",
107
dataUploadingBatchSize = 30,
108
dataUploadingBatchWindow = 20,
109
dataStorageSize = 5000,
110
shouldClearLocalDataWhenCollision = true
114
setDefaultConfig(defaultConfig)
115
setLocationConfig(locationConfig)
116
setNotificationConfig(notificationConfig)
117
setDataTrackingConfig(dataTrackingConfig)
120
initialize("PUT YOUR API KEY HERE")
123
private fun createAsset() {
124
val assetAttributes: Map<String, String> = mapOf("attribute 1" to "test 1", "attribute 2" to "test 2")
125
val assetProfile = AssetProfile.Builder().setCustomId(UUID.randomUUID().toString()).setName("testName")
126
.setDescription("testDescription").setAttributes(assetAttributes).build()
128
createNewAsset(assetProfile, object : AssetApiCallback<AssetCreationResponse> {
129
@SuppressLint("SetTextI18n")
130
override fun onSuccess(result: AssetCreationResponse) {
131
assetId = result.data.id
133
this@UpdateConfiguration,
134
"create asset successfully with asset id: $assetId",
137
bindAssetAndStartTracking()
140
override fun onFailure(exception: Exception) {
142
this@UpdateConfiguration,
143
"create asset failed with error: " + exception.message,
150
private fun bindAssetAndStartTracking() {
151
bindAsset(assetId, object : AssetApiCallback<Unit> {
152
@SuppressLint("SetTextI18n")
153
override fun onSuccess(result: Unit) {
155
this@UpdateConfiguration,
157
"bind asset successfully with assetId: %s",
162
checkPermissionsAndStartTracking()
165
override fun onFailure(exception: Exception) {
166
val exceptionMessage = exception.message ?: ""
168
this@UpdateConfiguration,
169
"bind asset failed: $exceptionMessage",
176
private fun checkPermissionsAndStartTracking() {
177
if (LocationPermissionsManager.areAllLocationPermissionGranted(this)) {
179
} else if (!LocationPermissionsManager.isLocationServiceEnabled(this)) {
180
showLocationServiceOffDialog()
182
permissionsManager = LocationPermissionsManager(object : LocationPermissionsListener {
183
override fun onExplanationNeeded(permissionsToExplain: List<String>?) {
185
this@UpdateConfiguration, "You need to accept location permissions.",
190
override fun onPermissionResult(granted: Boolean) {
192
if (LocationPermissionsManager.isBackgroundLocationPermissionGranted(this@UpdateConfiguration)) {
195
permissionsManager?.requestBackgroundLocationPermissions(this@UpdateConfiguration)
199
this@UpdateConfiguration, "You need to accept location permissions.$granted",
205
permissionsManager?.requestLocationPermissions(this)
209
@SuppressLint("MissingPermission", "SetTextI18n")
210
fun startTracking() {
214
private fun showLocationServiceOffDialog() {
215
val alertDialogBuilder = AlertDialog.Builder(this)
217
alertDialogBuilder.setTitle("Location Services Disabled")
218
alertDialogBuilder.setMessage("To enable location services, please go to Settings > Privacy > Location Services.")
220
alertDialogBuilder.setPositiveButton("OK") { dialogInterface: DialogInterface, _: Int ->
221
dialogInterface.dismiss()
224
val alertDialog = alertDialogBuilder.create()