Home | History | Annotate | Download | only in enterprise
      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.app.admin.DevicePolicyManager;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.PackageManager;
     24 import android.content.pm.UserInfo;
     25 import android.content.res.Resources;
     26 import android.net.ConnectivityManager;
     27 import android.os.UserHandle;
     28 import android.os.UserManager;
     29 import android.provider.Settings;
     30 import android.text.SpannableStringBuilder;
     31 import android.text.style.ClickableSpan;
     32 import android.view.View;
     33 
     34 import com.android.settings.R;
     35 import com.android.settings.vpn2.VpnUtils;
     36 import com.android.settingslib.wrapper.PackageManagerWrapper;
     37 
     38 import java.util.Date;
     39 import java.util.List;
     40 
     41 public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFeatureProvider {
     42 
     43     private final Context mContext;
     44     private final DevicePolicyManager mDpm;
     45     private final PackageManagerWrapper mPm;
     46     private final UserManager mUm;
     47     private final ConnectivityManager mCm;
     48     private final Resources mResources;
     49 
     50     private static final int MY_USER_ID = UserHandle.myUserId();
     51 
     52     public EnterprisePrivacyFeatureProviderImpl(Context context, DevicePolicyManager dpm,
     53             PackageManagerWrapper pm, UserManager um, ConnectivityManager cm,
     54             Resources resources) {
     55         mContext = context.getApplicationContext();
     56         mDpm = dpm;
     57         mPm = pm;
     58         mUm = um;
     59         mCm = cm;
     60         mResources = resources;
     61     }
     62 
     63     @Override
     64     public boolean hasDeviceOwner() {
     65         if (!mPm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) {
     66             return false;
     67         }
     68         return mDpm.getDeviceOwnerComponentOnAnyUser() != null;
     69     }
     70 
     71     private int getManagedProfileUserId() {
     72         for (final UserInfo userInfo : mUm.getProfiles(MY_USER_ID)) {
     73             if (userInfo.isManagedProfile()) {
     74                 return userInfo.id;
     75             }
     76         }
     77         return UserHandle.USER_NULL;
     78     }
     79 
     80     @Override
     81     public boolean isInCompMode() {
     82         return hasDeviceOwner() && getManagedProfileUserId() != UserHandle.USER_NULL;
     83     }
     84 
     85     @Override
     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
     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(mResources.getString(R.string.do_disclosure_with_name,
    105                     organizationName));
    106         } else {
    107             disclosure.append(mResources.getString(R.string.do_disclosure_generic));
    108         }
    109         disclosure.append(mResources.getString(R.string.do_disclosure_learn_more_separator));
    110         disclosure.append(mResources.getString(R.string.learn_more),
    111                 new EnterprisePrivacySpan(mContext), 0);
    112         return disclosure;
    113     }
    114 
    115     @Override
    116     public Date getLastSecurityLogRetrievalTime() {
    117         final long timestamp = mDpm.getLastSecurityLogRetrievalTime();
    118         return timestamp < 0 ? null : new Date(timestamp);
    119     }
    120 
    121     @Override
    122     public Date getLastBugReportRequestTime() {
    123         final long timestamp = mDpm.getLastBugReportRequestTime();
    124         return timestamp < 0 ? null : new Date(timestamp);
    125     }
    126 
    127     @Override
    128     public Date getLastNetworkLogRetrievalTime() {
    129         final long timestamp = mDpm.getLastNetworkLogRetrievalTime();
    130         return timestamp < 0 ? null : new Date(timestamp);
    131     }
    132 
    133     @Override
    134     public boolean isSecurityLoggingEnabled() {
    135         return mDpm.isSecurityLoggingEnabled(null);
    136     }
    137 
    138     @Override
    139     public boolean isNetworkLoggingEnabled() {
    140         return mDpm.isNetworkLoggingEnabled(null);
    141     }
    142 
    143     @Override
    144     public boolean isAlwaysOnVpnSetInCurrentUser() {
    145         return VpnUtils.isAlwaysOnVpnSet(mCm, MY_USER_ID);
    146     }
    147 
    148     @Override
    149     public boolean isAlwaysOnVpnSetInManagedProfile() {
    150         final int managedProfileUserId = getManagedProfileUserId();
    151         return managedProfileUserId != UserHandle.USER_NULL &&
    152                 VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId);
    153     }
    154 
    155     @Override
    156     public boolean isGlobalHttpProxySet() {
    157         return mCm.getGlobalProxy() != null;
    158     }
    159 
    160     @Override
    161     public int getMaximumFailedPasswordsBeforeWipeInCurrentUser() {
    162         ComponentName owner = mDpm.getDeviceOwnerComponentOnCallingUser();
    163         if (owner == null) {
    164             owner = mDpm.getProfileOwnerAsUser(MY_USER_ID);
    165         }
    166         if (owner == null) {
    167             return 0;
    168         }
    169         return mDpm.getMaximumFailedPasswordsForWipe(owner, MY_USER_ID);
    170     }
    171 
    172     @Override
    173     public int getMaximumFailedPasswordsBeforeWipeInManagedProfile() {
    174         final int userId = getManagedProfileUserId();
    175         if (userId == UserHandle.USER_NULL) {
    176             return 0;
    177         }
    178         final ComponentName profileOwner = mDpm.getProfileOwnerAsUser(userId);
    179         if (profileOwner == null) {
    180             return 0;
    181         }
    182         return mDpm.getMaximumFailedPasswordsForWipe(profileOwner, userId);
    183     }
    184 
    185     @Override
    186     public String getImeLabelIfOwnerSet() {
    187         if (!mDpm.isCurrentInputMethodSetByOwner()) {
    188             return null;
    189         }
    190         final String packageName = Settings.Secure.getStringForUser(mContext.getContentResolver(),
    191                 Settings.Secure.DEFAULT_INPUT_METHOD, MY_USER_ID);
    192         if (packageName == null) {
    193             return null;
    194         }
    195         try {
    196             return mPm.getApplicationInfoAsUser(packageName, 0 /* flags */, MY_USER_ID)
    197                     .loadLabel(mPm.getPackageManager()).toString();
    198         } catch (PackageManager.NameNotFoundException e) {
    199             return null;
    200         }
    201     }
    202 
    203     @Override
    204     public int getNumberOfOwnerInstalledCaCertsForCurrentUser() {
    205         final List<String> certs = mDpm.getOwnerInstalledCaCerts(new UserHandle(MY_USER_ID));
    206         if (certs == null) {
    207             return 0;
    208         }
    209         return certs.size();
    210     }
    211 
    212     @Override
    213     public int getNumberOfOwnerInstalledCaCertsForManagedProfile() {
    214         final int userId = getManagedProfileUserId();
    215         if (userId == UserHandle.USER_NULL) {
    216             return 0;
    217         }
    218         final List<String> certs = mDpm.getOwnerInstalledCaCerts(new UserHandle(userId));
    219         if (certs == null) {
    220             return 0;
    221         }
    222         return certs.size();
    223     }
    224 
    225     @Override
    226     public int getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile() {
    227         int activeAdmins = 0;
    228         for (final UserInfo userInfo : mUm.getProfiles(MY_USER_ID)) {
    229             final List<ComponentName> activeAdminsForUser
    230                     = mDpm.getActiveAdminsAsUser(userInfo.id);
    231             if (activeAdminsForUser != null) {
    232                 activeAdmins += activeAdminsForUser.size();
    233             }
    234         }
    235         return activeAdmins;
    236     }
    237 
    238     @Override
    239     public boolean areBackupsMandatory() {
    240         return null != mDpm.getMandatoryBackupTransport();
    241     }
    242 
    243     protected static class EnterprisePrivacySpan extends ClickableSpan {
    244         private final Context mContext;
    245 
    246         public EnterprisePrivacySpan(Context context) {
    247             mContext = context;
    248         }
    249 
    250         @Override
    251         public void onClick(View widget) {
    252             mContext.startActivity(new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS)
    253                     .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    254         }
    255 
    256         @Override
    257         public boolean equals(Object object) {
    258             return object instanceof EnterprisePrivacySpan
    259                     && ((EnterprisePrivacySpan) object).mContext == mContext;
    260         }
    261     }
    262 }
    263