• 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 android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.UserInfo;
24 import android.content.res.Resources;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.provider.Settings;
28 import android.text.SpannableStringBuilder;
29 import android.text.style.ClickableSpan;
30 import android.view.View;
31 
32 import com.android.settings.R;
33 import com.android.settings.applications.PackageManagerWrapper;
34 import com.android.settings.vpn2.ConnectivityManagerWrapper;
35 import com.android.settings.vpn2.VpnUtils;
36 
37 import java.util.Date;
38 import java.util.List;
39 
40 public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFeatureProvider {
41 
42     private final Context mContext;
43     private final DevicePolicyManagerWrapper mDpm;
44     private final PackageManagerWrapper mPm;
45     private final UserManager mUm;
46     private final ConnectivityManagerWrapper mCm;
47     private final Resources mResources;
48 
49     private static final int MY_USER_ID = UserHandle.myUserId();
50 
EnterprisePrivacyFeatureProviderImpl(Context context, DevicePolicyManagerWrapper dpm, PackageManagerWrapper pm, UserManager um, ConnectivityManagerWrapper cm, Resources resources)51     public EnterprisePrivacyFeatureProviderImpl(Context context, DevicePolicyManagerWrapper dpm,
52             PackageManagerWrapper pm, UserManager um, ConnectivityManagerWrapper cm,
53             Resources resources) {
54         mContext = context.getApplicationContext();
55         mDpm = dpm;
56         mPm = pm;
57         mUm = um;
58         mCm = cm;
59         mResources = resources;
60     }
61 
62     @Override
hasDeviceOwner()63     public boolean hasDeviceOwner() {
64         if (!mPm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) {
65             return false;
66         }
67         return mDpm.getDeviceOwnerComponentOnAnyUser() != null;
68     }
69 
getManagedProfileUserId()70     private int getManagedProfileUserId() {
71         for (final UserInfo userInfo : mUm.getProfiles(MY_USER_ID)) {
72             if (userInfo.isManagedProfile()) {
73                 return userInfo.id;
74             }
75         }
76         return UserHandle.USER_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(mResources.getString(R.string.do_disclosure_with_name,
104                     organizationName));
105         } else {
106             disclosure.append(mResources.getString(R.string.do_disclosure_generic));
107         }
108         disclosure.append(mResources.getString(R.string.do_disclosure_learn_more_separator));
109         disclosure.append(mResources.getString(R.string.do_disclosure_learn_more),
110                 new EnterprisePrivacySpan(mContext), 0);
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(mCm, 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(mCm, managedProfileUserId);
152     }
153 
154     @Override
isGlobalHttpProxySet()155     public boolean isGlobalHttpProxySet() {
156         return mCm.getGlobalProxy() != null;
157     }
158 
159     @Override
getMaximumFailedPasswordsBeforeWipeInCurrentUser()160     public int getMaximumFailedPasswordsBeforeWipeInCurrentUser() {
161         final ComponentName profileOwner = mDpm.getProfileOwnerAsUser(MY_USER_ID);
162         if (profileOwner == null) {
163             return 0;
164         }
165         return mDpm.getMaximumFailedPasswordsForWipe(profileOwner, MY_USER_ID);
166     }
167 
168     @Override
getMaximumFailedPasswordsBeforeWipeInManagedProfile()169     public int getMaximumFailedPasswordsBeforeWipeInManagedProfile() {
170         final int userId = getManagedProfileUserId();
171         if (userId == UserHandle.USER_NULL) {
172             return 0;
173         }
174         final ComponentName profileOwner = mDpm.getProfileOwnerAsUser(userId);
175         if (profileOwner == null) {
176             return 0;
177         }
178         return mDpm.getMaximumFailedPasswordsForWipe(profileOwner, userId);
179     }
180 
181     @Override
getImeLabelIfOwnerSet()182     public String getImeLabelIfOwnerSet() {
183         if (!mDpm.isCurrentInputMethodSetByOwner()) {
184             return null;
185         }
186         final String packageName = Settings.Secure.getStringForUser(mContext.getContentResolver(),
187                 Settings.Secure.DEFAULT_INPUT_METHOD, MY_USER_ID);
188         if (packageName == null) {
189             return null;
190         }
191         try {
192             return mPm.getApplicationInfoAsUser(packageName, 0 /* flags */, MY_USER_ID)
193                     .loadLabel(mPm.getPackageManager()).toString();
194         } catch (PackageManager.NameNotFoundException e) {
195             return null;
196         }
197     }
198 
199     @Override
getNumberOfOwnerInstalledCaCertsForCurrentUserAndManagedProfile()200     public int getNumberOfOwnerInstalledCaCertsForCurrentUserAndManagedProfile() {
201         int num = 0;
202         List<String> certs = mDpm.getOwnerInstalledCaCerts(new UserHandle(MY_USER_ID));
203         if (certs != null) {
204             num += certs.size();
205         }
206         final int userId = getManagedProfileUserId();
207         if (userId != UserHandle.USER_NULL) {
208             certs = mDpm.getOwnerInstalledCaCerts(new UserHandle(userId));
209             if (certs != null) {
210                 num += certs.size();
211             }
212         }
213         return num;
214     }
215 
216     @Override
getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()217     public int getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile() {
218         int activeAdmins = 0;
219         for (final UserInfo userInfo : mUm.getProfiles(MY_USER_ID)) {
220             final List<ComponentName> activeAdminsForUser
221                     = mDpm.getActiveAdminsAsUser(userInfo.id);
222             if (activeAdminsForUser != null) {
223                 activeAdmins += activeAdminsForUser.size();
224             }
225         }
226         return activeAdmins;
227     }
228 
229     protected static class EnterprisePrivacySpan extends ClickableSpan {
230         private final Context mContext;
231 
EnterprisePrivacySpan(Context context)232         public EnterprisePrivacySpan(Context context) {
233             mContext = context;
234         }
235 
236         @Override
onClick(View widget)237         public void onClick(View widget) {
238             mContext.startActivity(new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS));
239         }
240 
241         @Override
equals(Object object)242         public boolean equals(Object object) {
243             return object instanceof EnterprisePrivacySpan
244                     && ((EnterprisePrivacySpan) object).mContext == mContext;
245         }
246     }
247 }
248