Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / android / CHIPTool / app / src / main / java / com / google / chip / chiptool / util / DeviceIdUtil.kt
1 package com.google.chip.chiptool.util
2
3 import android.content.Context
4 import android.content.SharedPreferences
5
6 /** Utils for storing and accessing available device IDs using shared preferences. */
7 object DeviceIdUtil {
8   private const val PREFERENCE_FILE_KEY = "com.google.chip.chiptool.PREFERENCE_FILE_KEY"
9   private const val DEVICE_ID_PREFS_KEY = "device_id"
10   private const val DEFAULT_DEVICE_ID = 1L
11
12   fun getNextAvailableId(context: Context): Long {
13     val prefs = getPrefs(context)
14     return if (prefs.contains(DEVICE_ID_PREFS_KEY)) {
15       prefs.getLong(DEVICE_ID_PREFS_KEY, DEFAULT_DEVICE_ID)
16     } else {
17       prefs.edit().putLong(DEVICE_ID_PREFS_KEY, DEFAULT_DEVICE_ID).apply()
18       DEFAULT_DEVICE_ID
19     }
20   }
21
22   fun setNextAvailableId(context: Context, newId: Long) {
23     getPrefs(context).edit().putLong(DEVICE_ID_PREFS_KEY, newId).apply()
24   }
25
26   fun getLastDeviceId(context: Context): Long = getNextAvailableId(context) - 1
27
28   private fun getPrefs(context: Context): SharedPreferences {
29     return context.getSharedPreferences(PREFERENCE_FILE_KEY, Context.MODE_PRIVATE)
30   }
31 }