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