1 /* 2 * Copyright (C) 2020 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.systemui.navigationbar; 18 19 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; 20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON; 21 22 import android.annotation.Nullable; 23 import android.content.Context; 24 import android.content.res.Configuration; 25 import android.graphics.drawable.Icon; 26 import android.util.AttributeSet; 27 import android.util.Log; 28 import android.util.SparseArray; 29 import android.view.Gravity; 30 import android.view.LayoutInflater; 31 import android.view.View; 32 import android.view.ViewGroup; 33 import android.widget.FrameLayout; 34 import android.widget.LinearLayout; 35 import android.widget.Space; 36 37 import com.android.internal.annotations.VisibleForTesting; 38 import com.android.systemui.Dependency; 39 import com.android.systemui.R; 40 import com.android.systemui.navigationbar.buttons.ButtonDispatcher; 41 import com.android.systemui.navigationbar.buttons.KeyButtonView; 42 import com.android.systemui.navigationbar.buttons.ReverseLinearLayout; 43 import com.android.systemui.navigationbar.buttons.ReverseLinearLayout.ReverseRelativeLayout; 44 import com.android.systemui.recents.OverviewProxyService; 45 import com.android.systemui.shared.system.QuickStepContract; 46 47 import java.io.PrintWriter; 48 import java.util.Objects; 49 50 public class NavigationBarInflaterView extends FrameLayout 51 implements NavigationModeController.ModeChangedListener { 52 53 private static final String TAG = "NavBarInflater"; 54 55 public static final String NAV_BAR_VIEWS = "sysui_nav_bar"; 56 public static final String NAV_BAR_LEFT = "sysui_nav_bar_left"; 57 public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right"; 58 59 public static final String MENU_IME_ROTATE = "menu_ime"; 60 public static final String BACK = "back"; 61 public static final String HOME = "home"; 62 public static final String RECENT = "recent"; 63 public static final String NAVSPACE = "space"; 64 public static final String CLIPBOARD = "clipboard"; 65 public static final String HOME_HANDLE = "home_handle"; 66 public static final String KEY = "key"; 67 public static final String LEFT = "left"; 68 public static final String RIGHT = "right"; 69 public static final String CONTEXTUAL = "contextual"; 70 public static final String IME_SWITCHER = "ime_switcher"; 71 72 public static final String GRAVITY_SEPARATOR = ";"; 73 public static final String BUTTON_SEPARATOR = ","; 74 75 public static final String SIZE_MOD_START = "["; 76 public static final String SIZE_MOD_END = "]"; 77 78 public static final String KEY_CODE_START = "("; 79 public static final String KEY_IMAGE_DELIM = ":"; 80 public static final String KEY_CODE_END = ")"; 81 private static final String WEIGHT_SUFFIX = "W"; 82 private static final String WEIGHT_CENTERED_SUFFIX = "WC"; 83 private static final String ABSOLUTE_SUFFIX = "A"; 84 private static final String ABSOLUTE_VERTICAL_CENTERED_SUFFIX = "C"; 85 86 protected LayoutInflater mLayoutInflater; 87 protected LayoutInflater mLandscapeInflater; 88 89 protected FrameLayout mHorizontal; 90 protected FrameLayout mVertical; 91 92 @VisibleForTesting 93 SparseArray<ButtonDispatcher> mButtonDispatchers; 94 private String mCurrentLayout; 95 96 private View mLastPortrait; 97 private View mLastLandscape; 98 99 private boolean mIsVertical; 100 private boolean mAlternativeOrder; 101 102 private OverviewProxyService mOverviewProxyService; 103 private int mNavBarMode = NAV_BAR_MODE_3BUTTON; 104 NavigationBarInflaterView(Context context, AttributeSet attrs)105 public NavigationBarInflaterView(Context context, AttributeSet attrs) { 106 super(context, attrs); 107 createInflaters(); 108 mOverviewProxyService = Dependency.get(OverviewProxyService.class); 109 mNavBarMode = Dependency.get(NavigationModeController.class).addListener(this); 110 } 111 112 @VisibleForTesting createInflaters()113 void createInflaters() { 114 mLayoutInflater = LayoutInflater.from(mContext); 115 Configuration landscape = new Configuration(); 116 landscape.setTo(mContext.getResources().getConfiguration()); 117 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE; 118 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape)); 119 } 120 121 @Override onFinishInflate()122 protected void onFinishInflate() { 123 super.onFinishInflate(); 124 inflateChildren(); 125 clearViews(); 126 inflateLayout(getDefaultLayout()); 127 } 128 inflateChildren()129 private void inflateChildren() { 130 removeAllViews(); 131 mHorizontal = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, 132 this /* root */, false /* attachToRoot */); 133 addView(mHorizontal); 134 mVertical = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_vertical, 135 this /* root */, false /* attachToRoot */); 136 addView(mVertical); 137 updateAlternativeOrder(); 138 } 139 getDefaultLayout()140 protected String getDefaultLayout() { 141 final int defaultResource = QuickStepContract.isGesturalMode(mNavBarMode) 142 ? R.string.config_navBarLayoutHandle 143 : mOverviewProxyService.shouldShowSwipeUpUI() 144 ? R.string.config_navBarLayoutQuickstep 145 : R.string.config_navBarLayout; 146 return getContext().getString(defaultResource); 147 } 148 149 @Override onNavigationModeChanged(int mode)150 public void onNavigationModeChanged(int mode) { 151 mNavBarMode = mode; 152 } 153 154 @Override onDetachedFromWindow()155 protected void onDetachedFromWindow() { 156 Dependency.get(NavigationModeController.class).removeListener(this); 157 super.onDetachedFromWindow(); 158 } 159 onLikelyDefaultLayoutChange()160 public void onLikelyDefaultLayoutChange() { 161 // Reevaluate new layout 162 final String newValue = getDefaultLayout(); 163 if (!Objects.equals(mCurrentLayout, newValue)) { 164 clearViews(); 165 inflateLayout(newValue); 166 } 167 } 168 setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers)169 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) { 170 mButtonDispatchers = buttonDispatchers; 171 clearDispatcherViews(); 172 for (int i = 0; i < buttonDispatchers.size(); i++) { 173 initiallyFill(buttonDispatchers.valueAt(i)); 174 } 175 } 176 updateButtonDispatchersCurrentView()177 void updateButtonDispatchersCurrentView() { 178 if (mButtonDispatchers != null) { 179 View view = mIsVertical ? mVertical : mHorizontal; 180 for (int i = 0; i < mButtonDispatchers.size(); i++) { 181 final ButtonDispatcher dispatcher = mButtonDispatchers.valueAt(i); 182 dispatcher.setCurrentView(view); 183 } 184 } 185 } 186 setVertical(boolean vertical)187 void setVertical(boolean vertical) { 188 if (vertical != mIsVertical) { 189 mIsVertical = vertical; 190 } 191 } 192 setAlternativeOrder(boolean alternativeOrder)193 void setAlternativeOrder(boolean alternativeOrder) { 194 if (alternativeOrder != mAlternativeOrder) { 195 mAlternativeOrder = alternativeOrder; 196 updateAlternativeOrder(); 197 } 198 } 199 updateAlternativeOrder()200 private void updateAlternativeOrder() { 201 updateAlternativeOrder(mHorizontal.findViewById(R.id.ends_group)); 202 updateAlternativeOrder(mHorizontal.findViewById(R.id.center_group)); 203 updateAlternativeOrder(mVertical.findViewById(R.id.ends_group)); 204 updateAlternativeOrder(mVertical.findViewById(R.id.center_group)); 205 } 206 updateAlternativeOrder(View v)207 private void updateAlternativeOrder(View v) { 208 if (v instanceof ReverseLinearLayout) { 209 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder); 210 } 211 } 212 initiallyFill(ButtonDispatcher buttonDispatcher)213 private void initiallyFill(ButtonDispatcher buttonDispatcher) { 214 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.ends_group)); 215 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.center_group)); 216 addAll(buttonDispatcher, mVertical.findViewById(R.id.ends_group)); 217 addAll(buttonDispatcher, mVertical.findViewById(R.id.center_group)); 218 } 219 addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent)220 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) { 221 for (int i = 0; i < parent.getChildCount(); i++) { 222 // Need to manually search for each id, just in case each group has more than one 223 // of a single id. It probably mostly a waste of time, but shouldn't take long 224 // and will only happen once. 225 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) { 226 buttonDispatcher.addView(parent.getChildAt(i)); 227 } 228 if (parent.getChildAt(i) instanceof ViewGroup) { 229 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i)); 230 } 231 } 232 } 233 inflateLayout(String newLayout)234 protected void inflateLayout(String newLayout) { 235 mCurrentLayout = newLayout; 236 if (newLayout == null) { 237 newLayout = getDefaultLayout(); 238 } 239 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3); 240 if (sets.length != 3) { 241 Log.d(TAG, "Invalid layout."); 242 newLayout = getDefaultLayout(); 243 sets = newLayout.split(GRAVITY_SEPARATOR, 3); 244 } 245 String[] start = sets[0].split(BUTTON_SEPARATOR); 246 String[] center = sets[1].split(BUTTON_SEPARATOR); 247 String[] end = sets[2].split(BUTTON_SEPARATOR); 248 // Inflate these in start to end order or accessibility traversal will be messed up. 249 inflateButtons(start, mHorizontal.findViewById(R.id.ends_group), 250 false /* landscape */, true /* start */); 251 inflateButtons(start, mVertical.findViewById(R.id.ends_group), 252 true /* landscape */, true /* start */); 253 254 inflateButtons(center, mHorizontal.findViewById(R.id.center_group), 255 false /* landscape */, false /* start */); 256 inflateButtons(center, mVertical.findViewById(R.id.center_group), 257 true /* landscape */, false /* start */); 258 259 addGravitySpacer(mHorizontal.findViewById(R.id.ends_group)); 260 addGravitySpacer(mVertical.findViewById(R.id.ends_group)); 261 262 inflateButtons(end, mHorizontal.findViewById(R.id.ends_group), 263 false /* landscape */, false /* start */); 264 inflateButtons(end, mVertical.findViewById(R.id.ends_group), 265 true /* landscape */, false /* start */); 266 267 updateButtonDispatchersCurrentView(); 268 } 269 addGravitySpacer(LinearLayout layout)270 private void addGravitySpacer(LinearLayout layout) { 271 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1)); 272 } 273 inflateButtons(String[] buttons, ViewGroup parent, boolean landscape, boolean start)274 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape, 275 boolean start) { 276 for (int i = 0; i < buttons.length; i++) { 277 inflateButton(buttons[i], parent, landscape, start); 278 } 279 } 280 copy(ViewGroup.LayoutParams layoutParams)281 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) { 282 if (layoutParams instanceof LinearLayout.LayoutParams) { 283 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height, 284 ((LinearLayout.LayoutParams) layoutParams).weight); 285 } 286 return new LayoutParams(layoutParams.width, layoutParams.height); 287 } 288 289 @Nullable inflateButton(String buttonSpec, ViewGroup parent, boolean landscape, boolean start)290 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape, 291 boolean start) { 292 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater; 293 View v = createView(buttonSpec, parent, inflater); 294 if (v == null) return null; 295 296 v = applySize(v, buttonSpec, landscape, start); 297 parent.addView(v); 298 addToDispatchers(v); 299 View lastView = landscape ? mLastLandscape : mLastPortrait; 300 View accessibilityView = v; 301 if (v instanceof ReverseRelativeLayout) { 302 accessibilityView = ((ReverseRelativeLayout) v).getChildAt(0); 303 } 304 if (lastView != null) { 305 accessibilityView.setAccessibilityTraversalAfter(lastView.getId()); 306 } 307 if (landscape) { 308 mLastLandscape = accessibilityView; 309 } else { 310 mLastPortrait = accessibilityView; 311 } 312 return v; 313 } 314 applySize(View v, String buttonSpec, boolean landscape, boolean start)315 private View applySize(View v, String buttonSpec, boolean landscape, boolean start) { 316 String sizeStr = extractSize(buttonSpec); 317 if (sizeStr == null) return v; 318 319 if (sizeStr.contains(WEIGHT_SUFFIX) || sizeStr.contains(ABSOLUTE_SUFFIX)) { 320 // To support gravity, wrap in RelativeLayout and apply gravity to it. 321 // Children wanting to use gravity must be smaller then the frame. 322 ReverseRelativeLayout frame = new ReverseRelativeLayout(mContext); 323 LayoutParams childParams = new LayoutParams(v.getLayoutParams()); 324 325 // Compute gravity to apply 326 int gravity = (landscape) ? (start ? Gravity.TOP : Gravity.BOTTOM) 327 : (start ? Gravity.START : Gravity.END); 328 if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) { 329 gravity = Gravity.CENTER; 330 } else if (sizeStr.endsWith(ABSOLUTE_VERTICAL_CENTERED_SUFFIX)) { 331 gravity = Gravity.CENTER_VERTICAL; 332 } 333 334 // Set default gravity, flipped if needed in reversed layouts (270 RTL and 90 LTR) 335 frame.setDefaultGravity(gravity); 336 frame.setGravity(gravity); // Apply gravity to root 337 338 frame.addView(v, childParams); 339 340 if (sizeStr.contains(WEIGHT_SUFFIX)) { 341 // Use weighting to set the width of the frame 342 float weight = Float.parseFloat( 343 sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX))); 344 frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight)); 345 } else { 346 int width = (int) convertDpToPx(mContext, 347 Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(ABSOLUTE_SUFFIX)))); 348 frame.setLayoutParams(new LinearLayout.LayoutParams(width, MATCH_PARENT)); 349 } 350 351 // Ensure ripples can be drawn outside bounds 352 frame.setClipChildren(false); 353 frame.setClipToPadding(false); 354 355 return frame; 356 } 357 358 float size = Float.parseFloat(sizeStr); 359 ViewGroup.LayoutParams params = v.getLayoutParams(); 360 params.width = (int) (params.width * size); 361 return v; 362 } 363 createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater)364 View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) { 365 View v = null; 366 String button = extractButton(buttonSpec); 367 if (LEFT.equals(button)) { 368 button = extractButton(NAVSPACE); 369 } else if (RIGHT.equals(button)) { 370 button = extractButton(MENU_IME_ROTATE); 371 } 372 if (HOME.equals(button)) { 373 v = inflater.inflate(R.layout.home, parent, false); 374 } else if (BACK.equals(button)) { 375 v = inflater.inflate(R.layout.back, parent, false); 376 } else if (RECENT.equals(button)) { 377 v = inflater.inflate(R.layout.recent_apps, parent, false); 378 } else if (MENU_IME_ROTATE.equals(button)) { 379 v = inflater.inflate(R.layout.menu_ime, parent, false); 380 } else if (NAVSPACE.equals(button)) { 381 v = inflater.inflate(R.layout.nav_key_space, parent, false); 382 } else if (CLIPBOARD.equals(button)) { 383 v = inflater.inflate(R.layout.clipboard, parent, false); 384 } else if (CONTEXTUAL.equals(button)) { 385 v = inflater.inflate(R.layout.contextual, parent, false); 386 } else if (HOME_HANDLE.equals(button)) { 387 v = inflater.inflate(R.layout.home_handle, parent, false); 388 } else if (IME_SWITCHER.equals(button)) { 389 v = inflater.inflate(R.layout.ime_switcher, parent, false); 390 } else if (button.startsWith(KEY)) { 391 String uri = extractImage(button); 392 int code = extractKeycode(button); 393 v = inflater.inflate(R.layout.custom_key, parent, false); 394 ((KeyButtonView) v).setCode(code); 395 if (uri != null) { 396 if (uri.contains(":")) { 397 ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri)); 398 } else if (uri.contains("/")) { 399 int index = uri.indexOf('/'); 400 String pkg = uri.substring(0, index); 401 int id = Integer.parseInt(uri.substring(index + 1)); 402 ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id)); 403 } 404 } 405 } 406 return v; 407 } 408 extractImage(String buttonSpec)409 public static String extractImage(String buttonSpec) { 410 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) { 411 return null; 412 } 413 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM); 414 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END)); 415 return subStr; 416 } 417 extractKeycode(String buttonSpec)418 public static int extractKeycode(String buttonSpec) { 419 if (!buttonSpec.contains(KEY_CODE_START)) { 420 return 1; 421 } 422 final int start = buttonSpec.indexOf(KEY_CODE_START); 423 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM)); 424 return Integer.parseInt(subStr); 425 } 426 extractSize(String buttonSpec)427 public static String extractSize(String buttonSpec) { 428 if (!buttonSpec.contains(SIZE_MOD_START)) { 429 return null; 430 } 431 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START); 432 return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END)); 433 } 434 extractButton(String buttonSpec)435 public static String extractButton(String buttonSpec) { 436 if (!buttonSpec.contains(SIZE_MOD_START)) { 437 return buttonSpec; 438 } 439 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START)); 440 } 441 addToDispatchers(View v)442 private void addToDispatchers(View v) { 443 if (mButtonDispatchers != null) { 444 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId()); 445 if (indexOfKey >= 0) { 446 mButtonDispatchers.valueAt(indexOfKey).addView(v); 447 } 448 if (v instanceof ViewGroup) { 449 final ViewGroup viewGroup = (ViewGroup)v; 450 final int N = viewGroup.getChildCount(); 451 for (int i = 0; i < N; i++) { 452 addToDispatchers(viewGroup.getChildAt(i)); 453 } 454 } 455 } 456 } 457 clearDispatcherViews()458 private void clearDispatcherViews() { 459 if (mButtonDispatchers != null) { 460 for (int i = 0; i < mButtonDispatchers.size(); i++) { 461 mButtonDispatchers.valueAt(i).clear(); 462 } 463 } 464 } 465 clearViews()466 private void clearViews() { 467 clearDispatcherViews(); 468 clearAllChildren(mHorizontal.findViewById(R.id.nav_buttons)); 469 clearAllChildren(mVertical.findViewById(R.id.nav_buttons)); 470 } 471 clearAllChildren(ViewGroup group)472 private void clearAllChildren(ViewGroup group) { 473 for (int i = 0; i < group.getChildCount(); i++) { 474 ((ViewGroup) group.getChildAt(i)).removeAllViews(); 475 } 476 } 477 convertDpToPx(Context context, float dp)478 private static float convertDpToPx(Context context, float dp) { 479 return dp * context.getResources().getDisplayMetrics().density; 480 } 481 dump(PrintWriter pw)482 public void dump(PrintWriter pw) { 483 pw.println("NavigationBarInflaterView"); 484 pw.println(" mCurrentLayout: " + mCurrentLayout); 485 } 486 } 487