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