blob: 4885c2f796d962c4c938395fb2ad739d947ec304 [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;
Matthew Ng1efc9c32018-04-11 18:13:56 -070028import android.view.Surface;
Jason Monka2081822016-01-18 14:41:03 -050029import android.view.View;
30import android.view.ViewGroup;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080031import android.view.WindowManager;
Jason Monka2081822016-01-18 14:41:03 -050032import android.widget.FrameLayout;
Jason Monk4f878ef2016-01-23 14:37:38 -050033import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050034import android.widget.Space;
Annie Chin1ea49352016-05-27 15:23:35 -070035
Jason Monkde850bb2017-02-01 19:26:30 -050036import com.android.systemui.Dependency;
Jason Monka2081822016-01-18 14:41:03 -050037import com.android.systemui.R;
Jason Monk197f4db2016-08-26 13:28:20 -040038import com.android.systemui.plugins.PluginListener;
39import com.android.systemui.plugins.PluginManager;
40import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider;
Jason Monk3b3f1f52017-06-28 10:45:13 -040041import com.android.systemui.statusbar.phone.ReverseLinearLayout.ReverseFrameLayout;
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 Monk7f6a2ab2017-04-21 13:48:19 -040050import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
51
Jason Monk197f4db2016-08-26 13:28:20 -040052public class NavigationBarInflaterView extends FrameLayout
53 implements Tunable, PluginListener<NavBarButtonProvider> {
Jason Monka2081822016-01-18 14:41:03 -050054
55 private static final String TAG = "NavBarInflater";
56
57 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
Jason Monkea05f872017-01-24 19:43:36 -050058 public static final String NAV_BAR_LEFT = "sysui_nav_bar_left";
59 public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right";
Jason Monka2081822016-01-18 14:41:03 -050060
Mike Digmana48cf192018-02-12 17:52:48 -080061 public static final String MENU_IME_ROTATE = "menu_ime";
Jason Monk3ebd2392016-01-22 10:01:44 -050062 public static final String BACK = "back";
63 public static final String HOME = "home";
64 public static final String RECENT = "recent";
65 public static final String NAVSPACE = "space";
Jason Monk3b587142016-01-23 16:47:59 -050066 public static final String CLIPBOARD = "clipboard";
Jason Monk8457ad82016-01-24 10:15:55 -050067 public static final String KEY = "key";
Jason Monkea05f872017-01-24 19:43:36 -050068 public static final String LEFT = "left";
69 public static final String RIGHT = "right";
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<>();
Matthew Ng1efc9c32018-04-11 18:13:56 -070084 private final Display mDisplay;
Jason Monk197f4db2016-08-26 13:28:20 -040085
Jason Monk67a1cce2016-02-05 13:31:03 -050086 protected LayoutInflater mLayoutInflater;
87 protected LayoutInflater mLandscapeInflater;
Jason Monk8457ad82016-01-24 10:15:55 -050088
Xiaohui Chen54816002016-01-25 11:11:11 -080089 protected FrameLayout mRot0;
90 protected FrameLayout mRot90;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080091 private boolean isRot0Landscape;
Jason Monk8457ad82016-01-24 10:15:55 -050092
Jason Monka2081822016-01-18 14:41:03 -050093 private SparseArray<ButtonDispatcher> mButtonDispatchers;
94 private String mCurrentLayout;
95
Xiaohui Chen40e978e2016-11-29 15:10:04 -080096 private View mLastPortrait;
97 private View mLastLandscape;
Jason Monkc62cf802016-05-10 11:02:24 -040098
Adrian Roosdb12b152016-07-12 15:38:55 -070099 private boolean mAlternativeOrder;
100
Jason Monka2081822016-01-18 14:41:03 -0500101 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
102 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -0500103 createInflaters();
Matthew Ng1efc9c32018-04-11 18:13:56 -0700104 mDisplay = ((WindowManager)
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800105 context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Matthew Ng1efc9c32018-04-11 18:13:56 -0700106 Mode displayMode = mDisplay.getMode();
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800107 isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight();
Jason Monk67a1cce2016-02-05 13:31:03 -0500108 }
109
110 private void createInflaters() {
111 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -0500112 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -0500113 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -0500114 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -0500115 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
116 }
117
118 @Override
Jason Monka2081822016-01-18 14:41:03 -0500119 protected void onFinishInflate() {
120 super.onFinishInflate();
Jason Monk9a6552d2016-05-20 11:21:59 -0400121 inflateChildren();
Jason Monka2081822016-01-18 14:41:03 -0500122 clearViews();
123 inflateLayout(getDefaultLayout());
124 }
125
Jason Monk9a6552d2016-05-20 11:21:59 -0400126 private void inflateChildren() {
127 removeAllViews();
128 mRot0 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, this, false);
129 mRot0.setId(R.id.rot0);
130 addView(mRot0);
131 mRot90 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_rot90, this,
132 false);
133 mRot90.setId(R.id.rot90);
134 addView(mRot90);
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() {
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
Matthew Ng6607c3d2018-04-26 15:23:27 -0700171 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) {
172 mButtonDispatchers = buttonDispatchers;
173 for (int i = 0; i < buttonDispatchers.size(); i++) {
174 initiallyFill(buttonDispatchers.valueAt(i));
Jason Monka2081822016-01-18 14:41:03 -0500175 }
176 }
177
Matthew Ng1efc9c32018-04-11 18:13:56 -0700178 public void updateButtonDispatchersCurrentView() {
179 if (mButtonDispatchers != null) {
180 final int rotation = mDisplay.getRotation();
181 final View view = rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180
182 ? mRot0 : mRot90;
183 for (int i = 0; i < mButtonDispatchers.size(); i++) {
184 mButtonDispatchers.valueAt(i).setCurrentView(view);
185 }
186 }
187 }
188
Adrian Roosdb12b152016-07-12 15:38:55 -0700189 public void setAlternativeOrder(boolean alternativeOrder) {
190 if (alternativeOrder != mAlternativeOrder) {
191 mAlternativeOrder = alternativeOrder;
192 updateAlternativeOrder();
193 }
194 }
195
196 private void updateAlternativeOrder() {
197 updateAlternativeOrder(mRot0.findViewById(R.id.ends_group));
198 updateAlternativeOrder(mRot0.findViewById(R.id.center_group));
199 updateAlternativeOrder(mRot90.findViewById(R.id.ends_group));
200 updateAlternativeOrder(mRot90.findViewById(R.id.center_group));
201 }
202
203 private void updateAlternativeOrder(View v) {
204 if (v instanceof ReverseLinearLayout) {
205 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
206 }
207 }
208
Jason Monka2081822016-01-18 14:41:03 -0500209 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500210 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500211 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500212 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500213 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500214 }
215
216 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
217 for (int i = 0; i < parent.getChildCount(); i++) {
218 // Need to manually search for each id, just in case each group has more than one
219 // of a single id. It probably mostly a waste of time, but shouldn't take long
220 // and will only happen once.
221 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
222 buttonDispatcher.addView(parent.getChildAt(i));
Matthew Ng6607c3d2018-04-26 15:23:27 -0700223 }
224 if (parent.getChildAt(i) instanceof ViewGroup) {
Jason Monka2081822016-01-18 14:41:03 -0500225 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
226 }
227 }
228 }
229
Xiaohui Chen54816002016-01-25 11:11:11 -0800230 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500231 mCurrentLayout = newLayout;
Jason Monk9a6552d2016-05-20 11:21:59 -0400232 if (newLayout == null) {
233 newLayout = getDefaultLayout();
234 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800235 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Mikael Magnussonc71ab602018-01-29 17:56:10 +0100236 if (sets.length != 3) {
237 Log.d(TAG, "Invalid layout.");
238 newLayout = getDefaultLayout();
239 sets = newLayout.split(GRAVITY_SEPARATOR, 3);
240 }
Jason Monka2081822016-01-18 14:41:03 -0500241 String[] start = sets[0].split(BUTTON_SEPARATOR);
242 String[] center = sets[1].split(BUTTON_SEPARATOR);
243 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400244 // Inflate these in start to end order or accessibility traversal will be messed up.
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400245 inflateButtons(start, mRot0.findViewById(R.id.ends_group), isRot0Landscape, true);
246 inflateButtons(start, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, true);
Jason Monk4f878ef2016-01-23 14:37:38 -0500247
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400248 inflateButtons(center, mRot0.findViewById(R.id.center_group), isRot0Landscape, false);
249 inflateButtons(center, mRot90.findViewById(R.id.center_group), !isRot0Landscape, false);
Xiaohui Chen06917032016-01-26 11:20:39 -0800250
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400251 addGravitySpacer(mRot0.findViewById(R.id.ends_group));
252 addGravitySpacer(mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500253
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400254 inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
255 inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
Matthew Ng1efc9c32018-04-11 18:13:56 -0700256
257 updateButtonDispatchersCurrentView();
Jason Monk4f878ef2016-01-23 14:37:38 -0500258 }
259
Jason Monk4f878ef2016-01-23 14:37:38 -0500260 private void addGravitySpacer(LinearLayout layout) {
261 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500262 }
263
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400264 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape,
265 boolean start) {
Jason Monka2081822016-01-18 14:41:03 -0500266 for (int i = 0; i < buttons.length; i++) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400267 inflateButton(buttons[i], parent, landscape, start);
Jason Monka2081822016-01-18 14:41:03 -0500268 }
269 }
270
Jason Monka2081822016-01-18 14:41:03 -0500271 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500272 if (layoutParams instanceof LinearLayout.LayoutParams) {
273 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
274 ((LinearLayout.LayoutParams) layoutParams).weight);
275 }
Jason Monka2081822016-01-18 14:41:03 -0500276 return new LayoutParams(layoutParams.width, layoutParams.height);
277 }
278
Xiaohui Chen54816002016-01-25 11:11:11 -0800279 @Nullable
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400280 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
281 boolean start) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800282 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400283 View v = createView(buttonSpec, parent, inflater);
Jason Monk197f4db2016-08-26 13:28:20 -0400284 if (v == null) return null;
285
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400286 v = applySize(v, buttonSpec, landscape, start);
Jason Monk197f4db2016-08-26 13:28:20 -0400287 parent.addView(v);
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800288 addToDispatchers(v);
289 View lastView = landscape ? mLastLandscape : mLastPortrait;
Jason Monkc7ba5432017-07-06 15:12:21 -0400290 View accessibilityView = v;
291 if (v instanceof ReverseFrameLayout) {
292 accessibilityView = ((ReverseFrameLayout) v).getChildAt(0);
293 }
Jason Monk197f4db2016-08-26 13:28:20 -0400294 if (lastView != null) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400295 accessibilityView.setAccessibilityTraversalAfter(lastView.getId());
Jason Monk197f4db2016-08-26 13:28:20 -0400296 }
297 if (landscape) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400298 mLastLandscape = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400299 } else {
Jason Monkc7ba5432017-07-06 15:12:21 -0400300 mLastPortrait = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400301 }
302 return v;
303 }
304
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400305 private View applySize(View v, String buttonSpec, boolean landscape, boolean start) {
306 String sizeStr = extractSize(buttonSpec);
307 if (sizeStr == null) return v;
308
309 if (sizeStr.contains(WEIGHT_SUFFIX)) {
310 float weight = Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX)));
Jason Monk3b3f1f52017-06-28 10:45:13 -0400311 FrameLayout frame = new ReverseFrameLayout(mContext);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400312 LayoutParams childParams = new LayoutParams(v.getLayoutParams());
313 if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) {
314 childParams.gravity = Gravity.CENTER;
315 } else {
316 childParams.gravity = landscape ? (start ? Gravity.BOTTOM : Gravity.TOP)
317 : (start ? Gravity.START : Gravity.END);
318 }
319 frame.addView(v, childParams);
320 frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight));
321 frame.setClipChildren(false);
322 frame.setClipToPadding(false);
323 return frame;
324 }
325 float size = Float.parseFloat(sizeStr);
326 ViewGroup.LayoutParams params = v.getLayoutParams();
327 params.width = (int) (params.width * size);
328 return v;
329 }
330
331 private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {
Jason Monk46a196e2016-01-23 15:28:10 -0500332 View v = null;
Jason Monk197f4db2016-08-26 13:28:20 -0400333 String button = extractButton(buttonSpec);
Jason Monkea05f872017-01-24 19:43:36 -0500334 if (LEFT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800335 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_LEFT, NAVSPACE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400336 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500337 } else if (RIGHT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800338 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_RIGHT, MENU_IME_ROTATE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400339 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500340 }
Jason Monk197f4db2016-08-26 13:28:20 -0400341 // Let plugins go first so they can override a standard view if they want.
342 for (NavBarButtonProvider provider : mPlugins) {
343 v = provider.createView(buttonSpec, parent);
344 if (v != null) return v;
345 }
Jason Monka2081822016-01-18 14:41:03 -0500346 if (HOME.equals(button)) {
Annie Chinb6ab24f2016-06-01 17:46:27 -0700347 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500348 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800349 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500350 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800351 v = inflater.inflate(R.layout.recent_apps, parent, false);
Mike Digmana48cf192018-02-12 17:52:48 -0800352 } else if (MENU_IME_ROTATE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800353 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500354 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800355 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500356 } else if (CLIPBOARD.equals(button)) {
357 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500358 } else if (button.startsWith(KEY)) {
359 String uri = extractImage(button);
360 int code = extractKeycode(button);
361 v = inflater.inflate(R.layout.custom_key, parent, false);
362 ((KeyButtonView) v).setCode(code);
363 if (uri != null) {
Jason Monkea05f872017-01-24 19:43:36 -0500364 if (uri.contains(":")) {
365 ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri));
366 } else if (uri.contains("/")) {
367 int index = uri.indexOf('/');
368 String pkg = uri.substring(0, index);
369 int id = Integer.parseInt(uri.substring(index + 1));
370 ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id));
371 }
Jason Monk8457ad82016-01-24 10:15:55 -0500372 }
Jason Monkc62cf802016-05-10 11:02:24 -0400373 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500374 return v;
375 }
376
Jason Monk8457ad82016-01-24 10:15:55 -0500377 public static String extractImage(String buttonSpec) {
378 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
379 return null;
380 }
381 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
382 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
383 return subStr;
384 }
385
386 public static int extractKeycode(String buttonSpec) {
387 if (!buttonSpec.contains(KEY_CODE_START)) {
388 return 1;
389 }
390 final int start = buttonSpec.indexOf(KEY_CODE_START);
391 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
392 return Integer.parseInt(subStr);
393 }
394
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400395 public static String extractSize(String buttonSpec) {
Jason Monk46a196e2016-01-23 15:28:10 -0500396 if (!buttonSpec.contains(SIZE_MOD_START)) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400397 return null;
Jason Monk46a196e2016-01-23 15:28:10 -0500398 }
399 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400400 return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
Jason Monk46a196e2016-01-23 15:28:10 -0500401 }
402
403 public static String extractButton(String buttonSpec) {
404 if (!buttonSpec.contains(SIZE_MOD_START)) {
405 return buttonSpec;
406 }
407 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
408 }
409
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800410 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500411 if (mButtonDispatchers != null) {
412 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
413 if (indexOfKey >= 0) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800414 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Matthew Ng6607c3d2018-04-26 15:23:27 -0700415 }
416 if (v instanceof ViewGroup) {
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700417 final ViewGroup viewGroup = (ViewGroup)v;
418 final int N = viewGroup.getChildCount();
419 for (int i = 0; i < N; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800420 addToDispatchers(viewGroup.getChildAt(i));
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700421 }
Jason Monka2081822016-01-18 14:41:03 -0500422 }
423 }
Jason Monka2081822016-01-18 14:41:03 -0500424 }
425
Jason Monka2081822016-01-18 14:41:03 -0500426
Jason Monka2081822016-01-18 14:41:03 -0500427
428 private void clearViews() {
429 if (mButtonDispatchers != null) {
430 for (int i = 0; i < mButtonDispatchers.size(); i++) {
431 mButtonDispatchers.valueAt(i).clear();
432 }
433 }
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400434 clearAllChildren(mRot0.findViewById(R.id.nav_buttons));
435 clearAllChildren(mRot90.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500436 }
437
438 private void clearAllChildren(ViewGroup group) {
439 for (int i = 0; i < group.getChildCount(); i++) {
440 ((ViewGroup) group.getChildAt(i)).removeAllViews();
441 }
442 }
Jason Monk197f4db2016-08-26 13:28:20 -0400443
444 @Override
Jason Monk20ff3f92017-01-09 15:13:23 -0500445 public void onPluginConnected(NavBarButtonProvider plugin, Context context) {
Jason Monk197f4db2016-08-26 13:28:20 -0400446 mPlugins.add(plugin);
447 clearViews();
448 inflateLayout(mCurrentLayout);
449 }
450
451 @Override
452 public void onPluginDisconnected(NavBarButtonProvider plugin) {
453 mPlugins.remove(plugin);
454 clearViews();
455 inflateLayout(mCurrentLayout);
456 }
Jason Monka2081822016-01-18 14:41:03 -0500457}