1 /*
2  * Copyright (C) 2019 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.settings.utils;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.os.IBinder;
22 import android.os.ServiceManager;
23 import android.os.UserHandle;
24 import android.provider.Settings;
25 import android.util.Log;
26 import android.view.contentcapture.ContentCaptureManager;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 
31 public final class ContentCaptureUtils {
32 
33     private static final String TAG = ContentCaptureUtils.class.getSimpleName();
34     private static final int MY_USER_ID = UserHandle.myUserId();
35 
isEnabledForUser(@onNull Context context)36     public static boolean isEnabledForUser(@NonNull Context context) {
37         boolean enabled = Settings.Secure.getIntForUser(context.getContentResolver(),
38                 Settings.Secure.CONTENT_CAPTURE_ENABLED, 1, MY_USER_ID) == 1;
39         return enabled;
40     }
41 
setEnabledForUser(@onNull Context context, boolean enabled)42     public static void setEnabledForUser(@NonNull Context context, boolean enabled) {
43         Settings.Secure.putIntForUser(context.getContentResolver(),
44                 Settings.Secure.CONTENT_CAPTURE_ENABLED, enabled ? 1 : 0, MY_USER_ID);
45     }
46 
isFeatureAvailable()47     public static boolean isFeatureAvailable() {
48         // We cannot look for ContentCaptureManager, because it's not available if the service
49         // didn't allowlist Settings
50         IBinder service = ServiceManager.checkService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
51         return service != null;
52     }
53 
54     @Nullable
getServiceSettingsComponentName()55     public static ComponentName getServiceSettingsComponentName() {
56         try {
57             return ContentCaptureManager.getServiceSettingsComponentName();
58         } catch (RuntimeException e) {
59             Log.w(TAG, "Could not get service settings: " + e);
60             return null;
61         }
62     }
63 
ContentCaptureUtils()64     private ContentCaptureUtils() {
65         throw new UnsupportedOperationException("contains only static methods");
66     }
67 }
68