blob: c8f167063add94b61d24bdca735acd1f4a7a76ec [file] [log] [blame]
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.android.systemui.qs;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.Nullable;
import android.app.Fragment;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout.LayoutParams;
import com.android.systemui.Interpolators;
import com.android.systemui.R;
import com.android.systemui.plugins.qs.QS;
import com.android.systemui.qs.customize.QSCustomizer;
import com.android.systemui.statusbar.phone.NotificationsQuickSettingsContainer;
import com.android.systemui.statusbar.phone.QSTileHost;
import com.android.systemui.statusbar.phone.QuickStatusBarHeader;
import com.android.systemui.statusbar.stack.StackStateAnimator;
public class QSFragment extends Fragment implements QS {
private static final String TAG = "QS";
private static final boolean DEBUG = false;
private final Rect mQsBounds = new Rect();
private boolean mQsExpanded;
private boolean mHeaderAnimating;
private boolean mKeyguardShowing;
private boolean mStackScrollerOverscrolling;
private long mDelay;
private QSAnimator mQSAnimator;
private HeightListener mPanelView;
protected QuickStatusBarHeader mHeader;
private QSCustomizer mQSCustomizer;
protected QSPanel mQSPanel;
private QSDetail mQSDetail;
private boolean mListening;
private QSContainerImpl mContainer;
private int mLayoutDirection;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.qs_panel, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mQSPanel = (QSPanel) view.findViewById(R.id.quick_settings_panel);
mQSDetail = (QSDetail) view.findViewById(R.id.qs_detail);
mHeader = (QuickStatusBarHeader) view.findViewById(R.id.header);
mContainer = (QSContainerImpl) view;
mQSDetail.setQsPanel(mQSPanel, mHeader);
mQSAnimator = new QSAnimator(this, (QuickQSPanel) mHeader.findViewById(R.id.quick_qs_panel),
mQSPanel);
mQSCustomizer = (QSCustomizer) view.findViewById(R.id.qs_customize);
mQSCustomizer.setQs(this);
}
public void setPanelView(HeightListener panelView) {
mPanelView = panelView;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.getLayoutDirection() != mLayoutDirection) {
mLayoutDirection = newConfig.getLayoutDirection();
mQSAnimator.onRtlChanged();
}
}
@Override
public void setContainer(ViewGroup container) {
if (container instanceof NotificationsQuickSettingsContainer) {
mQSCustomizer.setContainer((NotificationsQuickSettingsContainer) container);
}
}
public boolean isCustomizing() {
return mQSCustomizer.isCustomizing();
}
public void setHost(QSTileHost qsh) {
mQSPanel.setHost(qsh, mQSCustomizer);
mHeader.setQSPanel(mQSPanel);
mQSDetail.setHost(qsh);
mQSAnimator.setHost(qsh);
}
private void updateQsState() {
final boolean expandVisually = mQsExpanded || mStackScrollerOverscrolling
|| mHeaderAnimating;
mQSPanel.setExpanded(mQsExpanded);
mQSDetail.setExpanded(mQsExpanded);
mHeader.setVisibility((mQsExpanded || !mKeyguardShowing || mHeaderAnimating)
? View.VISIBLE
: View.INVISIBLE);
mHeader.setExpanded((mKeyguardShowing && !mHeaderAnimating)
|| (mQsExpanded && !mStackScrollerOverscrolling));
mQSPanel.setVisibility(expandVisually ? View.VISIBLE : View.INVISIBLE);
}
public BaseStatusBarHeader getHeader() {
return mHeader;
}
public QSPanel getQsPanel() {
return mQSPanel;
}
public QSCustomizer getCustomizer() {
return mQSCustomizer;
}
public boolean isShowingDetail() {
return mQSPanel.isShowingCustomize() || mQSDetail.isShowingDetail();
}
public void setHeaderClickable(boolean clickable) {
if (DEBUG) Log.d(TAG, "setHeaderClickable " + clickable);
mHeader.setClickable(clickable);
}
public void setExpanded(boolean expanded) {
if (DEBUG) Log.d(TAG, "setExpanded " + expanded);
mQsExpanded = expanded;
mQSPanel.setListening(mListening && mQsExpanded);
updateQsState();
}
public void setKeyguardShowing(boolean keyguardShowing) {
if (DEBUG) Log.d(TAG, "setKeyguardShowing " + keyguardShowing);
mKeyguardShowing = keyguardShowing;
mQSAnimator.setOnKeyguard(keyguardShowing);
updateQsState();
}
public void setOverscrolling(boolean stackScrollerOverscrolling) {
if (DEBUG) Log.d(TAG, "setOverscrolling " + stackScrollerOverscrolling);
mStackScrollerOverscrolling = stackScrollerOverscrolling;
updateQsState();
}
public void setListening(boolean listening) {
if (DEBUG) Log.d(TAG, "setListening " + listening);
mListening = listening;
mHeader.setListening(listening);
mQSPanel.setListening(mListening && mQsExpanded);
}
public void setHeaderListening(boolean listening) {
mHeader.setListening(listening);
}
public void setQsExpansion(float expansion, float headerTranslation) {
if (DEBUG) Log.d(TAG, "setQSExpansion " + expansion + " " + headerTranslation);
mContainer.setExpansion(expansion);
final float translationScaleY = expansion - 1;
if (!mHeaderAnimating) {
getView().setTranslationY(mKeyguardShowing ? (translationScaleY * mHeader.getHeight())
: headerTranslation);
}
mHeader.setExpansion(mKeyguardShowing ? 1 : expansion);
mQSPanel.setTranslationY(translationScaleY * mQSPanel.getHeight());
mQSDetail.setFullyExpanded(expansion == 1);
mQSAnimator.setPosition(expansion);
// Set bounds on the QS panel so it doesn't run over the header.
mQsBounds.top = (int) (mQSPanel.getHeight() * (1 - expansion));
mQsBounds.right = mQSPanel.getWidth();
mQsBounds.bottom = mQSPanel.getHeight();
mQSPanel.setClipBounds(mQsBounds);
}
public void animateHeaderSlidingIn(long delay) {
if (DEBUG) Log.d(TAG, "animateHeaderSlidingIn");
// If the QS is already expanded we don't need to slide in the header as it's already
// visible.
if (!mQsExpanded) {
mHeaderAnimating = true;
mDelay = delay;
getView().getViewTreeObserver().addOnPreDrawListener(mStartHeaderSlidingIn);
}
}
public void animateHeaderSlidingOut() {
if (DEBUG) Log.d(TAG, "animateHeaderSlidingOut");
mHeaderAnimating = true;
getView().animate().y(-mHeader.getHeight())
.setStartDelay(0)
.setDuration(StackStateAnimator.ANIMATION_DURATION_STANDARD)
.setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
getView().animate().setListener(null);
mHeaderAnimating = false;
updateQsState();
}
})
.start();
}
@Override
public void closeDetail() {
mQSPanel.closeDetail();
}
public void notifyCustomizeChanged() {
// The customize state changed, so our height changed.
mContainer.updateBottom();
mQSPanel.setVisibility(!mQSCustomizer.isCustomizing() ? View.VISIBLE : View.INVISIBLE);
mHeader.setVisibility(!mQSCustomizer.isCustomizing() ? View.VISIBLE : View.INVISIBLE);
// Let the panel know the position changed and it needs to update where notifications
// and whatnot are.
mPanelView.onQsHeightChanged();
}
/**
* The height this view wants to be. This is different from {@link #getMeasuredHeight} such that
* during closing the detail panel, this already returns the smaller height.
*/
public int getDesiredHeight() {
if (mQSCustomizer.isCustomizing()) {
return getView().getHeight();
}
if (mQSDetail.isClosingDetail()) {
int panelHeight = ((LayoutParams) mQSPanel.getLayoutParams()).topMargin
+ mQSPanel.getMeasuredHeight();
return panelHeight + getView().getPaddingBottom();
} else {
return getView().getMeasuredHeight();
}
}
@Override
public void setHeightOverride(int desiredHeight) {
mContainer.setHeightOverride(desiredHeight);
}
public int getQsMinExpansionHeight() {
return mHeader.getHeight();
}
@Override
public void hideImmediately() {
getView().animate().cancel();
getView().setY(-mHeader.getHeight());
}
private final ViewTreeObserver.OnPreDrawListener mStartHeaderSlidingIn
= new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
getView().getViewTreeObserver().removeOnPreDrawListener(this);
getView().animate()
.translationY(0f)
.setStartDelay(mDelay)
.setDuration(StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE)
.setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
.setListener(mAnimateHeaderSlidingInListener)
.start();
getView().setY(-mHeader.getHeight());
return true;
}
};
private final Animator.AnimatorListener mAnimateHeaderSlidingInListener
= new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mHeaderAnimating = false;
updateQsState();
}
};
}