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.settings.enterprise;
17 
18 import android.app.admin.DevicePolicyManager;
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 import android.provider.Settings;
29 
30 import androidx.annotation.Nullable;
31 import androidx.preference.Preference;
32 
33 import com.android.car.settings.R;
34 import com.android.car.settings.common.FragmentController;
35 import com.android.car.settings.common.Logger;
36 import com.android.car.settings.common.PreferenceController;
37 
38 import java.util.List;
39 
40 /**
41  * Controller for showing the work policy info in the privacy dashboard.
42  */
43 public final class WorkPolicyInfoPreferenceController extends PreferenceController<Preference> {
44 
45     private static final Logger LOG = new Logger(WorkPolicyInfoPreferenceController.class);
46     private static final int MY_USER_ID = UserHandle.myUserId();
47 
48     private final DevicePolicyManager mDpm;
49     private final PackageManager mPm;
50     private final UserManager mUm;
51     private final boolean mEnabled;
52 
53     @Nullable
54     private Intent mIntent;
55 
56     @Nullable
57     private CharSequence mAdminName;
58 
WorkPolicyInfoPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)59     public WorkPolicyInfoPreferenceController(Context context, String preferenceKey,
60             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
61         super(context, preferenceKey, fragmentController, uxRestrictions);
62 
63         mDpm = context.getSystemService(DevicePolicyManager.class);
64         mPm = context.getPackageManager();
65         mUm = context.getSystemService(UserManager.class);
66         mEnabled = mPm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
67 
68         LOG.d("Constructed on user " + MY_USER_ID + ": " + (mEnabled ? "enabled" : "disabled"));
69     }
70 
71     @Override
getPreferenceType()72     protected Class<Preference> getPreferenceType() {
73         return Preference.class;
74     }
75 
76     @Override
updateState(Preference preference)77     protected void updateState(Preference preference) {
78         updateState();
79 
80         if (mIntent == null || mAdminName == null) return;
81 
82         preference.setIntent(mIntent);
83 
84         CharSequence title = getContext().getString(R.string.work_policy_privacy_settings,
85                 mAdminName);
86         LOG.d("Setting title to " + title);
87         preference.setTitle(title);
88     };
89 
90     @Override
getAvailabilityStatus()91     protected int getAvailabilityStatus() {
92         if (!mEnabled) return UNSUPPORTED_ON_DEVICE;
93 
94         updateState();
95 
96         return (mIntent == null || mAdminName == null) ? DISABLED_FOR_PROFILE : AVAILABLE;
97     }
98 
updateState()99     private void updateState() {
100         resetState();
101 
102         ComponentName admin = mDpm.getProfileOwner();
103         if (admin == null) {
104             LOG.d("no profile owner for user " + MY_USER_ID + ")");
105             return;
106         }
107         String adminPkgName = admin.getPackageName();
108         mIntent = new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO)
109                 .setPackage(adminPkgName)
110                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111 
112         List<ResolveInfo> activities = mPm.queryIntentActivities(mIntent, /* flags= */ 0);
113         if (activities.isEmpty()) {
114             LOG.d(admin.flattenToShortString() + " does not declare "
115                     + Settings.ACTION_SHOW_WORK_POLICY_INFO);
116             resetState();
117             return;
118         }
119 
120         try {
121             ApplicationInfo appInfo = mPm.getApplicationInfo(adminPkgName, /* flags= */ 0);
122             mAdminName = appInfo.loadLabel(mPm);
123         } catch (Exception e) {
124             LOG.e("could not get name of app " + adminPkgName, e);
125             resetState();
126             return;
127         }
128 
129         LOG.d("updateState(): name=" + mAdminName + ", admin=" + admin.flattenToShortString());
130     }
131 
resetState()132     private void resetState() {
133         mIntent = null;
134         mAdminName = null;
135     }
136 }
137