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