diff options
author | Bartosz Fabianowski <bartfab@google.com> | 2017-01-09 12:05:59 +0100 |
---|---|---|
committer | Bartosz Fabianowski <bartfab@google.com> | 2017-01-12 20:04:56 +0100 |
commit | fc018e467279f8d54aee7eba837ef2d604d795c1 (patch) | |
tree | 7f2b947e55738c7e3cce4f3128570156ddd64aee /src/com/android | |
parent | 78427d7467f294d400fb91311fb90c72dd437da7 (diff) | |
download | packages_apps_Settings-fc018e467279f8d54aee7eba837ef2d604d795c1.tar.gz packages_apps_Settings-fc018e467279f8d54aee7eba837ef2d604d795c1.tar.bz2 packages_apps_Settings-fc018e467279f8d54aee7eba837ef2d604d795c1.zip |
Add Always on VPN to Privacy Settings page
This CL adds information about always-on VPNs to the Enterprise
Privacy Settings page.
Test: make RunSettingsRoboTests
Bug: 32692748
Change-Id: I2b59e2ec4c55308b323aaa478cd9c847fe0b4b55
Diffstat (limited to 'src/com/android')
9 files changed, 234 insertions, 2 deletions
diff --git a/src/com/android/settings/enterprise/AlwaysOnVpnManagedProfilePreferenceController.java b/src/com/android/settings/enterprise/AlwaysOnVpnManagedProfilePreferenceController.java new file mode 100644 index 0000000000..52625ec77f --- /dev/null +++ b/src/com/android/settings/enterprise/AlwaysOnVpnManagedProfilePreferenceController.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; +import android.support.v7.preference.Preference; + +import com.android.settings.core.PreferenceController; +import com.android.settings.overlay.FeatureFactory; + +public class AlwaysOnVpnManagedProfilePreferenceController extends PreferenceController { + + private static final String KEY_ALWAYS_ON_VPN_MANAGED_PROFILE = "always_on_vpn_managed_profile"; + private final EnterprisePrivacyFeatureProvider mFeatureProvider; + + public AlwaysOnVpnManagedProfilePreferenceController(Context context) { + super(context); + mFeatureProvider = FeatureFactory.getFactory(context) + .getEnterprisePrivacyFeatureProvider(context); + } + + @Override + public void updateState(Preference preference) { + preference.setVisible(mFeatureProvider.isAlwaysOnVpnSetInManagedProfile()); + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public String getPreferenceKey() { + return KEY_ALWAYS_ON_VPN_MANAGED_PROFILE; + } +} diff --git a/src/com/android/settings/enterprise/AlwaysOnVpnPrimaryUserPreferenceController.java b/src/com/android/settings/enterprise/AlwaysOnVpnPrimaryUserPreferenceController.java new file mode 100644 index 0000000000..c7ffeaf01d --- /dev/null +++ b/src/com/android/settings/enterprise/AlwaysOnVpnPrimaryUserPreferenceController.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; +import android.support.v7.preference.Preference; + +import com.android.settings.R; +import com.android.settings.core.PreferenceController; +import com.android.settings.overlay.FeatureFactory; + +public class AlwaysOnVpnPrimaryUserPreferenceController extends PreferenceController { + + private static final String KEY_ALWAYS_ON_VPN_PRIMARY_USER = "always_on_vpn_primary_user"; + private final EnterprisePrivacyFeatureProvider mFeatureProvider; + + public AlwaysOnVpnPrimaryUserPreferenceController(Context context) { + super(context); + mFeatureProvider = FeatureFactory.getFactory(context) + .getEnterprisePrivacyFeatureProvider(context); + } + + @Override + public void updateState(Preference preference) { + preference.setTitle(mFeatureProvider.isInCompMode() + ? R.string.enterprise_privacy_always_on_vpn_personal + : R.string.enterprise_privacy_always_on_vpn_device); + preference.setVisible(mFeatureProvider.isAlwaysOnVpnSetInPrimaryUser()); + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public String getPreferenceKey() { + return KEY_ALWAYS_ON_VPN_PRIMARY_USER; + } +} diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java index efc02d624c..dec2d80c44 100644 --- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java +++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java @@ -26,6 +26,12 @@ public interface EnterprisePrivacyFeatureProvider { boolean hasDeviceOwner(); /** + * Returns whether the device is in COMP mode (primary user managed by a Device Owner app and + * work profile managed by a Profile Owner app). + */ + boolean isInCompMode(); + + /** * Returns the time at which the Device Owner last retrieved security logs, or {@code null} if * logs were never retrieved by the Device Owner on this device. */ @@ -42,4 +48,14 @@ public interface EnterprisePrivacyFeatureProvider { * logs were never retrieved by the Device Owner on this device. */ Date getLastNetworkLogRetrievalTime(); + + /** + * Returns whether the Device Owner in the primary user set an always-on VPN. + */ + boolean isAlwaysOnVpnSetInPrimaryUser(); + + /** + * Returns whether the Profile Owner in the managed profile (if any) set an always-on VPN. + */ + boolean isAlwaysOnVpnSetInManagedProfile(); } diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java index 2e8b7f6a20..a742cc361b 100644 --- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java +++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java @@ -17,20 +17,32 @@ package com.android.settings.enterprise; import android.content.pm.PackageManager; +import android.content.pm.UserInfo; +import android.os.UserHandle; +import android.os.UserManager; import com.android.settings.applications.PackageManagerWrapper; +import com.android.settings.vpn2.ConnectivityManagerWrapper; +import com.android.settings.vpn2.VpnUtils; import java.util.Date; +import java.util.List; public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFeatureProvider { private final DevicePolicyManagerWrapper mDpm; private final PackageManagerWrapper mPm; + private final UserManager mUm; + private final ConnectivityManagerWrapper mCm; + + private static final int MY_USER_ID = UserHandle.myUserId(); public EnterprisePrivacyFeatureProviderImpl(DevicePolicyManagerWrapper dpm, - PackageManagerWrapper pm) { + PackageManagerWrapper pm, UserManager um, ConnectivityManagerWrapper cm) { mDpm = dpm; mPm = pm; + mUm = um; + mCm = cm; } @Override @@ -41,19 +53,47 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe return mDpm.getDeviceOwnerComponentOnAnyUser() != null; } + private int getManagedProfileUserId() { + for (final UserInfo userInfo : mUm.getProfiles(MY_USER_ID)) { + if (userInfo.isManagedProfile()) { + return userInfo.id; + } + } + return -1; + } + + @Override + public boolean isInCompMode() { + return hasDeviceOwner() && getManagedProfileUserId() != -1; + } + @Override public Date getLastSecurityLogRetrievalTime() { final long timestamp = mDpm.getLastSecurityLogRetrievalTime(); return timestamp < 0 ? null : new Date(timestamp); } + @Override public Date getLastBugReportRequestTime() { final long timestamp = mDpm.getLastBugReportRequestTime(); return timestamp < 0 ? null : new Date(timestamp); } + @Override public Date getLastNetworkLogRetrievalTime() { final long timestamp = mDpm.getLastNetworkLogRetrievalTime(); return timestamp < 0 ? null : new Date(timestamp); } + + @Override + public boolean isAlwaysOnVpnSetInPrimaryUser() { + return VpnUtils.isAlwaysOnVpnSet(mCm, MY_USER_ID); + } + + @Override + public boolean isAlwaysOnVpnSetInManagedProfile() { + final int managedProfileUserId = getManagedProfileUserId(); + return managedProfileUserId != -1 && + VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId); + } } diff --git a/src/com/android/settings/enterprise/EnterprisePrivacySettings.java b/src/com/android/settings/enterprise/EnterprisePrivacySettings.java index 91d3a651e3..478f7fedf9 100644 --- a/src/com/android/settings/enterprise/EnterprisePrivacySettings.java +++ b/src/com/android/settings/enterprise/EnterprisePrivacySettings.java @@ -61,6 +61,8 @@ public class EnterprisePrivacySettings extends DashboardFragment { controllers.add(new NetworkLogsPreferenceController(context)); controllers.add(new BugReportsPreferenceController(context)); controllers.add(new SecurityLogsPreferenceController(context)); + controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context)); + controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context)); return controllers; } diff --git a/src/com/android/settings/overlay/FeatureFactoryImpl.java b/src/com/android/settings/overlay/FeatureFactoryImpl.java index eb5d065703..060b58c158 100644 --- a/src/com/android/settings/overlay/FeatureFactoryImpl.java +++ b/src/com/android/settings/overlay/FeatureFactoryImpl.java @@ -18,6 +18,8 @@ package com.android.settings.overlay; import android.app.admin.DevicePolicyManager; import android.content.Context; +import android.net.ConnectivityManager; +import android.os.UserManager; import android.support.annotation.Keep; import com.android.settings.applications.ApplicationFeatureProvider; @@ -37,6 +39,7 @@ import com.android.settings.security.SecurityFeatureProvider; import com.android.settings.security.SecurityFeatureProviderImpl; import com.android.settings.search2.SearchFeatureProvider; import com.android.settings.search2.SearchFeatureProviderImpl; +import com.android.settings.vpn2.ConnectivityManagerWrapperImpl; /** * {@link FeatureFactory} implementation for AOSP Settings. @@ -101,7 +104,10 @@ public class FeatureFactoryImpl extends FeatureFactory { mEnterprisePrivacyFeatureProvider = new EnterprisePrivacyFeatureProviderImpl( new DevicePolicyManagerWrapperImpl((DevicePolicyManager) context .getSystemService(Context.DEVICE_POLICY_SERVICE)), - new PackageManagerWrapperImpl(context.getPackageManager())); + new PackageManagerWrapperImpl(context.getPackageManager()), + UserManager.get(context), + new ConnectivityManagerWrapperImpl((ConnectivityManager) context + .getSystemService(Context.CONNECTIVITY_SERVICE))); } return mEnterprisePrivacyFeatureProvider; } diff --git a/src/com/android/settings/vpn2/ConnectivityManagerWrapper.java b/src/com/android/settings/vpn2/ConnectivityManagerWrapper.java new file mode 100644 index 0000000000..938db506a8 --- /dev/null +++ b/src/com/android/settings/vpn2/ConnectivityManagerWrapper.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2017 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.vpn2; + +/** + * This interface replicates a subset of the android.net.ConnectivityManager (CM). The interface + * exists so that we can use a thin wrapper around the CM in production code and a mock in tests. + * We cannot directly mock or shadow the CM, because some of the methods we rely on are marked as + * hidden and are thus invisible to Robolectric. + */ +public interface ConnectivityManagerWrapper { + + /** + * Calls {@code ConnectivityManager.getAlwaysOnVpnPackageForUser()}. + * + * @see android.net.ConnectivityManager#getAlwaysOnVpnPackageForUser + */ + String getAlwaysOnVpnPackageForUser(int userId); +} diff --git a/src/com/android/settings/vpn2/ConnectivityManagerWrapperImpl.java b/src/com/android/settings/vpn2/ConnectivityManagerWrapperImpl.java new file mode 100644 index 0000000000..ad1b4ebffd --- /dev/null +++ b/src/com/android/settings/vpn2/ConnectivityManagerWrapperImpl.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2017 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.vpn2; + +import android.net.ConnectivityManager; + +public class ConnectivityManagerWrapperImpl implements ConnectivityManagerWrapper { + + private final ConnectivityManager mCm; + + public ConnectivityManagerWrapperImpl(ConnectivityManager cm) { + mCm = cm; + } + + @Override + public String getAlwaysOnVpnPackageForUser(int userId) { + return mCm.getAlwaysOnVpnPackageForUser(userId); + } +} diff --git a/src/com/android/settings/vpn2/VpnUtils.java b/src/com/android/settings/vpn2/VpnUtils.java index 07e6c52be2..c9f971d7d4 100644 --- a/src/com/android/settings/vpn2/VpnUtils.java +++ b/src/com/android/settings/vpn2/VpnUtils.java @@ -82,4 +82,8 @@ public class VpnUtils { return IConnectivityManager.Stub.asInterface( ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); } + + public static boolean isAlwaysOnVpnSet(ConnectivityManagerWrapper cm, final int userId) { + return cm.getAlwaysOnVpnPackageForUser(userId) != null; + } } |