Merge "Updated privacy settings to launch custom content capture settings when available."
This commit is contained in:
committed by
Android (Google) Code Review
commit
4d33ead8ef
@@ -63,12 +63,25 @@
|
|||||||
settings:searchable="false"/>
|
settings:searchable="false"/>
|
||||||
|
|
||||||
<!-- Content Capture -->
|
<!-- Content Capture -->
|
||||||
|
|
||||||
|
<!-- NOTE: content capture has a different preference, depending whether or not the
|
||||||
|
ContentCaptureService implementations defines a custom settings activitiy on its manifest.
|
||||||
|
Hence, we show both here, but the controller itself will decide if it's available or not.
|
||||||
|
-->
|
||||||
|
|
||||||
<SwitchPreference
|
<SwitchPreference
|
||||||
android:key="content_capture"
|
android:key="content_capture"
|
||||||
android:title="@string/content_capture"
|
android:title="@string/content_capture"
|
||||||
android:summary="@string/content_capture_summary"
|
android:summary="@string/content_capture_summary"
|
||||||
settings:controller="com.android.settings.privacy.EnableContentCapturePreferenceController"/>
|
settings:controller="com.android.settings.privacy.EnableContentCapturePreferenceController"/>
|
||||||
|
|
||||||
|
<com.android.settings.widget.MasterSwitchPreference
|
||||||
|
android:key="content_capture_custom_settings"
|
||||||
|
android:title="@string/content_capture"
|
||||||
|
android:summary="@string/content_capture_summary"
|
||||||
|
settings:controller="com.android.settings.privacy.EnableContentCaptureWithServiceSettingsPreferenceController">
|
||||||
|
</com.android.settings.widget.MasterSwitchPreference>
|
||||||
|
|
||||||
<!-- Privacy Service -->
|
<!-- Privacy Service -->
|
||||||
<PreferenceCategory
|
<PreferenceCategory
|
||||||
android:key="privacy_services"/>
|
android:key="privacy_services"/>
|
||||||
|
@@ -16,43 +16,34 @@
|
|||||||
|
|
||||||
package com.android.settings.privacy;
|
package com.android.settings.privacy;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.IBinder;
|
|
||||||
import android.os.ServiceManager;
|
|
||||||
import android.os.UserHandle;
|
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.view.contentcapture.ContentCaptureManager;
|
|
||||||
|
|
||||||
import com.android.settings.core.TogglePreferenceController;
|
import com.android.settings.core.TogglePreferenceController;
|
||||||
|
import com.android.settings.utils.ContentCaptureUtils;
|
||||||
|
|
||||||
public class EnableContentCapturePreferenceController extends TogglePreferenceController {
|
public final class EnableContentCapturePreferenceController extends TogglePreferenceController {
|
||||||
|
|
||||||
private static final String KEY_SHOW_PASSWORD = "content_capture";
|
public EnableContentCapturePreferenceController(@NonNull Context context, @NonNull String key) {
|
||||||
private static final int MY_USER_ID = UserHandle.myUserId();
|
super(context, key);
|
||||||
|
|
||||||
public EnableContentCapturePreferenceController(Context context) {
|
|
||||||
super(context, KEY_SHOW_PASSWORD);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isChecked() {
|
public boolean isChecked() {
|
||||||
boolean enabled = Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
return ContentCaptureUtils.isEnabledForUser(mContext);
|
||||||
Settings.Secure.CONTENT_CAPTURE_ENABLED, 1, MY_USER_ID) == 1;
|
|
||||||
return enabled;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setChecked(boolean isChecked) {
|
public boolean setChecked(boolean isChecked) {
|
||||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
ContentCaptureUtils.setEnabledForUser(mContext, isChecked);
|
||||||
Settings.Secure.CONTENT_CAPTURE_ENABLED, isChecked ? 1 : 0, MY_USER_ID);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAvailabilityStatus() {
|
public int getAvailabilityStatus() {
|
||||||
// We cannot look for ContentCaptureManager, because it's not available if the service
|
boolean available = ContentCaptureUtils.isFeatureAvailable()
|
||||||
// didn't whitelist Settings
|
&& ContentCaptureUtils.getServiceSettingsComponentName() == null;
|
||||||
IBinder service = ServiceManager.checkService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
|
return available ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||||
return service != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.privacy;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
|
import com.android.settings.core.TogglePreferenceController;
|
||||||
|
import com.android.settings.utils.ContentCaptureUtils;
|
||||||
|
import com.android.settings.widget.MasterSwitchPreference;
|
||||||
|
|
||||||
|
public final class EnableContentCaptureWithServiceSettingsPreferenceController
|
||||||
|
extends TogglePreferenceController {
|
||||||
|
|
||||||
|
private static final String TAG = "ContentCaptureController";
|
||||||
|
|
||||||
|
public EnableContentCaptureWithServiceSettingsPreferenceController(@NonNull Context context,
|
||||||
|
@NonNull String key) {
|
||||||
|
super(context, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isChecked() {
|
||||||
|
return ContentCaptureUtils.isEnabledForUser(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setChecked(boolean isChecked) {
|
||||||
|
ContentCaptureUtils.setEnabledForUser(mContext, isChecked);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateState(Preference preference) {
|
||||||
|
super.updateState(preference);
|
||||||
|
|
||||||
|
ComponentName componentName = ContentCaptureUtils.getServiceSettingsComponentName();
|
||||||
|
if (componentName != null) {
|
||||||
|
preference.setIntent(new Intent(Intent.ACTION_MAIN).setComponent(componentName));
|
||||||
|
} else {
|
||||||
|
// Should not happen - preference should be disabled by controller
|
||||||
|
Log.w(TAG, "No component name for custom service settings");
|
||||||
|
preference.setSelectable(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
boolean available = ContentCaptureUtils.isFeatureAvailable()
|
||||||
|
&& ContentCaptureUtils.getServiceSettingsComponentName() != null;
|
||||||
|
return available ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||||
|
}
|
||||||
|
}
|
66
src/com/android/settings/utils/ContentCaptureUtils.java
Normal file
66
src/com/android/settings/utils/ContentCaptureUtils.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.utils;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.Nullable;
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.os.ServiceManager;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.contentcapture.ContentCaptureManager;
|
||||||
|
|
||||||
|
public final class ContentCaptureUtils {
|
||||||
|
|
||||||
|
private static final String TAG = ContentCaptureUtils.class.getSimpleName();
|
||||||
|
private static final int MY_USER_ID = UserHandle.myUserId();
|
||||||
|
|
||||||
|
public static boolean isEnabledForUser(@NonNull Context context) {
|
||||||
|
boolean enabled = Settings.Secure.getIntForUser(context.getContentResolver(),
|
||||||
|
Settings.Secure.CONTENT_CAPTURE_ENABLED, 1, MY_USER_ID) == 1;
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setEnabledForUser(@NonNull Context context, boolean enabled) {
|
||||||
|
Settings.Secure.putIntForUser(context.getContentResolver(),
|
||||||
|
Settings.Secure.CONTENT_CAPTURE_ENABLED, enabled ? 1 : 0, MY_USER_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFeatureAvailable() {
|
||||||
|
// We cannot look for ContentCaptureManager, because it's not available if the service
|
||||||
|
// didn't whitelist Settings
|
||||||
|
IBinder service = ServiceManager.checkService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
|
||||||
|
return service != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static ComponentName getServiceSettingsComponentName() {
|
||||||
|
try {
|
||||||
|
return ContentCaptureManager.getServiceSettingsComponentName();
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
Log.w(TAG, "Could not get service settings: " + e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ContentCaptureUtils() {
|
||||||
|
throw new UnsupportedOperationException("contains only static methods");
|
||||||
|
}
|
||||||
|
}
|
@@ -51,7 +51,7 @@ public class EnableContentCapturePreferenceControllerTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
mController = new EnableContentCapturePreferenceController(mContext);
|
mController = new EnableContentCapturePreferenceController(mContext, "THE_KEY_TO_SUCCESS");
|
||||||
mPreference = new Preference(mContext);
|
mPreference = new Preference(mContext);
|
||||||
mPreference.setKey(mController.getPreferenceKey());
|
mPreference.setKey(mController.getPreferenceKey());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user