• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.FrameLayout;
32 import android.widget.ImageView;
33 import android.widget.LinearLayout;
34 import android.widget.TextView;
35 
36 import androidx.fragment.app.Fragment;
37 
38 import com.android.hardware.input.Flags;
39 import com.android.settings.R;
40 import com.android.settings.activityembedding.ActivityEmbeddingUtils;
41 
42 //TODO: b/316243168 - [Physical Keyboard Setting] Refactor NewKeyboardLayoutPickerFragment
43 public class NewKeyboardLayoutPickerFragment extends Fragment {
44     private static final int DEFAULT_KEYBOARD_PREVIEW_WIDTH = 1630;
45     private static final int DEFAULT_KEYBOARD_PREVIEW_HEIGHT = 540;
46 
47     private ImageView mKeyboardLayoutPreview;
48     private TextView mKeyboardLayoutPreviewText;
49     private InputManager mInputManager;
50     private final NewKeyboardLayoutPickerController.KeyboardLayoutSelectedCallback
51             mKeyboardLayoutSelectedCallback =
52             new NewKeyboardLayoutPickerController.KeyboardLayoutSelectedCallback() {
53                 @Override
54                 public void onSelected(KeyboardLayout keyboardLayout) {
55                     if (mInputManager != null
56                             && mKeyboardLayoutPreview != null
57                             && mKeyboardLayoutPreviewText != null && keyboardLayout != null) {
58                         Drawable previewDrawable = mInputManager.getKeyboardLayoutPreview(
59                                 keyboardLayout,
60                                 DEFAULT_KEYBOARD_PREVIEW_WIDTH, DEFAULT_KEYBOARD_PREVIEW_HEIGHT);
61                         mKeyboardLayoutPreview.setVisibility(
62                                 previewDrawable == null ? GONE : VISIBLE);
63                         mKeyboardLayoutPreviewText.setVisibility(
64                                 previewDrawable == null ? GONE : VISIBLE);
65                         if (previewDrawable != null) {
66                             mKeyboardLayoutPreviewText.setText(keyboardLayout.getLabel());
67                             mKeyboardLayoutPreview.setImageDrawable(previewDrawable);
68                         }
69                     }
70                 }
71             };
72 
73     private final NewKeyboardLayoutPickerContent.ControllerUpdateCallback
74             mControllerUpdateCallback =
75                     newKeyboardLayoutPickerController -> {
76                         if (newKeyboardLayoutPickerController != null) {
77                             newKeyboardLayoutPickerController.registerKeyboardSelectedCallback(
78                                     mKeyboardLayoutSelectedCallback);
79                         }
80                     };
81 
82     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)83     public View onCreateView(LayoutInflater inflater, ViewGroup container,
84             Bundle savedInstanceState) {
85         mInputManager = requireContext().getSystemService(InputManager.class);
86         ViewGroup fragmentView = (ViewGroup) inflater.inflate(
87                 getPickerLayout(getResources().getConfiguration()), container, false);
88         mKeyboardLayoutPreview = fragmentView.findViewById(R.id.keyboard_layout_preview);
89         mKeyboardLayoutPreviewText = fragmentView.findViewById(R.id.keyboard_layout_preview_name);
90         if (!Flags.keyboardLayoutPreviewFlag()) {
91             updateViewMarginForPreviewFlagOff(fragmentView);
92         }
93         getActivity().getSupportFragmentManager()
94                 .beginTransaction()
95                 .replace(R.id.keyboard_layout_title, new NewKeyboardLayoutPickerTitle())
96                 .commit();
97 
98         NewKeyboardLayoutPickerContent fragment = new NewKeyboardLayoutPickerContent();
99         fragment.setControllerUpdateCallback(mControllerUpdateCallback);
100         fragment.setArguments(getArguments());
101         getActivity().getSupportFragmentManager()
102                 .beginTransaction()
103                 .replace(R.id.keyboard_layouts, fragment)
104                 .commit();
105         return fragmentView;
106     }
107 
getPickerLayout(Configuration configuration)108     private int getPickerLayout(Configuration configuration) {
109         return !ActivityEmbeddingUtils.isAlreadyEmbedded(this.getActivity())
110                 && configuration.orientation == ORIENTATION_LANDSCAPE
111                 ? R.layout.keyboard_layout_picker_one_pane_land : R.layout.keyboard_layout_picker;
112     }
113 
updateViewMarginForPreviewFlagOff(ViewGroup fragmentView)114     private void updateViewMarginForPreviewFlagOff(ViewGroup fragmentView) {
115         LinearLayout previewContainer = fragmentView.findViewById(
116                 R.id.keyboard_layout_picker_container);
117         FrameLayout.LayoutParams previewContainerLayoutParams =
118                 (FrameLayout.LayoutParams) previewContainer.getLayoutParams();
119         previewContainerLayoutParams.setMargins(0, 0, 0, 0);
120         previewContainer.setLayoutParams(previewContainerLayoutParams);
121     }
122 }
123