blob: 989423530599bc35e110b20e358cba7280a77f93 [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 Monk3b3f1f52017-06-28 10:45:13 -040040import com.android.systemui.statusbar.phone.ReverseLinearLayout.ReverseFrameLayout;
Jason Monk8457ad82016-01-24 10:15:55 -050041import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monka2081822016-01-18 14:41:03 -050042import com.android.systemui.tuner.TunerService;
Jason Monk197f4db2016-08-26 13:28:20 -040043import com.android.systemui.tuner.TunerService.Tunable;
Jason Monka2081822016-01-18 14:41:03 -050044
Jason Monk197f4db2016-08-26 13:28:20 -040045import java.util.ArrayList;
46import java.util.List;
Jason Monka2081822016-01-18 14:41:03 -050047import java.util.Objects;
48
Jason Monk7f6a2ab2017-04-21 13:48:19 -040049import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
50
Jason Monk197f4db2016-08-26 13:28:20 -040051public class NavigationBarInflaterView extends FrameLayout
52 implements Tunable, PluginListener<NavBarButtonProvider> {
Jason Monka2081822016-01-18 14:41:03 -050053
54 private static final String TAG = "NavBarInflater";
55
56 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
Jason Monkea05f872017-01-24 19:43:36 -050057 public static final String NAV_BAR_LEFT = "sysui_nav_bar_left";
58 public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right";
Jason Monka2081822016-01-18 14:41:03 -050059
Mike Digmana48cf192018-02-12 17:52:48 -080060 public static final String MENU_IME_ROTATE = "menu_ime";
Jason Monk3ebd2392016-01-22 10:01:44 -050061 public static final String BACK = "back";
62 public static final String HOME = "home";
63 public static final String RECENT = "recent";
64 public static final String NAVSPACE = "space";
Jason Monk3b587142016-01-23 16:47:59 -050065 public static final String CLIPBOARD = "clipboard";
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";
Jason Monka2081822016-01-18 14:41:03 -050069
70 public static final String GRAVITY_SEPARATOR = ";";
71 public static final String BUTTON_SEPARATOR = ",";
72
Jason Monk46a196e2016-01-23 15:28:10 -050073 public static final String SIZE_MOD_START = "[";
74 public static final String SIZE_MOD_END = "]";
75
Jason Monk8457ad82016-01-24 10:15:55 -050076 public static final String KEY_CODE_START = "(";
77 public static final String KEY_IMAGE_DELIM = ":";
78 public static final String KEY_CODE_END = ")";
Jason Monk7f6a2ab2017-04-21 13:48:19 -040079 private static final String WEIGHT_SUFFIX = "W";
80 private static final String WEIGHT_CENTERED_SUFFIX = "WC";
Jason Monk8457ad82016-01-24 10:15:55 -050081
Jason Monk197f4db2016-08-26 13:28:20 -040082 private final List<NavBarButtonProvider> mPlugins = new ArrayList<>();
83
Jason Monk67a1cce2016-02-05 13:31:03 -050084 protected LayoutInflater mLayoutInflater;
85 protected LayoutInflater mLandscapeInflater;
Jason Monk8457ad82016-01-24 10:15:55 -050086
Xiaohui Chen54816002016-01-25 11:11:11 -080087 protected FrameLayout mRot0;
88 protected FrameLayout mRot90;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080089 private boolean isRot0Landscape;
Jason Monk8457ad82016-01-24 10:15:55 -050090
Jason Monka2081822016-01-18 14:41:03 -050091 private SparseArray<ButtonDispatcher> mButtonDispatchers;
92 private String mCurrentLayout;
93
Xiaohui Chen40e978e2016-11-29 15:10:04 -080094 private View mLastPortrait;
95 private View mLastLandscape;
Jason Monkc62cf802016-05-10 11:02:24 -040096
Adrian Roosdb12b152016-07-12 15:38:55 -070097 private boolean mAlternativeOrder;
98
Jason Monka2081822016-01-18 14:41:03 -050099 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
100 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -0500101 createInflaters();
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800102 Display display = ((WindowManager)
103 context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
104 Mode displayMode = display.getMode();
105 isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight();
Jason Monk67a1cce2016-02-05 13:31:03 -0500106 }
107
108 private void createInflaters() {
109 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -0500110 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -0500111 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -0500112 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -0500113 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
114 }
115
116 @Override
Jason Monka2081822016-01-18 14:41:03 -0500117 protected void onFinishInflate() {
118 super.onFinishInflate();
Jason Monk9a6552d2016-05-20 11:21:59 -0400119 inflateChildren();
Jason Monka2081822016-01-18 14:41:03 -0500120 clearViews();
121 inflateLayout(getDefaultLayout());
122 }
123
Jason Monk9a6552d2016-05-20 11:21:59 -0400124 private void inflateChildren() {
125 removeAllViews();
126 mRot0 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, this, false);
127 mRot0.setId(R.id.rot0);
128 addView(mRot0);
129 mRot90 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_rot90, this,
130 false);
131 mRot90.setId(R.id.rot90);
132 addView(mRot90);
Adrian Roosdb12b152016-07-12 15:38:55 -0700133 updateAlternativeOrder();
Jason Monk9a6552d2016-05-20 11:21:59 -0400134 }
135
Xiaohui Chen54816002016-01-25 11:11:11 -0800136 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -0500137 return mContext.getString(R.string.config_navBarLayout);
138 }
139
140 @Override
141 protected void onAttachedToWindow() {
142 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500143 Dependency.get(TunerService.class).addTunable(this, NAV_BAR_VIEWS, NAV_BAR_LEFT,
Jason Monkea05f872017-01-24 19:43:36 -0500144 NAV_BAR_RIGHT);
Jason Monk5bec68f2017-02-08 20:45:10 -0800145 Dependency.get(PluginManager.class).addPluginListener(this,
146 NavBarButtonProvider.class, true /* Allow multiple */);
Jason Monka2081822016-01-18 14:41:03 -0500147 }
148
149 @Override
150 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500151 Dependency.get(TunerService.class).removeTunable(this);
152 Dependency.get(PluginManager.class).removePluginListener(this);
Jason Monka2081822016-01-18 14:41:03 -0500153 super.onDetachedFromWindow();
154 }
155
156 @Override
157 public void onTuningChanged(String key, String newValue) {
158 if (NAV_BAR_VIEWS.equals(key)) {
Jason Monka2081822016-01-18 14:41:03 -0500159 if (!Objects.equals(mCurrentLayout, newValue)) {
160 clearViews();
161 inflateLayout(newValue);
162 }
Jason Monkea05f872017-01-24 19:43:36 -0500163 } else if (NAV_BAR_LEFT.equals(key) || NAV_BAR_RIGHT.equals(key)) {
164 clearViews();
165 inflateLayout(mCurrentLayout);
Jason Monka2081822016-01-18 14:41:03 -0500166 }
167 }
168
169 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
170 mButtonDispatchers = buttonDisatchers;
171 for (int i = 0; i < buttonDisatchers.size(); i++) {
172 initiallyFill(buttonDisatchers.valueAt(i));
173 }
174 }
175
Adrian Roosdb12b152016-07-12 15:38:55 -0700176 public void setAlternativeOrder(boolean alternativeOrder) {
177 if (alternativeOrder != mAlternativeOrder) {
178 mAlternativeOrder = alternativeOrder;
179 updateAlternativeOrder();
180 }
181 }
182
183 private void updateAlternativeOrder() {
184 updateAlternativeOrder(mRot0.findViewById(R.id.ends_group));
185 updateAlternativeOrder(mRot0.findViewById(R.id.center_group));
186 updateAlternativeOrder(mRot90.findViewById(R.id.ends_group));
187 updateAlternativeOrder(mRot90.findViewById(R.id.center_group));
188 }
189
190 private void updateAlternativeOrder(View v) {
191 if (v instanceof ReverseLinearLayout) {
192 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
193 }
194 }
195
Jason Monka2081822016-01-18 14:41:03 -0500196 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500197 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500198 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500199 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500200 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500201 }
202
203 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
204 for (int i = 0; i < parent.getChildCount(); i++) {
205 // Need to manually search for each id, just in case each group has more than one
206 // of a single id. It probably mostly a waste of time, but shouldn't take long
207 // and will only happen once.
208 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
209 buttonDispatcher.addView(parent.getChildAt(i));
210 } else if (parent.getChildAt(i) instanceof ViewGroup) {
211 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
212 }
213 }
214 }
215
Xiaohui Chen54816002016-01-25 11:11:11 -0800216 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500217 mCurrentLayout = newLayout;
Jason Monk9a6552d2016-05-20 11:21:59 -0400218 if (newLayout == null) {
219 newLayout = getDefaultLayout();
220 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800221 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Mikael Magnussonc71ab602018-01-29 17:56:10 +0100222 if (sets.length != 3) {
223 Log.d(TAG, "Invalid layout.");
224 newLayout = getDefaultLayout();
225 sets = newLayout.split(GRAVITY_SEPARATOR, 3);
226 }
Jason Monka2081822016-01-18 14:41:03 -0500227 String[] start = sets[0].split(BUTTON_SEPARATOR);
228 String[] center = sets[1].split(BUTTON_SEPARATOR);
229 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400230 // Inflate these in start to end order or accessibility traversal will be messed up.
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400231 inflateButtons(start, mRot0.findViewById(R.id.ends_group), isRot0Landscape, true);
232 inflateButtons(start, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, true);
Jason Monk4f878ef2016-01-23 14:37:38 -0500233
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400234 inflateButtons(center, mRot0.findViewById(R.id.center_group), isRot0Landscape, false);
235 inflateButtons(center, mRot90.findViewById(R.id.center_group), !isRot0Landscape, false);
Xiaohui Chen06917032016-01-26 11:20:39 -0800236
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400237 addGravitySpacer(mRot0.findViewById(R.id.ends_group));
238 addGravitySpacer(mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500239
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400240 inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
241 inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
Jason Monk4f878ef2016-01-23 14:37:38 -0500242 }
243
Jason Monk4f878ef2016-01-23 14:37:38 -0500244 private void addGravitySpacer(LinearLayout layout) {
245 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500246 }
247
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400248 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape,
249 boolean start) {
Jason Monka2081822016-01-18 14:41:03 -0500250 for (int i = 0; i < buttons.length; i++) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400251 inflateButton(buttons[i], parent, landscape, start);
Jason Monka2081822016-01-18 14:41:03 -0500252 }
253 }
254
Jason Monka2081822016-01-18 14:41:03 -0500255 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500256 if (layoutParams instanceof LinearLayout.LayoutParams) {
257 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
258 ((LinearLayout.LayoutParams) layoutParams).weight);
259 }
Jason Monka2081822016-01-18 14:41:03 -0500260 return new LayoutParams(layoutParams.width, layoutParams.height);
261 }
262
Xiaohui Chen54816002016-01-25 11:11:11 -0800263 @Nullable
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400264 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
265 boolean start) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800266 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400267 View v = createView(buttonSpec, parent, inflater);
Jason Monk197f4db2016-08-26 13:28:20 -0400268 if (v == null) return null;
269
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400270 v = applySize(v, buttonSpec, landscape, start);
Jason Monk197f4db2016-08-26 13:28:20 -0400271 parent.addView(v);
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800272 addToDispatchers(v);
273 View lastView = landscape ? mLastLandscape : mLastPortrait;
Jason Monkc7ba5432017-07-06 15:12:21 -0400274 View accessibilityView = v;
275 if (v instanceof ReverseFrameLayout) {
276 accessibilityView = ((ReverseFrameLayout) v).getChildAt(0);
277 }
Jason Monk197f4db2016-08-26 13:28:20 -0400278 if (lastView != null) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400279 accessibilityView.setAccessibilityTraversalAfter(lastView.getId());
Jason Monk197f4db2016-08-26 13:28:20 -0400280 }
281 if (landscape) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400282 mLastLandscape = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400283 } else {
Jason Monkc7ba5432017-07-06 15:12:21 -0400284 mLastPortrait = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400285 }
286 return v;
287 }
288
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400289 private View applySize(View v, String buttonSpec, boolean landscape, boolean start) {
290 String sizeStr = extractSize(buttonSpec);
291 if (sizeStr == null) return v;
292
293 if (sizeStr.contains(WEIGHT_SUFFIX)) {
294 float weight = Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX)));
Jason Monk3b3f1f52017-06-28 10:45:13 -0400295 FrameLayout frame = new ReverseFrameLayout(mContext);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400296 LayoutParams childParams = new LayoutParams(v.getLayoutParams());
297 if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) {
298 childParams.gravity = Gravity.CENTER;
299 } else {
300 childParams.gravity = landscape ? (start ? Gravity.BOTTOM : Gravity.TOP)
301 : (start ? Gravity.START : Gravity.END);
302 }
303 frame.addView(v, childParams);
304 frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight));
305 frame.setClipChildren(false);
306 frame.setClipToPadding(false);
307 return frame;
308 }
309 float size = Float.parseFloat(sizeStr);
310 ViewGroup.LayoutParams params = v.getLayoutParams();
311 params.width = (int) (params.width * size);
312 return v;
313 }
314
315 private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {
Jason Monk46a196e2016-01-23 15:28:10 -0500316 View v = null;
Jason Monk197f4db2016-08-26 13:28:20 -0400317 String button = extractButton(buttonSpec);
Jason Monkea05f872017-01-24 19:43:36 -0500318 if (LEFT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800319 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_LEFT, NAVSPACE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400320 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500321 } else if (RIGHT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800322 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_RIGHT, MENU_IME_ROTATE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400323 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500324 }
Jason Monk197f4db2016-08-26 13:28:20 -0400325 // Let plugins go first so they can override a standard view if they want.
326 for (NavBarButtonProvider provider : mPlugins) {
327 v = provider.createView(buttonSpec, parent);
328 if (v != null) return v;
329 }
Jason Monka2081822016-01-18 14:41:03 -0500330 if (HOME.equals(button)) {
Annie Chinb6ab24f2016-06-01 17:46:27 -0700331 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500332 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800333 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500334 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800335 v = inflater.inflate(R.layout.recent_apps, parent, false);
Mike Digmana48cf192018-02-12 17:52:48 -0800336 } else if (MENU_IME_ROTATE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800337 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500338 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800339 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500340 } else if (CLIPBOARD.equals(button)) {
341 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500342 } else if (button.startsWith(KEY)) {
343 String uri = extractImage(button);
344 int code = extractKeycode(button);
345 v = inflater.inflate(R.layout.custom_key, parent, false);
346 ((KeyButtonView) v).setCode(code);
347 if (uri != null) {
Jason Monkea05f872017-01-24 19:43:36 -0500348 if (uri.contains(":")) {
349 ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri));
350 } else if (uri.contains("/")) {
351 int index = uri.indexOf('/');
352 String pkg = uri.substring(0, index);
353 int id = Integer.parseInt(uri.substring(index + 1));
354 ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id));
355 }
Jason Monk8457ad82016-01-24 10:15:55 -0500356 }
Jason Monkc62cf802016-05-10 11:02:24 -0400357 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500358 return v;
359 }
360
Jason Monk8457ad82016-01-24 10:15:55 -0500361 public static String extractImage(String buttonSpec) {
362 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
363 return null;
364 }
365 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
366 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
367 return subStr;
368 }
369
370 public static int extractKeycode(String buttonSpec) {
371 if (!buttonSpec.contains(KEY_CODE_START)) {
372 return 1;
373 }
374 final int start = buttonSpec.indexOf(KEY_CODE_START);
375 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
376 return Integer.parseInt(subStr);
377 }
378
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400379 public static String extractSize(String buttonSpec) {
Jason Monk46a196e2016-01-23 15:28:10 -0500380 if (!buttonSpec.contains(SIZE_MOD_START)) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400381 return null;
Jason Monk46a196e2016-01-23 15:28:10 -0500382 }
383 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400384 return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
Jason Monk46a196e2016-01-23 15:28:10 -0500385 }
386
387 public static String extractButton(String buttonSpec) {
388 if (!buttonSpec.contains(SIZE_MOD_START)) {
389 return buttonSpec;
390 }
391 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
392 }
393
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800394 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500395 if (mButtonDispatchers != null) {
396 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
397 if (indexOfKey >= 0) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800398 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700399 } else if (v instanceof ViewGroup) {
400 final ViewGroup viewGroup = (ViewGroup)v;
401 final int N = viewGroup.getChildCount();
402 for (int i = 0; i < N; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800403 addToDispatchers(viewGroup.getChildAt(i));
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700404 }
Jason Monka2081822016-01-18 14:41:03 -0500405 }
406 }
Jason Monka2081822016-01-18 14:41:03 -0500407 }
408
Jason Monka2081822016-01-18 14:41:03 -0500409
Jason Monka2081822016-01-18 14:41:03 -0500410
411 private void clearViews() {
412 if (mButtonDispatchers != null) {
413 for (int i = 0; i < mButtonDispatchers.size(); i++) {
414 mButtonDispatchers.valueAt(i).clear();
415 }
416 }
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400417 clearAllChildren(mRot0.findViewById(R.id.nav_buttons));
418 clearAllChildren(mRot90.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500419 }
420
421 private void clearAllChildren(ViewGroup group) {
422 for (int i = 0; i < group.getChildCount(); i++) {
423 ((ViewGroup) group.getChildAt(i)).removeAllViews();
424 }
425 }
Jason Monk197f4db2016-08-26 13:28:20 -0400426
427 @Override
Jason Monk20ff3f92017-01-09 15:13:23 -0500428 public void onPluginConnected(NavBarButtonProvider plugin, Context context) {
Jason Monk197f4db2016-08-26 13:28:20 -0400429 mPlugins.add(plugin);
430 clearViews();
431 inflateLayout(mCurrentLayout);
432 }
433
434 @Override
435 public void onPluginDisconnected(NavBarButtonProvider plugin) {
436 mPlugins.remove(plugin);
437 clearViews();
438 inflateLayout(mCurrentLayout);
439 }
Jason Monka2081822016-01-18 14:41:03 -0500440}