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