1 /* 2 * Copyright (C) 2016 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.enterprise; 18 19 import static android.app.admin.DevicePolicyResources.Strings.Settings.DEVICE_MANAGED_WITHOUT_NAME; 20 import static android.app.admin.DevicePolicyResources.Strings.Settings.DEVICE_MANAGED_WITH_NAME; 21 22 import android.app.admin.DevicePolicyManager; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.pm.PackageManager; 27 import android.content.pm.ResolveInfo; 28 import android.content.pm.UserInfo; 29 import android.content.pm.UserProperties; 30 import android.content.res.Resources; 31 import android.net.ConnectivityManager; 32 import android.net.VpnManager; 33 import android.os.UserHandle; 34 import android.os.UserManager; 35 import android.provider.Settings; 36 import android.text.SpannableStringBuilder; 37 38 import com.android.settings.R; 39 import com.android.settings.vpn2.VpnUtils; 40 import com.android.settingslib.supervision.SupervisionIntentProvider; 41 import com.android.settingslib.utils.WorkPolicyUtils; 42 43 import java.util.Date; 44 import java.util.List; 45 46 public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFeatureProvider { 47 48 @Deprecated 49 public static final String ACTION_PARENTAL_CONTROLS = 50 "android.settings.SHOW_PARENTAL_CONTROLS"; 51 52 private final Context mContext; 53 private final DevicePolicyManager mDpm; 54 private final PackageManager mPm; 55 private final UserManager mUm; 56 private final ConnectivityManager mCm; 57 private final VpnManager mVm; 58 private final Resources mResources; 59 private final WorkPolicyUtils mWorkPolicyUtils; 60 61 private static final int MY_USER_ID = UserHandle.myUserId(); 62 EnterprisePrivacyFeatureProviderImpl(Context context, DevicePolicyManager dpm, PackageManager pm, UserManager um, ConnectivityManager cm, VpnManager vm, Resources resources)63 public EnterprisePrivacyFeatureProviderImpl(Context context, DevicePolicyManager dpm, 64 PackageManager pm, UserManager um, ConnectivityManager cm, VpnManager vm, 65 Resources resources) { 66 mContext = context.getApplicationContext(); 67 mDpm = dpm; 68 mPm = pm; 69 mUm = um; 70 mCm = cm; 71 mVm = vm; 72 mResources = resources; 73 mWorkPolicyUtils = new WorkPolicyUtils(mContext); 74 } 75 76 @Override hasDeviceOwner()77 public boolean hasDeviceOwner() { 78 return getDeviceOwnerComponent() != null; 79 } 80 81 @Override isInCompMode()82 public boolean isInCompMode() { 83 return hasDeviceOwner() && getManagedProfileUserId() != UserHandle.USER_NULL; 84 } 85 86 @Override getDeviceOwnerOrganizationName()87 public String getDeviceOwnerOrganizationName() { 88 final CharSequence organizationName = mDpm.getDeviceOwnerOrganizationName(); 89 if (organizationName == null) { 90 return null; 91 } else { 92 return organizationName.toString(); 93 } 94 } 95 96 @Override getDeviceOwnerDisclosure()97 public CharSequence getDeviceOwnerDisclosure() { 98 if (!hasDeviceOwner()) { 99 return null; 100 } 101 102 final SpannableStringBuilder disclosure = new SpannableStringBuilder(); 103 final CharSequence organizationName = mDpm.getDeviceOwnerOrganizationName(); 104 if (organizationName != null) { 105 disclosure.append(mDpm.getResources().getString(DEVICE_MANAGED_WITH_NAME, 106 () -> mResources.getString(R.string.do_disclosure_with_name, 107 organizationName), organizationName)); 108 } else { 109 disclosure.append(mDpm.getResources().getString(DEVICE_MANAGED_WITHOUT_NAME, 110 () -> mResources.getString(R.string.do_disclosure_generic))); 111 } 112 return disclosure; 113 } 114 115 @Override getLastSecurityLogRetrievalTime()116 public Date getLastSecurityLogRetrievalTime() { 117 final long timestamp = mDpm.getLastSecurityLogRetrievalTime(); 118 return timestamp < 0 ? null : new Date(timestamp); 119 } 120 121 @Override getLastBugReportRequestTime()122 public Date getLastBugReportRequestTime() { 123 final long timestamp = mDpm.getLastBugReportRequestTime(); 124 return timestamp < 0 ? null : new Date(timestamp); 125 } 126 127 @Override getLastNetworkLogRetrievalTime()128 public Date getLastNetworkLogRetrievalTime() { 129 final long timestamp = mDpm.getLastNetworkLogRetrievalTime(); 130 return timestamp < 0 ? null : new Date(timestamp); 131 } 132 133 @Override isSecurityLoggingEnabled()134 public boolean isSecurityLoggingEnabled() { 135 return mDpm.isSecurityLoggingEnabled(null); 136 } 137 138 @Override isNetworkLoggingEnabled()139 public boolean isNetworkLoggingEnabled() { 140 return mDpm.isNetworkLoggingEnabled(null); 141 } 142 143 @Override isAlwaysOnVpnSetInCurrentUser()144 public boolean isAlwaysOnVpnSetInCurrentUser() { 145 return VpnUtils.isAlwaysOnVpnSet(mVm, MY_USER_ID); 146 } 147 148 @Override isAlwaysOnVpnSetInManagedProfile()149 public boolean isAlwaysOnVpnSetInManagedProfile() { 150 final int managedProfileUserId = getManagedProfileUserId(); 151 return managedProfileUserId != UserHandle.USER_NULL && 152 VpnUtils.isAlwaysOnVpnSet(mVm, managedProfileUserId); 153 } 154 155 @Override getMaximumFailedPasswordsBeforeWipeInCurrentUser()156 public int getMaximumFailedPasswordsBeforeWipeInCurrentUser() { 157 ComponentName owner = mDpm.getDeviceOwnerComponentOnCallingUser(); 158 if (owner == null) { 159 owner = mDpm.getProfileOwnerAsUser(MY_USER_ID); 160 } 161 if (owner == null) { 162 return 0; 163 } 164 return mDpm.getMaximumFailedPasswordsForWipe(owner, MY_USER_ID); 165 } 166 167 @Override getMaximumFailedPasswordsBeforeWipeInManagedProfile()168 public int getMaximumFailedPasswordsBeforeWipeInManagedProfile() { 169 final int userId = getManagedProfileUserId(); 170 if (userId == UserHandle.USER_NULL) { 171 return 0; 172 } 173 final ComponentName profileOwner = mDpm.getProfileOwnerAsUser(userId); 174 if (profileOwner == null) { 175 return 0; 176 } 177 return mDpm.getMaximumFailedPasswordsForWipe(profileOwner, userId); 178 } 179 180 @Override getImeLabelIfOwnerSet()181 public String getImeLabelIfOwnerSet() { 182 if (!mDpm.isCurrentInputMethodSetByOwner()) { 183 return null; 184 } 185 final String packageName = Settings.Secure.getStringForUser(mContext.getContentResolver(), 186 Settings.Secure.DEFAULT_INPUT_METHOD, MY_USER_ID); 187 if (packageName == null) { 188 return null; 189 } 190 try { 191 return mPm.getApplicationInfoAsUser(packageName, 0 /* flags */, MY_USER_ID) 192 .loadLabel(mPm).toString(); 193 } catch (PackageManager.NameNotFoundException e) { 194 return null; 195 } 196 } 197 198 @Override getNumberOfOwnerInstalledCaCertsForCurrentUser()199 public int getNumberOfOwnerInstalledCaCertsForCurrentUser() { 200 final List<String> certs = mDpm.getOwnerInstalledCaCerts(new UserHandle(MY_USER_ID)); 201 if (certs == null) { 202 return 0; 203 } 204 return certs.size(); 205 } 206 207 @Override getNumberOfOwnerInstalledCaCertsForManagedProfile()208 public int getNumberOfOwnerInstalledCaCertsForManagedProfile() { 209 final int userId = getManagedProfileUserId(); 210 if (userId == UserHandle.USER_NULL) { 211 return 0; 212 } 213 final List<String> certs = mDpm.getOwnerInstalledCaCerts(new UserHandle(userId)); 214 if (certs == null) { 215 return 0; 216 } 217 return certs.size(); 218 } 219 220 @Override getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()221 public int getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile() { 222 int activeAdmins = 0; 223 for (final UserInfo userInfo : mUm.getProfiles(MY_USER_ID)) { 224 if (shouldSkipProfile(userInfo)) { 225 continue; 226 } 227 final List<ComponentName> activeAdminsForUser 228 = mDpm.getActiveAdminsAsUser(userInfo.id); 229 if (activeAdminsForUser != null) { 230 activeAdmins += activeAdminsForUser.size(); 231 } 232 } 233 return activeAdmins; 234 } 235 236 @Override hasWorkPolicyInfo()237 public boolean hasWorkPolicyInfo() { 238 return mWorkPolicyUtils.hasWorkPolicy(); 239 } 240 241 @Override showWorkPolicyInfo(Context activityContext)242 public boolean showWorkPolicyInfo(Context activityContext) { 243 return mWorkPolicyUtils.showWorkPolicyInfo(activityContext); 244 } 245 246 @Override showParentalControls()247 public boolean showParentalControls() { 248 Intent intent = 249 android.app.supervision.flags.Flags.deprecateDpmSupervisionApis() 250 ? SupervisionIntentProvider.getSettingsIntent(mContext) 251 : getParentalControlsIntent(); 252 if (intent != null) { 253 mContext.startActivity(intent); 254 return true; 255 } 256 257 return false; 258 } 259 shouldSkipProfile(UserInfo userInfo)260 private boolean shouldSkipProfile(UserInfo userInfo) { 261 return android.os.Flags.allowPrivateProfile() 262 && android.multiuser.Flags.handleInterleavedSettingsForPrivateSpace() 263 && android.multiuser.Flags.enablePrivateSpaceFeatures() 264 && userInfo.isQuietModeEnabled() 265 && mUm.getUserProperties(userInfo.getUserHandle()).getShowInQuietMode() 266 == UserProperties.SHOW_IN_QUIET_MODE_HIDDEN; 267 } 268 269 @Deprecated getParentalControlsIntent()270 private Intent getParentalControlsIntent() { 271 final ComponentName componentName = 272 mDpm.getProfileOwnerOrDeviceOwnerSupervisionComponent(new UserHandle(MY_USER_ID)); 273 if (componentName == null) { 274 return null; 275 } 276 277 final Intent intent = new Intent(ACTION_PARENTAL_CONTROLS) 278 .setPackage(componentName.getPackageName()) 279 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 280 final List<ResolveInfo> activities = mPm.queryIntentActivitiesAsUser(intent, 0, MY_USER_ID); 281 if (activities.size() != 0) { 282 return intent; 283 } 284 return null; 285 } 286 getDeviceOwnerComponent()287 private ComponentName getDeviceOwnerComponent() { 288 if (!mPm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { 289 return null; 290 } 291 return mDpm.getDeviceOwnerComponentOnAnyUser(); 292 } 293 getManagedProfileUserInfo()294 private UserInfo getManagedProfileUserInfo() { 295 for (final UserInfo userInfo : mUm.getProfiles(MY_USER_ID)) { 296 if (userInfo.isManagedProfile()) { 297 return userInfo; 298 } 299 } 300 return null; 301 } 302 getManagedProfileUserId()303 private int getManagedProfileUserId() { 304 final UserInfo userInfo = getManagedProfileUserInfo(); 305 if (userInfo != null) { 306 return userInfo.id; 307 } 308 return UserHandle.USER_NULL; 309 } 310 } 311