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 | |
| 17 | import android.content.Context; |
| 18 | import android.content.res.Configuration; |
| 19 | import android.content.res.Resources; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.util.SparseArray; |
| 22 | import android.view.LayoutInflater; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | import android.widget.FrameLayout; |
| 26 | import android.widget.ImageView; |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame^] | 27 | import android.widget.LinearLayout; |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 28 | import android.widget.Space; |
| 29 | import com.android.systemui.R; |
| 30 | import com.android.systemui.tuner.TunerService; |
| 31 | |
| 32 | import java.util.Objects; |
| 33 | |
| 34 | public class NavigationBarInflaterView extends FrameLayout implements TunerService.Tunable { |
| 35 | |
| 36 | private static final String TAG = "NavBarInflater"; |
| 37 | |
| 38 | public static final String NAV_BAR_VIEWS = "sysui_nav_bar"; |
| 39 | |
| 40 | private static final String MENU_IME = "menu_ime"; |
| 41 | private static final String BACK = "back"; |
| 42 | private static final String HOME = "home"; |
| 43 | private static final String RECENT = "recent"; |
| 44 | private static final String NAVSPACE = "space"; |
| 45 | |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame^] | 46 | private static final String APP_SHELF = "app_shelf"; |
| 47 | |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 48 | public static final String GRAVITY_SEPARATOR = ";"; |
| 49 | public static final String BUTTON_SEPARATOR = ","; |
| 50 | |
| 51 | private final LayoutInflater mLayoutInflater; |
| 52 | private final LayoutInflater mLandscapeInflater; |
| 53 | |
| 54 | private FrameLayout mRot0; |
| 55 | private FrameLayout mRot90; |
| 56 | private SparseArray<ButtonDispatcher> mButtonDispatchers; |
| 57 | private String mCurrentLayout; |
| 58 | |
| 59 | public NavigationBarInflaterView(Context context, AttributeSet attrs) { |
| 60 | super(context, attrs); |
| 61 | mLayoutInflater = LayoutInflater.from(context); |
| 62 | Configuration landscape = new Configuration(); |
| 63 | landscape.setTo(context.getResources().getConfiguration()); |
| 64 | landscape.orientation = Configuration.ORIENTATION_LANDSCAPE; |
| 65 | mLandscapeInflater = LayoutInflater.from(context.createConfigurationContext(landscape)); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void onFinishInflate() { |
| 70 | super.onFinishInflate(); |
| 71 | mRot0 = (FrameLayout) findViewById(R.id.rot0); |
| 72 | mRot90 = (FrameLayout) findViewById(R.id.rot90); |
| 73 | clearViews(); |
| 74 | inflateLayout(getDefaultLayout()); |
| 75 | } |
| 76 | |
| 77 | private String getDefaultLayout() { |
| 78 | return mContext.getString(R.string.config_navBarLayout); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void onAttachedToWindow() { |
| 83 | super.onAttachedToWindow(); |
| 84 | TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | protected void onDetachedFromWindow() { |
| 89 | TunerService.get(getContext()).removeTunable(this); |
| 90 | super.onDetachedFromWindow(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onTuningChanged(String key, String newValue) { |
| 95 | if (NAV_BAR_VIEWS.equals(key)) { |
| 96 | if (newValue == null) { |
| 97 | newValue = getDefaultLayout(); |
| 98 | } |
| 99 | if (!Objects.equals(mCurrentLayout, newValue)) { |
| 100 | clearViews(); |
| 101 | inflateLayout(newValue); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) { |
| 107 | mButtonDispatchers = buttonDisatchers; |
| 108 | for (int i = 0; i < buttonDisatchers.size(); i++) { |
| 109 | initiallyFill(buttonDisatchers.valueAt(i)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private void initiallyFill(ButtonDispatcher buttonDispatcher) { |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame^] | 114 | addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 115 | addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group)); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame^] | 116 | addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 117 | addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) { |
| 121 | for (int i = 0; i < parent.getChildCount(); i++) { |
| 122 | // Need to manually search for each id, just in case each group has more than one |
| 123 | // of a single id. It probably mostly a waste of time, but shouldn't take long |
| 124 | // and will only happen once. |
| 125 | if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) { |
| 126 | buttonDispatcher.addView(parent.getChildAt(i)); |
| 127 | } else if (parent.getChildAt(i) instanceof ViewGroup) { |
| 128 | addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i)); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private void inflateLayout(String newLayout) { |
| 134 | mCurrentLayout = newLayout; |
| 135 | String[] sets = newLayout.split(GRAVITY_SEPARATOR); |
| 136 | String[] start = sets[0].split(BUTTON_SEPARATOR); |
| 137 | String[] center = sets[1].split(BUTTON_SEPARATOR); |
| 138 | String[] end = sets[2].split(BUTTON_SEPARATOR); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame^] | 139 | inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group), |
| 140 | (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false); |
| 141 | inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group), |
| 142 | (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true); |
| 143 | |
| 144 | if (center.length == 1 && APP_SHELF.equals(center[0])) { |
| 145 | inflateShelf((LinearLayout) mRot0.findViewById(R.id.ends_group), |
| 146 | (LinearLayout) mRot0.findViewById(R.id.ends_group_lightsout), false); |
| 147 | inflateShelf((LinearLayout) mRot90.findViewById(R.id.ends_group), |
| 148 | (LinearLayout) mRot90.findViewById(R.id.ends_group_lightsout), true); |
| 149 | } else { |
| 150 | inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group), |
| 151 | (ViewGroup) mRot0.findViewById(R.id.center_group_lightsout), false); |
| 152 | inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group), |
| 153 | (ViewGroup) mRot90.findViewById(R.id.center_group_lightsout), true); |
| 154 | addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group)); |
| 155 | addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group)); |
| 156 | } |
| 157 | |
| 158 | inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group), |
| 159 | (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false); |
| 160 | inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group), |
| 161 | (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true); |
| 162 | } |
| 163 | |
| 164 | private void inflateShelf(LinearLayout layout, LinearLayout lightsOut, boolean landscape) { |
| 165 | View v = (landscape ? mLandscapeInflater : mLayoutInflater) |
| 166 | .inflate(R.layout.apps_bar, layout, false); |
| 167 | layout.addView(v); |
| 168 | addToDispatchers(v); |
| 169 | copyToLightsout(v, lightsOut); |
| 170 | } |
| 171 | |
| 172 | private void addGravitySpacer(LinearLayout layout) { |
| 173 | layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1)); |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private void inflateButtons(String[] buttons, ViewGroup parent, ViewGroup lightsOutParent, |
| 177 | boolean landscape) { |
| 178 | for (int i = 0; i < buttons.length; i++) { |
| 179 | copyToLightsout(inflateButton(buttons[i], parent, landscape), lightsOutParent); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private void copyToLightsout(View view, ViewGroup lightsOutParent) { |
| 184 | if (view instanceof FrameLayout) { |
| 185 | // The only ViewGroup we support in here is a FrameLayout, so copy those manually. |
| 186 | FrameLayout original = (FrameLayout) view; |
| 187 | FrameLayout layout = new FrameLayout(view.getContext()); |
| 188 | for (int i = 0; i < original.getChildCount(); i++) { |
| 189 | copyToLightsout(original.getChildAt(i), layout); |
| 190 | } |
| 191 | lightsOutParent.addView(layout, copy(view.getLayoutParams())); |
| 192 | } else if (view instanceof Space) { |
| 193 | lightsOutParent.addView(new Space(view.getContext()), copy(view.getLayoutParams())); |
| 194 | } else { |
| 195 | lightsOutParent.addView(generateLightsOutView(view), copy(view.getLayoutParams())); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private View generateLightsOutView(View view) { |
| 200 | ImageView imageView = new ImageView(view.getContext()); |
| 201 | // Copy everything we can about the original view. |
| 202 | imageView.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), |
| 203 | view.getPaddingBottom()); |
| 204 | imageView.setContentDescription(view.getContentDescription()); |
| 205 | imageView.setId(view.getId()); |
| 206 | // Only home gets a big dot, everything else will be little. |
| 207 | imageView.setImageResource(view.getId() == R.id.home |
| 208 | ? R.drawable.ic_sysbar_lights_out_dot_large |
| 209 | : R.drawable.ic_sysbar_lights_out_dot_small); |
| 210 | return imageView; |
| 211 | } |
| 212 | |
| 213 | private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) { |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame^] | 214 | if (layoutParams instanceof LinearLayout.LayoutParams) { |
| 215 | return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height, |
| 216 | ((LinearLayout.LayoutParams) layoutParams).weight); |
| 217 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 218 | return new LayoutParams(layoutParams.width, layoutParams.height); |
| 219 | } |
| 220 | |
| 221 | private View inflateButton(String button, ViewGroup parent, boolean landscape) { |
| 222 | View v = null; |
| 223 | if (HOME.equals(button)) { |
| 224 | v = (landscape ? mLandscapeInflater : mLayoutInflater) |
| 225 | .inflate(R.layout.home, parent, false); |
| 226 | if (landscape && isSw600Dp()) { |
| 227 | setupLandButton(v); |
| 228 | } |
| 229 | } else if (BACK.equals(button)) { |
| 230 | v = (landscape ? mLandscapeInflater : mLayoutInflater) |
| 231 | .inflate(R.layout.back, parent, false); |
| 232 | if (landscape && isSw600Dp()) { |
| 233 | setupLandButton(v); |
| 234 | } |
| 235 | } else if (RECENT.equals(button)) { |
| 236 | v = (landscape ? mLandscapeInflater : mLayoutInflater) |
| 237 | .inflate(R.layout.recent_apps, parent, false); |
| 238 | if (landscape && isSw600Dp()) { |
| 239 | setupLandButton(v); |
| 240 | } |
| 241 | } else if (MENU_IME.equals(button)) { |
| 242 | v = (landscape ? mLandscapeInflater : mLayoutInflater) |
| 243 | .inflate(R.layout.menu_ime, parent, false); |
| 244 | } else if (NAVSPACE.equals(button)) { |
| 245 | v = (landscape ? mLandscapeInflater : mLayoutInflater) |
| 246 | .inflate(R.layout.nav_key_space, parent, false); |
| 247 | } else { |
| 248 | throw new IllegalArgumentException("Unknown button " + button); |
| 249 | } |
| 250 | parent.addView(v); |
Jason Monk | 4f878ef | 2016-01-23 14:37:38 -0500 | [diff] [blame^] | 251 | addToDispatchers(v); |
| 252 | return v; |
| 253 | } |
| 254 | |
| 255 | private void addToDispatchers(View v) { |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 256 | if (mButtonDispatchers != null) { |
| 257 | final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId()); |
| 258 | if (indexOfKey >= 0) { |
| 259 | mButtonDispatchers.valueAt(indexOfKey).addView(v); |
| 260 | } |
| 261 | } |
Jason Monk | a208182 | 2016-01-18 14:41:03 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private boolean isSw600Dp() { |
| 265 | Configuration configuration = mContext.getResources().getConfiguration(); |
| 266 | return (configuration.smallestScreenWidthDp >= 600); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * This manually sets the width of sw600dp landscape buttons because despite |
| 271 | * overriding the configuration from the overridden resources aren't loaded currently. |
| 272 | */ |
| 273 | private void setupLandButton(View v) { |
| 274 | Resources res = mContext.getResources(); |
| 275 | v.getLayoutParams().width = res.getDimensionPixelOffset( |
| 276 | R.dimen.navigation_key_width_sw600dp_land); |
| 277 | int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land); |
| 278 | v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom()); |
| 279 | } |
| 280 | |
| 281 | private void clearViews() { |
| 282 | if (mButtonDispatchers != null) { |
| 283 | for (int i = 0; i < mButtonDispatchers.size(); i++) { |
| 284 | mButtonDispatchers.valueAt(i).clear(); |
| 285 | } |
| 286 | } |
| 287 | clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons)); |
| 288 | clearAllChildren((ViewGroup) mRot0.findViewById(R.id.lights_out)); |
| 289 | clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons)); |
| 290 | clearAllChildren((ViewGroup) mRot90.findViewById(R.id.lights_out)); |
| 291 | } |
| 292 | |
| 293 | private void clearAllChildren(ViewGroup group) { |
| 294 | for (int i = 0; i < group.getChildCount(); i++) { |
| 295 | ((ViewGroup) group.getChildAt(i)).removeAllViews(); |
| 296 | } |
| 297 | } |
| 298 | } |