1 /*
2  * Copyright (C) 2018 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.car.settings.security;
18 
19 
20 import android.annotation.Nullable;
21 import android.car.Car;
22 import android.car.CarNotConnectedException;
23 import android.car.drivingstate.CarUxRestrictions;
24 import android.car.trust.CarTrustAgentEnrollmentManager;
25 import android.car.userlib.CarUserManagerHelper;
26 import android.content.Context;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.car.settings.R;
31 import com.android.car.settings.common.FragmentController;
32 import com.android.car.settings.common.Logger;
33 import com.android.car.settings.common.PreferenceController;
34 
35 /**
36  * Business logic for trusted device preference.
37  */
38 public class TrustedDeviceEntryPreferenceController extends PreferenceController<Preference> {
39 
40     private static final Logger LOG = new Logger(TrustedDeviceEntryPreferenceController.class);
41     private final Car mCar;
42     private final CarUserManagerHelper mCarUserManagerHelper;
43     @Nullable
44     private CarTrustAgentEnrollmentManager mCarTrustAgentEnrollmentManager;
45 
TrustedDeviceEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46     public TrustedDeviceEntryPreferenceController(Context context, String preferenceKey,
47             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
48         super(context, preferenceKey, fragmentController, uxRestrictions);
49         mCar = Car.createCar(context);
50         mCarUserManagerHelper = new CarUserManagerHelper(context);
51         try {
52             mCarTrustAgentEnrollmentManager = (CarTrustAgentEnrollmentManager) mCar.getCarManager(
53                     Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE);
54         } catch (CarNotConnectedException e) {
55             LOG.e(e.getMessage(), e);
56         }
57     }
58 
59     @Override
getPreferenceType()60     protected Class<Preference> getPreferenceType() {
61         return Preference.class;
62     }
63 
64     @Override
updateState(Preference preference)65     protected void updateState(Preference preference) {
66         int listSize = 0;
67         try {
68             listSize = mCarTrustAgentEnrollmentManager.getEnrolledDeviceInfoForUser(
69                     mCarUserManagerHelper.getCurrentProcessUserId()).size();
70         } catch (CarNotConnectedException e) {
71             LOG.e(e.getMessage(), e);
72         }
73         preference.setSummary(
74                 getContext().getResources().getQuantityString(R.plurals.trusted_device_subtitle,
75                         listSize, listSize));
76     }
77 
78     @Override
handlePreferenceClicked(Preference preference)79     protected boolean handlePreferenceClicked(Preference preference) {
80         getFragmentController().launchFragment(new ChooseTrustedDeviceFragment());
81         return true;
82     }
83 }
84