1 package com.google.chip.chiptool.util
3 import android.content.Context
4 import android.content.SharedPreferences
6 /** Utils for storing and accessing available device IDs using shared preferences. */
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
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)
17 prefs.edit().putLong(DEVICE_ID_PREFS_KEY, DEFAULT_DEVICE_ID).apply()
22 fun setNextAvailableId(context: Context, newId: Long) {
23 getPrefs(context).edit().putLong(DEVICE_ID_PREFS_KEY, newId).apply()
26 fun getLastDeviceId(context: Context): Long = getNextAvailableId(context) - 1
28 private fun getPrefs(context: Context): SharedPreferences {
29 return context.getSharedPreferences(PREFERENCE_FILE_KEY, Context.MODE_PRIVATE)