/
AuthActivity.java
141 lines (122 loc) · 5.01 KB
/
AuthActivity.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
package com.geoloqi.android.sample.ui;
import org.apache.http.Header;
import org.apache.http.StatusLine;
import org.json.JSONObject;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.geoloqi.android.sample.R;
import com.geoloqi.android.sdk.LQException;
import com.geoloqi.android.sdk.LQSession;
import com.geoloqi.android.sdk.LQSession.OnRunApiRequestListener;
import com.geoloqi.android.sdk.LQSharedPreferences;
import com.geoloqi.android.sdk.LQTracker;
import com.geoloqi.android.sdk.service.LQService;
import com.geoloqi.android.sdk.service.LQService.LQBinder;
/**
* <p>This activity class is used to demonstrate how a user
* can be authenticated with the tracking service.</p>
*
* @author Tristan Waddington
*/
public class AuthActivity extends Activity implements OnClickListener {
public static final String TAG = "AuthActivity";
private Handler mHandler = new Handler();
private LQService mService;
private boolean mBound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auth);
Button submit = (Button) findViewById(R.id.submit_button);
if (submit != null) {
submit.setOnClickListener(this);
}
}
@Override
public void onResume() {
super.onResume();
// Bind to the tracking service so we can call public methods on it
Intent intent = new Intent(this, LQService.class);
bindService(intent, mConnection, 0);
}
@Override
public void onPause() {
super.onPause();
// Unbind from LQService
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit_button:
if (mService != null) {
// Get the username and password from the form fields
String username = ((EditText) findViewById(R.id.username)).getText().toString();
String password = ((EditText) findViewById(R.id.password)).getText().toString();
// Reset push token
LQSharedPreferences.removePushToken(this);
// Authenticate the session
LQSession.requestSession(username, password, new OnRunApiRequestListener() {
@Override
public void onSuccess(LQSession session, JSONObject json,
Header[] headers) {
// Swap out the tracker session with our fresh one
LQTracker tracker = mService.getTracker();
if (tracker != null) {
// Update the saved session
mService.setSavedSession(session);
// Update the tracker with the new session
tracker.setSession(session);
// Finish the activity
finish();
}
}
@Override
public void onFailure(LQSession session, LQException e) {
// An error occurred!
Toast.makeText(AuthActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onComplete(LQSession session, JSONObject json,
Header[] headers, StatusLine status) {
// The request was successful, but returned a non-200 response!
Toast.makeText(AuthActivity.this, String.format("Server returned a %s response!",
status.getStatusCode()), Toast.LENGTH_LONG).show();
}
}, mHandler, this);
}
break;
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
try {
// We've bound to LocalService, cast the IBinder and get LocalService instance.
LQBinder binder = (LQBinder) service;
mService = binder.getService();
mBound = true;
} catch (ClassCastException e) {
// Pass
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
}