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