blob: a14d1bc564ffe6aeda95fd1ee4c18eef99082b17 [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
Xiaohui Chen54816002016-01-25 11:11:11 -080017import android.annotation.Nullable;
Jason Monka2081822016-01-18 14:41:03 -050018import android.content.Context;
19import android.content.res.Configuration;
Jason Monkea05f872017-01-24 19:43:36 -050020import android.graphics.drawable.Icon;
Jason Monka2081822016-01-18 14:41:03 -050021import android.util.AttributeSet;
Jason Monk7f6a2ab2017-04-21 13:48:19 -040022import android.util.Log;
Jason Monka2081822016-01-18 14:41:03 -050023import android.util.SparseArray;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080024import android.view.Display;
25import android.view.Display.Mode;
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;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080030import android.view.WindowManager;
Jason Monka2081822016-01-18 14:41:03 -050031import android.widget.FrameLayout;
Jason Monk4f878ef2016-01-23 14:37:38 -050032import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050033import android.widget.Space;
Annie Chin1ea49352016-05-27 15:23:35 -070034
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;
38import com.android.systemui.plugins.PluginManager;
39import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider;
Jason Monk8457ad82016-01-24 10:15:55 -050040import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monka2081822016-01-18 14:41:03 -050041import com.android.systemui.tuner.TunerService;
Jason Monk197f4db2016-08-26 13:28:20 -040042import com.android.systemui.tuner.TunerService.Tunable;
Jason Monka2081822016-01-18 14:41:03 -050043
Jason Monk197f4db2016-08-26 13:28:20 -040044import java.util.ArrayList;
45import java.util.List;
Jason Monka2081822016-01-18 14:41:03 -050046import java.util.Objects;
47
Jason Monk7f6a2ab2017-04-21 13:48:19 -040048import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
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
Jason Monk3ebd2392016-01-22 10:01:44 -050059 public static final String MENU_IME = "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";
Jason Monk3b587142016-01-23 16:47:59 -050064 public static final String CLIPBOARD = "clipboard";
Jason Monk8457ad82016-01-24 10:15:55 -050065 public static final String KEY = "key";
Jason Monkea05f872017-01-24 19:43:36 -050066 public static final String LEFT = "left";
67 public static final String RIGHT = "right";
Jason Monka2081822016-01-18 14:41:03 -050068
69 public static final String GRAVITY_SEPARATOR = ";";
70 public static final String BUTTON_SEPARATOR = ",";
71
Jason Monk46a196e2016-01-23 15:28:10 -050072 public static final String SIZE_MOD_START = "[";
73 public static final String SIZE_MOD_END = "]";
74
Jason Monk8457ad82016-01-24 10:15:55 -050075 public static final String KEY_CODE_START = "(";
76 public static final String KEY_IMAGE_DELIM = ":";
77 public static final String KEY_CODE_END = ")";
Jason Monk7f6a2ab2017-04-21 13:48:19 -040078 private static final String WEIGHT_SUFFIX = "W";
79 private static final String WEIGHT_CENTERED_SUFFIX = "WC";
Jason Monk8457ad82016-01-24 10:15:55 -050080
Jason Monk197f4db2016-08-26 13:28:20 -040081 private final List<NavBarButtonProvider> mPlugins = new ArrayList<>();
82
Jason Monk67a1cce2016-02-05 13:31:03 -050083 protected LayoutInflater mLayoutInflater;
84 protected LayoutInflater mLandscapeInflater;
Jason Monk8457ad82016-01-24 10:15:55 -050085
Xiaohui Chen54816002016-01-25 11:11:11 -080086 protected FrameLayout mRot0;
87 protected FrameLayout mRot90;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080088 private boolean isRot0Landscape;
Jason Monk8457ad82016-01-24 10:15:55 -050089
Jason Monka2081822016-01-18 14:41:03 -050090 private SparseArray<ButtonDispatcher> mButtonDispatchers;
91 private String mCurrentLayout;
92
Xiaohui Chen40e978e2016-11-29 15:10:04 -080093 private View mLastPortrait;
94 private View mLastLandscape;
Jason Monkc62cf802016-05-10 11:02:24 -040095
Adrian Roosdb12b152016-07-12 15:38:55 -070096 private boolean mAlternativeOrder;
97
Jason Monka2081822016-01-18 14:41:03 -050098 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
99 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -0500100 createInflaters();
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800101 Display display = ((WindowManager)
102 context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
103 Mode displayMode = display.getMode();
104 isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight();
Jason Monk67a1cce2016-02-05 13:31:03 -0500105 }
106
107 private void createInflaters() {
108 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -0500109 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -0500110 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -0500111 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -0500112 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
113 }
114
115 @Override
Jason Monka2081822016-01-18 14:41:03 -0500116 protected void onFinishInflate() {
117 super.onFinishInflate();
Jason Monk9a6552d2016-05-20 11:21:59 -0400118 inflateChildren();
Jason Monka2081822016-01-18 14:41:03 -0500119 clearViews();
120 inflateLayout(getDefaultLayout());
121 }
122
Jason Monk9a6552d2016-05-20 11:21:59 -0400123 private void inflateChildren() {
124 removeAllViews();
125 mRot0 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, this, false);
126 mRot0.setId(R.id.rot0);
127 addView(mRot0);
128 mRot90 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_rot90, this,
129 false);
130 mRot90.setId(R.id.rot90);
131 addView(mRot90);
Adrian Roosdb12b152016-07-12 15:38:55 -0700132 updateAlternativeOrder();
Jason Monk9a6552d2016-05-20 11:21:59 -0400133 if (getParent() instanceof NavigationBarView) {
134 ((NavigationBarView) getParent()).updateRotatedViews();
135 }
136 }
137
Xiaohui Chen54816002016-01-25 11:11:11 -0800138 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -0500139 return mContext.getString(R.string.config_navBarLayout);
140 }
141
142 @Override
143 protected void onAttachedToWindow() {
144 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500145 Dependency.get(TunerService.class).addTunable(this, NAV_BAR_VIEWS, NAV_BAR_LEFT,
Jason Monkea05f872017-01-24 19:43:36 -0500146 NAV_BAR_RIGHT);
Jason Monk5bec68f2017-02-08 20:45:10 -0800147 Dependency.get(PluginManager.class).addPluginListener(this,
148 NavBarButtonProvider.class, true /* Allow multiple */);
Jason Monka2081822016-01-18 14:41:03 -0500149 }
150
151 @Override
152 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500153 Dependency.get(TunerService.class).removeTunable(this);
154 Dependency.get(PluginManager.class).removePluginListener(this);
Jason Monka2081822016-01-18 14:41:03 -0500155 super.onDetachedFromWindow();
156 }
157
158 @Override
159 public void onTuningChanged(String key, String newValue) {
160 if (NAV_BAR_VIEWS.equals(key)) {
Jason Monka2081822016-01-18 14:41:03 -0500161 if (!Objects.equals(mCurrentLayout, newValue)) {
162 clearViews();
163 inflateLayout(newValue);
164 }
Jason Monkea05f872017-01-24 19:43:36 -0500165 } else if (NAV_BAR_LEFT.equals(key) || NAV_BAR_RIGHT.equals(key)) {
166 clearViews();
167 inflateLayout(mCurrentLayout);
Jason Monka2081822016-01-18 14:41:03 -0500168 }
169 }
170
171 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
172 mButtonDispatchers = buttonDisatchers;
173 for (int i = 0; i < buttonDisatchers.size(); i++) {
174 initiallyFill(buttonDisatchers.valueAt(i));
175 }
176 }
177
Adrian Roosdb12b152016-07-12 15:38:55 -0700178 public void setAlternativeOrder(boolean alternativeOrder) {
179 if (alternativeOrder != mAlternativeOrder) {
180 mAlternativeOrder = alternativeOrder;
181 updateAlternativeOrder();
182 }
183 }
184
185 private void updateAlternativeOrder() {
186 updateAlternativeOrder(mRot0.findViewById(R.id.ends_group));
187 updateAlternativeOrder(mRot0.findViewById(R.id.center_group));
188 updateAlternativeOrder(mRot90.findViewById(R.id.ends_group));
189 updateAlternativeOrder(mRot90.findViewById(R.id.center_group));
190 }
191
192 private void updateAlternativeOrder(View v) {
193 if (v instanceof ReverseLinearLayout) {
194 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
195 }
196 }
197
Jason Monka2081822016-01-18 14:41:03 -0500198 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500199 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500200 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500201 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500202 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500203 }
204
205 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
206 for (int i = 0; i < parent.getChildCount(); i++) {
207 // Need to manually search for each id, just in case each group has more than one
208 // of a single id. It probably mostly a waste of time, but shouldn't take long
209 // and will only happen once.
210 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
211 buttonDispatcher.addView(parent.getChildAt(i));
212 } else if (parent.getChildAt(i) instanceof ViewGroup) {
213 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
214 }
215 }
216 }
217
Xiaohui Chen54816002016-01-25 11:11:11 -0800218 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500219 mCurrentLayout = newLayout;
Jason Monk9a6552d2016-05-20 11:21:59 -0400220 if (newLayout == null) {
221 newLayout = getDefaultLayout();
222 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800223 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500224 String[] start = sets[0].split(BUTTON_SEPARATOR);
225 String[] center = sets[1].split(BUTTON_SEPARATOR);
226 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400227 // Inflate these in start to end order or accessibility traversal will be messed up.
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400228 inflateButtons(start, mRot0.findViewById(R.id.ends_group), isRot0Landscape, true);
229 inflateButtons(start, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, true);
Jason Monk4f878ef2016-01-23 14:37:38 -0500230
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400231 inflateButtons(center, mRot0.findViewById(R.id.center_group), isRot0Landscape, false);
232 inflateButtons(center, mRot90.findViewById(R.id.center_group), !isRot0Landscape, false);
Xiaohui Chen06917032016-01-26 11:20:39 -0800233
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400234 addGravitySpacer(mRot0.findViewById(R.id.ends_group));
235 addGravitySpacer(mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500236
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400237 inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
238 inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
Jason Monk4f878ef2016-01-23 14:37:38 -0500239 }
240
Jason Monk4f878ef2016-01-23 14:37:38 -0500241 private void addGravitySpacer(LinearLayout layout) {
242 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500243 }
244
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400245 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape,
246 boolean start) {
Jason Monka2081822016-01-18 14:41:03 -0500247 for (int i = 0; i < buttons.length; i++) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400248 inflateButton(buttons[i], parent, landscape, start);
Jason Monka2081822016-01-18 14:41:03 -0500249 }
250 }
251
Jason Monka2081822016-01-18 14:41:03 -0500252 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500253 if (layoutParams instanceof LinearLayout.LayoutParams) {
254 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
255 ((LinearLayout.LayoutParams) layoutParams).weight);
256 }
Jason Monka2081822016-01-18 14:41:03 -0500257 return new LayoutParams(layoutParams.width, layoutParams.height);
258 }
259
Xiaohui Chen54816002016-01-25 11:11:11 -0800260 @Nullable
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400261 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
262 boolean start) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800263 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400264 View v = createView(buttonSpec, parent, inflater);
Jason Monk197f4db2016-08-26 13:28:20 -0400265 if (v == null) return null;
266
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400267 v = applySize(v, buttonSpec, landscape, start);
Jason Monk197f4db2016-08-26 13:28:20 -0400268 parent.addView(v);
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800269 addToDispatchers(v);
270 View lastView = landscape ? mLastLandscape : mLastPortrait;
Jason Monk197f4db2016-08-26 13:28:20 -0400271 if (lastView != null) {
272 v.setAccessibilityTraversalAfter(lastView.getId());
273 }
274 if (landscape) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800275 mLastLandscape = v;
Jason Monk197f4db2016-08-26 13:28:20 -0400276 } else {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800277 mLastPortrait = v;
Jason Monk197f4db2016-08-26 13:28:20 -0400278 }
279 return v;
280 }
281
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400282 private View applySize(View v, String buttonSpec, boolean landscape, boolean start) {
283 String sizeStr = extractSize(buttonSpec);
284 if (sizeStr == null) return v;
285
286 if (sizeStr.contains(WEIGHT_SUFFIX)) {
287 float weight = Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX)));
288 FrameLayout frame = new FrameLayout(mContext);
289 LayoutParams childParams = new LayoutParams(v.getLayoutParams());
290 if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) {
291 childParams.gravity = Gravity.CENTER;
292 } else {
293 childParams.gravity = landscape ? (start ? Gravity.BOTTOM : Gravity.TOP)
294 : (start ? Gravity.START : Gravity.END);
295 }
296 frame.addView(v, childParams);
297 frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight));
298 frame.setClipChildren(false);
299 frame.setClipToPadding(false);
300 return frame;
301 }
302 float size = Float.parseFloat(sizeStr);
303 ViewGroup.LayoutParams params = v.getLayoutParams();
304 params.width = (int) (params.width * size);
305 return v;
306 }
307
308 private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {
Jason Monk46a196e2016-01-23 15:28:10 -0500309 View v = null;
Jason Monk197f4db2016-08-26 13:28:20 -0400310 String button = extractButton(buttonSpec);
Jason Monkea05f872017-01-24 19:43:36 -0500311 if (LEFT.equals(button)) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400312 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_LEFT, NAVSPACE);
313 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500314 } else if (RIGHT.equals(button)) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400315 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_RIGHT, MENU_IME);
316 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500317 }
Jason Monk197f4db2016-08-26 13:28:20 -0400318 // Let plugins go first so they can override a standard view if they want.
319 for (NavBarButtonProvider provider : mPlugins) {
320 v = provider.createView(buttonSpec, parent);
321 if (v != null) return v;
322 }
Jason Monka2081822016-01-18 14:41:03 -0500323 if (HOME.equals(button)) {
Annie Chinb6ab24f2016-06-01 17:46:27 -0700324 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500325 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800326 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500327 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800328 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500329 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800330 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500331 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800332 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500333 } else if (CLIPBOARD.equals(button)) {
334 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500335 } else if (button.startsWith(KEY)) {
336 String uri = extractImage(button);
337 int code = extractKeycode(button);
338 v = inflater.inflate(R.layout.custom_key, parent, false);
339 ((KeyButtonView) v).setCode(code);
340 if (uri != null) {
Jason Monkea05f872017-01-24 19:43:36 -0500341 if (uri.contains(":")) {
342 ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri));
343 } else if (uri.contains("/")) {
344 int index = uri.indexOf('/');
345 String pkg = uri.substring(0, index);
346 int id = Integer.parseInt(uri.substring(index + 1));
347 ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id));
348 }
Jason Monk8457ad82016-01-24 10:15:55 -0500349 }
Jason Monkc62cf802016-05-10 11:02:24 -0400350 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500351 return v;
352 }
353
Jason Monk8457ad82016-01-24 10:15:55 -0500354 public static String extractImage(String buttonSpec) {
355 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
356 return null;
357 }
358 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
359 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
360 return subStr;
361 }
362
363 public static int extractKeycode(String buttonSpec) {
364 if (!buttonSpec.contains(KEY_CODE_START)) {
365 return 1;
366 }
367 final int start = buttonSpec.indexOf(KEY_CODE_START);
368 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
369 return Integer.parseInt(subStr);
370 }
371
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400372 public static String extractSize(String buttonSpec) {
Jason Monk46a196e2016-01-23 15:28:10 -0500373 if (!buttonSpec.contains(SIZE_MOD_START)) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400374 return null;
Jason Monk46a196e2016-01-23 15:28:10 -0500375 }
376 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400377 return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
Jason Monk46a196e2016-01-23 15:28:10 -0500378 }
379
380 public static String extractButton(String buttonSpec) {
381 if (!buttonSpec.contains(SIZE_MOD_START)) {
382 return buttonSpec;
383 }
384 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
385 }
386
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800387 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500388 if (mButtonDispatchers != null) {
389 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
390 if (indexOfKey >= 0) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800391 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700392 } else if (v instanceof ViewGroup) {
393 final ViewGroup viewGroup = (ViewGroup)v;
394 final int N = viewGroup.getChildCount();
395 for (int i = 0; i < N; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800396 addToDispatchers(viewGroup.getChildAt(i));
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700397 }
Jason Monka2081822016-01-18 14:41:03 -0500398 }
399 }
Jason Monka2081822016-01-18 14:41:03 -0500400 }
401
Jason Monka2081822016-01-18 14:41:03 -0500402
Jason Monka2081822016-01-18 14:41:03 -0500403
404 private void clearViews() {
405 if (mButtonDispatchers != null) {
406 for (int i = 0; i < mButtonDispatchers.size(); i++) {
407 mButtonDispatchers.valueAt(i).clear();
408 }
409 }
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400410 clearAllChildren(mRot0.findViewById(R.id.nav_buttons));
411 clearAllChildren(mRot90.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500412 }
413
414 private void clearAllChildren(ViewGroup group) {
415 for (int i = 0; i < group.getChildCount(); i++) {
416 ((ViewGroup) group.getChildAt(i)).removeAllViews();
417 }
418 }
Jason Monk197f4db2016-08-26 13:28:20 -0400419
420 @Override
Jason Monk20ff3f92017-01-09 15:13:23 -0500421 public void onPluginConnected(NavBarButtonProvider plugin, Context context) {
Jason Monk197f4db2016-08-26 13:28:20 -0400422 mPlugins.add(plugin);
423 clearViews();
424 inflateLayout(mCurrentLayout);
425 }
426
427 @Override
428 public void onPluginDisconnected(NavBarButtonProvider plugin) {
429 mPlugins.remove(plugin);
430 clearViews();
431 inflateLayout(mCurrentLayout);
432 }
Jason Monka2081822016-01-18 14:41:03 -0500433}