-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
317 lines (264 loc) · 11.5 KB
/
MainActivity.java
File metadata and controls
317 lines (264 loc) · 11.5 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
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
package com.littlehouse_design.qpstoday;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.app.FragmentTransaction;
import android.app.ActionBar;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private String hostName;
private int portNumber;
private static String staticHost;
private static int staticPort;
//static EditText editTextIP, editTextPort;
private boolean connectedToServer = false;
//static String[] connectionStatusWords = {"Status: Not Connected", "Status: Connected"};
private DBHandler dbHandler = new DBHandler(this);
static ButtonChanger changerOne;
static ButtonChanger changerTwo;
static ButtonChanger changerThree;
static ButtonChanger changerFour;
private String submitStat;
private static String[] tabs = {"Sending", "Settings"};
private TCPClient mTcpClient;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
changerOne = new ButtonChanger(0);
changerTwo = new ButtonChanger(1);
changerThree = new ButtonChanger(2);
changerFour = new ButtonChanger(3);
if(!"Not Set".equals(dbHandler.getIpAddress())) {
hostName = dbHandler.getIpAddress();
staticHost = hostName;
}
else {
hostName = "this";
//staticHost = hostName;
}
if(dbHandler.getPort() > 0) {
portNumber = dbHandler.getPort();
staticPort = portNumber;
}
else {
portNumber = 123;
//staticPort = portNumber;
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
static public void connectionStatus(int status) {
//connectionStatus.setText(connectionStatusWords[status]);
}
public class connectTask extends AsyncTask<String,String,TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
//we create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
}
}, hostName, portNumber);
mTcpClient.run(submitStat);
return null;
}
//This is my listener for the server to say something back.
// Currently, I don't do anything with it but print it to the log.
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.e("Server says","Server sent something back!" + values);
}
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// Here is where the meat of the app lives. Create and Send to the server.
return new SendingSection();
default:
// Set IP and Port here
return new SettingsSection();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
/**
* A fragment that launches other parts of the demo application.
*/
public static class SendingSection extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sending_action, container, false);
final ImageView buttonOne = (ImageView) rootView.findViewById(R.id.button_one);
buttonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changerOne.changeStatus();
buttonOne.setImageResource(changerOne.getImage());
}
});
final ImageView buttonTwo = (ImageView) rootView.findViewById(R.id.button_two);
buttonTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changerTwo.changeStatus();
buttonTwo.setImageResource(changerTwo.getImage());
}
});
final ImageView buttonThree = (ImageView) rootView.findViewById(R.id.button_three);
buttonThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changerThree.changeStatus();
buttonThree.setImageResource(changerThree.getImage());
}
});
final ImageView buttonFour = (ImageView) rootView.findViewById(R.id.button_four);
buttonFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changerFour.changeStatus();
buttonFour.setImageResource(changerFour.getImage());
}
});
/*ImageView sendTCP = (ImageView) rootView.findViewById(R.id.send_tcp);
sendTCP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitStat = changerOne.getStatus() + ", " + changerTwo.getStatus() + ", "
+ changerThree.getStatus() + ", " + changerFour.getStatus();
//If the client connection has been initiated already, don't try again. just send it.
if(mTcpClient == null) {
new connectTask().execute("");
connectedToServer = true;
} else if(connectedToServer){
mTcpClient.sendMessage(submitStat);
}
}
});*/
return rootView;
}
}
public void sendMessage(View view) {
submitStat = changerOne.getStatus() + ", " + changerTwo.getStatus() + ", "
+ changerThree.getStatus() + ", " + changerFour.getStatus();
//If the client connection has been initiated already, don't try again. just send it.
if(mTcpClient == null) {
new connectTask().execute("");
connectedToServer = true;
} else if(connectedToServer){
mTcpClient.sendMessage(submitStat);
}
}
public static class SettingsSection extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.setting_action, container, false);
final EditText editTextIP = (EditText) rootView.findViewById(R.id.host_name);
if(staticHost != null) {
editTextIP.setText(staticHost);
}
System.out.println(staticHost);
final EditText editTextPort = (EditText) rootView.findViewById(R.id.port_number);
if(staticPort > 0) {
editTextPort.setText(String.valueOf(staticPort));
}
System.out.println(staticPort);
return rootView;
}
}
public void settingsUpdate(View view) {
EditText editTextIP = (EditText) findViewById(R.id.host_name);
EditText editTextPort = (EditText) findViewById(R.id.port_number);
if (editTextIP.getText().toString().length() < 1
|| editTextPort.getText().toString().length() < 1) {
Toast.makeText(this, "Input for IP and Port are required", Toast.LENGTH_LONG).show();
//dbHandler.dropDB();
} else {
hostName = editTextIP.getText().toString();
portNumber = Integer.parseInt(editTextPort.getText().toString());
dbHandler.addIP(hostName, portNumber);
System.out.println(hostName + ":" + portNumber);
Toast.makeText(this, "Updated IP/Port settings" + hostName + ":" + portNumber,Toast.LENGTH_LONG).show();
System.out.println(dbHandler.getIpAddress() + " : " + dbHandler.getPort());
}
}
}