blob: 92475da697bda3ab18dbb2fc0e6d120462e005b1 [file] [log] [blame]
Anthony Chen54daefe2017-04-07 17:19:54 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.qs;
18
Charles Hece2a7c02017-10-11 20:25:20 +010019import static android.app.StatusBarManager.DISABLE2_QUICK_SETTINGS;
20
Anthony Chen54daefe2017-04-07 17:19:54 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.res.Configuration;
24import android.content.res.Resources;
25import android.graphics.PorterDuff.Mode;
26import android.graphics.drawable.Drawable;
27import android.graphics.drawable.RippleDrawable;
28import android.os.UserManager;
Anthony Chen54daefe2017-04-07 17:19:54 -070029import android.support.annotation.Nullable;
30import android.support.annotation.VisibleForTesting;
31import android.util.AttributeSet;
32import android.view.View;
33import android.view.View.OnClickListener;
34import android.widget.FrameLayout;
35import android.widget.ImageView;
Anthony Chen54daefe2017-04-07 17:19:54 -070036import android.widget.Toast;
37
38import com.android.internal.logging.MetricsLogger;
39import com.android.internal.logging.nano.MetricsProto;
Selim Cinek1a891a92017-12-04 17:41:27 +010040import com.android.keyguard.KeyguardUpdateMonitor;
Anthony Chen54daefe2017-04-07 17:19:54 -070041import com.android.settingslib.Utils;
Jason Monk826b6092017-08-29 11:30:52 -040042import com.android.settingslib.drawable.UserIconDrawable;
Anthony Chen54daefe2017-04-07 17:19:54 -070043import com.android.systemui.Dependency;
Anthony Chen54daefe2017-04-07 17:19:54 -070044import com.android.systemui.R;
45import com.android.systemui.R.dimen;
Charles Hece2a7c02017-10-11 20:25:20 +010046import com.android.systemui.SysUiServiceProvider;
Anthony Chen54daefe2017-04-07 17:19:54 -070047import com.android.systemui.plugins.ActivityStarter;
48import com.android.systemui.qs.TouchAnimator.Builder;
Charles Hece2a7c02017-10-11 20:25:20 +010049import com.android.systemui.statusbar.CommandQueue;
Anthony Chen54daefe2017-04-07 17:19:54 -070050import com.android.systemui.statusbar.phone.ExpandableIndicator;
51import com.android.systemui.statusbar.phone.MultiUserSwitch;
52import com.android.systemui.statusbar.phone.SettingsButton;
53import com.android.systemui.statusbar.policy.DeviceProvisionedController;
54import com.android.systemui.statusbar.policy.NetworkController;
55import com.android.systemui.statusbar.policy.NetworkController.EmergencyListener;
56import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
Anthony Chen54daefe2017-04-07 17:19:54 -070057import com.android.systemui.statusbar.policy.UserInfoController;
58import com.android.systemui.statusbar.policy.UserInfoController.OnUserInfoChangedListener;
59import com.android.systemui.tuner.TunerService;
60
61public class QSFooterImpl extends FrameLayout implements QSFooter,
Evan Laird2cf56822017-12-18 11:22:39 -050062 OnClickListener, OnUserInfoChangedListener, EmergencyListener,
Charles Hece2a7c02017-10-11 20:25:20 +010063 SignalCallback, CommandQueue.Callbacks {
Anthony Chen54daefe2017-04-07 17:19:54 -070064 private ActivityStarter mActivityStarter;
Anthony Chen54daefe2017-04-07 17:19:54 -070065 private UserInfoController mUserInfoController;
66 private SettingsButton mSettingsButton;
67 protected View mSettingsContainer;
Evan Laird39254d42018-01-18 16:05:30 -050068 private View mCarrierText;
Anthony Chen54daefe2017-04-07 17:19:54 -070069
Charles Hece2a7c02017-10-11 20:25:20 +010070 private boolean mQsDisabled;
Anthony Chen54daefe2017-04-07 17:19:54 -070071 private QSPanel mQsPanel;
72
73 private boolean mExpanded;
Anthony Chen54daefe2017-04-07 17:19:54 -070074
75 private boolean mListening;
Anthony Chen54daefe2017-04-07 17:19:54 -070076
77 private boolean mShowEmergencyCallsOnly;
78 protected MultiUserSwitch mMultiUserSwitch;
79 private ImageView mMultiUserAvatar;
80
81 protected TouchAnimator mSettingsAlpha;
82 private float mExpansionAmount;
83
84 protected View mEdit;
85 private TouchAnimator mAnimator;
Anthony Chen54daefe2017-04-07 17:19:54 -070086
87 public QSFooterImpl(Context context, AttributeSet attrs) {
88 super(context, attrs);
89 }
90
91 @Override
92 protected void onFinishInflate() {
93 super.onFinishInflate();
94 Resources res = getResources();
95
96 mEdit = findViewById(android.R.id.edit);
97 mEdit.setOnClickListener(view ->
98 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() ->
99 mQsPanel.showEdit(view)));
100
Anthony Chen54daefe2017-04-07 17:19:54 -0700101 mSettingsButton = findViewById(R.id.settings_button);
102 mSettingsContainer = findViewById(R.id.settings_button_container);
103 mSettingsButton.setOnClickListener(this);
104
Evan Laird39254d42018-01-18 16:05:30 -0500105 mCarrierText = findViewById(R.id.qs_carrier_text);
106
Anthony Chen54daefe2017-04-07 17:19:54 -0700107 mMultiUserSwitch = findViewById(R.id.multi_user_switch);
108 mMultiUserAvatar = mMultiUserSwitch.findViewById(R.id.multi_user_avatar);
109
110 // RenderThread is doing more harm than good when touching the header (to expand quick
111 // settings), so disable it for this view
112 ((RippleDrawable) mSettingsButton.getBackground()).setForceSoftware(true);
Anthony Chen54daefe2017-04-07 17:19:54 -0700113
114 updateResources();
115
Anthony Chen54daefe2017-04-07 17:19:54 -0700116 mUserInfoController = Dependency.get(UserInfoController.class);
117 mActivityStarter = Dependency.get(ActivityStarter.class);
118 addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight,
119 oldBottom) -> updateAnimator(right - left));
120 }
121
122 private void updateAnimator(int width) {
123 int numTiles = QuickQSPanel.getNumQuickTiles(mContext);
124 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size)
125 - mContext.getResources().getDimensionPixelSize(dimen.qs_quick_tile_padding);
126 int remaining = (width - numTiles * size) / (numTiles - 1);
127 int defSpace = mContext.getResources().getDimensionPixelOffset(R.dimen.default_gear_space);
128
129 mAnimator = new Builder()
Jason Monk6dadd3e42017-09-01 09:36:19 -0400130 .addFloat(mSettingsContainer, "translationX",
131 isLayoutRtl() ? (remaining - defSpace) : -(remaining - defSpace), 0)
Anthony Chen54daefe2017-04-07 17:19:54 -0700132 .addFloat(mSettingsButton, "rotation", -120, 0)
133 .build();
Anthony Chen54daefe2017-04-07 17:19:54 -0700134
Anthony Chen54daefe2017-04-07 17:19:54 -0700135 setExpansion(mExpansionAmount);
136 }
137
138 @Override
139 protected void onConfigurationChanged(Configuration newConfig) {
140 super.onConfigurationChanged(newConfig);
141 updateResources();
142 }
143
144 @Override
145 public void onRtlPropertiesChanged(int layoutDirection) {
146 super.onRtlPropertiesChanged(layoutDirection);
147 updateResources();
148 }
149
150 private void updateResources() {
Anthony Chen54daefe2017-04-07 17:19:54 -0700151 updateSettingsAnimator();
152 }
153
154 private void updateSettingsAnimator() {
155 mSettingsAlpha = createSettingsAlphaAnimator();
Anthony Chen54daefe2017-04-07 17:19:54 -0700156 }
157
158 @Nullable
159 private TouchAnimator createSettingsAlphaAnimator() {
160 return new TouchAnimator.Builder()
161 .addFloat(mEdit, "alpha", 0, 1)
162 .addFloat(mMultiUserSwitch, "alpha", 0, 1)
Evan Laird39254d42018-01-18 16:05:30 -0500163 .addFloat(mCarrierText, "alpha", 0, 1)
164 .addFloat(mSettingsButton, "alpha", 0, 1)
Anthony Chen54daefe2017-04-07 17:19:54 -0700165 .build();
166 }
167
168 @Override
169 public void setKeyguardShowing(boolean keyguardShowing) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700170 setExpansion(mExpansionAmount);
171 }
172
173 @Override
174 public void setExpanded(boolean expanded) {
175 if (mExpanded == expanded) return;
176 mExpanded = expanded;
177 updateEverything();
178 }
179
180 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700181 public void setExpansion(float headerExpansionFraction) {
182 mExpansionAmount = headerExpansionFraction;
183 if (mAnimator != null) mAnimator.setPosition(headerExpansionFraction);
Anthony Chen54daefe2017-04-07 17:19:54 -0700184
185 if (mSettingsAlpha != null) {
186 mSettingsAlpha.setPosition(headerExpansionFraction);
187 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700188 }
189
190 @Override
Charles Hece2a7c02017-10-11 20:25:20 +0100191 public void onAttachedToWindow() {
192 super.onAttachedToWindow();
193 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
194 }
195
196 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700197 @VisibleForTesting
198 public void onDetachedFromWindow() {
199 setListening(false);
Charles Hece2a7c02017-10-11 20:25:20 +0100200 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).removeCallbacks(this);
Anthony Chen54daefe2017-04-07 17:19:54 -0700201 super.onDetachedFromWindow();
202 }
203
Anthony Chen54daefe2017-04-07 17:19:54 -0700204 @Override
205 public void setListening(boolean listening) {
206 if (listening == mListening) {
207 return;
208 }
209 mListening = listening;
210 updateListeners();
211 }
212
213 @Override
214 public View getExpandView() {
215 return findViewById(R.id.expand_indicator);
216 }
217
Charles Hece2a7c02017-10-11 20:25:20 +0100218 @Override
219 public void disable(int state1, int state2, boolean animate) {
220 final boolean disabled = (state2 & DISABLE2_QUICK_SETTINGS) != 0;
221 if (disabled == mQsDisabled) return;
222 mQsDisabled = disabled;
223 updateEverything();
224 }
225
Anthony Chen54daefe2017-04-07 17:19:54 -0700226 public void updateEverything() {
227 post(() -> {
228 updateVisibilities();
229 setClickable(false);
230 });
231 }
232
233 private void updateVisibilities() {
Charles Hece2a7c02017-10-11 20:25:20 +0100234 mSettingsContainer.setVisibility(mQsDisabled ? View.GONE : View.VISIBLE);
Anthony Chen54daefe2017-04-07 17:19:54 -0700235 mSettingsContainer.findViewById(R.id.tuner_icon).setVisibility(
236 TunerService.isTunerEnabled(mContext) ? View.VISIBLE : View.INVISIBLE);
Charles Hece2a7c02017-10-11 20:25:20 +0100237
Anthony Chen54daefe2017-04-07 17:19:54 -0700238 final boolean isDemo = UserManager.isDeviceInDemoMode(mContext);
239
Benjamin Franzff66fa92017-08-10 10:39:44 +0100240
241 mMultiUserSwitch.setVisibility(mExpanded
242 && UserManager.get(mContext).isUserSwitcherEnabled()
Anthony Chen54daefe2017-04-07 17:19:54 -0700243 ? View.VISIBLE : View.INVISIBLE);
244
245 mEdit.setVisibility(isDemo || !mExpanded ? View.INVISIBLE : View.VISIBLE);
246 }
247
248 private void updateListeners() {
249 if (mListening) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700250 mUserInfoController.addCallback(this);
251 if (Dependency.get(NetworkController.class).hasVoiceCallingFeature()) {
252 Dependency.get(NetworkController.class).addEmergencyListener(this);
253 Dependency.get(NetworkController.class).addCallback(this);
254 }
255 } else {
Anthony Chen54daefe2017-04-07 17:19:54 -0700256 mUserInfoController.removeCallback(this);
257 Dependency.get(NetworkController.class).removeEmergencyListener(this);
258 Dependency.get(NetworkController.class).removeCallback(this);
259 }
260 }
261
262 @Override
263 public void setQSPanel(final QSPanel qsPanel) {
264 mQsPanel = qsPanel;
265 if (mQsPanel != null) {
266 mMultiUserSwitch.setQsPanel(qsPanel);
267 }
268 }
269
270 @Override
271 public void onClick(View v) {
272 if (v == mSettingsButton) {
273 if (!Dependency.get(DeviceProvisionedController.class).isCurrentUserSetup()) {
274 // If user isn't setup just unlock the device and dump them back at SUW.
275 mActivityStarter.postQSRunnableDismissingKeyguard(() -> { });
276 return;
277 }
278 MetricsLogger.action(mContext,
279 mExpanded ? MetricsProto.MetricsEvent.ACTION_QS_EXPANDED_SETTINGS_LAUNCH
280 : MetricsProto.MetricsEvent.ACTION_QS_COLLAPSED_SETTINGS_LAUNCH);
281 if (mSettingsButton.isTunerClick()) {
282 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() -> {
283 if (TunerService.isTunerEnabled(mContext)) {
284 TunerService.showResetRequest(mContext, () -> {
285 // Relaunch settings so that the tuner disappears.
286 startSettingsActivity();
287 });
288 } else {
289 Toast.makeText(getContext(), R.string.tuner_toast,
290 Toast.LENGTH_LONG).show();
291 TunerService.setTunerEnabled(mContext, true);
292 }
293 startSettingsActivity();
294
295 });
296 } else {
297 startSettingsActivity();
298 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700299 }
300 }
301
302 private void startSettingsActivity() {
303 mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS),
304 true /* dismissShade */);
305 }
306
307 @Override
308 public void setEmergencyCallsOnly(boolean show) {
309 boolean changed = show != mShowEmergencyCallsOnly;
310 if (changed) {
311 mShowEmergencyCallsOnly = show;
312 if (mExpanded) {
313 updateEverything();
314 }
315 }
316 }
317
318 @Override
319 public void onUserInfoChanged(String name, Drawable picture, String userAccount) {
320 if (picture != null &&
Selim Cinek1a891a92017-12-04 17:41:27 +0100321 UserManager.get(mContext).isGuestUser(KeyguardUpdateMonitor.getCurrentUser()) &&
Jason Monk826b6092017-08-29 11:30:52 -0400322 !(picture instanceof UserIconDrawable)) {
323 picture = picture.getConstantState().newDrawable(mContext.getResources()).mutate();
Anthony Chen54daefe2017-04-07 17:19:54 -0700324 picture.setColorFilter(
325 Utils.getColorAttr(mContext, android.R.attr.colorForeground),
326 Mode.SRC_IN);
327 }
328 mMultiUserAvatar.setImageDrawable(picture);
329 }
330}