• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.app.sdksandbox.testutils;
18 
19 import static org.junit.Assume.assumeTrue;
20 
21 import android.provider.DeviceConfig;
22 
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25 
26 public class DeviceConfigUtils {
27     private final ConfigListener mConfigListener;
28     private CountDownLatch mLatch;
29     private final String mNamespace;
30 
DeviceConfigUtils(ConfigListener configListener, String namespace)31     public DeviceConfigUtils(ConfigListener configListener, String namespace) {
32         mConfigListener = configListener;
33         mNamespace = namespace;
34     }
35 
deleteProperty(String property)36     public void deleteProperty(String property) throws Exception {
37         if (DeviceConfig.getProperty(mNamespace, property) == null) {
38             return;
39         }
40         mLatch = new CountDownLatch(1);
41         mConfigListener.setLatchForProperty(mLatch, property);
42         DeviceConfig.deleteProperty(mNamespace, property);
43         assumeTrue(mLatch.await(30, TimeUnit.SECONDS));
44     }
45 
setProperty(String property, String value)46     public void setProperty(String property, String value) throws Exception {
47         mLatch = new CountDownLatch(1);
48         mConfigListener.setLatchForProperty(mLatch, property);
49         DeviceConfig.setProperty(mNamespace, property, value, /*makeDefault= */ false);
50         assumeTrue(mLatch.await(30, TimeUnit.SECONDS));
51     }
52 
resetToInitialValue(String property, String value)53     public void resetToInitialValue(String property, String value) throws Exception {
54         if (value != null) {
55             setProperty(property, value);
56         } else {
57             deleteProperty(property);
58         }
59     }
60 }
61