1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.intentresolver.platform
18 
19 /** A proxy to Settings.Global */
20 interface GlobalSettings : SettingsProxy
21 
22 /** A proxy to Settings.Secure */
23 interface SecureSettings : SettingsProxy
24 
25 /** A proxy to Settings.System */
26 interface SystemSettings : SettingsProxy
27 
28 /** A generic Settings proxy interface */
29 sealed interface SettingsProxy {
30 
31     /** Returns the String value set for the given settings key, or null if no value exists. */
getStringOrNullnull32     fun getStringOrNull(name: String): String?
33 
34     /**
35      * Writes a new string value for the given settings key.
36      *
37      * @return true if the value did not previously exist or was modified
38      */
39     fun putString(name: String, value: String): Boolean
40 
41     /**
42      * Returns the Int value for the given settings key or null if no value exists or it cannot be
43      * interpreted as an Int.
44      */
45     fun getIntOrNull(name: String): Int? = getStringOrNull(name)?.toIntOrNull()
46 
47     /**
48      * Writes a new int value for the given settings key.
49      *
50      * @return true if the value did not previously exist or was modified
51      */
52     fun putInt(name: String, value: Int): Boolean = putString(name, value.toString())
53 
54     /**
55      * Returns the Boolean value for the given settings key or null if no value exists or it cannot
56      * be interpreted as a Boolean.
57      */
58     fun getBooleanOrNull(name: String): Boolean? = getIntOrNull(name)?.let { it != 0 }
59 
60     /**
61      * Writes a new Boolean value for the given settings key.
62      *
63      * @return true if the value did not previously exist or was modified
64      */
putBooleannull65     fun putBoolean(name: String, value: Boolean): Boolean = putInt(name, if (value) 1 else 0)
66 
67     /**
68      * Returns the Long value for the given settings key or null if no value exists or it cannot be
69      * interpreted as a Long.
70      */
71     fun getLongOrNull(name: String): Long? = getStringOrNull(name)?.toLongOrNull()
72 
73     /**
74      * Writes a new Long value for the given settings key.
75      *
76      * @return true if the value did not previously exist or was modified
77      */
78     fun putLong(name: String, value: Long): Boolean = putString(name, value.toString())
79 
80     /**
81      * Returns the Float value for the given settings key or null if no value exists or it cannot be
82      * interpreted as a Float.
83      */
84     fun getFloatOrNull(name: String): Float? = getStringOrNull(name)?.toFloatOrNull()
85 
86     /**
87      * Writes a new float value for the given settings key.
88      *
89      * @return true if the value did not previously exist or was modified
90      */
91     fun putFloat(name: String, value: Float): Boolean = putString(name, value.toString())
92 }
93