Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2015 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 
     17 package com.android.systemui.statusbar.notification;
     18 
     19 import android.annotation.Nullable;
     20 import android.content.Context;
     21 import android.text.TextUtils;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.widget.TextView;
     25 
     26 import com.android.keyguard.AlphaOptimizedLinearLayout;
     27 import com.android.systemui.R;
     28 import com.android.systemui.ViewInvertHelper;
     29 import com.android.systemui.statusbar.CrossFadeHelper;
     30 import com.android.systemui.statusbar.TransformableView;
     31 import com.android.systemui.statusbar.ViewTransformationHelper;
     32 import com.android.systemui.statusbar.phone.NotificationPanelView;
     33 
     34 /**
     35  * A hybrid view which may contain information about one ore more notifications.
     36  */
     37 public class HybridNotificationView extends AlphaOptimizedLinearLayout
     38         implements TransformableView {
     39 
     40     private ViewTransformationHelper mTransformationHelper;
     41 
     42     protected TextView mTitleView;
     43     protected TextView mTextView;
     44     private ViewInvertHelper mInvertHelper;
     45 
     46     public HybridNotificationView(Context context) {
     47         this(context, null);
     48     }
     49 
     50     public HybridNotificationView(Context context, @Nullable AttributeSet attrs) {
     51         this(context, attrs, 0);
     52     }
     53 
     54     public HybridNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
     55         this(context, attrs, defStyleAttr, 0);
     56     }
     57 
     58     public HybridNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
     59             int defStyleRes) {
     60         super(context, attrs, defStyleAttr, defStyleRes);
     61     }
     62 
     63     public TextView getTitleView() {
     64         return mTitleView;
     65     }
     66 
     67     public TextView getTextView() {
     68         return mTextView;
     69     }
     70 
     71     @Override
     72     protected void onFinishInflate() {
     73         super.onFinishInflate();
     74         mTitleView = (TextView) findViewById(R.id.notification_title);
     75         mTextView = (TextView) findViewById(R.id.notification_text);
     76         mInvertHelper = new ViewInvertHelper(this, NotificationPanelView.DOZE_ANIMATION_DURATION);
     77         mTransformationHelper = new ViewTransformationHelper();
     78         mTransformationHelper.setCustomTransformation(
     79                 new ViewTransformationHelper.CustomTransformation() {
     80                     @Override
     81                     public boolean transformTo(TransformState ownState, TransformableView notification,
     82                             float transformationAmount) {
     83                         // We want to transform to the same y location as the title
     84                         TransformState otherState = notification.getCurrentState(
     85                                 TRANSFORMING_VIEW_TITLE);
     86                         CrossFadeHelper.fadeOut(mTextView, transformationAmount);
     87                         if (otherState != null) {
     88                             ownState.transformViewVerticalTo(otherState, transformationAmount);
     89                             otherState.recycle();
     90                         }
     91                         return true;
     92                     }
     93 
     94                     @Override
     95                     public boolean transformFrom(TransformState ownState,
     96                             TransformableView notification, float transformationAmount) {
     97                         // We want to transform from the same y location as the title
     98                         TransformState otherState = notification.getCurrentState(
     99                                 TRANSFORMING_VIEW_TITLE);
    100                         CrossFadeHelper.fadeIn(mTextView, transformationAmount);
    101                         if (otherState != null) {
    102                             ownState.transformViewVerticalFrom(otherState, transformationAmount);
    103                             otherState.recycle();
    104                         }
    105                         return true;
    106                     }
    107                 }, TRANSFORMING_VIEW_TEXT);
    108         mTransformationHelper.addTransformedView(TRANSFORMING_VIEW_TITLE, mTitleView);
    109         mTransformationHelper.addTransformedView(TRANSFORMING_VIEW_TEXT, mTextView);
    110     }
    111 
    112     public void bind(CharSequence title) {
    113         bind(title, null);
    114     }
    115 
    116     public void bind(CharSequence title, CharSequence text) {
    117         mTitleView.setText(title);
    118         mTitleView.setVisibility(TextUtils.isEmpty(title) ? GONE : VISIBLE);
    119         if (TextUtils.isEmpty(text)) {
    120             mTextView.setVisibility(GONE);
    121             mTextView.setText(null);
    122         } else {
    123             mTextView.setVisibility(VISIBLE);
    124             mTextView.setText(text.toString());
    125         }
    126         requestLayout();
    127     }
    128 
    129     public void setDark(boolean dark, boolean fade, long delay) {
    130         mInvertHelper.setInverted(dark, fade, delay);
    131     }
    132 
    133     @Override
    134     public TransformState getCurrentState(int fadingView) {
    135         return mTransformationHelper.getCurrentState(fadingView);
    136     }
    137 
    138     @Override
    139     public void transformTo(TransformableView notification, Runnable endRunnable) {
    140         mTransformationHelper.transformTo(notification, endRunnable);
    141     }
    142 
    143     @Override
    144     public void transformTo(TransformableView notification, float transformationAmount) {
    145         mTransformationHelper.transformTo(notification, transformationAmount);
    146     }
    147 
    148     @Override
    149     public void transformFrom(TransformableView notification) {
    150         mTransformationHelper.transformFrom(notification);
    151     }
    152 
    153     @Override
    154     public void transformFrom(TransformableView notification, float transformationAmount) {
    155         mTransformationHelper.transformFrom(notification, transformationAmount);
    156     }
    157 
    158     @Override
    159     public void setVisible(boolean visible) {
    160         setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    161         mTransformationHelper.setVisible(visible);
    162     }
    163 }
    164