blob: b01749f0c5e7efb30889a23f94cf154eb50f2ba3 [file] [log] [blame]
Felipe Lemefcf98252019-03-20 08:54:56 -07001/*
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
17package com.android.settings.utils;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.content.ComponentName;
22import android.content.Context;
23import android.os.IBinder;
24import android.os.ServiceManager;
25import android.os.UserHandle;
26import android.provider.Settings;
27import android.util.Log;
28import android.view.contentcapture.ContentCaptureManager;
29
30public final class ContentCaptureUtils {
31
32 private static final String TAG = ContentCaptureUtils.class.getSimpleName();
33 private static final int MY_USER_ID = UserHandle.myUserId();
34
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
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
46 public static boolean isFeatureAvailable() {
47 // We cannot look for ContentCaptureManager, because it's not available if the service
Edgar Wang8829e512020-07-29 14:57:34 +080048 // didn't allowlist Settings
Felipe Lemefcf98252019-03-20 08:54:56 -070049 IBinder service = ServiceManager.checkService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
50 return service != null;
51 }
52
53 @Nullable
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
63 private ContentCaptureUtils() {
64 throw new UnsupportedOperationException("contains only static methods");
65 }
66}