blob: 7f34acb2ef94ae743fdea68638e71ff6719a0f14 [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)
Evan Laird39254d42018-01-18 16:05:30 -0500172 .addFloat(mCarrierText, "alpha", 0, 1)
Evan Laird00e43c42018-01-22 20:25:45 -0500173 .addFloat(mActionsContainer, "alpha", 0, 1)
Evan Lairdeb38aa72018-02-02 11:10:30 -0500174 .addFloat(mDragHandle, "translationY", 0, -mDragHandleExpandOffset)
Anthony Chen54daefe2017-04-07 17:19:54 -0700175 .build();
176 }
177
178 @Override
179 public void setKeyguardShowing(boolean keyguardShowing) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700180 setExpansion(mExpansionAmount);
181 }
182
183 @Override
184 public void setExpanded(boolean expanded) {
185 if (mExpanded == expanded) return;
186 mExpanded = expanded;
187 updateEverything();
188 }
189
190 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700191 public void setExpansion(float headerExpansionFraction) {
192 mExpansionAmount = headerExpansionFraction;
Evan Lairdeb38aa72018-02-02 11:10:30 -0500193 if (mSettingsCogAnimator != null) mSettingsCogAnimator.setPosition(headerExpansionFraction);
Anthony Chen54daefe2017-04-07 17:19:54 -0700194
Evan Lairdeb38aa72018-02-02 11:10:30 -0500195 if (mFooterAnimator != null) {
196 mFooterAnimator.setPosition(headerExpansionFraction);
Anthony Chen54daefe2017-04-07 17:19:54 -0700197 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700198 }
199
200 @Override
Charles Hece2a7c02017-10-11 20:25:20 +0100201 public void onAttachedToWindow() {
202 super.onAttachedToWindow();
203 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
204 }
205
206 @Override
Anthony Chen54daefe2017-04-07 17:19:54 -0700207 @VisibleForTesting
208 public void onDetachedFromWindow() {
209 setListening(false);
Charles Hece2a7c02017-10-11 20:25:20 +0100210 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).removeCallbacks(this);
Anthony Chen54daefe2017-04-07 17:19:54 -0700211 super.onDetachedFromWindow();
212 }
213
Anthony Chen54daefe2017-04-07 17:19:54 -0700214 @Override
215 public void setListening(boolean listening) {
216 if (listening == mListening) {
217 return;
218 }
219 mListening = listening;
220 updateListeners();
221 }
222
223 @Override
224 public View getExpandView() {
225 return findViewById(R.id.expand_indicator);
226 }
227
Charles Hece2a7c02017-10-11 20:25:20 +0100228 @Override
229 public void disable(int state1, int state2, boolean animate) {
230 final boolean disabled = (state2 & DISABLE2_QUICK_SETTINGS) != 0;
231 if (disabled == mQsDisabled) return;
232 mQsDisabled = disabled;
233 updateEverything();
234 }
235
Anthony Chen54daefe2017-04-07 17:19:54 -0700236 public void updateEverything() {
237 post(() -> {
238 updateVisibilities();
239 setClickable(false);
240 });
241 }
242
243 private void updateVisibilities() {
Charles Hece2a7c02017-10-11 20:25:20 +0100244 mSettingsContainer.setVisibility(mQsDisabled ? View.GONE : View.VISIBLE);
Anthony Chen54daefe2017-04-07 17:19:54 -0700245 mSettingsContainer.findViewById(R.id.tuner_icon).setVisibility(
246 TunerService.isTunerEnabled(mContext) ? View.VISIBLE : View.INVISIBLE);
Charles Hece2a7c02017-10-11 20:25:20 +0100247
Anthony Chen54daefe2017-04-07 17:19:54 -0700248 final boolean isDemo = UserManager.isDeviceInDemoMode(mContext);
249
Benjamin Franzff66fa92017-08-10 10:39:44 +0100250
251 mMultiUserSwitch.setVisibility(mExpanded
252 && UserManager.get(mContext).isUserSwitcherEnabled()
Anthony Chen54daefe2017-04-07 17:19:54 -0700253 ? View.VISIBLE : View.INVISIBLE);
254
255 mEdit.setVisibility(isDemo || !mExpanded ? View.INVISIBLE : View.VISIBLE);
256 }
257
258 private void updateListeners() {
259 if (mListening) {
Anthony Chen54daefe2017-04-07 17:19:54 -0700260 mUserInfoController.addCallback(this);
261 if (Dependency.get(NetworkController.class).hasVoiceCallingFeature()) {
262 Dependency.get(NetworkController.class).addEmergencyListener(this);
263 Dependency.get(NetworkController.class).addCallback(this);
264 }
265 } else {
Anthony Chen54daefe2017-04-07 17:19:54 -0700266 mUserInfoController.removeCallback(this);
267 Dependency.get(NetworkController.class).removeEmergencyListener(this);
268 Dependency.get(NetworkController.class).removeCallback(this);
269 }
270 }
271
272 @Override
273 public void setQSPanel(final QSPanel qsPanel) {
274 mQsPanel = qsPanel;
275 if (mQsPanel != null) {
276 mMultiUserSwitch.setQsPanel(qsPanel);
277 }
278 }
279
280 @Override
281 public void onClick(View v) {
Evan Laird00e43c42018-01-22 20:25:45 -0500282 // Don't do anything until view are unhidden
283 if (!mExpanded) {
284 return;
285 }
286
Anthony Chen54daefe2017-04-07 17:19:54 -0700287 if (v == mSettingsButton) {
288 if (!Dependency.get(DeviceProvisionedController.class).isCurrentUserSetup()) {
289 // If user isn't setup just unlock the device and dump them back at SUW.
290 mActivityStarter.postQSRunnableDismissingKeyguard(() -> { });
291 return;
292 }
293 MetricsLogger.action(mContext,
294 mExpanded ? MetricsProto.MetricsEvent.ACTION_QS_EXPANDED_SETTINGS_LAUNCH
295 : MetricsProto.MetricsEvent.ACTION_QS_COLLAPSED_SETTINGS_LAUNCH);
296 if (mSettingsButton.isTunerClick()) {
297 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() -> {
298 if (TunerService.isTunerEnabled(mContext)) {
299 TunerService.showResetRequest(mContext, () -> {
300 // Relaunch settings so that the tuner disappears.
301 startSettingsActivity();
302 });
303 } else {
304 Toast.makeText(getContext(), R.string.tuner_toast,
305 Toast.LENGTH_LONG).show();
306 TunerService.setTunerEnabled(mContext, true);
307 }
308 startSettingsActivity();
309
310 });
311 } else {
312 startSettingsActivity();
313 }
Anthony Chen54daefe2017-04-07 17:19:54 -0700314 }
315 }
316
317 private void startSettingsActivity() {
318 mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS),
319 true /* dismissShade */);
320 }
321
322 @Override
323 public void setEmergencyCallsOnly(boolean show) {
324 boolean changed = show != mShowEmergencyCallsOnly;
325 if (changed) {
326 mShowEmergencyCallsOnly = show;
327 if (mExpanded) {
328 updateEverything();
329 }
330 }
331 }
332
333 @Override
334 public void onUserInfoChanged(String name, Drawable picture, String userAccount) {
335 if (picture != null &&
Selim Cinek1a891a92017-12-04 17:41:27 +0100336 UserManager.get(mContext).isGuestUser(KeyguardUpdateMonitor.getCurrentUser()) &&
Jason Monk826b6092017-08-29 11:30:52 -0400337 !(picture instanceof UserIconDrawable)) {
338 picture = picture.getConstantState().newDrawable(mContext.getResources()).mutate();
Anthony Chen54daefe2017-04-07 17:19:54 -0700339 picture.setColorFilter(
340 Utils.getColorAttr(mContext, android.R.attr.colorForeground),
341 Mode.SRC_IN);
342 }
343 mMultiUserAvatar.setImageDrawable(picture);
344 }
345}