1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.inputmethodservice.navigationbar;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.navigationbar.ReverseLinearLayout.ReverseRelativeLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Space;
/**
* @hide
*/
public final class NavigationBarInflaterView extends FrameLayout {
private static final String TAG = "NavBarInflater";
public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
public static final String NAV_BAR_LEFT = "sysui_nav_bar_left";
public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right";
public static final String MENU_IME_ROTATE = "menu_ime";
public static final String BACK = "back";
public static final String HOME = "home";
public static final String RECENT = "recent";
public static final String NAVSPACE = "space";
public static final String CLIPBOARD = "clipboard";
public static final String HOME_HANDLE = "home_handle";
public static final String KEY = "key";
public static final String LEFT = "left";
public static final String RIGHT = "right";
public static final String CONTEXTUAL = "contextual";
public static final String IME_SWITCHER = "ime_switcher";
public static final String GRAVITY_SEPARATOR = ";";
public static final String BUTTON_SEPARATOR = ",";
public static final String SIZE_MOD_START = "[";
public static final String SIZE_MOD_END = "]";
public static final String KEY_CODE_START = "(";
public static final String KEY_IMAGE_DELIM = ":";
public static final String KEY_CODE_END = ")";
private static final String WEIGHT_SUFFIX = "W";
private static final String WEIGHT_CENTERED_SUFFIX = "WC";
private static final String ABSOLUTE_SUFFIX = "A";
private static final String ABSOLUTE_VERTICAL_CENTERED_SUFFIX = "C";
// Copied from "config_navBarLayoutHandle:
private static final String CONFIG_NAV_BAR_LAYOUT_HANDLE =
"back[70AC];home_handle;ime_switcher[70AC]";
protected LayoutInflater mLayoutInflater;
protected LayoutInflater mLandscapeInflater;
protected FrameLayout mHorizontal;
SparseArray<ButtonDispatcher> mButtonDispatchers;
private View mLastPortrait;
private View mLastLandscape;
private boolean mAlternativeOrder;
public NavigationBarInflaterView(Context context, AttributeSet attrs) {
super(context, attrs);
createInflaters();
}
void createInflaters() {
mLayoutInflater = LayoutInflater.from(mContext);
Configuration landscape = new Configuration();
landscape.setTo(mContext.getResources().getConfiguration());
landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
inflateChildren();
clearViews();
inflateLayout(getDefaultLayout());
}
private void inflateChildren() {
removeAllViews();
mHorizontal = (FrameLayout) mLayoutInflater.inflate(
com.android.internal.R.layout.input_method_navigation_layout,
this /* root */, false /* attachToRoot */);
addView(mHorizontal);
updateAlternativeOrder();
}
String getDefaultLayout() {
return CONFIG_NAV_BAR_LAYOUT_HANDLE;
}
void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) {
mButtonDispatchers = buttonDispatchers;
for (int i = 0; i < buttonDispatchers.size(); i++) {
initiallyFill(buttonDispatchers.valueAt(i));
}
}
void updateButtonDispatchersCurrentView() {
if (mButtonDispatchers != null) {
View view = mHorizontal;
for (int i = 0; i < mButtonDispatchers.size(); i++) {
final ButtonDispatcher dispatcher = mButtonDispatchers.valueAt(i);
dispatcher.setCurrentView(view);
}
}
}
void setAlternativeOrder(boolean alternativeOrder) {
if (alternativeOrder != mAlternativeOrder) {
mAlternativeOrder = alternativeOrder;
updateAlternativeOrder();
}
}
private void updateAlternativeOrder() {
updateAlternativeOrder(mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_ends_group));
updateAlternativeOrder(mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_center_group));
}
private void updateAlternativeOrder(View v) {
if (v instanceof ReverseLinearLayout) {
((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
}
}
private void initiallyFill(
ButtonDispatcher buttonDispatcher) {
addAll(buttonDispatcher, mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_ends_group));
addAll(buttonDispatcher, mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_center_group));
}
private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
for (int i = 0; i < parent.getChildCount(); i++) {
// Need to manually search for each id, just in case each group has more than one
// of a single id. It probably mostly a waste of time, but shouldn't take long
// and will only happen once.
if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
buttonDispatcher.addView(parent.getChildAt(i));
}
if (parent.getChildAt(i) instanceof ViewGroup) {
addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
}
}
}
protected void inflateLayout(String newLayout) {
if (newLayout == null) {
newLayout = getDefaultLayout();
}
String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
if (sets.length != 3) {
Log.d(TAG, "Invalid layout.");
newLayout = getDefaultLayout();
sets = newLayout.split(GRAVITY_SEPARATOR, 3);
}
String[] start = sets[0].split(BUTTON_SEPARATOR);
String[] center = sets[1].split(BUTTON_SEPARATOR);
String[] end = sets[2].split(BUTTON_SEPARATOR);
// Inflate these in start to end order or accessibility traversal will be messed up.
inflateButtons(start, mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_ends_group),
false /* landscape */, true /* start */);
inflateButtons(center, mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_center_group),
false /* landscape */, false /* start */);
addGravitySpacer(mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_ends_group));
inflateButtons(end, mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_ends_group),
false /* landscape */, false /* start */);
updateButtonDispatchersCurrentView();
}
private void addGravitySpacer(LinearLayout layout) {
layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
}
private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape,
boolean start) {
for (int i = 0; i < buttons.length; i++) {
inflateButton(buttons[i], parent, landscape, start);
}
}
private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
if (layoutParams instanceof LinearLayout.LayoutParams) {
return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
((LinearLayout.LayoutParams) layoutParams).weight);
}
return new LayoutParams(layoutParams.width, layoutParams.height);
}
@Nullable
protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
boolean start) {
LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
View v = createView(buttonSpec, parent, inflater);
if (v == null) return null;
v = applySize(v, buttonSpec, landscape, start);
parent.addView(v);
addToDispatchers(v);
View lastView = landscape ? mLastLandscape : mLastPortrait;
View accessibilityView = v;
if (v instanceof ReverseRelativeLayout) {
accessibilityView = ((ReverseRelativeLayout) v).getChildAt(0);
}
if (lastView != null) {
accessibilityView.setAccessibilityTraversalAfter(lastView.getId());
}
if (landscape) {
mLastLandscape = accessibilityView;
} else {
mLastPortrait = accessibilityView;
}
return v;
}
private View applySize(View v, String buttonSpec, boolean landscape, boolean start) {
String sizeStr = extractSize(buttonSpec);
if (sizeStr == null) return v;
if (sizeStr.contains(WEIGHT_SUFFIX) || sizeStr.contains(ABSOLUTE_SUFFIX)) {
// To support gravity, wrap in RelativeLayout and apply gravity to it.
// Children wanting to use gravity must be smaller than the frame.
ReverseRelativeLayout frame = new ReverseRelativeLayout(mContext);
LayoutParams childParams = new LayoutParams(v.getLayoutParams());
// Compute gravity to apply
int gravity = (landscape) ? (start ? Gravity.TOP : Gravity.BOTTOM)
: (start ? Gravity.START : Gravity.END);
if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) {
gravity = Gravity.CENTER;
} else if (sizeStr.endsWith(ABSOLUTE_VERTICAL_CENTERED_SUFFIX)) {
gravity = Gravity.CENTER_VERTICAL;
}
// Set default gravity, flipped if needed in reversed layouts (270 RTL and 90 LTR)
frame.setDefaultGravity(gravity);
frame.setGravity(gravity); // Apply gravity to root
frame.addView(v, childParams);
if (sizeStr.contains(WEIGHT_SUFFIX)) {
// Use weighting to set the width of the frame
float weight = Float.parseFloat(
sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX)));
frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight));
} else {
int width = (int) convertDpToPx(mContext,
Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(ABSOLUTE_SUFFIX))));
frame.setLayoutParams(new LinearLayout.LayoutParams(width, MATCH_PARENT));
}
// Ensure ripples can be drawn outside bounds
frame.setClipChildren(false);
frame.setClipToPadding(false);
return frame;
}
float size = Float.parseFloat(sizeStr);
ViewGroup.LayoutParams params = v.getLayoutParams();
params.width = (int) (params.width * size);
return v;
}
View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {
View v = null;
String button = extractButton(buttonSpec);
if (LEFT.equals(button)) {
button = extractButton(NAVSPACE);
} else if (RIGHT.equals(button)) {
button = extractButton(MENU_IME_ROTATE);
}
if (HOME.equals(button)) {
//v = inflater.inflate(R.layout.home, parent, false);
} else if (BACK.equals(button)) {
v = inflater.inflate(com.android.internal.R.layout.input_method_nav_back, parent,
false);
} else if (RECENT.equals(button)) {
//v = inflater.inflate(R.layout.recent_apps, parent, false);
} else if (MENU_IME_ROTATE.equals(button)) {
//v = inflater.inflate(R.layout.menu_ime, parent, false);
} else if (NAVSPACE.equals(button)) {
//v = inflater.inflate(R.layout.nav_key_space, parent, false);
} else if (CLIPBOARD.equals(button)) {
//v = inflater.inflate(R.layout.clipboard, parent, false);
} else if (CONTEXTUAL.equals(button)) {
//v = inflater.inflate(R.layout.contextual, parent, false);
} else if (HOME_HANDLE.equals(button)) {
v = inflater.inflate(com.android.internal.R.layout.input_method_nav_home_handle,
parent, false);
} else if (IME_SWITCHER.equals(button)) {
v = inflater.inflate(com.android.internal.R.layout.input_method_nav_ime_switcher,
parent, false);
} else if (button.startsWith(KEY)) {
/*
String uri = extractImage(button);
int code = extractKeycode(button);
v = inflater.inflate(R.layout.custom_key, parent, false);
((KeyButtonView) v).setCode(code);
if (uri != null) {
if (uri.contains(":")) {
((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri));
} else if (uri.contains("/")) {
int index = uri.indexOf('/');
String pkg = uri.substring(0, index);
int id = Integer.parseInt(uri.substring(index + 1));
((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id));
}
}
*/
}
return v;
}
/*
public static String extractImage(String buttonSpec) {
if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
return null;
}
final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
return subStr;
}
public static int extractKeycode(String buttonSpec) {
if (!buttonSpec.contains(KEY_CODE_START)) {
return 1;
}
final int start = buttonSpec.indexOf(KEY_CODE_START);
String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
return Integer.parseInt(subStr);
}
*/
private static String extractSize(String buttonSpec) {
if (!buttonSpec.contains(SIZE_MOD_START)) {
return null;
}
final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
}
private static String extractButton(String buttonSpec) {
if (!buttonSpec.contains(SIZE_MOD_START)) {
return buttonSpec;
}
return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
}
private void addToDispatchers(View v) {
if (mButtonDispatchers != null) {
final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
if (indexOfKey >= 0) {
mButtonDispatchers.valueAt(indexOfKey).addView(v);
}
if (v instanceof ViewGroup) {
final ViewGroup viewGroup = (ViewGroup) v;
final int numChildViews = viewGroup.getChildCount();
for (int i = 0; i < numChildViews; i++) {
addToDispatchers(viewGroup.getChildAt(i));
}
}
}
}
private void clearViews() {
if (mButtonDispatchers != null) {
for (int i = 0; i < mButtonDispatchers.size(); i++) {
mButtonDispatchers.valueAt(i).clear();
}
}
clearAllChildren(mHorizontal.findViewById(
com.android.internal.R.id.input_method_nav_buttons));
}
private void clearAllChildren(ViewGroup group) {
for (int i = 0; i < group.getChildCount(); i++) {
((ViewGroup) group.getChildAt(i)).removeAllViews();
}
}
private static float convertDpToPx(Context context, float dp) {
return dp * context.getResources().getDisplayMetrics().density;
}
}
|