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.google.android.car.kitchensink.customizationtool;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.provider.Settings;
22 
23 public class CustomizationToolController {
24 
25     private static final String PACKAGE = "com.android.car.customization.tool";
26     private static final String SERVICE = PACKAGE + '/' + PACKAGE + ".CustomizationToolService";
27 
28     private final ContentResolver mContentResolver;
29 
CustomizationToolController(Context context)30     public CustomizationToolController(Context context) {
31         mContentResolver = context.getContentResolver();
32     }
33 
isCustomizationToolActive()34     boolean isCustomizationToolActive() {
35         String accessibilityServices = Settings.Secure.getString(
36                 mContentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
37         if (accessibilityServices == null) {
38             return false;
39         }
40         String serviceStatus = Settings.Secure.getString(
41                 mContentResolver, Settings.Secure.ACCESSIBILITY_ENABLED);
42         return accessibilityServices.contains(SERVICE) && "1".equals(serviceStatus);
43     }
44 
toggleCustomizationTool(boolean newState)45     public void toggleCustomizationTool(boolean newState) {
46         if (newState) {
47             Settings.Secure.putString(
48                     mContentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, SERVICE);
49             Settings.Secure.putString(mContentResolver,
50                     Settings.Secure.ACCESSIBILITY_ENABLED, /* value = */"1");
51         } else {
52             String newAccessibilityServices = removeServiceFromList();
53             Settings.Secure.putString(mContentResolver,
54                     Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, newAccessibilityServices);
55             Settings.Secure.putString(mContentResolver,
56                     Settings.Secure.ACCESSIBILITY_ENABLED, /* value = */"0");
57         }
58     }
59 
removeServiceFromList()60     private String removeServiceFromList() {
61         String currentServices = Settings.Secure.getString(
62                 mContentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
63         if (currentServices == null || currentServices.isEmpty()) {
64             return "";
65         }
66         return currentServices.replace(
67                 SERVICE, /* replacement = */"").replace(/* target = */"::", /* replacement = */":");
68     }
69 }
70