-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentsScreenController.java
325 lines (286 loc) · 11.1 KB
/
ComponentsScreenController.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.lukakralj.GpioManager_App;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.lukakralj.GpioManager_App.backend.GpioComponent;
import com.lukakralj.GpioManager_App.backend.RequestCode;
import com.lukakralj.GpioManager_App.backend.ServerConnection;
import com.lukakralj.GpioManager_App.backend.logger.Level;
import com.lukakralj.GpioManager_App.backend.logger.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* This activity displays the list of all the components that are currently registered.
*
* @author Luka Kralj
* @version 1.0
*/
public class ComponentsScreenController extends ListActivity {
private static List<GpioComponent> components = new ArrayList<>();
public static boolean joinedRoom;
private ComponentsAdapter curAdapter;
private boolean itemsEnabled;
private TextView componentsMsg;
private ImageButton newComponentBtn;
private View loadingScreen;
private TextView loadingScreenMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_components_screen);
loadingScreen = (View) findViewById(R.id.loadingScreen);
loadingScreenMsg = (TextView) findViewById(R.id.loadingScreenMsg);
itemsEnabled = true;
// Set custom adapter.
curAdapter = new ComponentsAdapter(this, components);
setListAdapter(curAdapter);
componentsMsg = (TextView) findViewById(R.id.componentsMsg);
newComponentBtn = (ImageButton) findViewById(R.id.newComponentBtn);
newComponentBtn.setOnClickListener((view) -> {
leaveComponentsRoom();
Intent intent = new Intent(this, NewComponentController.class);
startActivity(intent);
});
ServerConnection.getInstance().subscribeOnConnectEvent(() -> {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
enableAll();
retrieveData();
});
});
ServerConnection.getInstance().subscribeOnDisconnectEvent(() -> {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(this::disableAll);
});
ServerConnection.getInstance().subscribeComponentsChangeEvent(() -> {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(this::retrieveData);
});
if (ServerConnection.getInstance().isConnected()) {
enableAll();
retrieveData();
}
else {
disableAll();
}
}
@Override
public void onBackPressed() {
leaveComponentsRoom();
Intent intent = new Intent(this, HomeScreenController.class);
startActivity(intent);
}
/**
* Display the loading screen with the given message.
*
* @param msg Loading screen message.
*/
private void loadingScreenON(CharSequence msg) {
loadingScreen.setVisibility(View.VISIBLE);
loadingScreenMsg.setText(msg);
}
/**
* Hide the loading screen.
*/
private void loadingScreenOFF() {
loadingScreen.setVisibility(View.GONE);
loadingScreenMsg.setText("");
}
/**
* Enable all elements and hide the loading screen.
*/
private void enableAll() {
loadingScreenOFF();
newComponentBtn.setEnabled(true);
itemsEnabled = true;
curAdapter.notifyDataSetChanged();
}
/**
* Disable all elements and display the loading screen.
*/
private void disableAll() {
loadingScreenON(getText(R.string.waitingConnection));
newComponentBtn.setEnabled(false);
itemsEnabled = false;
curAdapter.notifyDataSetChanged();
}
@Override
protected void onListItemClick(ListView list, View vi, int position, long id) {
super.onListItemClick(list, vi, position, id);
}
private void leaveComponentsRoom() {
ServerConnection.getInstance().scheduleRequest(RequestCode.LEAVE_COMPONENTS_ROOM, data -> {
Logger.log("Left components room.");
joinedRoom = false;
});
}
/**
* Open edit component screen for the selected component.
*
* @param componentToEdit Component that the user wants to edit.
*/
private void startEditComponentActivity(GpioComponent componentToEdit) {
leaveComponentsRoom();
Intent intent = new Intent(this, EditComponentController.class);
intent.putExtra("editComp", componentToEdit);
startActivity(intent);
}
/**
* Retrieves data of all the components from the server.
*/
private void retrieveData() {
disableAll();
loadingScreenON(getText(R.string.retrievingData));
if (!joinedRoom) {
ServerConnection.getInstance().scheduleRequest(RequestCode.JOIN_COMPONENTS_ROOM, data -> {
Logger.log("Joined components room.");
joinedRoom = true;
});
}
ServerConnection.getInstance().scheduleRequest(RequestCode.COMPONENTS, data -> {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
int msgId;
try {
if (data.getString("status").equals("OK")) {
JSONArray comps = data.getJSONArray("components");
components.clear();
for (int i = 0; i < comps.length(); ++i) {
components.add(new GpioComponent(comps.getJSONObject(i)));
}
msgId = R.string.upToDate;
}
else {
msgId = R.string.sthWentWrong;
}
}
catch (JSONException e) {
Logger.log(e.getLocalizedMessage(), Level.ERROR);
msgId = R.string.sthWentWrong;
}
curAdapter.notifyDataSetChanged();
componentsMsg.setText(msgId);
enableAll();
});
});
}
/**
* Custom adapter for the ListView.
*/
private class ComponentsAdapter extends BaseAdapter {
Context context;
List<GpioComponent> data;
private LayoutInflater inflater;
private ComponentsAdapter(Context context, List<GpioComponent> data) {
this.context = context;
this.data = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return data.get(position).getId();
}
@Override
public boolean isEnabled(int position) {
return true; // If true, the whole row is clickable.
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi;
if (data.get(position).getDirection().equals("out")) {
vi = inflater.inflate(R.layout.out_component, null);
}
else {
vi = inflater.inflate(R.layout.in_component, null);
}
((TextView) vi.findViewById(R.id.mainTitle)).setText(data.get(position).getName());
// roughly 28 characters plus 3 dots can fit on the screen nicely.
String description = data.get(position).getDescription();
if (description.length() > 28) {
description = description.substring(0, 28) + "...";
}
((TextView) vi.findViewById(R.id.subtitle)).setText(description);
if (data.get(position).getDirection().equals("out")) {
RadioGroup toggle = (RadioGroup) vi.findViewById(R.id.toggle);
RadioButton toggleOn = vi.findViewById(R.id.toggleOn);
RadioButton toggleOff = vi.findViewById(R.id.toggleOff);
// Must set to null first otherwise an action is fired during setup.
toggle.setOnCheckedChangeListener(null);
if (data.get(position).getIsOn()) {
toggle.check(toggleOn.getId());
}
else {
toggle.check(toggleOff.getId());
}
toggle.setOnCheckedChangeListener((radioGroup, checkedId) -> {
JSONObject extra;
try {
extra = new JSONObject();
extra.put("id", data.get(position).getId());
extra.put("status", (data.get(position).getIsOn()) ? "off" : "on");
}
catch (JSONException e) {
Logger.log(e.getMessage(), Level.ERROR);
extra = null;
}
ServerConnection.getInstance().scheduleRequest(RequestCode.TOGGLE_COMPONENT, extra, (serverData) -> {
try {
if (serverData.getString("status").equals("OK")) {
Logger.log("Toggle successful for component id: " + data.get(position).getId() + ".");
}
}
catch (JSONException e) {
Logger.log(e.getMessage(), Level.ERROR);
}
});
});
toggleOn.setEnabled(itemsEnabled);
toggleOff.setEnabled(itemsEnabled);
}
else {
TextView statusIndicator = (TextView) vi.findViewById(R.id.statusIndicator);
if (data.get(position).getCurValue() == 1) {
statusIndicator.setEnabled(true);
statusIndicator.setText(R.string.ON);
}
else {
statusIndicator.setEnabled(false);
statusIndicator.setText(R.string.OFF);
}
// If nothing is set the edit screen will pop up.
statusIndicator.setOnClickListener(v -> { /* Do nothing. */ });
}
vi.setOnClickListener(v -> startEditComponentActivity(data.get(position)));
// Needed for correct rendering.
vi.setEnabled(itemsEnabled);
return vi;
}
}
}