-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainActivity.java
214 lines (189 loc) · 7.04 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
package com.pryv.appAndroidExample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.pryv.Connection;
import com.pryv.model.Event;
import com.pryv.model.Filter;
import com.pryv.model.Stream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Main screen allowing to create note events to your Pryv
* and retrieve all of them as a list of events
*/
public class MainActivity extends AppCompatActivity {
private TextView progressView;
private ListView notesList;
private EditText noteText;
private Button login;
private Credentials credentials;
private static final int LOGIN_REQUEST = 1;
private static final String NOTE_STREAM_ID = "sampleNotes";
private static final String NOTE_STREAM_NAME = "Notes";
private static final String NOTE_EVENT_TYPE = "note/txt";
private Stream noteStream;
public static final String TOO_LONG_ERROR = "Please make your note shorter (20 characters max)!";
public static final String TOO_SHORT_ERROR = "Your note must contain some text!";
public static final String NOTES_RETRIEVED_MESSAGE = "All notes retrieved!";
private BaseAdapter adapter;
private ArrayList<String> retrievedEvents;
private Connection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressView = (TextView) findViewById(R.id.progress);
noteText = (EditText) findViewById(R.id.note);
notesList = (ListView) findViewById(R.id.notesList);
login = (Button) findViewById(R.id.login);
retrievedEvents = new ArrayList<>();
adapter = new ArrayAdapter(this, R.layout.list_item, retrievedEvents);
notesList.setAdapter(adapter);
credentials = new Credentials(this);
if(credentials.hasCredentials()) {
initPryvConnection();
setLogoutView();
} else {
setLoginView();
}
}
/**
* Called when "Create a new note" button is clicked
* Ask for the creation of a new note
*/
public void addNote(View v) {
if(credentials.hasCredentials()) {
final String text = noteText.getText().toString();
if(!text.isEmpty()) {
if(text.length()>20) {
progressView.setText(TOO_LONG_ERROR);
} else {
noteText.setText("");
new Thread() {
public void run() {
try {
Event newEvent = new Event()
.setStreamId(noteStream.getId())
.setType(NOTE_EVENT_TYPE)
.setContent(text);
newEvent = connection.events.create(newEvent);
updateStatusText("New event created with id: " + newEvent.getId());
} catch (IOException e) {
updateStatusText(e.toString());
}
}
}.start();
}
} else {
progressView.setText(TOO_SHORT_ERROR);
}
} else {
startLogin();
}
}
/**
* Called when "Retrieve notes" button is clicked
* Ask for the retrieval of all stored notes
*/
public void retrieveNotes(View v) {
if(credentials.hasCredentials()) {
new Thread() {
public void run() {
try {
Filter filter = new Filter().addStream(noteStream);
List<Event> retrievedEvents = connection.events.get(filter);
updateEventsList(retrievedEvents);
} catch (IOException e) {
updateStatusText(e.toString());
}
}
}.start();
} else {
startLogin();
}
}
private void startLogin() {
Intent intent = new Intent(this, LoginActivity.class);
startActivityForResult(intent, LOGIN_REQUEST);
}
private void initPryvConnection() {
// Initiate new connection to Pryv with connected account
connection = new Connection(credentials.getUsername(), credentials.getToken(), LoginActivity.DOMAIN);
// Initiate a "Notes" stream containing notes if not already created
noteStream = new Stream()
.setId(NOTE_STREAM_ID)
.setName(NOTE_STREAM_NAME);
new Thread() {
public void run() {
try {
Stream createdStream = connection.streams.create(noteStream);
updateStatusText("Stream created with id: " + createdStream.getId());
} catch (IOException e) {
updateStatusText(e.toString());
}
}
}.start();
}
private void setLoginView() {
progressView.setText("Hello guest!");
retrievedEvents.clear();
adapter.notifyDataSetChanged();
login.setText("Login");
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLogin();
}
});
}
private void setLogoutView() {
progressView.setText("Hello " + credentials.getUsername() + "!");
login.setText("Logout");
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
credentials.resetCredentials();
setLoginView();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == LOGIN_REQUEST && resultCode == RESULT_OK) {
setLogoutView();
initPryvConnection();
}
}
private void updateEventsList(final List<Event> events) {
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<String> eventsContent = new ArrayList<>();
for(Event e: events) {
eventsContent.add(""+e.getContent());
}
retrievedEvents.clear();
retrievedEvents.addAll(eventsContent);
adapter.notifyDataSetChanged();
progressView.setText(NOTES_RETRIEVED_MESSAGE);
}
});
}
private void updateStatusText(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressView.setText(text);
}
});
}
}