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 for (int i = 0; i < buttonDispatchers.size(); i++) { 172 initiallyFill(buttonDispatchers.valueAt(i)); 173 } 174 } 175 updateButtonDispatchersCurrentView()176 void updateButtonDispatchersCurrentView() { 177 if (mButtonDispatchers != null) { 178 View view = mIsVertical ? mVertical : mHorizontal; 179 for (int i = 0; i < mButtonDispatchers.size(); i++) { 180 final ButtonDispatcher dispatcher = mButtonDispatchers.valueAt(i); 181 dispatcher.setCurrentView(view); 182 } 183 } 184 } 185 setVertical(boolean vertical)186 void setVertical(boolean vertical) { 187 if (vertical != mIsVertical) { 188 mIsVertical = vertical; 189 } 190 } 191 setAlternativeOrder(boolean alternativeOrder)192 void setAlternativeOrder(boolean alternativeOrder) { 193 if (alternativeOrder != mAlternativeOrder) { 194 mAlternativeOrder = alternativeOrder; 195 updateAlternativeOrder(); 196 } 197 } 198 updateAlternativeOrder()199 private void updateAlternativeOrder() { 200 updateAlternativeOrder(mHorizontal.findViewById(R.id.ends_group)); 201 updateAlternativeOrder(mHorizontal.findViewById(R.id.center_group)); 202 updateAlternativeOrder(mVertical.findViewById(R.id.ends_group)); 203 updateAlternativeOrder(mVertical.findViewById(R.id.center_group)); 204 } 205 updateAlternativeOrder(View v)206 private void updateAlternativeOrder(View v) { 207 if (v instanceof ReverseLinearLayout) { 208 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder); 209 } 210 } 211 initiallyFill(ButtonDispatcher buttonDispatcher)212 private void initiallyFill(ButtonDispatcher buttonDispatcher) { 213 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.ends_group)); 214 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.center_group)); 215 addAll(buttonDispatcher, mVertical.findViewById(R.id.ends_group)); 216 addAll(buttonDispatcher, mVertical.findViewById(R.id.center_group)); 217 } 218 addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent)219 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) { 220 for (int i = 0; i < parent.getChildCount(); i++) { 221 // Need to manually search for each id, just in case each group has more than one 222 // of a single id. It probably mostly a waste of time, but shouldn't take long 223 // and will only happen once. 224 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) { 225 buttonDispatcher.addView(parent.getChildAt(i)); 226 } 227 if (parent.getChildAt(i) instanceof ViewGroup) { 228 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i)); 229 } 230 } 231 } 232 inflateLayout(String newLayout)233 protected void inflateLayout(String newLayout) { 234 mCurrentLayout = newLayout; 235 if (newLayout == null) { 236 newLayout = getDefaultLayout(); 237 } 238 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3); 239 if (sets.length != 3) { 240 Log.d(TAG, "Invalid layout."); 241 newLayout = getDefaultLayout(); 242 sets = newLayout.split(GRAVITY_SEPARATOR, 3); 243 } 244 String[] start = sets[0].split(BUTTON_SEPARATOR); 245 String[] center = sets[1].split(BUTTON_SEPARATOR); 246 String[] end = sets[2].split(BUTTON_SEPARATOR); 247 // Inflate these in start to end order or accessibility traversal will be messed up. 248 inflateButtons(start, mHorizontal.findViewById(R.id.ends_group), 249 false /* landscape */, true /* start */); 250 inflateButtons(start, mVertical.findViewById(R.id.ends_group), 251 true /* landscape */, true /* start */); 252 253 inflateButtons(center, mHorizontal.findViewById(R.id.center_group), 254 false /* landscape */, false /* start */); 255 inflateButtons(center, mVertical.findViewById(R.id.center_group), 256 true /* landscape */, false /* start */); 257 258 addGravitySpacer(mHorizontal.findViewById(R.id.ends_group)); 259 addGravitySpacer(mVertical.findViewById(R.id.ends_group)); 260 261 inflateButtons(end, mHorizontal.findViewById(R.id.ends_group), 262 false /* landscape */, false /* start */); 263 inflateButtons(end, mVertical.findViewById(R.id.ends_group), 264 true /* landscape */, false /* start */); 265 266 updateButtonDispatchersCurrentView(); 267 } 268 addGravitySpacer(LinearLayout layout)269 private void addGravitySpacer(LinearLayout layout) { 270 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1)); 271 } 272 inflateButtons(String[] buttons, ViewGroup parent, boolean landscape, boolean start)273 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape, 274 boolean start) { 275 for (int i = 0; i < buttons.length; i++) { 276 inflateButton(buttons[i], parent, landscape, start); 277 } 278 } 279 copy(ViewGroup.LayoutParams layoutParams)280 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) { 281 if (layoutParams instanceof LinearLayout.LayoutParams) { 282 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height, 283 ((LinearLayout.LayoutParams) layoutParams).weight); 284 } 285 return new LayoutParams(layoutParams.width, layoutParams.height); 286 } 287 288 @Nullable inflateButton(String buttonSpec, ViewGroup parent, boolean landscape, boolean start)289 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape, 290 boolean start) { 291 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater; 292 View v = createView(buttonSpec, parent, inflater); 293 if (v == null) return null; 294 295 v = applySize(v, buttonSpec, landscape, start); 296 parent.addView(v); 297 addToDispatchers(v); 298 View lastView = landscape ? mLastLandscape : mLastPortrait; 299 View accessibilityView = v; 300 if (v instanceof ReverseRelativeLayout) { 301 accessibilityView = ((ReverseRelativeLayout) v).getChildAt(0); 302 } 303 if (lastView != null) { 304 accessibilityView.setAccessibilityTraversalAfter(lastView.getId()); 305 } 306 if (landscape) { 307 mLastLandscape = accessibilityView; 308 } else { 309 mLastPortrait = accessibilityView; 310 } 311 return v; 312 } 313 applySize(View v, String buttonSpec, boolean landscape, boolean start)314 private View applySize(View v, String buttonSpec, boolean landscape, boolean start) { 315 String sizeStr = extractSize(buttonSpec); 316 if (sizeStr == null) return v; 317 318 if (sizeStr.contains(WEIGHT_SUFFIX) || sizeStr.contains(ABSOLUTE_SUFFIX)) { 319 // To support gravity, wrap in RelativeLayout and apply gravity to it. 320 // Children wanting to use gravity must be smaller then the frame. 321 ReverseRelativeLayout frame = new ReverseRelativeLayout(mContext); 322 LayoutParams childParams = new LayoutParams(v.getLayoutParams()); 323 324 // Compute gravity to apply 325 int gravity = (landscape) ? (start ? Gravity.TOP : Gravity.BOTTOM) 326 : (start ? Gravity.START : Gravity.END); 327 if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) { 328 gravity = Gravity.CENTER; 329 } else if (sizeStr.endsWith(ABSOLUTE_VERTICAL_CENTERED_SUFFIX)) { 330 gravity = Gravity.CENTER_VERTICAL; 331 } 332 333 // Set default gravity, flipped if needed in reversed layouts (270 RTL and 90 LTR) 334 frame.setDefaultGravity(gravity); 335 frame.setGravity(gravity); // Apply gravity to root 336 337 frame.addView(v, childParams); 338 339 if (sizeStr.contains(WEIGHT_SUFFIX)) { 340 // Use weighting to set the width of the frame 341 float weight = Float.parseFloat( 342 sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX))); 343 frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight)); 344 } else { 345 int width = (int) convertDpToPx(mContext, 346 Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(ABSOLUTE_SUFFIX)))); 347 frame.setLayoutParams(new LinearLayout.LayoutParams(width, MATCH_PARENT)); 348 } 349 350 // Ensure ripples can be drawn outside bounds 351 frame.setClipChildren(false); 352 frame.setClipToPadding(false); 353 354 return frame; 355 } 356 357 float size = Float.parseFloat(sizeStr); 358 ViewGroup.LayoutParams params = v.getLayoutParams(); 359 params.width = (int) (params.width * size); 360 return v; 361 } 362 createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater)363 View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) { 364 View v = null; 365 String button = extractButton(buttonSpec); 366 if (LEFT.equals(button)) { 367 button = extractButton(NAVSPACE); 368 } else if (RIGHT.equals(button)) { 369 button = extractButton(MENU_IME_ROTATE); 370 } 371 if (HOME.equals(button)) { 372 v = inflater.inflate(R.layout.home, parent, false); 373 } else if (BACK.equals(button)) { 374 v = inflater.inflate(R.layout.back, parent, false); 375 } else if (RECENT.equals(button)) { 376 v = inflater.inflate(R.layout.recent_apps, parent, false); 377 } else if (MENU_IME_ROTATE.equals(button)) { 378 v = inflater.inflate(R.layout.menu_ime, parent, false); 379 } else if (NAVSPACE.equals(button)) { 380 v = inflater.inflate(R.layout.nav_key_space, parent, false); 381 } else if (CLIPBOARD.equals(button)) { 382 v = inflater.inflate(R.layout.clipboard, parent, false); 383 } else if (CONTEXTUAL.equals(button)) { 384 v = inflater.inflate(R.layout.contextual, parent, false); 385 } else if (HOME_HANDLE.equals(button)) { 386 v = inflater.inflate(R.layout.home_handle, parent, false); 387 } else if (IME_SWITCHER.equals(button)) { 388 v = inflater.inflate(R.layout.ime_switcher, parent, false); 389 } else if (button.startsWith(KEY)) { 390 String uri = extractImage(button); 391 int code = extractKeycode(button); 392 v = inflater.inflate(R.layout.custom_key, parent, false); 393 ((KeyButtonView) v).setCode(code); 394 if (uri != null) { 395 if (uri.contains(":")) { 396 ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri)); 397 } else if (uri.contains("/")) { 398 int index = uri.indexOf('/'); 399 String pkg = uri.substring(0, index); 400 int id = Integer.parseInt(uri.substring(index + 1)); 401 ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id)); 402 } 403 } 404 } 405 return v; 406 } 407 extractImage(String buttonSpec)408 public static String extractImage(String buttonSpec) { 409 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) { 410 return null; 411 } 412 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM); 413 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END)); 414 return subStr; 415 } 416 extractKeycode(String buttonSpec)417 public static int extractKeycode(String buttonSpec) { 418 if (!buttonSpec.contains(KEY_CODE_START)) { 419 return 1; 420 } 421 final int start = buttonSpec.indexOf(KEY_CODE_START); 422 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM)); 423 return Integer.parseInt(subStr); 424 } 425 extractSize(String buttonSpec)426 public static String extractSize(String buttonSpec) { 427 if (!buttonSpec.contains(SIZE_MOD_START)) { 428 return null; 429 } 430 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START); 431 return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END)); 432 } 433 extractButton(String buttonSpec)434 public static String extractButton(String buttonSpec) { 435 if (!buttonSpec.contains(SIZE_MOD_START)) { 436 return buttonSpec; 437 } 438 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START)); 439 } 440 addToDispatchers(View v)441 private void addToDispatchers(View v) { 442 if (mButtonDispatchers != null) { 443 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId()); 444 if (indexOfKey >= 0) { 445 mButtonDispatchers.valueAt(indexOfKey).addView(v); 446 } 447 if (v instanceof ViewGroup) { 448 final ViewGroup viewGroup = (ViewGroup)v; 449 final int N = viewGroup.getChildCount(); 450 for (int i = 0; i < N; i++) { 451 addToDispatchers(viewGroup.getChildAt(i)); 452 } 453 } 454 } 455 } 456 clearViews()457 private void clearViews() { 458 if (mButtonDispatchers != null) { 459 for (int i = 0; i < mButtonDispatchers.size(); i++) { 460 mButtonDispatchers.valueAt(i).clear(); 461 } 462 } 463 clearAllChildren(mHorizontal.findViewById(R.id.nav_buttons)); 464 clearAllChildren(mVertical.findViewById(R.id.nav_buttons)); 465 } 466 clearAllChildren(ViewGroup group)467 private void clearAllChildren(ViewGroup group) { 468 for (int i = 0; i < group.getChildCount(); i++) { 469 ((ViewGroup) group.getChildAt(i)).removeAllViews(); 470 } 471 } 472 convertDpToPx(Context context, float dp)473 private static float convertDpToPx(Context context, float dp) { 474 return dp * context.getResources().getDisplayMetrics().density; 475 } 476 dump(PrintWriter pw)477 public void dump(PrintWriter pw) { 478 pw.println("NavigationBarInflaterView"); 479 pw.println(" mCurrentLayout: " + mCurrentLayout); 480 } 481 } 482