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 android.service.settings.preferences
18 
19 import android.os.OutcomeReceiver
20 import java.lang.Exception
21 
22 class CtsSettingsPreferenceService : SettingsPreferenceService() {
23 
24     private val metadata = SettingsPreferenceMetadata.Builder("s", "k").build()
25 
onGetAllPreferenceMetadatanull26     override fun onGetAllPreferenceMetadata(
27         request: MetadataRequest,
28         callback: OutcomeReceiver<MetadataResult, Exception>
29     ) {
30         callback.onResult(
31             MetadataResult.Builder(MetadataResult.RESULT_OK)
32                 .setMetadataList(listOf(metadata))
33                 .build()
34         )
35     }
36 
onGetPreferenceValuenull37     override fun onGetPreferenceValue(
38         request: GetValueRequest,
39         callback: OutcomeReceiver<GetValueResult, Exception>
40     ) {
41         if (request.preferenceKey == "k" && request.screenKey == "s") {
42             callback.onResult(
43                 GetValueResult.Builder(GetValueResult.RESULT_OK)
44                     .setValue(
45                         SettingsPreferenceValue.Builder(SettingsPreferenceValue.TYPE_STRING)
46                             .setStringValue("value")
47                             .build()
48                     )
49                     .setMetadata(metadata)
50                     .build()
51             )
52         } else {
53             callback.onResult(
54                 GetValueResult.Builder(GetValueResult.RESULT_UNSUPPORTED).build()
55             )
56         }
57     }
58 
onSetPreferenceValuenull59     override fun onSetPreferenceValue(
60         request: SetValueRequest,
61         callback: OutcomeReceiver<SetValueResult, Exception>
62     ) {
63         if (request.preferenceKey == "k" && request.screenKey == "s") {
64             callback.onResult(
65                 SetValueResult.Builder(SetValueResult.RESULT_OK).build()
66             )
67         } else {
68             callback.onResult(
69                 SetValueResult.Builder(SetValueResult.RESULT_UNSUPPORTED).build()
70             )
71         }
72     }
73 }
74