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 com.android.server.deviceconfig;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.os.ParcelFileDescriptor;
22 import android.os.RemoteException;
23 import android.provider.aidl.IDeviceConfigManager;
24 import android.provider.DeviceConfigInitializer;
25 
26 import com.android.server.deviceconfig.db.DeviceConfigDbAdapter;
27 import com.android.server.deviceconfig.db.DeviceConfigDbHelper;
28 
29 import java.io.PrintWriter;
30 import java.util.Map;
31 
32 import com.android.modules.utils.BasicShellCommandHandler;
33 
34 /**
35  * DeviceConfig Service implementation (updatable via Mainline) that uses a SQLite database as a storage mechanism
36  * for the configuration values.
37  *
38  * @hide
39  */
40 public class DeviceConfigServiceImpl extends IDeviceConfigManager.Stub {
41     private final DeviceConfigDbAdapter mDbAdapter;
42 
DeviceConfigServiceImpl(Context context)43     public DeviceConfigServiceImpl(Context context) {
44         DeviceConfigDbHelper dbHelper = new DeviceConfigDbHelper(context);
45         mDbAdapter = new DeviceConfigDbAdapter(dbHelper.getWritableDatabase());
46 
47         DeviceConfigInitializer.getDeviceConfigServiceManager()
48                 .getDeviceConfigUpdatableServiceRegisterer()
49                 .register(this);
50     }
51 
52     @Override
getProperties(String namespace, String[] names)53     public Map<String, String> getProperties(String namespace, String[] names) throws RemoteException {
54         return mDbAdapter.getValuesForNamespace(namespace, names);
55     }
56 
57     @Override
setProperties(String namespace, Map<String, String> values)58     public boolean setProperties(String namespace, Map<String, String> values) {
59         return mDbAdapter.setValues(namespace, values);
60     }
61 
62     @Override
setProperty(String namespace, String key, String value, boolean makeDefault)63     public boolean setProperty(String namespace, String key, String value, boolean makeDefault) {
64         return mDbAdapter.setValue(namespace, key, value, makeDefault);
65     }
66 
67     @Override
deleteProperty(String namespace, String key)68     public  boolean deleteProperty(String namespace, String key) {
69         return mDbAdapter.deleteValue(namespace, key);
70     }
71 
72     @Override
handleShellCommand(@onNull ParcelFileDescriptor in, @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err, @NonNull String[] args)73     public int handleShellCommand(@NonNull ParcelFileDescriptor in,
74             @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
75             @NonNull String[] args) {
76         return (new MyShellCommand()).exec(
77                 this, in.getFileDescriptor(), out.getFileDescriptor(), err.getFileDescriptor(),
78                 args);
79     }
80 
81     static final class MyShellCommand extends BasicShellCommandHandler {
82         // TODO(b/265948938) implement this
83 
84         @Override
onCommand(String cmd)85         public int onCommand(String cmd) {
86             if (cmd == null || "help".equals(cmd) || "-h".equals(cmd)) {
87                 onHelp();
88                 return -1;
89             }
90             return -1;
91         }
92 
93         @Override
onHelp()94         public void onHelp() {
95             PrintWriter pw = getOutPrintWriter();
96             pw.println("Device Config implemented in mainline");
97         }
98     }
99 }
100