blob: faa2ab105816cccd0bb67b7d1f8f0bf707183297 [file] [log] [blame]
Jason Monka2081822016-01-18 14:41:03 -05001/*
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
15package com.android.systemui.statusbar.phone;
16
Gus Prevasab336792018-11-14 13:52:20 -050017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
18
Xiaohui Chen54816002016-01-25 11:11:11 -080019import android.annotation.Nullable;
Jason Monka2081822016-01-18 14:41:03 -050020import android.content.Context;
21import android.content.res.Configuration;
Jason Monkea05f872017-01-24 19:43:36 -050022import android.graphics.drawable.Icon;
Jason Monka2081822016-01-18 14:41:03 -050023import android.util.AttributeSet;
Jason Monk7f6a2ab2017-04-21 13:48:19 -040024import android.util.Log;
Jason Monka2081822016-01-18 14:41:03 -050025import android.util.SparseArray;
Jason Monk7f6a2ab2017-04-21 13:48:19 -040026import android.view.Gravity;
Jason Monka2081822016-01-18 14:41:03 -050027import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.FrameLayout;
Jason Monk4f878ef2016-01-23 14:37:38 -050031import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050032import android.widget.Space;
Annie Chin1ea49352016-05-27 15:23:35 -070033
Charles Chenaa94d95b2019-02-19 14:18:19 +080034import com.android.internal.annotations.VisibleForTesting;
Jason Monkde850bb2017-02-01 19:26:30 -050035import com.android.systemui.Dependency;
Jason Monka2081822016-01-18 14:41:03 -050036import com.android.systemui.R;
Jason Monk197f4db2016-08-26 13:28:20 -040037import com.android.systemui.plugins.PluginListener;
Jason Monk197f4db2016-08-26 13:28:20 -040038import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider;
Gus Prevasab336792018-11-14 13:52:20 -050039import com.android.systemui.recents.OverviewProxyService;
40import com.android.systemui.shared.plugins.PluginManager;
Mike Digmanf77fb912018-05-21 11:21:30 -070041import com.android.systemui.statusbar.phone.ReverseLinearLayout.ReverseRelativeLayout;
Jason Monk8457ad82016-01-24 10:15:55 -050042import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monka2081822016-01-18 14:41:03 -050043import com.android.systemui.tuner.TunerService;
Jason Monk197f4db2016-08-26 13:28:20 -040044import com.android.systemui.tuner.TunerService.Tunable;
Jason Monka2081822016-01-18 14:41:03 -050045
Jason Monk197f4db2016-08-26 13:28:20 -040046import java.util.ArrayList;
47import java.util.List;
Jason Monka2081822016-01-18 14:41:03 -050048import java.util.Objects;
49
Jason Monk197f4db2016-08-26 13:28:20 -040050public class NavigationBarInflaterView extends FrameLayout
51 implements Tunable, PluginListener<NavBarButtonProvider> {
Jason Monka2081822016-01-18 14:41:03 -050052
53 private static final String TAG = "NavBarInflater";
54
55 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
Jason Monkea05f872017-01-24 19:43:36 -050056 public static final String NAV_BAR_LEFT = "sysui_nav_bar_left";
57 public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right";
Jason Monka2081822016-01-18 14:41:03 -050058
Mike Digmana48cf192018-02-12 17:52:48 -080059 public static final String MENU_IME_ROTATE = "menu_ime";
Jason Monk3ebd2392016-01-22 10:01:44 -050060 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";
Jason Monk3b587142016-01-23 16:47:59 -050064 public static final String CLIPBOARD = "clipboard";
Matthew Ngb55c02c2019-02-15 16:20:31 -080065 public static final String HOME_HANDLE = "home_handle";
Jason Monk8457ad82016-01-24 10:15:55 -050066 public static final String KEY = "key";
Jason Monkea05f872017-01-24 19:43:36 -050067 public static final String LEFT = "left";
68 public static final String RIGHT = "right";
Mike Digmanf77fb912018-05-21 11:21:30 -070069 public static final String CONTEXTUAL = "contextual";
Jason Monka2081822016-01-18 14:41:03 -050070
71 public static final String GRAVITY_SEPARATOR = ";";
72 public static final String BUTTON_SEPARATOR = ",";
73
Jason Monk46a196e2016-01-23 15:28:10 -050074 public static final String SIZE_MOD_START = "[";
75 public static final String SIZE_MOD_END = "]";
76
Jason Monk8457ad82016-01-24 10:15:55 -050077 public static final String KEY_CODE_START = "(";
78 public static final String KEY_IMAGE_DELIM = ":";
79 public static final String KEY_CODE_END = ")";
Jason Monk7f6a2ab2017-04-21 13:48:19 -040080 private static final String WEIGHT_SUFFIX = "W";
81 private static final String WEIGHT_CENTERED_SUFFIX = "WC";
Jason Monk8457ad82016-01-24 10:15:55 -050082
Jason Monk197f4db2016-08-26 13:28:20 -040083 private final List<NavBarButtonProvider> mPlugins = new ArrayList<>();
84
Jason Monk67a1cce2016-02-05 13:31:03 -050085 protected LayoutInflater mLayoutInflater;
86 protected LayoutInflater mLandscapeInflater;
Jason Monk8457ad82016-01-24 10:15:55 -050087
Charles Chenaa94d95b2019-02-19 14:18:19 +080088 protected FrameLayout mHorizontal;
89 protected FrameLayout mVertical;
Jason Monk8457ad82016-01-24 10:15:55 -050090
Charles Chenaa94d95b2019-02-19 14:18:19 +080091 @VisibleForTesting
92 SparseArray<ButtonDispatcher> mButtonDispatchers;
Jason Monka2081822016-01-18 14:41:03 -050093 private String mCurrentLayout;
94
Xiaohui Chen40e978e2016-11-29 15:10:04 -080095 private View mLastPortrait;
96 private View mLastLandscape;
Jason Monkc62cf802016-05-10 11:02:24 -040097
Charles Chenaa94d95b2019-02-19 14:18:19 +080098 private boolean mIsVertical;
Adrian Roosdb12b152016-07-12 15:38:55 -070099 private boolean mAlternativeOrder;
Mike Digmanf77fb912018-05-21 11:21:30 -0700100 private boolean mUsingCustomLayout;
101
102 private OverviewProxyService mOverviewProxyService;
Adrian Roosdb12b152016-07-12 15:38:55 -0700103
Jason Monka2081822016-01-18 14:41:03 -0500104 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
105 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -0500106 createInflaters();
Mike Digmanf77fb912018-05-21 11:21:30 -0700107 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk67a1cce2016-02-05 13:31:03 -0500108 }
109
Charles Chenaa94d95b2019-02-19 14:18:19 +0800110 @VisibleForTesting
111 void createInflaters() {
Jason Monk67a1cce2016-02-05 13:31:03 -0500112 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -0500113 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -0500114 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -0500115 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -0500116 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
117 }
118
119 @Override
Jason Monka2081822016-01-18 14:41:03 -0500120 protected void onFinishInflate() {
121 super.onFinishInflate();
Jason Monk9a6552d2016-05-20 11:21:59 -0400122 inflateChildren();
Jason Monka2081822016-01-18 14:41:03 -0500123 clearViews();
124 inflateLayout(getDefaultLayout());
125 }
126
Jason Monk9a6552d2016-05-20 11:21:59 -0400127 private void inflateChildren() {
128 removeAllViews();
Charles Chenaa94d95b2019-02-19 14:18:19 +0800129 mHorizontal = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout,
130 this /* root */, false /* attachToRoot */);
131 addView(mHorizontal);
132 mVertical = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_vertical,
133 this /* root */, false /* attachToRoot */);
134 addView(mVertical);
Adrian Roosdb12b152016-07-12 15:38:55 -0700135 updateAlternativeOrder();
Jason Monk9a6552d2016-05-20 11:21:59 -0400136 }
137
Xiaohui Chen54816002016-01-25 11:11:11 -0800138 protected String getDefaultLayout() {
Mike Digmanf77fb912018-05-21 11:21:30 -0700139 final int defaultResource = mOverviewProxyService.shouldShowSwipeUpUI()
140 ? R.string.config_navBarLayoutQuickstep
141 : R.string.config_navBarLayout;
142 return mContext.getString(defaultResource);
Jason Monka2081822016-01-18 14:41:03 -0500143 }
144
145 @Override
146 protected void onAttachedToWindow() {
147 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500148 Dependency.get(TunerService.class).addTunable(this, NAV_BAR_VIEWS, NAV_BAR_LEFT,
Jason Monkea05f872017-01-24 19:43:36 -0500149 NAV_BAR_RIGHT);
Jason Monk5bec68f2017-02-08 20:45:10 -0800150 Dependency.get(PluginManager.class).addPluginListener(this,
151 NavBarButtonProvider.class, true /* Allow multiple */);
Jason Monka2081822016-01-18 14:41:03 -0500152 }
153
154 @Override
155 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500156 Dependency.get(TunerService.class).removeTunable(this);
157 Dependency.get(PluginManager.class).removePluginListener(this);
Jason Monka2081822016-01-18 14:41:03 -0500158 super.onDetachedFromWindow();
159 }
160
161 @Override
162 public void onTuningChanged(String key, String newValue) {
163 if (NAV_BAR_VIEWS.equals(key)) {
Jason Monka2081822016-01-18 14:41:03 -0500164 if (!Objects.equals(mCurrentLayout, newValue)) {
Mike Digmanf77fb912018-05-21 11:21:30 -0700165 mUsingCustomLayout = newValue != null;
Jason Monka2081822016-01-18 14:41:03 -0500166 clearViews();
167 inflateLayout(newValue);
168 }
Jason Monkea05f872017-01-24 19:43:36 -0500169 } else if (NAV_BAR_LEFT.equals(key) || NAV_BAR_RIGHT.equals(key)) {
170 clearViews();
171 inflateLayout(mCurrentLayout);
Jason Monka2081822016-01-18 14:41:03 -0500172 }
173 }
174
Mike Digmanf77fb912018-05-21 11:21:30 -0700175 public void onLikelyDefaultLayoutChange() {
176 // Don't override custom layouts
177 if (mUsingCustomLayout) return;
178
179 // Reevaluate new layout
180 final String newValue = getDefaultLayout();
181 if (!Objects.equals(mCurrentLayout, newValue)) {
182 clearViews();
183 inflateLayout(newValue);
184 }
185 }
186
Matthew Ng6607c3d2018-04-26 15:23:27 -0700187 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) {
188 mButtonDispatchers = buttonDispatchers;
189 for (int i = 0; i < buttonDispatchers.size(); i++) {
190 initiallyFill(buttonDispatchers.valueAt(i));
Jason Monka2081822016-01-18 14:41:03 -0500191 }
192 }
193
Charles Chenaa94d95b2019-02-19 14:18:19 +0800194 void updateButtonDispatchersCurrentView() {
Matthew Ng1efc9c32018-04-11 18:13:56 -0700195 if (mButtonDispatchers != null) {
Charles Chenaa94d95b2019-02-19 14:18:19 +0800196 View view = mIsVertical ? mVertical : mHorizontal;
Matthew Ng1efc9c32018-04-11 18:13:56 -0700197 for (int i = 0; i < mButtonDispatchers.size(); i++) {
Mike Digmanf77fb912018-05-21 11:21:30 -0700198 final ButtonDispatcher dispatcher = mButtonDispatchers.valueAt(i);
199 dispatcher.setCurrentView(view);
Matthew Ng1efc9c32018-04-11 18:13:56 -0700200 }
201 }
202 }
203
Charles Chenaa94d95b2019-02-19 14:18:19 +0800204 void setVertical(boolean vertical) {
205 if (vertical != mIsVertical) {
206 mIsVertical = vertical;
207 }
208 }
209
210 void setAlternativeOrder(boolean alternativeOrder) {
Adrian Roosdb12b152016-07-12 15:38:55 -0700211 if (alternativeOrder != mAlternativeOrder) {
212 mAlternativeOrder = alternativeOrder;
213 updateAlternativeOrder();
214 }
215 }
216
217 private void updateAlternativeOrder() {
Charles Chenaa94d95b2019-02-19 14:18:19 +0800218 updateAlternativeOrder(mHorizontal.findViewById(R.id.ends_group));
219 updateAlternativeOrder(mHorizontal.findViewById(R.id.center_group));
220 updateAlternativeOrder(mVertical.findViewById(R.id.ends_group));
221 updateAlternativeOrder(mVertical.findViewById(R.id.center_group));
Adrian Roosdb12b152016-07-12 15:38:55 -0700222 }
223
224 private void updateAlternativeOrder(View v) {
225 if (v instanceof ReverseLinearLayout) {
226 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
227 }
228 }
229
Jason Monka2081822016-01-18 14:41:03 -0500230 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Charles Chenaa94d95b2019-02-19 14:18:19 +0800231 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.ends_group));
232 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.center_group));
233 addAll(buttonDispatcher, mVertical.findViewById(R.id.ends_group));
234 addAll(buttonDispatcher, mVertical.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500235 }
236
237 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
238 for (int i = 0; i < parent.getChildCount(); i++) {
239 // Need to manually search for each id, just in case each group has more than one
240 // of a single id. It probably mostly a waste of time, but shouldn't take long
241 // and will only happen once.
242 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
243 buttonDispatcher.addView(parent.getChildAt(i));
Matthew Ng6607c3d2018-04-26 15:23:27 -0700244 }
245 if (parent.getChildAt(i) instanceof ViewGroup) {
Jason Monka2081822016-01-18 14:41:03 -0500246 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
247 }
248 }
249 }
250
Xiaohui Chen54816002016-01-25 11:11:11 -0800251 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500252 mCurrentLayout = newLayout;
Jason Monk9a6552d2016-05-20 11:21:59 -0400253 if (newLayout == null) {
254 newLayout = getDefaultLayout();
255 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800256 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Mikael Magnussonc71ab602018-01-29 17:56:10 +0100257 if (sets.length != 3) {
258 Log.d(TAG, "Invalid layout.");
259 newLayout = getDefaultLayout();
260 sets = newLayout.split(GRAVITY_SEPARATOR, 3);
261 }
Jason Monka2081822016-01-18 14:41:03 -0500262 String[] start = sets[0].split(BUTTON_SEPARATOR);
263 String[] center = sets[1].split(BUTTON_SEPARATOR);
264 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400265 // Inflate these in start to end order or accessibility traversal will be messed up.
Charles Chenaa94d95b2019-02-19 14:18:19 +0800266 inflateButtons(start, mHorizontal.findViewById(R.id.ends_group),
267 false /* landscape */, true /* start */);
268 inflateButtons(start, mVertical.findViewById(R.id.ends_group),
269 true /* landscape */, true /* start */);
Jason Monk4f878ef2016-01-23 14:37:38 -0500270
Charles Chenaa94d95b2019-02-19 14:18:19 +0800271 inflateButtons(center, mHorizontal.findViewById(R.id.center_group),
272 false /* landscape */, false /* start */);
273 inflateButtons(center, mVertical.findViewById(R.id.center_group),
274 true /* landscape */, false /* start */);
Xiaohui Chen06917032016-01-26 11:20:39 -0800275
Charles Chenaa94d95b2019-02-19 14:18:19 +0800276 addGravitySpacer(mHorizontal.findViewById(R.id.ends_group));
277 addGravitySpacer(mVertical.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500278
Charles Chenaa94d95b2019-02-19 14:18:19 +0800279 inflateButtons(end, mHorizontal.findViewById(R.id.ends_group),
280 false /* landscape */, false /* start */);
281 inflateButtons(end, mVertical.findViewById(R.id.ends_group),
282 true /* landscape */, false /* start */);
Matthew Ng1efc9c32018-04-11 18:13:56 -0700283
284 updateButtonDispatchersCurrentView();
Jason Monk4f878ef2016-01-23 14:37:38 -0500285 }
286
Jason Monk4f878ef2016-01-23 14:37:38 -0500287 private void addGravitySpacer(LinearLayout layout) {
288 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500289 }
290
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400291 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape,
292 boolean start) {
Jason Monka2081822016-01-18 14:41:03 -0500293 for (int i = 0; i < buttons.length; i++) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400294 inflateButton(buttons[i], parent, landscape, start);
Jason Monka2081822016-01-18 14:41:03 -0500295 }
296 }
297
Jason Monka2081822016-01-18 14:41:03 -0500298 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500299 if (layoutParams instanceof LinearLayout.LayoutParams) {
300 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
301 ((LinearLayout.LayoutParams) layoutParams).weight);
302 }
Jason Monka2081822016-01-18 14:41:03 -0500303 return new LayoutParams(layoutParams.width, layoutParams.height);
304 }
305
Xiaohui Chen54816002016-01-25 11:11:11 -0800306 @Nullable
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400307 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
308 boolean start) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800309 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400310 View v = createView(buttonSpec, parent, inflater);
Jason Monk197f4db2016-08-26 13:28:20 -0400311 if (v == null) return null;
312
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400313 v = applySize(v, buttonSpec, landscape, start);
Jason Monk197f4db2016-08-26 13:28:20 -0400314 parent.addView(v);
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800315 addToDispatchers(v);
316 View lastView = landscape ? mLastLandscape : mLastPortrait;
Jason Monkc7ba5432017-07-06 15:12:21 -0400317 View accessibilityView = v;
Mike Digmanf77fb912018-05-21 11:21:30 -0700318 if (v instanceof ReverseRelativeLayout) {
319 accessibilityView = ((ReverseRelativeLayout) v).getChildAt(0);
Jason Monkc7ba5432017-07-06 15:12:21 -0400320 }
Jason Monk197f4db2016-08-26 13:28:20 -0400321 if (lastView != null) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400322 accessibilityView.setAccessibilityTraversalAfter(lastView.getId());
Jason Monk197f4db2016-08-26 13:28:20 -0400323 }
324 if (landscape) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400325 mLastLandscape = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400326 } else {
Jason Monkc7ba5432017-07-06 15:12:21 -0400327 mLastPortrait = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400328 }
329 return v;
330 }
331
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400332 private View applySize(View v, String buttonSpec, boolean landscape, boolean start) {
333 String sizeStr = extractSize(buttonSpec);
334 if (sizeStr == null) return v;
335
336 if (sizeStr.contains(WEIGHT_SUFFIX)) {
Mike Digmanf77fb912018-05-21 11:21:30 -0700337 // To support gravity, wrap in RelativeLayout and apply gravity to it.
338 // Children wanting to use gravity must be smaller then the frame.
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400339 float weight = Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX)));
Mike Digmanf77fb912018-05-21 11:21:30 -0700340 ReverseRelativeLayout frame = new ReverseRelativeLayout(mContext);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400341 LayoutParams childParams = new LayoutParams(v.getLayoutParams());
Mike Digmanf77fb912018-05-21 11:21:30 -0700342
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)) gravity = Gravity.CENTER;
347
348 // Set default gravity, flipped if needed in reversed layouts (270 RTL and 90 LTR)
349 frame.setDefaultGravity(gravity);
350 frame.setGravity(gravity); // Apply gravity to root
351
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400352 frame.addView(v, childParams);
Mike Digmanf77fb912018-05-21 11:21:30 -0700353
354 // Use weighting to set the width of the frame
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400355 frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight));
Mike Digmanf77fb912018-05-21 11:21:30 -0700356
357 // Ensure ripples can be drawn outside bounds
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400358 frame.setClipChildren(false);
359 frame.setClipToPadding(false);
Mike Digmanf77fb912018-05-21 11:21:30 -0700360
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400361 return frame;
362 }
Mike Digmanf77fb912018-05-21 11:21:30 -0700363
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400364 float size = Float.parseFloat(sizeStr);
365 ViewGroup.LayoutParams params = v.getLayoutParams();
366 params.width = (int) (params.width * size);
367 return v;
368 }
369
370 private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {
Jason Monk46a196e2016-01-23 15:28:10 -0500371 View v = null;
Jason Monk197f4db2016-08-26 13:28:20 -0400372 String button = extractButton(buttonSpec);
Jason Monkea05f872017-01-24 19:43:36 -0500373 if (LEFT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800374 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_LEFT, NAVSPACE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400375 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500376 } else if (RIGHT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800377 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_RIGHT, MENU_IME_ROTATE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400378 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500379 }
Jason Monk197f4db2016-08-26 13:28:20 -0400380 // Let plugins go first so they can override a standard view if they want.
381 for (NavBarButtonProvider provider : mPlugins) {
382 v = provider.createView(buttonSpec, parent);
383 if (v != null) return v;
384 }
Jason Monka2081822016-01-18 14:41:03 -0500385 if (HOME.equals(button)) {
Annie Chinb6ab24f2016-06-01 17:46:27 -0700386 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500387 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800388 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500389 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800390 v = inflater.inflate(R.layout.recent_apps, parent, false);
Mike Digmana48cf192018-02-12 17:52:48 -0800391 } else if (MENU_IME_ROTATE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800392 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500393 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800394 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500395 } else if (CLIPBOARD.equals(button)) {
396 v = inflater.inflate(R.layout.clipboard, parent, false);
Mike Digmanf77fb912018-05-21 11:21:30 -0700397 } else if (CONTEXTUAL.equals(button)) {
398 v = inflater.inflate(R.layout.contextual, parent, false);
Matthew Ngb55c02c2019-02-15 16:20:31 -0800399 } else if (HOME_HANDLE.equals(button)) {
400 v = inflater.inflate(R.layout.home_handle, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500401 } else if (button.startsWith(KEY)) {
402 String uri = extractImage(button);
403 int code = extractKeycode(button);
404 v = inflater.inflate(R.layout.custom_key, parent, false);
405 ((KeyButtonView) v).setCode(code);
406 if (uri != null) {
Jason Monkea05f872017-01-24 19:43:36 -0500407 if (uri.contains(":")) {
408 ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri));
409 } else if (uri.contains("/")) {
410 int index = uri.indexOf('/');
411 String pkg = uri.substring(0, index);
412 int id = Integer.parseInt(uri.substring(index + 1));
413 ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id));
414 }
Jason Monk8457ad82016-01-24 10:15:55 -0500415 }
Jason Monkc62cf802016-05-10 11:02:24 -0400416 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500417 return v;
418 }
419
Jason Monk8457ad82016-01-24 10:15:55 -0500420 public static String extractImage(String buttonSpec) {
421 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
422 return null;
423 }
424 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
425 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
426 return subStr;
427 }
428
429 public static int extractKeycode(String buttonSpec) {
430 if (!buttonSpec.contains(KEY_CODE_START)) {
431 return 1;
432 }
433 final int start = buttonSpec.indexOf(KEY_CODE_START);
434 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
435 return Integer.parseInt(subStr);
436 }
437
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400438 public static String extractSize(String buttonSpec) {
Jason Monk46a196e2016-01-23 15:28:10 -0500439 if (!buttonSpec.contains(SIZE_MOD_START)) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400440 return null;
Jason Monk46a196e2016-01-23 15:28:10 -0500441 }
442 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400443 return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
Jason Monk46a196e2016-01-23 15:28:10 -0500444 }
445
446 public static String extractButton(String buttonSpec) {
447 if (!buttonSpec.contains(SIZE_MOD_START)) {
448 return buttonSpec;
449 }
450 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
451 }
452
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800453 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500454 if (mButtonDispatchers != null) {
455 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
456 if (indexOfKey >= 0) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800457 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Matthew Ng6607c3d2018-04-26 15:23:27 -0700458 }
459 if (v instanceof ViewGroup) {
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700460 final ViewGroup viewGroup = (ViewGroup)v;
461 final int N = viewGroup.getChildCount();
462 for (int i = 0; i < N; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800463 addToDispatchers(viewGroup.getChildAt(i));
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700464 }
Jason Monka2081822016-01-18 14:41:03 -0500465 }
466 }
Jason Monka2081822016-01-18 14:41:03 -0500467 }
468
Jason Monka2081822016-01-18 14:41:03 -0500469
Jason Monka2081822016-01-18 14:41:03 -0500470
471 private void clearViews() {
472 if (mButtonDispatchers != null) {
473 for (int i = 0; i < mButtonDispatchers.size(); i++) {
474 mButtonDispatchers.valueAt(i).clear();
475 }
476 }
Charles Chenaa94d95b2019-02-19 14:18:19 +0800477 clearAllChildren(mHorizontal.findViewById(R.id.nav_buttons));
478 clearAllChildren(mVertical.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500479 }
480
481 private void clearAllChildren(ViewGroup group) {
482 for (int i = 0; i < group.getChildCount(); i++) {
483 ((ViewGroup) group.getChildAt(i)).removeAllViews();
484 }
485 }
Jason Monk197f4db2016-08-26 13:28:20 -0400486
487 @Override
Jason Monk20ff3f92017-01-09 15:13:23 -0500488 public void onPluginConnected(NavBarButtonProvider plugin, Context context) {
Jason Monk197f4db2016-08-26 13:28:20 -0400489 mPlugins.add(plugin);
490 clearViews();
491 inflateLayout(mCurrentLayout);
492 }
493
494 @Override
495 public void onPluginDisconnected(NavBarButtonProvider plugin) {
496 mPlugins.remove(plugin);
497 clearViews();
498 inflateLayout(mCurrentLayout);
499 }
Jason Monka2081822016-01-18 14:41:03 -0500500}