1 /* 2 * Copyright (C) 2021 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 package com.android.car.settingslib.enterprise; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.os.UserHandle; 23 import android.provider.Settings; 24 25 import com.android.car.settings.common.Logger; 26 27 import java.util.Date; 28 29 /** 30 * TODO(b/208511815): copied from phone (but stripped what's not used), should be moved to 31 * SettingsLib 32 */ 33 public final class EnterprisePrivacyFeatureProviderImpl 34 implements EnterprisePrivacyFeatureProvider { 35 36 private static final int MY_USER_ID = UserHandle.myUserId(); 37 38 private final Logger mLogger = new Logger(getClass()); 39 private final Context mContext; 40 private final DevicePolicyManager mDpm; 41 private final PackageManager mPm; 42 EnterprisePrivacyFeatureProviderImpl(Context context, DevicePolicyManager dpm, PackageManager pm)43 public EnterprisePrivacyFeatureProviderImpl(Context context, DevicePolicyManager dpm, 44 PackageManager pm) { 45 mContext = context; 46 mDpm = dpm; 47 mPm = pm; 48 } 49 50 @Override getLastSecurityLogRetrievalTime()51 public Date getLastSecurityLogRetrievalTime() { 52 long timestamp = mDpm.getLastSecurityLogRetrievalTime(); 53 return timestamp < 0 ? null : new Date(timestamp); 54 } 55 56 @Override getLastBugReportRequestTime()57 public Date getLastBugReportRequestTime() { 58 long timestamp = mDpm.getLastBugReportRequestTime(); 59 return timestamp < 0 ? null : new Date(timestamp); 60 } 61 62 @Override getLastNetworkLogRetrievalTime()63 public Date getLastNetworkLogRetrievalTime() { 64 long timestamp = mDpm.getLastNetworkLogRetrievalTime(); 65 return timestamp < 0 ? null : new Date(timestamp); 66 } 67 68 @Override getImeLabelIfOwnerSet()69 public String getImeLabelIfOwnerSet() { 70 if (!mDpm.isCurrentInputMethodSetByOwner()) { 71 return null; 72 } 73 String packageName = Settings.Secure.getStringForUser(mContext.getContentResolver(), 74 Settings.Secure.DEFAULT_INPUT_METHOD, MY_USER_ID); 75 if (packageName == null) { 76 return null; 77 } 78 try { 79 return mPm.getApplicationInfoAsUser(packageName, /* flags= */ 0, MY_USER_ID) 80 .loadLabel(mPm).toString(); 81 } catch (PackageManager.NameNotFoundException e) { 82 mLogger.w("Could not get label for " + packageName + " and user " + MY_USER_ID, e); 83 return null; 84 } 85 } 86 87 @Override getMaximumFailedPasswordsBeforeWipeInCurrentUser()88 public int getMaximumFailedPasswordsBeforeWipeInCurrentUser() { 89 ComponentName owner = mDpm.getDeviceOwnerComponentOnCallingUser(); 90 if (owner == null) { 91 owner = mDpm.getProfileOwnerAsUser(MY_USER_ID); 92 } 93 if (owner == null) { 94 return 0; 95 } 96 return mDpm.getMaximumFailedPasswordsForWipe(owner, MY_USER_ID); 97 }; 98 } 99