blob: 7b48e021b715af7aea7471110a32d1b95b386ebe [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;
Anthony Chen54daefe2017-04-07 17:19:54 -070024import android.graphics.PorterDuff.Mode;
25import android.graphics.drawable.Drawable;
26import android.graphics.drawable.RippleDrawable;
27import android.os.UserManager;
Anthony Chen54daefe2017-04-07 17:19:54 -070028import android.support.annotation.Nullable;
29import android.support.annotation.VisibleForTesting;
30import android.util.AttributeSet;
31import android.view.View;
32import android.view.View.OnClickListener;
33import android.widget.FrameLayout;
34import android.widget.ImageView;
Anthony Chen54daefe2017-04-07 17:19:54 -070035import android.widget.Toast;
36
37import com.android.internal.logging.MetricsLogger;
38import com.android.internal.logging.nano.MetricsProto;
Selim Cinek1a891a92017-12-04 17:41:27 +010039import com.android.keyguard.KeyguardUpdateMonitor;
Anthony Chen54daefe2017-04-07 17:19:54 -070040import com.android.settingslib.Utils;
Jason Monk826b6092017-08-29 11:30:52 -040041import com.android.settingslib.drawable.UserIconDrawable;
Anthony Chen54daefe2017-04-07 17:19:54 -070042import com.android.systemui.Dependency;
Anthony Chen54daefe2017-04-07 17:19:54 -070043import com.android.systemui.R;
44import com.android.systemui.R.dimen;
Charles Hece2a7c02017-10-11 20:25:20 +010045import com.android.systemui.SysUiServiceProvider;
Anthony Chen54daefe2017-04-07 17:19:54 -070046import com.android.systemui.plugins.ActivityStarter;
47import com.android.systemui.qs.TouchAnimator.Builder;
Charles Hece2a7c02017-10-11 20:25:20 +010048import com.android.systemui.statusbar.CommandQueue;
Anthony Chen54daefe2017-04-07 17:19:54 -070049import com.android.systemui.statusbar.phone.MultiUserSwitch;
50import com.android.systemui.statusbar.phone.SettingsButton;
51import com.android.systemui.statusbar.policy.DeviceProvisionedController;
52import com.android.systemui.statusbar.policy.NetworkController;
53import com.android.systemui.statusbar.policy.NetworkController.EmergencyListener;
54import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
Anthony Chen54daefe2017-04-07 17:19:54 -070055import com.android.systemui.statusbar.policy.UserInfoController;
56import com.android.systemui.statusbar.policy.UserInfoController.OnUserInfoChangedListener;
57import com.android.systemui.tuner.TunerService;
58
59public class QSFooterImpl extends FrameLayout implements QSFooter,
Evan Laird2cf56822017-12-18 11:22:39 -050060 OnClickListener, OnUserInfoChangedListener, EmergencyListener,
Charles Hece2a7c02017-10-11 20:25:20 +010061 SignalCallback, CommandQueue.Callbacks {
Evan Lairdeb38aa72018-02-02 11:10:30 -050062
Anthony Chen54daefe2017-04-07 17:19:54 -070063 private ActivityStarter mActivityStarter;
Anthony Chen54daefe2017-04-07 17:19:54 -070064 private UserInfoController mUserInfoController;
65 private SettingsButton mSettingsButton;
66 protected View mSettingsContainer;
Evan Laird39254d42018-01-18 16:05:30 -050067 private View mCarrierText;
Anthony Chen54daefe2017-04-07 17:19:54 -070068
Charles Hece2a7c02017-10-11 20:25:20 +010069 private boolean mQsDisabled;
Anthony Chen54daefe2017-04-07 17:19:54 -070070 private QSPanel mQsPanel;
71
72 private boolean mExpanded;
Anthony Chen54daefe2017-04-07 17:19:54 -070073
74 private boolean mListening;
Anthony Chen54daefe2017-04-07 17:19:54 -070075
76 private boolean mShowEmergencyCallsOnly;
Amin Shaikhacf322d2018-01-31 17:04:56 -050077 private View mDivider;
Anthony Chen54daefe2017-04-07 17:19:54 -070078 protected MultiUserSwitch mMultiUserSwitch;
79 private ImageView mMultiUserAvatar;
80
Evan Lairdeb38aa72018-02-02 11:10:30 -050081 protected TouchAnimator mFooterAnimator;
Anthony Chen54daefe2017-04-07 17:19:54 -070082 private float mExpansionAmount;
83
84 protected View mEdit;
Evan Lairdeb38aa72018-02-02 11:10:30 -050085 private TouchAnimator mSettingsCogAnimator;
Anthony Chen54daefe2017-04-07 17:19:54 -070086
Evan Laird00e43c42018-01-22 20:25:45 -050087 private View mActionsContainer;
Evan Lairdeb38aa72018-02-02 11:10:30 -050088 private View mDragHandle;
89 private final int mDragHandleExpandOffset;
Evan Laird00e43c42018-01-22 20:25:45 -050090
Anthony Chen54daefe2017-04-07 17:19:54 -070091 public QSFooterImpl(Context context, AttributeSet attrs) {
92 super(context, attrs);
Evan Lairdeb38aa72018-02-02 11:10:30 -050093
94 mDragHandleExpandOffset = getResources().
95 getDimensionPixelSize(R.dimen.qs_footer_drag_handle_offset);
96
Anthony Chen54daefe2017-04-07 17:19:54 -070097 }
98
99 @Override
100 protected void onFinishInflate() {
101 super.onFinishInflate();
Amin Shaikhacf322d2018-01-31 17:04:56 -0500102 mDivider = findViewById(R.id.qs_footer_divider);
Anthony Chen54daefe2017-04-07 17:19:54 -0700103 mEdit = findViewById(android.R.id.edit);
104 mEdit.setOnClickListener(view ->
105 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() ->
106 mQsPanel.showEdit(view)));
107
Anthony Chen54daefe2017-04-07 17:19:54 -0700108 mSettingsButton = findViewById(R.id.settings_button);
109 mSettingsContainer = findViewById(R.id.settings_button_container);
110 mSettingsButton.setOnClickListener(this);
111
Evan Laird39254d42018-01-18 16:05:30 -0500112 mCarrierText = findViewById(R.id.qs_carrier_text);
113
Anthony Chen54daefe2017-04-07 17:19:54 -0700114 mMultiUserSwitch = findViewById(R.id.multi_user_switch);
115 mMultiUserAvatar = mMultiUserSwitch.findViewById(R.id.multi_user_avatar);
116
Evan Lairdeb38aa72018-02-02 11:10:30 -0500117 mDragHandle = findViewById(R.id.qs_drag_handle_view);
Evan Laird00e43c42018-01-22 20:25:45 -0500118 mActionsContainer = findViewById(R.id.qs_footer_actions_container);
119
Anthony Chen54daefe2017-04-07 17:19:54 -0700120 // RenderThread is doing more harm than good when touching the header (to expand quick
121 // settings), so disable it for this view
122 ((RippleDrawable) mSettingsButton.getBackground()).setForceSoftware(true);
Anthony Chen54daefe2017-04-07 17:19:54 -0700123
124 updateResources();
125
Anthony Chen54daefe2017-04-07 17:19:54 -0700126 mUserInfoController = Dependency.get(UserInfoController.class);
127 mActivityStarter = Dependency.get(ActivityStarter.class);
128 addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight,
129 oldBottom) -> updateAnimator(right - left));
130 }
131
132 private void updateAnimator(int width) {
133 int numTiles = QuickQSPanel.getNumQuickTiles(mContext);
134 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size)
135 - mContext.getResources().getDimensionPixelSize(dimen.qs_quick_tile_padding);
136 int remaining = (width - numTiles * size) / (numTiles - 1);
137 int defSpace = mContext.getResources().getDimensionPixelOffset(R.dimen.default_gear_space);
138
Evan Lairdeb38aa72018-02-02 11:10:30 -0500139 mSettingsCogAnimator = new Builder()
Jason Monk6dadd3e42017-09-01 09:36:19 -0400140 .addFloat(mSettingsContainer, "translationX",
141 isLayoutRtl() ? (remaining - defSpace) : -(remaining - defSpace), 0)
Anthony Chen54daefe2017-04-07 17:19:54 -0700142 .addFloat(mSettingsButton, "rotation", -120, 0)
143 .build();
Anthony Chen54daefe2017-04-07 17:19:54 -0700144
Anthony Chen54daefe2017-04-07 17:19:54 -0700145 setExpansion(mExpansionAmount);
146 }
147
148 @Override
149 protected void onConfigurationChanged(Configuration newConfig) {
150 super.onConfigurationChanged(newConfig);
151 updateResources();
152 }
153
154 @Override
155 public void onRtlPropertiesChanged(int layoutDirection) {
156 super.onRtlPropertiesChanged(layoutDirection);
157 updateResources();
158 }
159
160 private void updateResources() {
Evan Lairdeb38aa72018-02-02 11:10:30 -0500161 updateFooterAnimator();
Anthony Chen54daefe2017-04-07 17:19:54 -0700162 }
163
Evan Lairdeb38aa72018-02-02 11:10:30 -0500164 private void updateFooterAnimator() {
165 mFooterAnimator = createFooterAnimator();
Anthony Chen54daefe2017-04-07 17:19:54 -0700166 }
167
168 @Nullable
Evan Lairdeb38aa72018-02-02 11:10:30 -0500169 private TouchAnimator createFooterAnimator() {
Anthony Chen54daefe2017-04-07 17:19:54 -0700170 return new TouchAnimator.Builder()
Amin Shaikhacf322d2018-01-31 17:04:56 -0500171 .addFloat(mDivider, "alpha", 0, 1)
Amin Shaikhd620def2018-02-27 16:52:53 -0500172 .addFloat(mCarrierText, "alpha", 0, 0, 1)
Evan Laird00e43c42018-01-22 20:25:45 -0500173 .addFloat(mActionsContainer, "alpha", 0, 1)
Amin Shaikh4f3f5522018-02-13 17:55:14 -0500174 .addFloat(mDragHandle, "translationY", mDragHandleExpandOffset, 0)
Amin Shaikhd620def2018-02-27 16:52:53 -0500175 .addFloat(mDragHandle, "alpha", 1, 0, 0)
Amin Shaikh92fac562018-02-16 15:08:16 -0500176 .setStartDelay(0.15f)
Anthony Chen54daefe2017-04-07 17:19:54 -0700177 .build();
178 }
179
180 @Override
181 public void setKeyguardShowing(boolean keyguardShowing) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700182 setExpansion(mExpansionAmount);
183 }
184
185 @Override
186 public void setExpanded(boolean expanded) {
187 if (mExpanded == expanded) return;
188 mExpanded = expanded;
189 updateEverything();
190 }
191
192 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700193 public void setExpansion(float headerExpansionFraction) {
194 mExpansionAmount = headerExpansionFraction;
Evan Lairdeb38aa72018-02-02 11:10:30 -0500195 if (mSettingsCogAnimator != null) mSettingsCogAnimator.setPosition(headerExpansionFraction);
Anthony Chen54daefe2017-04-07 17:19:54 -0700196
Evan Lairdeb38aa72018-02-02 11:10:30 -0500197 if (mFooterAnimator != null) {
198 mFooterAnimator.setPosition(headerExpansionFraction);
Anthony Chen54daefe2017-04-07 17:19:54 -0700199 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700200 }
201
202 @Override
Charles Hece2a7c02017-10-11 20:25:20 +0100203 public void onAttachedToWindow() {
204 super.onAttachedToWindow();
205 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
206 }
207
208 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700209 @VisibleForTesting
210 public void onDetachedFromWindow() {
211 setListening(false);
Charles Hece2a7c02017-10-11 20:25:20 +0100212 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).removeCallbacks(this);
Anthony Chen54daefe2017-04-07 17:19:54 -0700213 super.onDetachedFromWindow();
214 }
215
Anthony Chen54daefe2017-04-07 17:19:54 -0700216 @Override
217 public void setListening(boolean listening) {
218 if (listening == mListening) {
219 return;
220 }
221 mListening = listening;
222 updateListeners();
223 }
224
225 @Override
226 public View getExpandView() {
227 return findViewById(R.id.expand_indicator);
228 }
229
Charles Hece2a7c02017-10-11 20:25:20 +0100230 @Override
231 public void disable(int state1, int state2, boolean animate) {
232 final boolean disabled = (state2 & DISABLE2_QUICK_SETTINGS) != 0;
233 if (disabled == mQsDisabled) return;
234 mQsDisabled = disabled;
235 updateEverything();
236 }
237
Anthony Chen54daefe2017-04-07 17:19:54 -0700238 public void updateEverything() {
239 post(() -> {
240 updateVisibilities();
241 setClickable(false);
242 });
243 }
244
245 private void updateVisibilities() {
Charles Hece2a7c02017-10-11 20:25:20 +0100246 mSettingsContainer.setVisibility(mQsDisabled ? View.GONE : View.VISIBLE);
Anthony Chen54daefe2017-04-07 17:19:54 -0700247 mSettingsContainer.findViewById(R.id.tuner_icon).setVisibility(
248 TunerService.isTunerEnabled(mContext) ? View.VISIBLE : View.INVISIBLE);
Charles Hece2a7c02017-10-11 20:25:20 +0100249
Anthony Chen54daefe2017-04-07 17:19:54 -0700250 final boolean isDemo = UserManager.isDeviceInDemoMode(mContext);
251
Benjamin Franzff66fa92017-08-10 10:39:44 +0100252
253 mMultiUserSwitch.setVisibility(mExpanded
254 && UserManager.get(mContext).isUserSwitcherEnabled()
Anthony Chen54daefe2017-04-07 17:19:54 -0700255 ? View.VISIBLE : View.INVISIBLE);
256
257 mEdit.setVisibility(isDemo || !mExpanded ? View.INVISIBLE : View.VISIBLE);
258 }
259
260 private void updateListeners() {
261 if (mListening) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700262 mUserInfoController.addCallback(this);
263 if (Dependency.get(NetworkController.class).hasVoiceCallingFeature()) {
264 Dependency.get(NetworkController.class).addEmergencyListener(this);
265 Dependency.get(NetworkController.class).addCallback(this);
266 }
267 } else {
Anthony Chen54daefe2017-04-07 17:19:54 -0700268 mUserInfoController.removeCallback(this);
269 Dependency.get(NetworkController.class).removeEmergencyListener(this);
270 Dependency.get(NetworkController.class).removeCallback(this);
271 }
272 }
273
274 @Override
275 public void setQSPanel(final QSPanel qsPanel) {
276 mQsPanel = qsPanel;
277 if (mQsPanel != null) {
278 mMultiUserSwitch.setQsPanel(qsPanel);
279 }
280 }
281
282 @Override
283 public void onClick(View v) {
Evan Laird00e43c42018-01-22 20:25:45 -0500284 // Don't do anything until view are unhidden
285 if (!mExpanded) {
286 return;
287 }
288
Anthony Chen54daefe2017-04-07 17:19:54 -0700289 if (v == mSettingsButton) {
290 if (!Dependency.get(DeviceProvisionedController.class).isCurrentUserSetup()) {
291 // If user isn't setup just unlock the device and dump them back at SUW.
292 mActivityStarter.postQSRunnableDismissingKeyguard(() -> { });
293 return;
294 }
295 MetricsLogger.action(mContext,
296 mExpanded ? MetricsProto.MetricsEvent.ACTION_QS_EXPANDED_SETTINGS_LAUNCH
297 : MetricsProto.MetricsEvent.ACTION_QS_COLLAPSED_SETTINGS_LAUNCH);
298 if (mSettingsButton.isTunerClick()) {
299 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() -> {
300 if (TunerService.isTunerEnabled(mContext)) {
301 TunerService.showResetRequest(mContext, () -> {
302 // Relaunch settings so that the tuner disappears.
303 startSettingsActivity();
304 });
305 } else {
306 Toast.makeText(getContext(), R.string.tuner_toast,
307 Toast.LENGTH_LONG).show();
308 TunerService.setTunerEnabled(mContext, true);
309 }
310 startSettingsActivity();
311
312 });
313 } else {
314 startSettingsActivity();
315 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700316 }
317 }
318
319 private void startSettingsActivity() {
320 mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS),
321 true /* dismissShade */);
322 }
323
324 @Override
325 public void setEmergencyCallsOnly(boolean show) {
326 boolean changed = show != mShowEmergencyCallsOnly;
327 if (changed) {
328 mShowEmergencyCallsOnly = show;
329 if (mExpanded) {
330 updateEverything();
331 }
332 }
333 }
334
335 @Override
336 public void onUserInfoChanged(String name, Drawable picture, String userAccount) {
337 if (picture != null &&
Selim Cinek1a891a92017-12-04 17:41:27 +0100338 UserManager.get(mContext).isGuestUser(KeyguardUpdateMonitor.getCurrentUser()) &&
Jason Monk826b6092017-08-29 11:30:52 -0400339 !(picture instanceof UserIconDrawable)) {
340 picture = picture.getConstantState().newDrawable(mContext.getResources()).mutate();
Anthony Chen54daefe2017-04-07 17:19:54 -0700341 picture.setColorFilter(
342 Utils.getColorAttr(mContext, android.R.attr.colorForeground),
343 Mode.SRC_IN);
344 }
345 mMultiUserAvatar.setImageDrawable(picture);
346 }
347}