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