blob: 70deeead6cf1cebcf05296a868a16fc86a4f155d [file] [log] [blame]
Sergey Vasilinets50f67332016-07-07 20:38:07 -07001/*
2 * Copyright (C) 2016 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 */
16package com.android.test.uibench;
17
18import android.content.Context;
19import android.graphics.drawable.Drawable;
20import android.os.Bundle;
21import android.support.annotation.Nullable;
22import android.support.v7.app.AppCompatActivity;
23import android.view.GestureDetector;
24import android.view.LayoutInflater;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.ViewConfiguration;
28import android.widget.LinearLayout;
29import android.widget.Scroller;
30import android.widget.TextView;
31
32import java.util.ArrayList;
33
34public class NotificationShadeActivity extends AppCompatActivity {
35
36 private static class FakeNotificationStackView extends LinearLayout {
37 private static final int INITIAL_ELEVATION = 40;
38
39 private LayoutInflater mLayoutInflater;
40 private GestureDetector mGestureDetector;
41 private Scroller mScroller;
42 private ArrayList<View> mChildren = new ArrayList<>();
43 private int mChildrenCount = 0;
44 private int mFullHeight = 0;
45 private int mScaledTouchSlop;
46
47 private Runnable mUpdateAction = new Runnable() {
48 @Override
49 public void run() {
50 if (mScroller.computeScrollOffset()) {
51 updateState(mScroller.getCurrY());
52 postOnAnimation(this);
53 }
54 }
55 };
56
57 private GestureDetector.OnGestureListener mGestureListener =
58 new GestureDetector.SimpleOnGestureListener() {
59 @Override
60 public boolean onDown(MotionEvent motionEvent) {
61 return true;
62 }
63
64 @Override
65 public boolean onFling(MotionEvent e1, MotionEvent e2, float vX, float vY) {
66 if (Math.abs(e1.getY() - e2.getY()) <= mScaledTouchSlop) {
67 return false;
68 }
69 mScroller.fling(0, mFullHeight, 0, (int) vY, 0, 0, 0, getHeight());
70 postOnAnimation(mUpdateAction);
71 return true;
72 }
73 };
74
75 private void generateNextView() {
76 View view = mLayoutInflater.inflate(R.layout.notification, this, false);
77 boolean even = mChildren.size() % 2 == 0;
78 Context context = getContext();
79 ((TextView) view.findViewById(R.id.title)).setText(even ?
80 "Very important notification" : "Next video to watch");
81 ((TextView) view.findViewById(R.id.line2)).setText(even ?
82 "Wifi nearby" : "Amazing cats");
83 TextView infoView = (TextView) view.findViewById(R.id.info);
84 Drawable drawable = context.getDrawable(even ? R.drawable.ic_menu_manage
85 : R.drawable.ic_menu_slideshow);
86 int size = context.getResources().getDimensionPixelSize(R.dimen.notification_icon_size);
87 drawable.setBounds(0, 0, size, size);
88 infoView.setCompoundDrawables(drawable, null, null, null);
89 infoView.setText(even ? "Android System" : "Youtube");
90 mChildren.add(view);
91 }
92
93 public FakeNotificationStackView(Context context) {
94 super(context);
95 setOrientation(LinearLayout.VERTICAL);
96 mLayoutInflater = LayoutInflater.from(getContext());
97 mGestureDetector = new GestureDetector(getContext(), mGestureListener);
98 mScaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
99 mScroller = new Scroller(getContext());
100 }
101
102 private int lastChildHeight() {
103 return (int) mChildren.get(mChildrenCount - 1).getTag();
104 }
105
106 private void updateState(int expectedHeight) {
107 if (expectedHeight == 0 && mChildrenCount == 0) {
108 return;
109 }
110 for (View v: mChildren) {
111 v.setTranslationY(0);
112 v.setElevation(INITIAL_ELEVATION);
113 }
114 if (mChildrenCount != 0 && expectedHeight < mFullHeight - lastChildHeight()) {
115 while (mChildrenCount > 0){
116 if (expectedHeight > mFullHeight - lastChildHeight()) {
117 break;
118 }
119 mFullHeight -= lastChildHeight();
120 removeView(mChildren.get(mChildrenCount - 1));
121 mChildrenCount--;
122 }
123 } else if (expectedHeight > mFullHeight) {
124 while (expectedHeight > mFullHeight) {
125 if (mChildrenCount == mChildren.size()) {
126 generateNextView();
127 }
128 mChildrenCount++;
129 View child = mChildren.get(mChildrenCount - 1);
130 child.setElevation(INITIAL_ELEVATION);
131 int widthSpec = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST);
132 int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
133 child.measure(widthSpec, heightSpec);
134 addView(child);
135 int measuredHeight = child.getMeasuredHeight();
136 child.setTag(measuredHeight);
137 mFullHeight += measuredHeight;
138 }
139 }
140 if (mChildrenCount == 0) {
141 return;
142 }
143 View lastChild = mChildren.get(mChildrenCount - 1);
144 int translationY = expectedHeight - mFullHeight;
145 lastChild.setTranslationY(translationY);
146 float p = - ((float) translationY) / lastChildHeight();
147 lastChild.setElevation((1 - p) * INITIAL_ELEVATION);
148 }
149
150 @Override
151 public boolean onTouchEvent(MotionEvent ev) {
152 return mGestureDetector.onTouchEvent(ev);
153 }
154 }
155
156 @Override
157 protected void onCreate(@Nullable Bundle savedInstanceState) {
158 super.onCreate(savedInstanceState);
159 setContentView(new FakeNotificationStackView(this));
160 }
161}