/
HeadListView.java
158 lines (127 loc) · 4.04 KB
/
HeadListView.java
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
package de.tud.ess;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
public class HeadListView extends ListView implements SensorEventListener {
private static final float INVALID_X = 10;
private static final String TAG = HeadListView.class.getSimpleName();
private Sensor mSensor;
private int mLastAccuracy;
private SensorManager mSensorManager;
private float mStartX = INVALID_X;
private static final int SENSOR_RATE_uS = 400000;
private static final float VELOCITY = (float) (Math.PI / 180 * 2); // scroll one item per 2°
/**
* our content is larger than our view, and thus there is something to scroll
*/
private boolean canScroll;
public HeadListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public HeadListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HeadListView(Context context) {
super(context);
init();
}
public void init() {
if (isInEditMode())
return;
mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}
public void activate() {
if (mSensor == null)
return;
mStartX = INVALID_X;
lastPosition = -1;
mSensorManager.registerListener(this, mSensor, SENSOR_RATE_uS);
Log.d(TAG, "Automatic scrolling enabled");
}
public void deactivate() {
mSensorManager.unregisterListener(this);
mStartX = INVALID_X;
lastPosition = -1;
Log.d(TAG, "Automatic scrolling disabled");
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
if (isSensorRequired()) {
activate();
} else {
deactivate();
}
}
private boolean isSensorRequired() {
ListAdapter adapter = getAdapter();
return adapter != null && adapter.getCount() > 1 && getVisibility() == View.VISIBLE;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
canScroll = getAdapter() != null && (getChildCount() < getAdapter().getCount());
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (isSensorRequired())
activate();
else {
deactivate();
}
}
@Deprecated
private boolean isContentBiggerThanView() {
View a = getChildAt(0),
b = getChildAt(getChildCount() - 1);
return (a != null && b != null) && (getListPaddingTop() < a.getTop() || b.getBottom() > getBottom() - getListPaddingBottom());
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
mLastAccuracy = accuracy;
}
protected int lastPosition = -1;
@Override
public void onSensorChanged(SensorEvent event) {
float[] mat = new float[9],
orientation = new float[3];
if (mLastAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
return;
SensorManager.getRotationMatrixFromVector(mat, event.values);
SensorManager.remapCoordinateSystem(mat, SensorManager.AXIS_X, SensorManager.AXIS_Z, mat);
SensorManager.getOrientation(mat, orientation);
float z = orientation[0], // see https://developers.google.com/glass/develop/gdk/location-sensors/index
x = orientation[1],
y = orientation[2];
if (mStartX == INVALID_X)
mStartX = x;
int position = (int) ((mStartX - x) * -1 / VELOCITY);
if (position < 0) {
mStartX = x;
position = 0;
} else if (position >= getCount()) {
float endX = (getCount() * VELOCITY) + mStartX;
mStartX += x - endX;
position = getCount()-1;
}
if (lastPosition != position) {
// Log.d(TAG, "Selecting pos. "+ position);
if (canScroll) {
smoothScrollToPosition(position);
}
setSelection(position);
lastPosition = position;
}
}
}