• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.car.settings.profiles;
18 
19 import android.annotation.Nullable;
20 import android.app.admin.DevicePolicyManager;
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 import android.content.pm.UserInfo;
24 import android.os.UserHandle;
25 
26 import com.android.car.settings.common.FragmentController;
27 import com.android.car.settings.common.Logger;
28 import com.android.car.ui.preference.CarUiPreference;
29 
30 /**
31  * Controller that ends a managed user session.
32  */
33 public class ProfileDetailsEndSessionPreferenceController
34         extends ProfileDetailsBasePreferenceController<CarUiPreference> {
35 
36     private static final Logger LOG = new Logger(
37             ProfileDetailsEndSessionPreferenceController.class);
38 
39     @Nullable
40     private final DevicePolicyManager mDpm;
41 
42     private @Nullable UserHandle mLogoutUser;
43 
ProfileDetailsEndSessionPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public ProfileDetailsEndSessionPreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47 
48         mDpm = context.getSystemService(DevicePolicyManager.class);
49     }
50 
51     @Override
getPreferenceType()52     protected Class<CarUiPreference> getPreferenceType() {
53         return CarUiPreference.class;
54     }
55 
56     @Override
setUserInfo(UserInfo userInfo)57     public void setUserInfo(UserInfo userInfo) {
58         super.setUserInfo(userInfo);
59     }
60 
61     @Override
handlePreferenceClicked(CarUiPreference preference)62     public boolean handlePreferenceClicked(CarUiPreference preference) {
63         LOG.i("ending session (" + getUserInfo().toFullString() + ") and switching back to user "
64                 + mLogoutUser);
65         boolean switched = ProfileHelper.getInstance(getContext()).logoutProfile();
66         if (!switched) {
67             LOG.e("Switch failed");
68         }
69         return true;
70     }
71 
72     @Override
getDefaultAvailabilityStatus()73     protected int getDefaultAvailabilityStatus() {
74         if (mDpm == null) {
75             LOG.d("getAvailabilityStatus(): no dpm");
76             return UNSUPPORTED_ON_DEVICE;
77         }
78         boolean isLogoutEnabled = mDpm.isLogoutEnabled();
79         mLogoutUser = mDpm.getLogoutUser();
80         LOG.d("getAvailabilityStatus(): isLogoutEnabled()=" + isLogoutEnabled + ", mLogoutUser="
81                 + mLogoutUser);
82         return isLogoutEnabled && mLogoutUser != null
83                 ? AVAILABLE
84                 : CONDITIONALLY_UNAVAILABLE;
85     }
86 }
87