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.inputmethod; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.provider.Settings; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settings.inputmethod.PhysicalKeyboardFragment.HardKeyboardDeviceInfo; 30 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 31 32 import java.util.List; 33 34 public class KeyboardSettingsPreferenceController extends BasePreferenceController { 35 36 private CachedBluetoothDevice mCachedDevice; 37 KeyboardSettingsPreferenceController(Context context, String key)38 public KeyboardSettingsPreferenceController(Context context, String key) { 39 super(context, key); 40 } 41 init(@onNull CachedBluetoothDevice cachedDevice)42 public void init(@NonNull CachedBluetoothDevice cachedDevice) { 43 mCachedDevice = cachedDevice; 44 } 45 46 @Override handlePreferenceTreeClick(Preference preference)47 public boolean handlePreferenceTreeClick(Preference preference) { 48 if (!getPreferenceKey().equals(preference.getKey())) { 49 return false; 50 } 51 List<HardKeyboardDeviceInfo> newHardKeyboards = getHardKeyboardList(); 52 for (HardKeyboardDeviceInfo hardKeyboardDeviceInfo : newHardKeyboards) { 53 if (mCachedDevice.getAddress().equals(hardKeyboardDeviceInfo.mBluetoothAddress)) { 54 Intent intent = new Intent(Settings.ACTION_HARD_KEYBOARD_SETTINGS); 55 intent.setPackage(mContext.getPackageName()); 56 intent.putExtra( 57 Settings.EXTRA_ENTRYPOINT, SettingsEnums.CONNECTED_DEVICES_SETTINGS); 58 intent.putExtra( 59 Settings.EXTRA_INPUT_DEVICE_IDENTIFIER, 60 hardKeyboardDeviceInfo.mDeviceIdentifier); 61 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 62 mContext.startActivity(intent); 63 break; 64 } 65 } 66 return true; 67 } 68 69 @Override getAvailabilityStatus()70 public int getAvailabilityStatus() { 71 List<HardKeyboardDeviceInfo> newHardKeyboards = getHardKeyboardList(); 72 if (!newHardKeyboards.isEmpty()) { 73 for (HardKeyboardDeviceInfo hardKeyboardDeviceInfo : newHardKeyboards) { 74 if (mCachedDevice.getAddress() != null 75 && hardKeyboardDeviceInfo.mBluetoothAddress != null 76 && mCachedDevice.getAddress().equals( 77 hardKeyboardDeviceInfo.mBluetoothAddress)) { 78 return AVAILABLE; 79 } 80 } 81 } 82 return CONDITIONALLY_UNAVAILABLE; 83 } 84 85 @VisibleForTesting getHardKeyboardList()86 List<HardKeyboardDeviceInfo> getHardKeyboardList() { 87 return PhysicalKeyboardFragment.getHardKeyboards(mContext); 88 } 89 } 90