blob: 76baee4c836faba58da5a2f7f9f0e00ef2a1099e [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
Evan Laird00e43c42018-01-22 20:25:45 -050087 private View mActionsContainer;
88
Anthony Chen54daefe2017-04-07 17:19:54 -070089 public QSFooterImpl(Context context, AttributeSet attrs) {
90 super(context, attrs);
91 }
92
93 @Override
94 protected void onFinishInflate() {
95 super.onFinishInflate();
96 Resources res = getResources();
97
98 mEdit = findViewById(android.R.id.edit);
99 mEdit.setOnClickListener(view ->
100 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() ->
101 mQsPanel.showEdit(view)));
102
Anthony Chen54daefe2017-04-07 17:19:54 -0700103 mSettingsButton = findViewById(R.id.settings_button);
104 mSettingsContainer = findViewById(R.id.settings_button_container);
105 mSettingsButton.setOnClickListener(this);
106
Evan Laird39254d42018-01-18 16:05:30 -0500107 mCarrierText = findViewById(R.id.qs_carrier_text);
108
Anthony Chen54daefe2017-04-07 17:19:54 -0700109 mMultiUserSwitch = findViewById(R.id.multi_user_switch);
110 mMultiUserAvatar = mMultiUserSwitch.findViewById(R.id.multi_user_avatar);
111
Evan Laird00e43c42018-01-22 20:25:45 -0500112 mActionsContainer = findViewById(R.id.qs_footer_actions_container);
113
Anthony Chen54daefe2017-04-07 17:19:54 -0700114 // RenderThread is doing more harm than good when touching the header (to expand quick
115 // settings), so disable it for this view
116 ((RippleDrawable) mSettingsButton.getBackground()).setForceSoftware(true);
Anthony Chen54daefe2017-04-07 17:19:54 -0700117
118 updateResources();
119
Anthony Chen54daefe2017-04-07 17:19:54 -0700120 mUserInfoController = Dependency.get(UserInfoController.class);
121 mActivityStarter = Dependency.get(ActivityStarter.class);
122 addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight,
123 oldBottom) -> updateAnimator(right - left));
124 }
125
126 private void updateAnimator(int width) {
127 int numTiles = QuickQSPanel.getNumQuickTiles(mContext);
128 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size)
129 - mContext.getResources().getDimensionPixelSize(dimen.qs_quick_tile_padding);
130 int remaining = (width - numTiles * size) / (numTiles - 1);
131 int defSpace = mContext.getResources().getDimensionPixelOffset(R.dimen.default_gear_space);
132
133 mAnimator = new Builder()
Jason Monk6dadd3e42017-09-01 09:36:19 -0400134 .addFloat(mSettingsContainer, "translationX",
135 isLayoutRtl() ? (remaining - defSpace) : -(remaining - defSpace), 0)
Anthony Chen54daefe2017-04-07 17:19:54 -0700136 .addFloat(mSettingsButton, "rotation", -120, 0)
137 .build();
Anthony Chen54daefe2017-04-07 17:19:54 -0700138
Anthony Chen54daefe2017-04-07 17:19:54 -0700139 setExpansion(mExpansionAmount);
140 }
141
142 @Override
143 protected void onConfigurationChanged(Configuration newConfig) {
144 super.onConfigurationChanged(newConfig);
145 updateResources();
146 }
147
148 @Override
149 public void onRtlPropertiesChanged(int layoutDirection) {
150 super.onRtlPropertiesChanged(layoutDirection);
151 updateResources();
152 }
153
154 private void updateResources() {
Anthony Chen54daefe2017-04-07 17:19:54 -0700155 updateSettingsAnimator();
156 }
157
158 private void updateSettingsAnimator() {
159 mSettingsAlpha = createSettingsAlphaAnimator();
Anthony Chen54daefe2017-04-07 17:19:54 -0700160 }
161
162 @Nullable
163 private TouchAnimator createSettingsAlphaAnimator() {
164 return new TouchAnimator.Builder()
Evan Laird39254d42018-01-18 16:05:30 -0500165 .addFloat(mCarrierText, "alpha", 0, 1)
Evan Laird00e43c42018-01-22 20:25:45 -0500166 .addFloat(mActionsContainer, "alpha", 0, 1)
Anthony Chen54daefe2017-04-07 17:19:54 -0700167 .build();
168 }
169
170 @Override
171 public void setKeyguardShowing(boolean keyguardShowing) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700172 setExpansion(mExpansionAmount);
173 }
174
175 @Override
176 public void setExpanded(boolean expanded) {
177 if (mExpanded == expanded) return;
178 mExpanded = expanded;
179 updateEverything();
180 }
181
182 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700183 public void setExpansion(float headerExpansionFraction) {
184 mExpansionAmount = headerExpansionFraction;
185 if (mAnimator != null) mAnimator.setPosition(headerExpansionFraction);
Anthony Chen54daefe2017-04-07 17:19:54 -0700186
187 if (mSettingsAlpha != null) {
188 mSettingsAlpha.setPosition(headerExpansionFraction);
189 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700190 }
191
192 @Override
Charles Hece2a7c02017-10-11 20:25:20 +0100193 public void onAttachedToWindow() {
194 super.onAttachedToWindow();
195 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
196 }
197
198 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700199 @VisibleForTesting
200 public void onDetachedFromWindow() {
201 setListening(false);
Charles Hece2a7c02017-10-11 20:25:20 +0100202 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).removeCallbacks(this);
Anthony Chen54daefe2017-04-07 17:19:54 -0700203 super.onDetachedFromWindow();
204 }
205
Anthony Chen54daefe2017-04-07 17:19:54 -0700206 @Override
207 public void setListening(boolean listening) {
208 if (listening == mListening) {
209 return;
210 }
211 mListening = listening;
212 updateListeners();
213 }
214
215 @Override
216 public View getExpandView() {
217 return findViewById(R.id.expand_indicator);
218 }
219
Charles Hece2a7c02017-10-11 20:25:20 +0100220 @Override
221 public void disable(int state1, int state2, boolean animate) {
222 final boolean disabled = (state2 & DISABLE2_QUICK_SETTINGS) != 0;
223 if (disabled == mQsDisabled) return;
224 mQsDisabled = disabled;
225 updateEverything();
226 }
227
Anthony Chen54daefe2017-04-07 17:19:54 -0700228 public void updateEverything() {
229 post(() -> {
230 updateVisibilities();
231 setClickable(false);
232 });
233 }
234
235 private void updateVisibilities() {
Charles Hece2a7c02017-10-11 20:25:20 +0100236 mSettingsContainer.setVisibility(mQsDisabled ? View.GONE : View.VISIBLE);
Anthony Chen54daefe2017-04-07 17:19:54 -0700237 mSettingsContainer.findViewById(R.id.tuner_icon).setVisibility(
238 TunerService.isTunerEnabled(mContext) ? View.VISIBLE : View.INVISIBLE);
Charles Hece2a7c02017-10-11 20:25:20 +0100239
Anthony Chen54daefe2017-04-07 17:19:54 -0700240 final boolean isDemo = UserManager.isDeviceInDemoMode(mContext);
241
Benjamin Franzff66fa92017-08-10 10:39:44 +0100242
243 mMultiUserSwitch.setVisibility(mExpanded
244 && UserManager.get(mContext).isUserSwitcherEnabled()
Anthony Chen54daefe2017-04-07 17:19:54 -0700245 ? View.VISIBLE : View.INVISIBLE);
246
247 mEdit.setVisibility(isDemo || !mExpanded ? View.INVISIBLE : View.VISIBLE);
248 }
249
250 private void updateListeners() {
251 if (mListening) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700252 mUserInfoController.addCallback(this);
253 if (Dependency.get(NetworkController.class).hasVoiceCallingFeature()) {
254 Dependency.get(NetworkController.class).addEmergencyListener(this);
255 Dependency.get(NetworkController.class).addCallback(this);
256 }
257 } else {
Anthony Chen54daefe2017-04-07 17:19:54 -0700258 mUserInfoController.removeCallback(this);
259 Dependency.get(NetworkController.class).removeEmergencyListener(this);
260 Dependency.get(NetworkController.class).removeCallback(this);
261 }
262 }
263
264 @Override
265 public void setQSPanel(final QSPanel qsPanel) {
266 mQsPanel = qsPanel;
267 if (mQsPanel != null) {
268 mMultiUserSwitch.setQsPanel(qsPanel);
269 }
270 }
271
272 @Override
273 public void onClick(View v) {
Evan Laird00e43c42018-01-22 20:25:45 -0500274 // Don't do anything until view are unhidden
275 if (!mExpanded) {
276 return;
277 }
278
Anthony Chen54daefe2017-04-07 17:19:54 -0700279 if (v == mSettingsButton) {
280 if (!Dependency.get(DeviceProvisionedController.class).isCurrentUserSetup()) {
281 // If user isn't setup just unlock the device and dump them back at SUW.
282 mActivityStarter.postQSRunnableDismissingKeyguard(() -> { });
283 return;
284 }
285 MetricsLogger.action(mContext,
286 mExpanded ? MetricsProto.MetricsEvent.ACTION_QS_EXPANDED_SETTINGS_LAUNCH
287 : MetricsProto.MetricsEvent.ACTION_QS_COLLAPSED_SETTINGS_LAUNCH);
288 if (mSettingsButton.isTunerClick()) {
289 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() -> {
290 if (TunerService.isTunerEnabled(mContext)) {
291 TunerService.showResetRequest(mContext, () -> {
292 // Relaunch settings so that the tuner disappears.
293 startSettingsActivity();
294 });
295 } else {
296 Toast.makeText(getContext(), R.string.tuner_toast,
297 Toast.LENGTH_LONG).show();
298 TunerService.setTunerEnabled(mContext, true);
299 }
300 startSettingsActivity();
301
302 });
303 } else {
304 startSettingsActivity();
305 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700306 }
307 }
308
309 private void startSettingsActivity() {
310 mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS),
311 true /* dismissShade */);
312 }
313
314 @Override
315 public void setEmergencyCallsOnly(boolean show) {
316 boolean changed = show != mShowEmergencyCallsOnly;
317 if (changed) {
318 mShowEmergencyCallsOnly = show;
319 if (mExpanded) {
320 updateEverything();
321 }
322 }
323 }
324
325 @Override
326 public void onUserInfoChanged(String name, Drawable picture, String userAccount) {
327 if (picture != null &&
Selim Cinek1a891a92017-12-04 17:41:27 +0100328 UserManager.get(mContext).isGuestUser(KeyguardUpdateMonitor.getCurrentUser()) &&
Jason Monk826b6092017-08-29 11:30:52 -0400329 !(picture instanceof UserIconDrawable)) {
330 picture = picture.getConstantState().newDrawable(mContext.getResources()).mutate();
Anthony Chen54daefe2017-04-07 17:19:54 -0700331 picture.setColorFilter(
332 Utils.getColorAttr(mContext, android.R.attr.colorForeground),
333 Mode.SRC_IN);
334 }
335 mMultiUserAvatar.setImageDrawable(picture);
336 }
337}