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