3
3
import static jmri .enginedriver .threaded_application .context ;
4
4
5
5
import android .content .SharedPreferences ;
6
- import android .os .AsyncTask ;
7
6
import android .util .Log ;
8
7
import android .widget .Toast ;
9
8
28
27
public class ImportExportConnectionList {
29
28
public ArrayList <HashMap <String , String >> connections_list ;
30
29
private final SharedPreferences prefs ;
31
- private boolean prefHideDemoServer ;
30
+ private final boolean prefHideDemoServer ;
32
31
public boolean foundDemoHost = false ;
33
32
34
33
private static final String demo_host = "jmri.mstevetodd.com" ;
@@ -60,8 +59,8 @@ public void getConnectionsList(String addressToRemove, String portToRemove) {
60
59
String ip_address ;
61
60
String host_name ;
62
61
String port_str ;
63
- Integer port = 0 ;
64
- String ssid_str = "" ;
62
+ int port = 0 ;
63
+ String ssid_str ;
65
64
String service_type = "" ;
66
65
List <String > parts = Arrays .asList (line .split (":" , 5 )); //split record from file, max of 4 parts
67
66
if (parts .size () > 1 ) { //skip if not split
@@ -91,19 +90,19 @@ public void getConnectionsList(String addressToRemove, String portToRemove) {
91
90
port = Integer .decode (port_str );
92
91
} catch (Exception ignored ) {
93
92
}
94
- if (!(ip_address .equals (addressToRemove )) || !(port .toString ().equals (portToRemove ))) {
93
+ if (!(ip_address .equals (addressToRemove )) || !(Integer .toString (port ).equals (portToRemove ))) {
95
94
if (port > 0 ) { //skip if port not converted to integer
96
95
97
96
boolean includeThisHost = true ;
98
- if (host_name .equals (demo_host ) && port .toString ().equals (demo_port )) {
97
+ if (host_name .equals (demo_host ) && Integer .toString (port ).equals (demo_port )) {
99
98
foundDemoHost = true ;
100
99
if (prefHideDemoServer ) includeThisHost = false ;
101
100
}
102
101
if (includeThisHost ) {
103
102
HashMap <String , String > hm = new HashMap <>();
104
103
hm .put ("ip_address" , ip_address );
105
104
hm .put ("host_name" , host_name );
106
- hm .put ("port" , port .toString ());
105
+ hm .put ("port" , Integer .toString (port ));
107
106
hm .put ("ssid" , ssid_str );
108
107
hm .put ("service_type" , service_type );
109
108
if (!connections_list .contains (hm )) { // suppress dups
@@ -139,10 +138,10 @@ public void getConnectionsList(String addressToRemove, String portToRemove) {
139
138
}
140
139
141
140
public void saveConnectionsListExecute (threaded_application myApp , String ip , String name , int port , String wsName , String ssidName , String serviceType ) {
142
- new saveConnectionsList (myApp , ip , name , port , wsName , ssidName , serviceType ). execute ( );
141
+ new SaveConnectionsList (myApp , ip , name , port , wsName , ssidName , serviceType );
143
142
}
144
143
145
- class saveConnectionsList extends AsyncTask < Void , Void , String > {
144
+ class SaveConnectionsList implements Runnable {
146
145
147
146
public threaded_application mainapp ; // hold pointer to mainapp
148
147
@@ -153,22 +152,23 @@ class saveConnectionsList extends AsyncTask<Void, Void, String> {
153
152
private final String connected_ssid ;
154
153
private final String serviceType ;
155
154
156
- public saveConnectionsList (threaded_application myApp , String ip , String name , int port , String wsName , String ssidName , String sserviceType ) {
155
+ public SaveConnectionsList (threaded_application myApp , String ip , String name , int port , String wsName , String ssidName , String sserviceType ) {
157
156
mainapp = myApp ;
158
157
connected_hostip = ip ;
159
158
connected_hostname = name ;
160
159
connected_port = port ;
161
160
connected_ssid = ssidName ;
162
161
webServerName = wsName ;
163
162
serviceType = sserviceType ;
163
+
164
+ new Thread (this ).start ();
164
165
}
165
166
166
- @ Override
167
- protected String doInBackground (Void ... params ) {
168
167
168
+ public void run () {
169
169
String errMsg = "" ;
170
170
//exit if values not set, avoid NPE reported to Play Store
171
- if (connected_hostip == null || connected_port == 0 ) return errMsg ;
171
+ if (connected_hostip == null || connected_port == 0 ) return ;
172
172
173
173
foundDemoHost = false ;
174
174
boolean isBlankOrDemo = false ;
@@ -177,14 +177,15 @@ protected String doInBackground(Void... params) {
177
177
isDemo = true ;
178
178
foundDemoHost = true ;
179
179
}
180
- if ( ((webServerName .isEmpty ()) || (connected_hostname .equals (webServerName )))
181
- || (isDemo ) ) {
180
+ if (((webServerName .isEmpty ()) || (connected_hostname .equals (webServerName )))
181
+ || (isDemo )) {
182
182
isBlankOrDemo = true ;
183
183
}
184
184
185
185
//if no SD Card present then nothing to do
186
- if (!android .os .Environment .getExternalStorageState ().equals (android .os .Environment .MEDIA_MOUNTED ))
187
- return errMsg ;
186
+ if (!android .os .Environment .getExternalStorageState ().equals (android .os .Environment .MEDIA_MOUNTED )) {
187
+ return ;
188
+ }
188
189
try {
189
190
File connections_list_file = new File (context .getExternalFilesDir (null ), "connections_list.txt" );
190
191
PrintWriter list_output = new PrintWriter (connections_list_file );
@@ -199,7 +200,7 @@ protected String doInBackground(Void... params) {
199
200
}
200
201
201
202
String smrc = prefs .getString ("maximum_recent_connections_preference" , "" ); //retrieve pref for max recents to show
202
- if (smrc .equals ( "" )) { //if no value or entry removed, set to default
203
+ if (smrc .isEmpty ( )) { //if no value or entry removed, set to default
203
204
smrc = mainapp .getApplicationContext ().getResources ().getString (R .string .prefMaximumRecentConnectionsDefaultValue );
204
205
}
205
206
int mrc = 10 ;
@@ -210,19 +211,23 @@ protected String doInBackground(Void... params) {
210
211
int clEntries = Math .min (connections_list .size (), mrc ); //don't keep more entries than specified in preference
211
212
for (int i = 0 ; i < clEntries ; i ++) { //loop thru entries from connections list, up to max in prefs
212
213
HashMap <String , String > t = connections_list .get (i );
213
- String li = t .get ("ip_address" );
214
- String lh = t .get ("host_name" );
215
- Integer lp = Integer . valueOf ( t .get ("port" ) );
216
- String ssid = t .get ("ssid" );
214
+ String sIpAddress = t .get ("ip_address" );
215
+ String sHostName = t .get ("host_name" );
216
+ String sPort = t .get ("port" );
217
+ String sSsid = t .get ("ssid" );
217
218
String sType = t .get ("service_type" );
219
+
220
+ if ( (sIpAddress !=null ) && (sHostName !=null ) &&(sPort !=null ) && (sSsid !=null ) && (sType !=null ) ) {
221
+ Integer port = Integer .valueOf (sPort );
218
222
219
- boolean doWrite = !connected_hostip .equals (li ) || (connected_port .intValue () != lp .intValue ());
220
- //don't write it out if same as selected
221
- if ( li .equals (demo_host ) && lp .toString ().equals (demo_port ) && (foundDemoHost ) ) {
222
- doWrite = false ;
223
- }
224
- if (doWrite ) {
225
- list_output .format ("%s:%s:%d:%s:%s\n " , lh , li , lp , ssid , sType );
223
+ boolean doWrite = !connected_hostip .equals (sIpAddress ) || (connected_port .intValue () != port .intValue ());
224
+ //don't write it out if same as selected
225
+ if (sIpAddress .equals (demo_host ) && port .toString ().equals (demo_port ) && (foundDemoHost )) {
226
+ doWrite = false ;
227
+ }
228
+ if (doWrite ) {
229
+ list_output .format ("%s:%s:%d:%s:%s\n " , sHostName , sIpAddress , port , sSsid , sType );
230
+ }
226
231
}
227
232
}
228
233
list_output .flush ();
@@ -241,30 +246,32 @@ protected String doInBackground(Void... params) {
241
246
File connections_log_file = new File (context .getExternalFilesDir (null ), connection_log_file_name );
242
247
FileWriter fileWriter = new FileWriter (connections_log_file , true );
243
248
BufferedWriter bufferedWriter = new BufferedWriter (fileWriter , 1024 );
244
- PrintWriter log_output = new PrintWriter (bufferedWriter );
245
-
246
- if (isBlankOrDemo ) {
247
- log_output .format ("%s:%s:%d:%s:%s:%s\n " , connected_hostname , connected_hostip , connected_port , connected_ssid , serviceType , currentDateAndTime );
248
- } else {
249
- log_output .format ("%s:%s:%d:%s:%s:%s\n " , webServerName , connected_hostip , connected_port , connected_ssid , serviceType , currentDateAndTime );
250
- }
251
-
252
- log_output .flush ();
249
+ PrintWriter log_output = getPrintWriter (bufferedWriter , isBlankOrDemo , currentDateAndTime );
253
250
log_output .close ();
254
251
bufferedWriter .close ();
255
252
fileWriter .close ();
256
253
} catch (IOException except ) {
257
254
errMsg = except .getMessage ();
258
255
}
259
256
}
257
+ if (errMsg != null ) displayError (errMsg );
258
+ }
259
+
260
+ private PrintWriter getPrintWriter (BufferedWriter bufferedWriter , boolean isBlankOrDemo , String currentDateAndTime ) {
261
+ PrintWriter log_output = new PrintWriter (bufferedWriter );
262
+
263
+ if (isBlankOrDemo ) {
264
+ log_output .format ("%s:%s:%d:%s:%s:%s\n " , connected_hostname , connected_hostip , connected_port , connected_ssid , serviceType , currentDateAndTime );
265
+ } else {
266
+ log_output .format ("%s:%s:%d:%s:%s:%s\n " , webServerName , connected_hostip , connected_port , connected_ssid , serviceType , currentDateAndTime );
267
+ }
260
268
261
- return errMsg ;
269
+ log_output .flush ();
270
+ return log_output ;
262
271
}
263
272
264
- @ Override
265
- protected void onPostExecute (String errMsg ) {
266
- if (errMsg .length () > 0 )
267
- // Toast.makeText(mainapp, mainapp.getResources().getString(R.string.toastConnectErrorSavingRecentConnection) + " " + errMsg, Toast.LENGTH_SHORT).show();
273
+ protected void displayError (String errMsg ) {
274
+ if (!errMsg .isEmpty ())
268
275
threaded_application .safeToast (mainapp .getResources ().getString (R .string .toastConnectErrorSavingRecentConnection ) + " " + errMsg , Toast .LENGTH_SHORT );
269
276
}
270
277
}
0 commit comments