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