1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/*
* Copyright (C) 2012 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;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import com.android.internal.widget.SizeAdaptiveLayout;
public class ExpandHelper implements Gefingerpoken, OnClickListener {
public interface Callback {
View getChildAtPosition(MotionEvent ev);
View getChildAtPosition(float x, float y);
}
private static final String TAG = "ExpandHelper";
protected static final boolean DEBUG = false;
private static final long EXPAND_DURATION = 250;
@SuppressWarnings("unused")
private Context mContext;
private boolean mStretching;
private View mCurrView;
private float mOldHeight;
private float mNaturalHeight;
private float mInitialTouchSpan;
private Callback mCallback;
private ScaleGestureDetector mDetector;
private ViewScaler mScaler;
private ObjectAnimator mAnimation;
private int mSmallSize;
private int mLargeSize;
private class ViewScaler {
View mView;
public ViewScaler() {}
public void setView(View v) {
mView = v;
}
public void setHeight(float h) {
Log.v(TAG, "SetHeight: setting to " + h);
ViewGroup.LayoutParams lp = mView.getLayoutParams();
lp.height = (int)h;
mView.setLayoutParams(lp);
mView.requestLayout();
}
public float getHeight() {
int height = mView.getLayoutParams().height;
if (height < 0) {
height = mView.getMeasuredHeight();
}
return (float) height;
}
public int getNaturalHeight(int maximum) {
ViewGroup.LayoutParams lp = mView.getLayoutParams();
if (DEBUG) Log.v(TAG, "Inspecting a child of type: " + mView.getClass().getName());
int oldHeight = lp.height;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mView.setLayoutParams(lp);
mView.measure(
View.MeasureSpec.makeMeasureSpec(mView.getMeasuredWidth(),
View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(maximum,
View.MeasureSpec.AT_MOST));
lp.height = oldHeight;
mView.setLayoutParams(lp);
return mView.getMeasuredHeight();
}
}
public ExpandHelper(Context context, Callback callback, int small, int large) {
mSmallSize = small;
mLargeSize = large;
mContext = context;
mCallback = callback;
mScaler = new ViewScaler();
mDetector =
new ScaleGestureDetector(context,
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
if (DEBUG) Log.v(TAG, "onscalebegin()");
View v = mCallback.getChildAtPosition(detector.getFocusX(), detector.getFocusY());
// your fingers have to be somewhat close to the bounds of the view in question
mInitialTouchSpan = Math.abs(detector.getCurrentSpanY());
if (DEBUG) Log.d(TAG, "got mInitialTouchSpan: " + mInitialTouchSpan);
mStretching = initScale(v);
return mStretching;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
if (DEBUG) Log.v(TAG, "onscale() on " + mCurrView);
float h = Math.abs(detector.getCurrentSpanY());
if (DEBUG) Log.d(TAG, "current span is: " + h);
h = h + mOldHeight - mInitialTouchSpan;
h = h<mSmallSize?mSmallSize:(h>mLargeSize?mLargeSize:h);
h = h>mNaturalHeight?mNaturalHeight:h;
if (DEBUG) Log.d(TAG, "scale continues: h=" + h);
mScaler.setHeight(h);
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
if (DEBUG) Log.v(TAG, "onscaleend()");
// I guess we're alone now
if (DEBUG) Log.d(TAG, "scale end");
finishScale(false);
}
});
}
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (DEBUG) Log.d(TAG, "interceptTouch: act=" + (ev.getAction()) +
" stretching=" + mStretching);
mDetector.onTouchEvent(ev);
return mStretching;
}
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
if (DEBUG) Log.d(TAG, "touch: act=" + (action) + " stretching=" + mStretching);
if (mStretching) {
mDetector.onTouchEvent(ev);
}
switch (action) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mStretching = false;
mCurrView = null;
break;
}
return true;
}
private boolean initScale(View v) {
if (v != null) {
if (DEBUG) Log.d(TAG, "scale begins on view: " + v);
mStretching = true;
mCurrView = v;
mScaler.setView(v);
mOldHeight = mScaler.getHeight();
mNaturalHeight = mScaler.getNaturalHeight(mLargeSize);
if (DEBUG) Log.d(TAG, "got mOldHeight: " + mOldHeight +
" mNaturalHeight: " + mNaturalHeight);
v.getParent().requestDisallowInterceptTouchEvent(true);
if (DEBUG) v.setBackgroundColor(0xFFFFFF00);
}
return mStretching;
}
private void finishScale(boolean force) {
float h = mScaler.getHeight();
final boolean wasClosed = (mOldHeight == mSmallSize);
if (wasClosed) {
h = (force || h > mSmallSize) ? mNaturalHeight : mSmallSize;
} else {
h = (force || h < mNaturalHeight) ? mSmallSize : mNaturalHeight;
}
if (DEBUG) mCurrView.setBackgroundColor(0);
mAnimation = ObjectAnimator.ofFloat(mScaler, "height", h).setDuration(EXPAND_DURATION);
mAnimation.start();
mStretching = false;
mCurrView = null;
}
@Override
public void onClick(View v) {
initScale(v);
finishScale(true);
}
}
|