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 | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 20 | import android.graphics.drawable.Icon; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 22 | import android.util.Log; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 23 | import android.util.SparseArray; |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 24 | import android.view.Display; |
| 25 | import android.view.Display.Mode; |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 26 | import android.view.Gravity; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 30 | import android.view.WindowManager; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 32 | import android.widget.LinearLayout; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 33 | import android.widget.Space; |
Annie Chin | 1ea4935 | 2016-05-27 15:23:35 -0700 | [diff] [blame] | 34 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 35 | import com.android.systemui.Dependency; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 36 | import com.android.systemui.R; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 37 | import com.android.systemui.plugins.PluginListener; |
| 38 | import com.android.systemui.plugins.PluginManager; |
| 39 | import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider; |
Jason Monk | 3b3f1f5 | 2017-06-28 10:45:13 -0400 | [diff] [blame] | 40 | import com.android.systemui.statusbar.phone.ReverseLinearLayout.ReverseFrameLayout; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 41 | import com.android.systemui.statusbar.policy.KeyButtonView; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 42 | import com.android.systemui.tuner.TunerService; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 43 | import com.android.systemui.tuner.TunerService.Tunable; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 44 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 47 | import java.util.Objects; |
| 48 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 49 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 50 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 51 | public class NavigationBarInflaterView extends FrameLayout |
| 52 | implements Tunable, PluginListener<NavBarButtonProvider> { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 53 | |
| 54 | private static final String TAG = "NavBarInflater"; |
| 55 | |
| 56 | public static final String NAV_BAR_VIEWS = "sysui_nav_bar"; |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 57 | public static final String NAV_BAR_LEFT = "sysui_nav_bar_left"; |
| 58 | public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right"; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 59 | |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 60 | public static final String MENU_IME_ROTATE = "menu_ime"; |
Jason Monk | 3ebd239 | 2016-01-22 10:01:44 -0500 | [diff] [blame] | 61 | 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 Monk | 3b58714 | 2016-01-23 16:47:59 -0500 | [diff] [blame] | 65 | public static final String CLIPBOARD = "clipboard"; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 66 | public static final String KEY = "key"; |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 67 | public static final String LEFT = "left"; |
| 68 | public static final String RIGHT = "right"; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 69 | |
| 70 | public static final String GRAVITY_SEPARATOR = ";"; |
| 71 | public static final String BUTTON_SEPARATOR = ","; |
| 72 | |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 73 | public static final String SIZE_MOD_START = "["; |
| 74 | public static final String SIZE_MOD_END = "]"; |
| 75 | |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 76 | public static final String KEY_CODE_START = "("; |
| 77 | public static final String KEY_IMAGE_DELIM = ":"; |
| 78 | public static final String KEY_CODE_END = ")"; |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 79 | private static final String WEIGHT_SUFFIX = "W"; |
| 80 | private static final String WEIGHT_CENTERED_SUFFIX = "WC"; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 81 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 82 | private final List<NavBarButtonProvider> mPlugins = new ArrayList<>(); |
| 83 | |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 84 | protected LayoutInflater mLayoutInflater; |
| 85 | protected LayoutInflater mLandscapeInflater; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 86 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 87 | protected FrameLayout mRot0; |
| 88 | protected FrameLayout mRot90; |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 89 | private boolean isRot0Landscape; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 90 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 91 | private SparseArray<ButtonDispatcher> mButtonDispatchers; |
| 92 | private String mCurrentLayout; |
| 93 | |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 94 | private View mLastPortrait; |
| 95 | private View mLastLandscape; |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 96 | |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 97 | private boolean mAlternativeOrder; |
| 98 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 99 | public NavigationBarInflaterView(Context context, AttributeSet attrs) { |
| 100 | super(context, attrs); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 101 | createInflaters(); |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 102 | Display display = ((WindowManager) |
| 103 | context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); |
| 104 | Mode displayMode = display.getMode(); |
| 105 | isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight(); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private void createInflaters() { |
| 109 | mLayoutInflater = LayoutInflater.from(mContext); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 110 | Configuration landscape = new Configuration(); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 111 | landscape.setTo(mContext.getResources().getConfiguration()); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 112 | landscape.orientation = Configuration.ORIENTATION_LANDSCAPE; |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 113 | mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape)); |
| 114 | } |
| 115 | |
| 116 | @Override |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 117 | protected void onFinishInflate() { |
| 118 | super.onFinishInflate(); |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 119 | inflateChildren(); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 120 | clearViews(); |
| 121 | inflateLayout(getDefaultLayout()); |
| 122 | } |
| 123 | |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 124 | 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 Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 133 | updateAlternativeOrder(); |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 136 | protected String getDefaultLayout() { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 137 | return mContext.getString(R.string.config_navBarLayout); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | protected void onAttachedToWindow() { |
| 142 | super.onAttachedToWindow(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 143 | Dependency.get(TunerService.class).addTunable(this, NAV_BAR_VIEWS, NAV_BAR_LEFT, |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 144 | NAV_BAR_RIGHT); |
Jason Monk | 5bec68f | 2017-02-08 20:45:10 -0800 | [diff] [blame] | 145 | Dependency.get(PluginManager.class).addPluginListener(this, |
| 146 | NavBarButtonProvider.class, true /* Allow multiple */); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | protected void onDetachedFromWindow() { |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 151 | Dependency.get(TunerService.class).removeTunable(this); |
| 152 | Dependency.get(PluginManager.class).removePluginListener(this); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 153 | super.onDetachedFromWindow(); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void onTuningChanged(String key, String newValue) { |
| 158 | if (NAV_BAR_VIEWS.equals(key)) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 159 | if (!Objects.equals(mCurrentLayout, newValue)) { |
| 160 | clearViews(); |
| 161 | inflateLayout(newValue); |
| 162 | } |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 163 | } else if (NAV_BAR_LEFT.equals(key) || NAV_BAR_RIGHT.equals(key)) { |
| 164 | clearViews(); |
| 165 | inflateLayout(mCurrentLayout); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 166 | } |
| 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 Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 176 | 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 Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 196 | private void initiallyFill(ButtonDispatcher buttonDispatcher) { |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 197 | addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 198 | addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group)); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 199 | addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 200 | addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 201 | } |
| 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 Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 216 | protected void inflateLayout(String newLayout) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 217 | mCurrentLayout = newLayout; |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 218 | if (newLayout == null) { |
| 219 | newLayout = getDefaultLayout(); |
| 220 | } |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 221 | String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3); |
Mikael Magnusson | c71ab60 | 2018-01-29 17:56:10 +0100 | [diff] [blame] | 222 | if (sets.length != 3) { |
| 223 | Log.d(TAG, "Invalid layout."); |
| 224 | newLayout = getDefaultLayout(); |
| 225 | sets = newLayout.split(GRAVITY_SEPARATOR, 3); |
| 226 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 227 | String[] start = sets[0].split(BUTTON_SEPARATOR); |
| 228 | String[] center = sets[1].split(BUTTON_SEPARATOR); |
| 229 | String[] end = sets[2].split(BUTTON_SEPARATOR); |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 230 | // Inflate these in start to end order or accessibility traversal will be messed up. |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 231 | inflateButtons(start, mRot0.findViewById(R.id.ends_group), isRot0Landscape, true); |
| 232 | inflateButtons(start, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, true); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 233 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 234 | inflateButtons(center, mRot0.findViewById(R.id.center_group), isRot0Landscape, false); |
| 235 | inflateButtons(center, mRot90.findViewById(R.id.center_group), !isRot0Landscape, false); |
Xiaohui Chen | 0691703 | 2016-01-26 11:20:39 -0800 | [diff] [blame] | 236 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 237 | addGravitySpacer(mRot0.findViewById(R.id.ends_group)); |
| 238 | addGravitySpacer(mRot90.findViewById(R.id.ends_group)); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 239 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 240 | inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false); |
| 241 | inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 242 | } |
| 243 | |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 244 | private void addGravitySpacer(LinearLayout layout) { |
| 245 | layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 248 | private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape, |
| 249 | boolean start) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 250 | for (int i = 0; i < buttons.length; i++) { |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 251 | inflateButton(buttons[i], parent, landscape, start); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 255 | private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) { |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 256 | if (layoutParams instanceof LinearLayout.LayoutParams) { |
| 257 | return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height, |
| 258 | ((LinearLayout.LayoutParams) layoutParams).weight); |
| 259 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 260 | return new LayoutParams(layoutParams.width, layoutParams.height); |
| 261 | } |
| 262 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 263 | @Nullable |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 264 | protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape, |
| 265 | boolean start) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 266 | LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater; |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 267 | View v = createView(buttonSpec, parent, inflater); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 268 | if (v == null) return null; |
| 269 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 270 | v = applySize(v, buttonSpec, landscape, start); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 271 | parent.addView(v); |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 272 | addToDispatchers(v); |
| 273 | View lastView = landscape ? mLastLandscape : mLastPortrait; |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 274 | View accessibilityView = v; |
| 275 | if (v instanceof ReverseFrameLayout) { |
| 276 | accessibilityView = ((ReverseFrameLayout) v).getChildAt(0); |
| 277 | } |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 278 | if (lastView != null) { |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 279 | accessibilityView.setAccessibilityTraversalAfter(lastView.getId()); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 280 | } |
| 281 | if (landscape) { |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 282 | mLastLandscape = accessibilityView; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 283 | } else { |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 284 | mLastPortrait = accessibilityView; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 285 | } |
| 286 | return v; |
| 287 | } |
| 288 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 289 | 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 Monk | 3b3f1f5 | 2017-06-28 10:45:13 -0400 | [diff] [blame] | 295 | FrameLayout frame = new ReverseFrameLayout(mContext); |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 296 | 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 Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 316 | View v = null; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 317 | String button = extractButton(buttonSpec); |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 318 | if (LEFT.equals(button)) { |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 319 | String s = Dependency.get(TunerService.class).getValue(NAV_BAR_LEFT, NAVSPACE); |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 320 | button = extractButton(s); |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 321 | } else if (RIGHT.equals(button)) { |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 322 | String s = Dependency.get(TunerService.class).getValue(NAV_BAR_RIGHT, MENU_IME_ROTATE); |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 323 | button = extractButton(s); |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 324 | } |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 325 | // 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 Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 330 | if (HOME.equals(button)) { |
Annie Chin | b6ab24f | 2016-06-01 17:46:27 -0700 | [diff] [blame] | 331 | v = inflater.inflate(R.layout.home, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 332 | } else if (BACK.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 333 | v = inflater.inflate(R.layout.back, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 334 | } else if (RECENT.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 335 | v = inflater.inflate(R.layout.recent_apps, parent, false); |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 336 | } else if (MENU_IME_ROTATE.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 337 | v = inflater.inflate(R.layout.menu_ime, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 338 | } else if (NAVSPACE.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 339 | v = inflater.inflate(R.layout.nav_key_space, parent, false); |
Jason Monk | 3b58714 | 2016-01-23 16:47:59 -0500 | [diff] [blame] | 340 | } else if (CLIPBOARD.equals(button)) { |
| 341 | v = inflater.inflate(R.layout.clipboard, parent, false); |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 342 | } 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 Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 348 | 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 Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 356 | } |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 357 | } |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 358 | return v; |
| 359 | } |
| 360 | |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 361 | 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 Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 379 | public static String extractSize(String buttonSpec) { |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 380 | if (!buttonSpec.contains(SIZE_MOD_START)) { |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 381 | return null; |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 382 | } |
| 383 | final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START); |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 384 | return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END)); |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 385 | } |
| 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 Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 394 | private void addToDispatchers(View v) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 395 | if (mButtonDispatchers != null) { |
| 396 | final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId()); |
| 397 | if (indexOfKey >= 0) { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 398 | mButtonDispatchers.valueAt(indexOfKey).addView(v); |
Keisuke Kuroyanagi | 47bd733 | 2016-05-06 10:49:54 -0700 | [diff] [blame] | 399 | } 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 Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 403 | addToDispatchers(viewGroup.getChildAt(i)); |
Keisuke Kuroyanagi | 47bd733 | 2016-05-06 10:49:54 -0700 | [diff] [blame] | 404 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 405 | } |
| 406 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 409 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 410 | |
| 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 Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 417 | clearAllChildren(mRot0.findViewById(R.id.nav_buttons)); |
| 418 | clearAllChildren(mRot90.findViewById(R.id.nav_buttons)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 419 | } |
| 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 Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 426 | |
| 427 | @Override |
Jason Monk | 20ff3f9 | 2017-01-09 15:13:23 -0500 | [diff] [blame] | 428 | public void onPluginConnected(NavBarButtonProvider plugin, Context context) { |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 429 | 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 Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 440 | } |