forked from segmentio/analytics-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
150 lines (131 loc) · 4.87 KB
/
MainActivity.java
File metadata and controls
150 lines (131 loc) · 4.87 KB
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Segment, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.bytegain.analytics.sample;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.bytegain.analytics.Analytics;
import com.bytegain.analytics.integrations.AttemptGoalPayload;
import com.bytegain.analytics.integrations.ReportGoalResultPayload;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
public class MainActivity extends Activity {
@BindView(R.id.user_id)
EditText userId;
/** Returns true if the string is null, or empty (when trimmed). */
public static boolean isNullOrEmpty(String text) {
return TextUtils.isEmpty(text) || text.trim().length() == 0;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.action_attempt_goal)
void onButtonAttemptGoalClicked() {
AttemptGoalPayload.YesCallback yesCallback =
(str) -> {
Log.i("SampleApp", "YesCallback was called: " + str);
// Add code to request user's email address here.
// If the user provides the info, call
// Analytics.with(this)
// .reportGoalResult("multi", null, null, ReportGoalResultPayload.GoalResult.success);
// If the user declines, call
// Analytics.with(this)
// .reportGoalResult("multi", null, null, ReportGoalResultPayload.GoalResult.failure);
};
for (int i = 0; i < 1; i++) {
Analytics.with(this).attemptGoal("requestEmail", null, null, yesCallback, null);
}
}
@OnClick(R.id.action_report_goal_success)
void onButtonReportGoalSuccess() {
Analytics.with(this)
.reportGoalResult("multi", null, null, ReportGoalResultPayload.GoalResult.success);
}
@OnClick(R.id.action_report_goal_failure)
void onButtonReportGoalFailure() {
Analytics.with(this)
.reportGoalResult("multi", null, null, ReportGoalResultPayload.GoalResult.failure);
}
@OnClick(R.id.action_track_a)
void onButtonAClicked() {
Analytics.with(this).track("Button A Clicked");
}
@OnClick(R.id.action_track_b)
void onButtonBClicked() {
Analytics.with(this).track("Button B Clicked");
}
@OnClick(R.id.action_identify)
void onIdentifyButtonClicked() {
String id = userId.getText().toString();
if (isNullOrEmpty(id)) {
Toast.makeText(this, R.string.id_required, Toast.LENGTH_LONG).show();
} else {
Analytics.with(this).identify(id);
}
}
@OnClick(R.id.action_flush)
void onFlushButtonClicked() {
Analytics.with(this).flush();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_view_docs) {
Intent intent =
new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://segment.com/docs/tutorials/quickstart-android/"));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.no_browser_available, Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
}