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