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