-
Notifications
You must be signed in to change notification settings - Fork 85
/
AppNotificationTestActivity.java
230 lines (195 loc) · 7.15 KB
/
AppNotificationTestActivity.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
/**
* Copyright (C) 2016 Clover Network, Inc.
*
* 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.clover.android.sdk.examples;
import android.accounts.Account;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.clover.sdk.util.AuthTask;
import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.util.CloverAuth;
import com.clover.sdk.v1.ResultStatus;
import com.clover.sdk.v1.ServiceConnector;
import com.clover.sdk.v1.app.AppNotification;
import com.clover.sdk.v1.app.AppNotificationReceiver;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Demonstrates sending an app notification by either binding to the app service
* or by sending an HTTP POST to the Clover Service REST endpoint.
*/
public class AppNotificationTestActivity extends Activity {
private static final String TAG = AppNotificationTestActivity.class.getSimpleName();
private static final String KEY_LOG = "log";
enum ConnectionType {HTTP, BoundService}
;
private TextView logText;
private EditText appEventText;
private EditText payloadText;
private Button sendButton;
private AuthTask authTask;
private Spinner connectionTypeSpinner;
private CloverAuth.AuthResult authResult;
private SimpleDateFormat dateFormat = new SimpleDateFormat("H:mm:ss");
/**
* Receives an app notification and logs it.
*/
private AppNotificationReceiver receiver = new AppNotificationReceiver() {
@Override
public void onReceive(Context context, AppNotification notification) {
log("Received " + notification);
}
};
private ConnectionType getConnectionType() {
if (connectionTypeSpinner.getSelectedItemPosition() == 0) {
return ConnectionType.BoundService;
}
return ConnectionType.HTTP;
}
/**
* Sends a notification by POSTing to the REST endpoint.
*/
private class HttpNotificationTask extends AsyncTask<AppNotification, Void, Exception> {
@Override
protected Exception doInBackground(AppNotification... notifications) {
AppNotification notification = notifications[0];
try {
// Post the app notification to the REST API.
InventoryTestActivity.CustomHttpClient client = InventoryTestActivity.CustomHttpClient.getHttpClient();
JSONObject request = new JSONObject();
request.put("notification", notification.toJson());
String uri = authResult.baseUrl + "/v2/merchant/" + authResult.merchantId + "/apps/" +
authResult.appId + "/notifications";
Log.i(TAG, "Posting app notification to " + uri);
client.post(uri, request.toString(), authResult.authToken);
} catch (Exception e) {
String msg = getResources().getString(R.string.error_while_sending, e.getMessage());
Log.w(TAG, msg);
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception e) {
super.onPostExecute(e);
if (e == null) {
log("Successfully sent notification with HTTP.");
} else {
log("Unable to send notification with HTTP: " + e.toString());
}
}
}
private ServiceConnector.Callback<Void> serviceNotifyCallback = new ServiceConnector.Callback<Void>() {
@Override
public void onServiceSuccess(Void result, ResultStatus status) {
log("Successfully sent notification with the app service: " + status);
}
@Override
public void onServiceFailure(ResultStatus status) {
log("Unable to send notification with the app service: " + status);
}
@Override
public void onServiceConnectionFailure() {
log("Service connection failure.");
}
};
private View.OnClickListener sendListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String appEvent = appEventText.getText().toString();
String payload = payloadText.getText().toString();
AppNotification notification = new AppNotification(appEvent, payload);
switch (getConnectionType()) {
case HTTP:
if (authResult == null) {
Toast.makeText(AppNotificationTestActivity.this, R.string.cannot_send_auth_failed, Toast.LENGTH_LONG).show();
return;
}
log("Sending notification with HTTP");
HttpNotificationTask httpTask = new HttpNotificationTask();
httpTask.execute(notification);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_notification_test);
getActionBar().setTitle(R.string.app_notification_test);
logText = (TextView) findViewById(R.id.log_text);
appEventText = (EditText) findViewById(R.id.app_event_text);
payloadText = (EditText) findViewById(R.id.payload_text);
sendButton = (Button) findViewById(R.id.send_button);
connectionTypeSpinner = (Spinner) findViewById(R.id.connection_type_spinner);
sendButton.setOnClickListener(sendListener);
if (savedInstanceState != null) {
logText.setText(savedInstanceState.getString(KEY_LOG));
}
receiver.register(this);
log("Starting app notification test.");
// Start authentication.
final Account account = CloverAccount.getAccount(AppNotificationTestActivity.this);
if (account == null) {
log("Account not found.");
return;
}
log("Authenticating.");
authTask = new AuthTask(this) {
@Override
protected void onAuthComplete(boolean success, CloverAuth.AuthResult result) {
if (success) {
authResult = result;
log("Auth successful: " + result + ", " + result.authData);
} else {
log("Auth error: " + getErrorMessage() + ". Cannot use HTTP.");
}
}
};
authTask.execute(account);
}
@Override
protected void onDestroy() {
super.onDestroy();
authTask.cancel(true);
receiver.unregister();
}
/**
* Write the given message to the device log and to the on-screen log.
*/
private void log(String text) {
Log.w(TAG, text);
StringBuilder sb = new StringBuilder(logText.getText().toString());
if (sb.length() > 0) {
sb.append('\n');
}
sb.append(dateFormat.format(new Date())).append(": ").append(text);
logText.setText(sb.toString());
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_LOG, logText.getText().toString());
}
}