-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainActivity.java
238 lines (194 loc) · 7.78 KB
/
MainActivity.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
236
237
238
package com.zapic.androiddemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import com.squareup.seismic.ShakeDetector;
import com.zapic.sdk.android.Zapic;
import org.json.JSONException;
import org.json.JSONObject;
import io.branch.referral.Branch;
import io.branch.referral.BranchError;
public class MainActivity extends Activity implements ShakeDetector.Listener {
private static final String TAG = "MainActivity";
private static final float TOUCH_TOLERANCE = 4f;
private PointF lastTouchPoint;
private SensorManager sensorManager;
private ShakeDetector shakeDetector;
private double totalTouchDistance;
public MainActivity() {
this.lastTouchPoint = null;
this.sensorManager = null;
this.shakeDetector = null;
this.totalTouchDistance = 0f;
}
private void enableImmersiveFullScreenMode() {
this.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
@Override
public void hearShake() {
}
@Override
@SuppressWarnings("deprecation")
protected void onCreate(final Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
// Hide the system status and navigation bars.
this.enableImmersiveFullScreenMode();
this.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(final int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
MainActivity.this.enableImmersiveFullScreenMode();
}
}
});
// Render the page.
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
final RelativeLayout zapicButton = this.findViewById(R.id.activity_main_zapic_button);
zapicButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Zapic.showDefaultPage(MainActivity.this);
}
});
final RelativeLayout challengesButton = this.findViewById(R.id.activity_main_challenges_button);
challengesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Zapic.showPage(MainActivity.this, "challenges");
}
});
this.findViewById(R.id.activity_main_container).setOnTouchListener(new View.OnTouchListener() {
@Override
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View view, MotionEvent motionEvent) {
return MainActivity.this.onTouch(motionEvent);
}
});
// Start Zapic.
Zapic.attachFragment(this);
// Get accelerometer.
this.sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (this.sensorManager != null) {
this.shakeDetector = new ShakeDetector(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
Log.d(TAG, "onNewIntent");
super.onNewIntent(intent);
this.setIntent(intent);
}
@Override
protected void onPause() {
Log.d(TAG, "onPause");
// Disable shake detector.
if (this.shakeDetector != null) {
this.shakeDetector.stop();
}
super.onPause();
}
@Override
protected void onResume() {
Log.d(TAG, "onResume");
// Hide the system status and navigation bars.
this.enableImmersiveFullScreenMode();
this.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(final int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
MainActivity.this.enableImmersiveFullScreenMode();
}
}
});
// Enable shake detector.
if (this.shakeDetector != null) {
this.shakeDetector.start(this.sensorManager);
}
super.onResume();
}
@Override
protected void onStart() {
Log.d(TAG, "onStart");
super.onStart();
Branch branch = Branch.getInstance();
branch.initSession(new Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (referringParams != null) {
Zapic.handleInteraction(referringParams);
} else {
Log.e(TAG, error.getMessage());
}
}
}, this.getIntent().getData(), this);
}
@Override
protected void onStop() {
Log.d(TAG, "onStop");
super.onStop();
}
private boolean onTouch(@NonNull final MotionEvent motionEvent) {
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
this.lastTouchPoint = new PointF(motionEvent.getX(), motionEvent.getY());
this.totalTouchDistance = 0f;
break;
case MotionEvent.ACTION_MOVE:
if (this.lastTouchPoint == null) {
this.lastTouchPoint = new PointF(motionEvent.getX(), motionEvent.getY());
this.totalTouchDistance = 0f;
} else {
float dx = Math.abs(this.lastTouchPoint.x - motionEvent.getX());
float dy = Math.abs(this.lastTouchPoint.y - motionEvent.getY());
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
this.lastTouchPoint.set(motionEvent.getX(), motionEvent.getY());
this.totalTouchDistance += Math.sqrt((dx * dx) + (dy * dy));
}
}
break;
case MotionEvent.ACTION_UP:
if (this.lastTouchPoint != null) {
float dx = Math.abs(this.lastTouchPoint.x - motionEvent.getX());
float dy = Math.abs(this.lastTouchPoint.y - motionEvent.getY());
long distance = Math.round(this.totalTouchDistance + Math.sqrt((dx * dx) + (dy * dy)));
// Send gameplay event to Zapic.
try {
Zapic.submitEvent(new JSONObject().put("DISTANCE", distance));
} catch (JSONException e) {
Log.e(TAG, "Failed to create gameplay event", e);
}
this.lastTouchPoint = null;
this.totalTouchDistance = 0f;
}
break;
default:
return false;
}
return true;
}
@Override
public void onWindowFocusChanged(final boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Hide the system status and navigation bars.
if (hasFocus) {
this.enableImmersiveFullScreenMode();
}
}
}