-
Notifications
You must be signed in to change notification settings - Fork 357
/
RecyclerViewActivity.java
235 lines (184 loc) · 6.76 KB
/
RecyclerViewActivity.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.asha.md360player4android;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.DrawableRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.asha.vrlib.MDVRLibrary;
import com.asha.vrlib.model.MDHitEvent;
import com.asha.vrlib.texture.MD360BitmapTexture;
import com.google.android.apps.muzei.render.GLTextureView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Uri[] sMockData;
private VRLibManager manager;
public static void start(Context context) {
Intent i = new Intent(context, RecyclerViewActivity.class);
context.startActivity(i);
}
public RecyclerViewActivity() {}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sMockData = new Uri[] {
getDrawableUri(R.drawable.bitmap360)
,getDrawableUri(R.drawable.texture)
};
setContentView(R.layout.activity_main);
manager = new VRLibManager(this);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
final FeedAdapter adapter = new FeedAdapter();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(null);
recyclerView.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
manager.fireResumed();
}
@Override
protected void onPause() {
super.onPause();
manager.firePaused();
}
@Override
protected void onDestroy() {
super.onDestroy();
manager.fireDestroy();
}
private static class FeedModel {
private final Uri uri;
private final int type;
public FeedModel(int type, Uri uri) {
this.type = type;
this.uri = uri;
}
}
private abstract class FeedVH extends RecyclerView.ViewHolder {
public FeedVH(ViewGroup vp, int layoutId) {
super(create(vp, layoutId));
}
public abstract void bind(FeedModel feedModel);
}
private class FeedTextVH extends FeedVH {
public FeedTextVH(ViewGroup vp) {
super(vp, R.layout.feed_text_layout);
}
@Override
public void bind(FeedModel feedModel) {
}
}
private class FeedVRVH extends FeedVH implements MDVRLibrary.IBitmapProvider {
private TextView text;
private GLTextureView glTextureView;
private ViewGroup parent;
private MDVRLibrary vrlib;
private FeedModel model;
private long ts;
public FeedVRVH(ViewGroup vp) {
super(vp, R.layout.feed_panorama_layout);
text = (TextView) itemView.findViewById(R.id.feed_text);
glTextureView = (GLTextureView) itemView.findViewById(R.id.feed_texture_view);
parent = (ViewGroup) glTextureView.getParent();
}
@Override
public void bind(FeedModel model) {
this.model = model;
ensureVRLib();
vrlib.notifyPlayerChanged();
}
private void ensureVRLib() {
if (vrlib == null) {
vrlib = manager.create(this, glTextureView);
vrlib.setEyePickChangedListener(new MDVRLibrary.IEyePickListener2() {
@Override
public void onHotspotHit(MDHitEvent hitEvent) {
long delta = System.currentTimeMillis() - ts;
if (delta < 500) {
return;
}
String brief = vrlib.getDirectorBrief().toString();
text.setText(brief);
ts = System.currentTimeMillis();
}
});
}
}
@Override
public void onProvideBitmap(final MD360BitmapTexture.Callback callback) {
if (model == null) {
return;
}
Picasso.with(itemView.getContext()).load(model.uri).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
vrlib.onTextureResize(bitmap.getWidth(), bitmap.getHeight());
callback.texture(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
}
private Uri getDrawableUri(@DrawableRes int resId){
Resources resources = getResources();
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId) );
}
private class FeedAdapter extends RecyclerView.Adapter<FeedVH> {
private List<FeedModel> feeds = new ArrayList<>();
public FeedAdapter() {
int i = 0;
while (i++ < 50) {
Uri uri = sMockData[(int) (Math.random() * sMockData.length)];
feeds.add(new FeedModel(Math.random() > 0.3 ? 0 : 1, uri));
}
}
@Override
public FeedVH onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
return new FeedVRVH(parent);
} else {
return new FeedTextVH(parent);
}
}
@Override
public void onBindViewHolder(FeedVH holder, int position) {
holder.bind(feeds.get(position));
}
@Override
public void onViewRecycled(FeedVH holder) {
super.onViewRecycled(holder);
}
@Override
public int getItemViewType(int position) {
return feeds.get(position).type;
}
@Override
public int getItemCount() {
return feeds.size();
}
}
private static View create(ViewGroup vp, int layout) {
return LayoutInflater.from(vp.getContext()).inflate(layout, vp, false);
}
}