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 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 17 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 18 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Configuration; |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 22 | import android.graphics.drawable.Icon; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 24 | import android.util.Log; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 25 | import android.util.SparseArray; |
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; |
| 30 | import android.widget.FrameLayout; |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 31 | import android.widget.LinearLayout; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 32 | import android.widget.Space; |
Annie Chin | 1ea4935 | 2016-05-27 15:23:35 -0700 | [diff] [blame] | 33 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 34 | import com.android.internal.annotations.VisibleForTesting; |
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; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 38 | import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 39 | import com.android.systemui.recents.OverviewProxyService; |
| 40 | import com.android.systemui.shared.plugins.PluginManager; |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 41 | import com.android.systemui.statusbar.phone.ReverseLinearLayout.ReverseRelativeLayout; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 42 | import com.android.systemui.statusbar.policy.KeyButtonView; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 43 | import com.android.systemui.tuner.TunerService; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 44 | import com.android.systemui.tuner.TunerService.Tunable; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 45 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 46 | import java.util.ArrayList; |
| 47 | import java.util.List; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 48 | import java.util.Objects; |
| 49 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 50 | public class NavigationBarInflaterView extends FrameLayout |
| 51 | implements Tunable, PluginListener<NavBarButtonProvider> { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 52 | |
| 53 | private static final String TAG = "NavBarInflater"; |
| 54 | |
| 55 | public static final String NAV_BAR_VIEWS = "sysui_nav_bar"; |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 56 | public static final String NAV_BAR_LEFT = "sysui_nav_bar_left"; |
| 57 | public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right"; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 58 | |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 59 | public static final String MENU_IME_ROTATE = "menu_ime"; |
Jason Monk | 3ebd239 | 2016-01-22 10:01:44 -0500 | [diff] [blame] | 60 | public static final String BACK = "back"; |
| 61 | public static final String HOME = "home"; |
| 62 | public static final String RECENT = "recent"; |
| 63 | public static final String NAVSPACE = "space"; |
Jason Monk | 3b58714 | 2016-01-23 16:47:59 -0500 | [diff] [blame] | 64 | public static final String CLIPBOARD = "clipboard"; |
Matthew Ng | b55c02c | 2019-02-15 16:20:31 -0800 | [diff] [blame] | 65 | public static final String HOME_HANDLE = "home_handle"; |
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"; |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 69 | public static final String CONTEXTUAL = "contextual"; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 70 | |
| 71 | public static final String GRAVITY_SEPARATOR = ";"; |
| 72 | public static final String BUTTON_SEPARATOR = ","; |
| 73 | |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 74 | public static final String SIZE_MOD_START = "["; |
| 75 | public static final String SIZE_MOD_END = "]"; |
| 76 | |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 77 | public static final String KEY_CODE_START = "("; |
| 78 | public static final String KEY_IMAGE_DELIM = ":"; |
| 79 | public static final String KEY_CODE_END = ")"; |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 80 | private static final String WEIGHT_SUFFIX = "W"; |
| 81 | private static final String WEIGHT_CENTERED_SUFFIX = "WC"; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 82 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 83 | private final List<NavBarButtonProvider> mPlugins = new ArrayList<>(); |
| 84 | |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 85 | protected LayoutInflater mLayoutInflater; |
| 86 | protected LayoutInflater mLandscapeInflater; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 87 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 88 | protected FrameLayout mHorizontal; |
| 89 | protected FrameLayout mVertical; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 90 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 91 | @VisibleForTesting |
| 92 | SparseArray<ButtonDispatcher> mButtonDispatchers; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 93 | private String mCurrentLayout; |
| 94 | |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 95 | private View mLastPortrait; |
| 96 | private View mLastLandscape; |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 97 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 98 | private boolean mIsVertical; |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 99 | private boolean mAlternativeOrder; |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 100 | private boolean mUsingCustomLayout; |
| 101 | |
| 102 | private OverviewProxyService mOverviewProxyService; |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 103 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 104 | public NavigationBarInflaterView(Context context, AttributeSet attrs) { |
| 105 | super(context, attrs); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 106 | createInflaters(); |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 107 | mOverviewProxyService = Dependency.get(OverviewProxyService.class); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 110 | @VisibleForTesting |
| 111 | void createInflaters() { |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 112 | mLayoutInflater = LayoutInflater.from(mContext); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 113 | Configuration landscape = new Configuration(); |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 114 | landscape.setTo(mContext.getResources().getConfiguration()); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 115 | landscape.orientation = Configuration.ORIENTATION_LANDSCAPE; |
Jason Monk | 67a1cce | 2016-02-05 13:31:03 -0500 | [diff] [blame] | 116 | mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape)); |
| 117 | } |
| 118 | |
| 119 | @Override |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 120 | protected void onFinishInflate() { |
| 121 | super.onFinishInflate(); |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 122 | inflateChildren(); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 123 | clearViews(); |
| 124 | inflateLayout(getDefaultLayout()); |
| 125 | } |
| 126 | |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 127 | private void inflateChildren() { |
| 128 | removeAllViews(); |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 129 | mHorizontal = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, |
| 130 | this /* root */, false /* attachToRoot */); |
| 131 | addView(mHorizontal); |
| 132 | mVertical = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_vertical, |
| 133 | this /* root */, false /* attachToRoot */); |
| 134 | addView(mVertical); |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 135 | updateAlternativeOrder(); |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 138 | protected String getDefaultLayout() { |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 139 | final int defaultResource = mOverviewProxyService.shouldShowSwipeUpUI() |
| 140 | ? R.string.config_navBarLayoutQuickstep |
| 141 | : R.string.config_navBarLayout; |
| 142 | return mContext.getString(defaultResource); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected void onAttachedToWindow() { |
| 147 | super.onAttachedToWindow(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 148 | Dependency.get(TunerService.class).addTunable(this, NAV_BAR_VIEWS, NAV_BAR_LEFT, |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 149 | NAV_BAR_RIGHT); |
Jason Monk | 5bec68f | 2017-02-08 20:45:10 -0800 | [diff] [blame] | 150 | Dependency.get(PluginManager.class).addPluginListener(this, |
| 151 | NavBarButtonProvider.class, true /* Allow multiple */); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void onDetachedFromWindow() { |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 156 | Dependency.get(TunerService.class).removeTunable(this); |
| 157 | Dependency.get(PluginManager.class).removePluginListener(this); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 158 | super.onDetachedFromWindow(); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onTuningChanged(String key, String newValue) { |
| 163 | if (NAV_BAR_VIEWS.equals(key)) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 164 | if (!Objects.equals(mCurrentLayout, newValue)) { |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 165 | mUsingCustomLayout = newValue != null; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 166 | clearViews(); |
| 167 | inflateLayout(newValue); |
| 168 | } |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 169 | } else if (NAV_BAR_LEFT.equals(key) || NAV_BAR_RIGHT.equals(key)) { |
| 170 | clearViews(); |
| 171 | inflateLayout(mCurrentLayout); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 175 | public void onLikelyDefaultLayoutChange() { |
| 176 | // Don't override custom layouts |
| 177 | if (mUsingCustomLayout) return; |
| 178 | |
| 179 | // Reevaluate new layout |
| 180 | final String newValue = getDefaultLayout(); |
| 181 | if (!Objects.equals(mCurrentLayout, newValue)) { |
| 182 | clearViews(); |
| 183 | inflateLayout(newValue); |
| 184 | } |
| 185 | } |
| 186 | |
Matthew Ng | 6607c3d | 2018-04-26 15:23:27 -0700 | [diff] [blame] | 187 | public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) { |
| 188 | mButtonDispatchers = buttonDispatchers; |
| 189 | for (int i = 0; i < buttonDispatchers.size(); i++) { |
| 190 | initiallyFill(buttonDispatchers.valueAt(i)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 194 | void updateButtonDispatchersCurrentView() { |
Matthew Ng | 1efc9c3 | 2018-04-11 18:13:56 -0700 | [diff] [blame] | 195 | if (mButtonDispatchers != null) { |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 196 | View view = mIsVertical ? mVertical : mHorizontal; |
Matthew Ng | 1efc9c3 | 2018-04-11 18:13:56 -0700 | [diff] [blame] | 197 | for (int i = 0; i < mButtonDispatchers.size(); i++) { |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 198 | final ButtonDispatcher dispatcher = mButtonDispatchers.valueAt(i); |
| 199 | dispatcher.setCurrentView(view); |
Matthew Ng | 1efc9c3 | 2018-04-11 18:13:56 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 204 | void setVertical(boolean vertical) { |
| 205 | if (vertical != mIsVertical) { |
| 206 | mIsVertical = vertical; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void setAlternativeOrder(boolean alternativeOrder) { |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 211 | if (alternativeOrder != mAlternativeOrder) { |
| 212 | mAlternativeOrder = alternativeOrder; |
| 213 | updateAlternativeOrder(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private void updateAlternativeOrder() { |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 218 | updateAlternativeOrder(mHorizontal.findViewById(R.id.ends_group)); |
| 219 | updateAlternativeOrder(mHorizontal.findViewById(R.id.center_group)); |
| 220 | updateAlternativeOrder(mVertical.findViewById(R.id.ends_group)); |
| 221 | updateAlternativeOrder(mVertical.findViewById(R.id.center_group)); |
Adrian Roos | db12b15 | 2016-07-12 15:38:55 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | private void updateAlternativeOrder(View v) { |
| 225 | if (v instanceof ReverseLinearLayout) { |
| 226 | ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder); |
| 227 | } |
| 228 | } |
| 229 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 230 | private void initiallyFill(ButtonDispatcher buttonDispatcher) { |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 231 | addAll(buttonDispatcher, mHorizontal.findViewById(R.id.ends_group)); |
| 232 | addAll(buttonDispatcher, mHorizontal.findViewById(R.id.center_group)); |
| 233 | addAll(buttonDispatcher, mVertical.findViewById(R.id.ends_group)); |
| 234 | addAll(buttonDispatcher, mVertical.findViewById(R.id.center_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) { |
| 238 | for (int i = 0; i < parent.getChildCount(); i++) { |
| 239 | // Need to manually search for each id, just in case each group has more than one |
| 240 | // of a single id. It probably mostly a waste of time, but shouldn't take long |
| 241 | // and will only happen once. |
| 242 | if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) { |
| 243 | buttonDispatcher.addView(parent.getChildAt(i)); |
Matthew Ng | 6607c3d | 2018-04-26 15:23:27 -0700 | [diff] [blame] | 244 | } |
| 245 | if (parent.getChildAt(i) instanceof ViewGroup) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 246 | addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i)); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 251 | protected void inflateLayout(String newLayout) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 252 | mCurrentLayout = newLayout; |
Jason Monk | 9a6552d | 2016-05-20 11:21:59 -0400 | [diff] [blame] | 253 | if (newLayout == null) { |
| 254 | newLayout = getDefaultLayout(); |
| 255 | } |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 256 | String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3); |
Mikael Magnusson | c71ab60 | 2018-01-29 17:56:10 +0100 | [diff] [blame] | 257 | if (sets.length != 3) { |
| 258 | Log.d(TAG, "Invalid layout."); |
| 259 | newLayout = getDefaultLayout(); |
| 260 | sets = newLayout.split(GRAVITY_SEPARATOR, 3); |
| 261 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 262 | String[] start = sets[0].split(BUTTON_SEPARATOR); |
| 263 | String[] center = sets[1].split(BUTTON_SEPARATOR); |
| 264 | String[] end = sets[2].split(BUTTON_SEPARATOR); |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 265 | // Inflate these in start to end order or accessibility traversal will be messed up. |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 266 | inflateButtons(start, mHorizontal.findViewById(R.id.ends_group), |
| 267 | false /* landscape */, true /* start */); |
| 268 | inflateButtons(start, mVertical.findViewById(R.id.ends_group), |
| 269 | true /* landscape */, true /* start */); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 270 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 271 | inflateButtons(center, mHorizontal.findViewById(R.id.center_group), |
| 272 | false /* landscape */, false /* start */); |
| 273 | inflateButtons(center, mVertical.findViewById(R.id.center_group), |
| 274 | true /* landscape */, false /* start */); |
Xiaohui Chen | 0691703 | 2016-01-26 11:20:39 -0800 | [diff] [blame] | 275 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 276 | addGravitySpacer(mHorizontal.findViewById(R.id.ends_group)); |
| 277 | addGravitySpacer(mVertical.findViewById(R.id.ends_group)); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 278 | |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 279 | inflateButtons(end, mHorizontal.findViewById(R.id.ends_group), |
| 280 | false /* landscape */, false /* start */); |
| 281 | inflateButtons(end, mVertical.findViewById(R.id.ends_group), |
| 282 | true /* landscape */, false /* start */); |
Matthew Ng | 1efc9c3 | 2018-04-11 18:13:56 -0700 | [diff] [blame] | 283 | |
| 284 | updateButtonDispatchersCurrentView(); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 287 | private void addGravitySpacer(LinearLayout layout) { |
| 288 | layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 291 | private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape, |
| 292 | boolean start) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 293 | for (int i = 0; i < buttons.length; i++) { |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 294 | inflateButton(buttons[i], parent, landscape, start); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 298 | private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) { |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 299 | if (layoutParams instanceof LinearLayout.LayoutParams) { |
| 300 | return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height, |
| 301 | ((LinearLayout.LayoutParams) layoutParams).weight); |
| 302 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 303 | return new LayoutParams(layoutParams.width, layoutParams.height); |
| 304 | } |
| 305 | |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 306 | @Nullable |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 307 | protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape, |
| 308 | boolean start) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 309 | LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater; |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 310 | View v = createView(buttonSpec, parent, inflater); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 311 | if (v == null) return null; |
| 312 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 313 | v = applySize(v, buttonSpec, landscape, start); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 314 | parent.addView(v); |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 315 | addToDispatchers(v); |
| 316 | View lastView = landscape ? mLastLandscape : mLastPortrait; |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 317 | View accessibilityView = v; |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 318 | if (v instanceof ReverseRelativeLayout) { |
| 319 | accessibilityView = ((ReverseRelativeLayout) v).getChildAt(0); |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 320 | } |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 321 | if (lastView != null) { |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 322 | accessibilityView.setAccessibilityTraversalAfter(lastView.getId()); |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 323 | } |
| 324 | if (landscape) { |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 325 | mLastLandscape = accessibilityView; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 326 | } else { |
Jason Monk | c7ba543 | 2017-07-06 15:12:21 -0400 | [diff] [blame] | 327 | mLastPortrait = accessibilityView; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 328 | } |
| 329 | return v; |
| 330 | } |
| 331 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 332 | private View applySize(View v, String buttonSpec, boolean landscape, boolean start) { |
| 333 | String sizeStr = extractSize(buttonSpec); |
| 334 | if (sizeStr == null) return v; |
| 335 | |
| 336 | if (sizeStr.contains(WEIGHT_SUFFIX)) { |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 337 | // To support gravity, wrap in RelativeLayout and apply gravity to it. |
| 338 | // Children wanting to use gravity must be smaller then the frame. |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 339 | float weight = Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX))); |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 340 | ReverseRelativeLayout frame = new ReverseRelativeLayout(mContext); |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 341 | LayoutParams childParams = new LayoutParams(v.getLayoutParams()); |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 342 | |
| 343 | // Compute gravity to apply |
| 344 | int gravity = (landscape) ? (start ? Gravity.TOP : Gravity.BOTTOM) |
| 345 | : (start ? Gravity.START : Gravity.END); |
| 346 | if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) gravity = Gravity.CENTER; |
| 347 | |
| 348 | // Set default gravity, flipped if needed in reversed layouts (270 RTL and 90 LTR) |
| 349 | frame.setDefaultGravity(gravity); |
| 350 | frame.setGravity(gravity); // Apply gravity to root |
| 351 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 352 | frame.addView(v, childParams); |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 353 | |
| 354 | // Use weighting to set the width of the frame |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 355 | frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight)); |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 356 | |
| 357 | // Ensure ripples can be drawn outside bounds |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 358 | frame.setClipChildren(false); |
| 359 | frame.setClipToPadding(false); |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 360 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 361 | return frame; |
| 362 | } |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 363 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 364 | float size = Float.parseFloat(sizeStr); |
| 365 | ViewGroup.LayoutParams params = v.getLayoutParams(); |
| 366 | params.width = (int) (params.width * size); |
| 367 | return v; |
| 368 | } |
| 369 | |
| 370 | private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) { |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 371 | View v = null; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 372 | String button = extractButton(buttonSpec); |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 373 | if (LEFT.equals(button)) { |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 374 | String s = Dependency.get(TunerService.class).getValue(NAV_BAR_LEFT, NAVSPACE); |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 375 | button = extractButton(s); |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 376 | } else if (RIGHT.equals(button)) { |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 377 | 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] | 378 | button = extractButton(s); |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 379 | } |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 380 | // Let plugins go first so they can override a standard view if they want. |
| 381 | for (NavBarButtonProvider provider : mPlugins) { |
| 382 | v = provider.createView(buttonSpec, parent); |
| 383 | if (v != null) return v; |
| 384 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 385 | if (HOME.equals(button)) { |
Annie Chin | b6ab24f | 2016-06-01 17:46:27 -0700 | [diff] [blame] | 386 | v = inflater.inflate(R.layout.home, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 387 | } else if (BACK.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 388 | v = inflater.inflate(R.layout.back, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 389 | } else if (RECENT.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 390 | v = inflater.inflate(R.layout.recent_apps, parent, false); |
Mike Digman | a48cf19 | 2018-02-12 17:52:48 -0800 | [diff] [blame] | 391 | } else if (MENU_IME_ROTATE.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 392 | v = inflater.inflate(R.layout.menu_ime, parent, false); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 393 | } else if (NAVSPACE.equals(button)) { |
Xiaohui Chen | 5481600 | 2016-01-25 11:11:11 -0800 | [diff] [blame] | 394 | v = inflater.inflate(R.layout.nav_key_space, parent, false); |
Jason Monk | 3b58714 | 2016-01-23 16:47:59 -0500 | [diff] [blame] | 395 | } else if (CLIPBOARD.equals(button)) { |
| 396 | v = inflater.inflate(R.layout.clipboard, parent, false); |
Mike Digman | f77fb91 | 2018-05-21 11:21:30 -0700 | [diff] [blame] | 397 | } else if (CONTEXTUAL.equals(button)) { |
| 398 | v = inflater.inflate(R.layout.contextual, parent, false); |
Matthew Ng | b55c02c | 2019-02-15 16:20:31 -0800 | [diff] [blame] | 399 | } else if (HOME_HANDLE.equals(button)) { |
| 400 | v = inflater.inflate(R.layout.home_handle, parent, false); |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 401 | } else if (button.startsWith(KEY)) { |
| 402 | String uri = extractImage(button); |
| 403 | int code = extractKeycode(button); |
| 404 | v = inflater.inflate(R.layout.custom_key, parent, false); |
| 405 | ((KeyButtonView) v).setCode(code); |
| 406 | if (uri != null) { |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 407 | if (uri.contains(":")) { |
| 408 | ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri)); |
| 409 | } else if (uri.contains("/")) { |
| 410 | int index = uri.indexOf('/'); |
| 411 | String pkg = uri.substring(0, index); |
| 412 | int id = Integer.parseInt(uri.substring(index + 1)); |
| 413 | ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id)); |
| 414 | } |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 415 | } |
Jason Monk | c62cf80 | 2016-05-10 11:02:24 -0400 | [diff] [blame] | 416 | } |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame] | 417 | return v; |
| 418 | } |
| 419 | |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 420 | public static String extractImage(String buttonSpec) { |
| 421 | if (!buttonSpec.contains(KEY_IMAGE_DELIM)) { |
| 422 | return null; |
| 423 | } |
| 424 | final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM); |
| 425 | String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END)); |
| 426 | return subStr; |
| 427 | } |
| 428 | |
| 429 | public static int extractKeycode(String buttonSpec) { |
| 430 | if (!buttonSpec.contains(KEY_CODE_START)) { |
| 431 | return 1; |
| 432 | } |
| 433 | final int start = buttonSpec.indexOf(KEY_CODE_START); |
| 434 | String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM)); |
| 435 | return Integer.parseInt(subStr); |
| 436 | } |
| 437 | |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 438 | public static String extractSize(String buttonSpec) { |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 439 | if (!buttonSpec.contains(SIZE_MOD_START)) { |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 440 | return null; |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 441 | } |
| 442 | final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START); |
Jason Monk | 7f6a2ab | 2017-04-21 13:48:19 -0400 | [diff] [blame] | 443 | return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END)); |
Jason Monk | 46a196e | 2016-01-23 15:28:10 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | public static String extractButton(String buttonSpec) { |
| 447 | if (!buttonSpec.contains(SIZE_MOD_START)) { |
| 448 | return buttonSpec; |
| 449 | } |
| 450 | return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START)); |
| 451 | } |
| 452 | |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 453 | private void addToDispatchers(View v) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 454 | if (mButtonDispatchers != null) { |
| 455 | final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId()); |
| 456 | if (indexOfKey >= 0) { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 457 | mButtonDispatchers.valueAt(indexOfKey).addView(v); |
Matthew Ng | 6607c3d | 2018-04-26 15:23:27 -0700 | [diff] [blame] | 458 | } |
| 459 | if (v instanceof ViewGroup) { |
Keisuke Kuroyanagi | 47bd733 | 2016-05-06 10:49:54 -0700 | [diff] [blame] | 460 | final ViewGroup viewGroup = (ViewGroup)v; |
| 461 | final int N = viewGroup.getChildCount(); |
| 462 | for (int i = 0; i < N; i++) { |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 463 | addToDispatchers(viewGroup.getChildAt(i)); |
Keisuke Kuroyanagi | 47bd733 | 2016-05-06 10:49:54 -0700 | [diff] [blame] | 464 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 465 | } |
| 466 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 467 | } |
| 468 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 469 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 470 | |
| 471 | private void clearViews() { |
| 472 | if (mButtonDispatchers != null) { |
| 473 | for (int i = 0; i < mButtonDispatchers.size(); i++) { |
| 474 | mButtonDispatchers.valueAt(i).clear(); |
| 475 | } |
| 476 | } |
Charles Chen | aa94d95b | 2019-02-19 14:18:19 +0800 | [diff] [blame] | 477 | clearAllChildren(mHorizontal.findViewById(R.id.nav_buttons)); |
| 478 | clearAllChildren(mVertical.findViewById(R.id.nav_buttons)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | private void clearAllChildren(ViewGroup group) { |
| 482 | for (int i = 0; i < group.getChildCount(); i++) { |
| 483 | ((ViewGroup) group.getChildAt(i)).removeAllViews(); |
| 484 | } |
| 485 | } |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 486 | |
| 487 | @Override |
Jason Monk | 20ff3f9 | 2017-01-09 15:13:23 -0500 | [diff] [blame] | 488 | public void onPluginConnected(NavBarButtonProvider plugin, Context context) { |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 489 | mPlugins.add(plugin); |
| 490 | clearViews(); |
| 491 | inflateLayout(mCurrentLayout); |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public void onPluginDisconnected(NavBarButtonProvider plugin) { |
| 496 | mPlugins.remove(plugin); |
| 497 | clearViews(); |
| 498 | inflateLayout(mCurrentLayout); |
| 499 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 500 | } |