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 static android.content.res.Configuration.ORIENTATION_LANDSCAPE; 20 import static android.view.View.GONE; 21 import static android.view.View.VISIBLE; 22 23 import android.content.res.Configuration; 24 import android.graphics.drawable.Drawable; 25 import android.hardware.input.InputManager; 26 import android.hardware.input.KeyboardLayout; 27 import android.os.Bundle; 28 import android.view.LayoutInflater; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.widget.ImageView; 32 import android.widget.TextView; 33 34 import androidx.annotation.Nullable; 35 import androidx.fragment.app.Fragment; 36 37 import com.android.settings.R; 38 import com.android.settings.activityembedding.ActivityEmbeddingUtils; 39 40 //TODO: b/316243168 - [Physical Keyboard Setting] Refactor NewKeyboardLayoutPickerFragment 41 public class NewKeyboardLayoutPickerFragment extends Fragment { 42 private static final int DEFAULT_KEYBOARD_PREVIEW_WIDTH = 1630; 43 private static final int DEFAULT_KEYBOARD_PREVIEW_HEIGHT = 540; 44 45 private ImageView mKeyboardLayoutPreview; 46 private TextView mKeyboardLayoutPreviewText; 47 private InputManager mInputManager; 48 private final NewKeyboardLayoutPickerController.KeyboardLayoutSelectedCallback 49 mKeyboardLayoutSelectedCallback = 50 new NewKeyboardLayoutPickerController.KeyboardLayoutSelectedCallback() { 51 @Override 52 public void onSelected(@Nullable KeyboardLayout keyboardLayout) { 53 if (mInputManager != null 54 && mKeyboardLayoutPreview != null 55 && mKeyboardLayoutPreviewText != null) { 56 Drawable previewDrawable = mInputManager.getKeyboardLayoutPreview( 57 keyboardLayout, 58 DEFAULT_KEYBOARD_PREVIEW_WIDTH, DEFAULT_KEYBOARD_PREVIEW_HEIGHT); 59 mKeyboardLayoutPreview.setVisibility( 60 previewDrawable == null ? GONE : VISIBLE); 61 mKeyboardLayoutPreviewText.setVisibility( 62 previewDrawable == null ? GONE : VISIBLE); 63 if (previewDrawable != null) { 64 mKeyboardLayoutPreview.setImageDrawable(previewDrawable); 65 } 66 mKeyboardLayoutPreviewText.setText( 67 keyboardLayout != null ? keyboardLayout.getLabel() 68 : requireContext().getString( 69 R.string.keyboard_default_layout)); 70 } 71 } 72 }; 73 74 private final NewKeyboardLayoutPickerContent.ControllerUpdateCallback 75 mControllerUpdateCallback = 76 newKeyboardLayoutPickerController -> { 77 if (newKeyboardLayoutPickerController != null) { 78 newKeyboardLayoutPickerController.registerKeyboardSelectedCallback( 79 mKeyboardLayoutSelectedCallback); 80 } 81 }; 82 83 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)84 public View onCreateView(LayoutInflater inflater, ViewGroup container, 85 Bundle savedInstanceState) { 86 mInputManager = requireContext().getSystemService(InputManager.class); 87 ViewGroup fragmentView = (ViewGroup) inflater.inflate( 88 getPickerLayout(getResources().getConfiguration()), container, false); 89 mKeyboardLayoutPreview = fragmentView.findViewById(R.id.keyboard_layout_preview); 90 mKeyboardLayoutPreviewText = fragmentView.findViewById(R.id.keyboard_layout_preview_name); 91 getActivity().getSupportFragmentManager() 92 .beginTransaction() 93 .replace(R.id.keyboard_layout_title, new NewKeyboardLayoutPickerTitle()) 94 .commit(); 95 96 NewKeyboardLayoutPickerContent fragment = new NewKeyboardLayoutPickerContent(); 97 fragment.setControllerUpdateCallback(mControllerUpdateCallback); 98 fragment.setArguments(getArguments()); 99 getActivity().getSupportFragmentManager() 100 .beginTransaction() 101 .replace(R.id.keyboard_layouts, fragment) 102 .commit(); 103 return fragmentView; 104 } 105 getPickerLayout(Configuration configuration)106 private int getPickerLayout(Configuration configuration) { 107 return !ActivityEmbeddingUtils.isAlreadyEmbedded(this.getActivity()) 108 && configuration.orientation == ORIENTATION_LANDSCAPE 109 ? R.layout.keyboard_layout_picker_one_pane_land : R.layout.keyboard_layout_picker; 110 } 111 } 112