Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.statusbar.phone; |
| 16 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 17 | import android.annotation.Nullable; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 18 | import android.content.Context; |
| 19 | import android.content.res.Configuration; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 20 | import android.util.AttributeSet; |
| 21 | import android.util.SparseArray; |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 22 | import android.view.Display; |
| 23 | import android.view.Display.Mode; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 27 | import android.view.WindowManager; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 28 | import android.widget.FrameLayout; |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 29 | import android.widget.LinearLayout; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 30 | import android.widget.Space; |
Annie Chin | 1ea4935 | 2016-05-27 15:23:35 -0700 | [diff] [blame] | 31 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 32 | import com.android.systemui.R; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 33 | import com.android.systemui.plugins.PluginListener; |
| 34 | import com.android.systemui.plugins.PluginManager; |
| 35 | import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.KeyButtonView; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 37 | import com.android.systemui.tuner.TunerService; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 38 | import com.android.systemui.tuner.TunerService.Tunable; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 39 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 42 | import java.util.Objects; |
| 43 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 44 | public class NavigationBarInflaterView extends FrameLayout |
| 45 | implements Tunable, PluginListener<NavBarButtonProvider> { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 46 | |
| 47 | private static final String TAG = "NavBarInflater"; |
| 48 | |
| 49 | public static final String NAV_BAR_VIEWS = "sysui_nav_bar"; |
| 50 | |
Jason Monk | 3ebd239 | 2016-01-22 10:01:44 -0500 | [diff] [blame] | 51 | public static final String MENU_IME = "menu_ime"; |
| 52 | public static final String BACK = "back"; |
| 53 | public static final String HOME = "home"; |
| 54 | public static final String RECENT = "recent"; |
| 55 | public static final String NAVSPACE = "space"; |
Jason Monk | 3b58714 | 2016-01-23 16:47:59 -0500 | [diff] [blame] | 56 | public static final String CLIPBOARD = "clipboard"; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 57 | public static final String KEY = "key"; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 58 | |
| 59 | public static final String GRAVITY_SEPARATOR = ";"; |
| 60 | public static final String BUTTON_SEPARATOR = ","; |
| 61 | |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 62 | public static final String SIZE_MOD_START = "["; |
| 63 | public static final String SIZE_MOD_END = "]"; |
| 64 | |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 65 | public static final String KEY_CODE_START = "("; |
| 66 | public static final String KEY_IMAGE_DELIM = ":"; |
| 67 | public static final String KEY_CODE_END = ")"; |
| 68 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 69 | private final List<NavBarButtonProvider> mPlugins = new ArrayList<>(); |
| 70 | |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 71 | protected LayoutInflater mLayoutInflater; |
| 72 | protected LayoutInflater mLandscapeInflater; |
| 73 | private int mDensity; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 74 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 75 | protected FrameLayout mRot0; |
| 76 | protected FrameLayout mRot90; |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 77 | private boolean isRot0Landscape; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 78 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 79 | private SparseArray<ButtonDispatcher> mButtonDispatchers; |
| 80 | private String mCurrentLayout; |
| 81 | |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 82 | private View mLastPortrait; |
| 83 | private View mLastLandscape; |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 84 | |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 85 | private boolean mAlternativeOrder; |
| 86 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 87 | public NavigationBarInflaterView(Context context, AttributeSet attrs) { |
| 88 | super(context, attrs); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 89 | mDensity = context.getResources().getConfiguration().densityDpi; |
| 90 | createInflaters(); |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 91 | Display display = ((WindowManager) |
| 92 | context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); |
| 93 | Mode displayMode = display.getMode(); |
| 94 | isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight(); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private void createInflaters() { |
| 98 | mLayoutInflater = LayoutInflater.from(mContext); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 99 | Configuration landscape = new Configuration(); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 100 | landscape.setTo(mContext.getResources().getConfiguration()); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 101 | landscape.orientation = Configuration.ORIENTATION_LANDSCAPE; |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 102 | mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected void onConfigurationChanged(Configuration newConfig) { |
| 107 | super.onConfigurationChanged(newConfig); |
| 108 | if (mDensity != newConfig.densityDpi) { |
| 109 | mDensity = newConfig.densityDpi; |
| 110 | createInflaters(); |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 111 | inflateChildren(); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 112 | clearViews(); |
| 113 | inflateLayout(mCurrentLayout); |
| 114 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void onFinishInflate() { |
| 119 | super.onFinishInflate(); |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 120 | inflateChildren(); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 121 | clearViews(); |
| 122 | inflateLayout(getDefaultLayout()); |
| 123 | } |
| 124 | |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 125 | private void inflateChildren() { |
| 126 | removeAllViews(); |
| 127 | mRot0 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, this, false); |
| 128 | mRot0.setId(R.id.rot0); |
| 129 | addView(mRot0); |
| 130 | mRot90 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_rot90, this, |
| 131 | false); |
| 132 | mRot90.setId(R.id.rot90); |
| 133 | addView(mRot90); |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 134 | updateAlternativeOrder(); |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 135 | if (getParent() instanceof NavigationBarView) { |
| 136 | ((NavigationBarView) getParent()).updateRotatedViews(); |
| 137 | } |
| 138 | } |
| 139 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 140 | protected String getDefaultLayout() { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 141 | return mContext.getString(R.string.config_navBarLayout); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | protected void onAttachedToWindow() { |
| 146 | super.onAttachedToWindow(); |
| 147 | TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 148 | PluginManager.getInstance(getContext()).addPluginListener(NavBarButtonProvider.ACTION, this, |
| 149 | NavBarButtonProvider.VERSION, true /* Allow multiple */); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | protected void onDetachedFromWindow() { |
| 154 | TunerService.get(getContext()).removeTunable(this); |
| 155 | super.onDetachedFromWindow(); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void onTuningChanged(String key, String newValue) { |
| 160 | if (NAV_BAR_VIEWS.equals(key)) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 161 | if (!Objects.equals(mCurrentLayout, newValue)) { |
| 162 | clearViews(); |
| 163 | inflateLayout(newValue); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) { |
| 169 | mButtonDispatchers = buttonDisatchers; |
| 170 | for (int i = 0; i < buttonDisatchers.size(); i++) { |
| 171 | initiallyFill(buttonDisatchers.valueAt(i)); |
| 172 | } |
| 173 | } |
| 174 | |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 175 | public void setAlternativeOrder(boolean alternativeOrder) { |
| 176 | if (alternativeOrder != mAlternativeOrder) { |
| 177 | mAlternativeOrder = alternativeOrder; |
| 178 | updateAlternativeOrder(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private void updateAlternativeOrder() { |
| 183 | updateAlternativeOrder(mRot0.findViewById(R.id.ends_group)); |
| 184 | updateAlternativeOrder(mRot0.findViewById(R.id.center_group)); |
| 185 | updateAlternativeOrder(mRot90.findViewById(R.id.ends_group)); |
| 186 | updateAlternativeOrder(mRot90.findViewById(R.id.center_group)); |
| 187 | } |
| 188 | |
| 189 | private void updateAlternativeOrder(View v) { |
| 190 | if (v instanceof ReverseLinearLayout) { |
| 191 | ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder); |
| 192 | } |
| 193 | } |
| 194 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 195 | private void initiallyFill(ButtonDispatcher buttonDispatcher) { |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 196 | addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 197 | addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group)); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 198 | addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 199 | addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) { |
| 203 | for (int i = 0; i < parent.getChildCount(); i++) { |
| 204 | // Need to manually search for each id, just in case each group has more than one |
| 205 | // of a single id. It probably mostly a waste of time, but shouldn't take long |
| 206 | // and will only happen once. |
| 207 | if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) { |
| 208 | buttonDispatcher.addView(parent.getChildAt(i)); |
| 209 | } else if (parent.getChildAt(i) instanceof ViewGroup) { |
| 210 | addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i)); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 215 | protected void inflateLayout(String newLayout) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 216 | mCurrentLayout = newLayout; |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 217 | if (newLayout == null) { |
| 218 | newLayout = getDefaultLayout(); |
| 219 | } |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 220 | String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 221 | String[] start = sets[0].split(BUTTON_SEPARATOR); |
| 222 | String[] center = sets[1].split(BUTTON_SEPARATOR); |
| 223 | String[] end = sets[2].split(BUTTON_SEPARATOR); |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 224 | // Inflate these in start to end order or accessibility traversal will be messed up. |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 225 | inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape); |
| 226 | inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 227 | |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 228 | inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group), isRot0Landscape); |
| 229 | inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group), !isRot0Landscape); |
Xiaohui Chen | 0691703 | 2016-01-26 11:20:39 -0800 | [diff] [blame] | 230 | |
| 231 | addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group)); |
| 232 | addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group)); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 233 | |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 234 | inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape); |
| 235 | inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 236 | } |
| 237 | |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 238 | private void addGravitySpacer(LinearLayout layout) { |
| 239 | layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 240 | } |
| 241 | |
Jason Monk | 5332e59 | 2016-02-15 15:16:57 -0500 | [diff] [blame] | 242 | private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 243 | for (int i = 0; i < buttons.length; i++) { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 244 | inflateButton(buttons[i], parent, landscape); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 248 | private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) { |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 249 | if (layoutParams instanceof LinearLayout.LayoutParams) { |
| 250 | return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height, |
| 251 | ((LinearLayout.LayoutParams) layoutParams).weight); |
| 252 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 253 | return new LayoutParams(layoutParams.width, layoutParams.height); |
| 254 | } |
| 255 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 256 | @Nullable |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 257 | protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 258 | LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater; |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 259 | float size = extractSize(buttonSpec); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 260 | View v = createView(buttonSpec, parent, inflater, landscape); |
| 261 | if (v == null) return null; |
| 262 | |
| 263 | if (size != 0) { |
| 264 | ViewGroup.LayoutParams params = v.getLayoutParams(); |
| 265 | params.width = (int) (params.width * size); |
| 266 | } |
| 267 | parent.addView(v); |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 268 | addToDispatchers(v); |
| 269 | View lastView = landscape ? mLastLandscape : mLastPortrait; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 270 | if (lastView != null) { |
| 271 | v.setAccessibilityTraversalAfter(lastView.getId()); |
| 272 | } |
| 273 | if (landscape) { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 274 | mLastLandscape = v; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 275 | } else { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 276 | mLastPortrait = v; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 277 | } |
| 278 | return v; |
| 279 | } |
| 280 | |
| 281 | private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater, |
| 282 | boolean landscape) { |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 283 | View v = null; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 284 | String button = extractButton(buttonSpec); |
| 285 | // Let plugins go first so they can override a standard view if they want. |
| 286 | for (NavBarButtonProvider provider : mPlugins) { |
| 287 | v = provider.createView(buttonSpec, parent); |
| 288 | if (v != null) return v; |
| 289 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 290 | if (HOME.equals(button)) { |
Annie Chin | b6ab24f | 2016-06-01 17:46:27 -0700 | [diff] [blame] | 291 | v = inflater.inflate(R.layout.home, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 292 | } else if (BACK.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 293 | v = inflater.inflate(R.layout.back, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 294 | } else if (RECENT.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 295 | v = inflater.inflate(R.layout.recent_apps, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 296 | } else if (MENU_IME.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 297 | v = inflater.inflate(R.layout.menu_ime, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 298 | } else if (NAVSPACE.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 299 | v = inflater.inflate(R.layout.nav_key_space, parent, false); |
Jason Monk | 3b58714 | 2016-01-23 16:47:59 -0500 | [diff] [blame] | 300 | } else if (CLIPBOARD.equals(button)) { |
| 301 | v = inflater.inflate(R.layout.clipboard, parent, false); |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 302 | } else if (button.startsWith(KEY)) { |
| 303 | String uri = extractImage(button); |
| 304 | int code = extractKeycode(button); |
| 305 | v = inflater.inflate(R.layout.custom_key, parent, false); |
| 306 | ((KeyButtonView) v).setCode(code); |
| 307 | if (uri != null) { |
| 308 | ((KeyButtonView) v).loadAsync(uri); |
| 309 | } |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 310 | } |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 311 | return v; |
| 312 | } |
| 313 | |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 314 | public static String extractImage(String buttonSpec) { |
| 315 | if (!buttonSpec.contains(KEY_IMAGE_DELIM)) { |
| 316 | return null; |
| 317 | } |
| 318 | final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM); |
| 319 | String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END)); |
| 320 | return subStr; |
| 321 | } |
| 322 | |
| 323 | public static int extractKeycode(String buttonSpec) { |
| 324 | if (!buttonSpec.contains(KEY_CODE_START)) { |
| 325 | return 1; |
| 326 | } |
| 327 | final int start = buttonSpec.indexOf(KEY_CODE_START); |
| 328 | String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM)); |
| 329 | return Integer.parseInt(subStr); |
| 330 | } |
| 331 | |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 332 | public static float extractSize(String buttonSpec) { |
| 333 | if (!buttonSpec.contains(SIZE_MOD_START)) { |
| 334 | return 1; |
| 335 | } |
| 336 | final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START); |
| 337 | String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END)); |
| 338 | return Float.parseFloat(sizeStr); |
| 339 | } |
| 340 | |
| 341 | public static String extractButton(String buttonSpec) { |
| 342 | if (!buttonSpec.contains(SIZE_MOD_START)) { |
| 343 | return buttonSpec; |
| 344 | } |
| 345 | return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START)); |
| 346 | } |
| 347 | |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 348 | private void addToDispatchers(View v) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 349 | if (mButtonDispatchers != null) { |
| 350 | final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId()); |
| 351 | if (indexOfKey >= 0) { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 352 | mButtonDispatchers.valueAt(indexOfKey).addView(v); |
Keisuke Kuroyanagi | 47bd733 | 2016-05-06 10:49:54 -0700 | [diff] [blame] | 353 | } else if (v instanceof ViewGroup) { |
| 354 | final ViewGroup viewGroup = (ViewGroup)v; |
| 355 | final int N = viewGroup.getChildCount(); |
| 356 | for (int i = 0; i < N; i++) { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame^] | 357 | addToDispatchers(viewGroup.getChildAt(i)); |
Keisuke Kuroyanagi | 47bd733 | 2016-05-06 10:49:54 -0700 | [diff] [blame] | 358 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 359 | } |
| 360 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 361 | } |
| 362 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 363 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 364 | |
| 365 | private void clearViews() { |
| 366 | if (mButtonDispatchers != null) { |
| 367 | for (int i = 0; i < mButtonDispatchers.size(); i++) { |
| 368 | mButtonDispatchers.valueAt(i).clear(); |
| 369 | } |
| 370 | } |
| 371 | clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 372 | clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | private void clearAllChildren(ViewGroup group) { |
| 376 | for (int i = 0; i < group.getChildCount(); i++) { |
| 377 | ((ViewGroup) group.getChildAt(i)).removeAllViews(); |
| 378 | } |
| 379 | } |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 380 | |
| 381 | @Override |
| 382 | public void onPluginConnected(NavBarButtonProvider plugin) { |
| 383 | mPlugins.add(plugin); |
| 384 | clearViews(); |
| 385 | inflateLayout(mCurrentLayout); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void onPluginDisconnected(NavBarButtonProvider plugin) { |
| 390 | mPlugins.remove(plugin); |
| 391 | clearViews(); |
| 392 | inflateLayout(mCurrentLayout); |
| 393 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 394 | } |