1 /* 2 * Copyright (C) 2022 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.settings.users; 18 19 import static android.provider.Settings.Secure.TIMEOUT_TO_DOCK_USER; 20 21 import android.content.Context; 22 import android.os.UserHandle; 23 import android.os.UserManager; 24 import android.provider.Settings; 25 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.Utils; 29 import com.android.settings.core.BasePreferenceController; 30 31 import java.util.Arrays; 32 33 /** 34 * Controls the preference which launches a settings screen for user to configure whether to 35 * automatically switch to the designated Dock User when the device is docked. 36 */ 37 public class TimeoutToDockUserPreferenceController extends BasePreferenceController { 38 private final UserManager mUserManager; 39 40 private final String[] mEntries; 41 private final String[] mValues; 42 TimeoutToDockUserPreferenceController(Context context, String preferenceKey)43 public TimeoutToDockUserPreferenceController(Context context, 44 String preferenceKey) { 45 super(context, preferenceKey); 46 47 mUserManager = context.getSystemService(UserManager.class); 48 49 mEntries = mContext.getResources().getStringArray( 50 com.android.settings.R.array.switch_to_dock_user_when_docked_timeout_entries); 51 mValues = mContext.getResources().getStringArray( 52 com.android.settings.R.array.switch_to_dock_user_when_docked_timeout_values); 53 } 54 55 @Override displayPreference(PreferenceScreen screen)56 public void displayPreference(PreferenceScreen screen) { 57 super.displayPreference(screen); 58 59 updateState(screen.findPreference(getPreferenceKey())); 60 } 61 62 @Override getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 // Feature not available on device. 65 if (!mContext.getResources().getBoolean( 66 com.android.internal.R.bool.config_enableTimeoutToDockUserWhenDocked)) { 67 return UNSUPPORTED_ON_DEVICE; 68 } 69 70 // Multi-user feature disabled by user, or user switching blocked on the user. 71 if (Settings.Global.getInt(mContext.getContentResolver(), 72 Settings.Global.USER_SWITCHER_ENABLED, 0) != 1 73 || mUserManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH)) { 74 return CONDITIONALLY_UNAVAILABLE; 75 } 76 77 if (Utils.canCurrentUserDream(mContext)) { 78 return DISABLED_FOR_USER; 79 } 80 81 return AVAILABLE; 82 } 83 84 @Override getSummary()85 public CharSequence getSummary() { 86 final String key = Settings.Secure.getStringForUser(mContext.getContentResolver(), 87 TIMEOUT_TO_DOCK_USER, UserHandle.myUserId()); 88 final int index = Arrays.asList(mValues).indexOf(key != null ? key : 89 mValues[TimeoutToDockUserSettings.DEFAULT_TIMEOUT_SETTING_VALUE_INDEX]); 90 91 return mEntries[index]; 92 } 93 } 94