Skip to content

Commit c2d2c6c

Browse files
committedMar 20, 2025·
- Replace the deprecated functions in ImageDownloader, Throttle, ImportExportConnectionsList and LogViewerActivity
- linting - tidy up some code
1 parent 98858c0 commit c2d2c6c

17 files changed

+430
-305
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ EngineDriver.iml
2828
.Trashes
2929
ehthumbs.db
3030
Thumbs.db
31+
32+
#other file types
33+
*.hprof

‎EngineDriver/src/main/java/jmri/enginedriver/SettingsActivity.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,8 @@ private void showHideLeftRightSwipePreferences() {
19361936
parentActivity.enableDisablePreference(getPreferenceScreen(), "prefFullScreenSwipeArea", !enable);
19371937
}
19381938

1939-
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
1939+
@SuppressLint("ApplySharedPref")
1940+
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
19401941
boolean prefForcedRestart = sharedPreferences.getBoolean("prefForcedRestart", false);
19411942

19421943
if ((key != null) && !prefForcedRestart) { // don't do anything if the preference have been loaded and we are about to reload the app.

‎EngineDriver/src/main/java/jmri/enginedriver/import_export/ImportExportConnectionList.java

+50-43
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static jmri.enginedriver.threaded_application.context;
44

55
import android.content.SharedPreferences;
6-
import android.os.AsyncTask;
76
import android.util.Log;
87
import android.widget.Toast;
98

@@ -28,7 +27,7 @@
2827
public class ImportExportConnectionList {
2928
public ArrayList<HashMap<String, String>> connections_list;
3029
private final SharedPreferences prefs;
31-
private boolean prefHideDemoServer;
30+
private final boolean prefHideDemoServer;
3231
public boolean foundDemoHost = false;
3332

3433
private static final String demo_host = "jmri.mstevetodd.com";
@@ -60,8 +59,8 @@ public void getConnectionsList(String addressToRemove, String portToRemove) {
6059
String ip_address;
6160
String host_name;
6261
String port_str;
63-
Integer port = 0;
64-
String ssid_str = "";
62+
int port = 0;
63+
String ssid_str;
6564
String service_type = "";
6665
List<String> parts = Arrays.asList(line.split(":", 5)); //split record from file, max of 4 parts
6766
if (parts.size() > 1) { //skip if not split
@@ -91,19 +90,19 @@ public void getConnectionsList(String addressToRemove, String portToRemove) {
9190
port = Integer.decode(port_str);
9291
} catch (Exception ignored) {
9392
}
94-
if (!(ip_address.equals(addressToRemove)) || !(port.toString().equals(portToRemove))) {
93+
if (!(ip_address.equals(addressToRemove)) || !(Integer.toString(port).equals(portToRemove))) {
9594
if (port > 0) { //skip if port not converted to integer
9695

9796
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)) {
9998
foundDemoHost = true;
10099
if (prefHideDemoServer) includeThisHost = false;
101100
}
102101
if (includeThisHost) {
103102
HashMap<String, String> hm = new HashMap<>();
104103
hm.put("ip_address", ip_address);
105104
hm.put("host_name", host_name);
106-
hm.put("port", port.toString());
105+
hm.put("port", Integer.toString(port));
107106
hm.put("ssid", ssid_str);
108107
hm.put("service_type", service_type);
109108
if (!connections_list.contains(hm)) { // suppress dups
@@ -139,10 +138,10 @@ public void getConnectionsList(String addressToRemove, String portToRemove) {
139138
}
140139

141140
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);
143142
}
144143

145-
class saveConnectionsList extends AsyncTask<Void, Void, String> {
144+
class SaveConnectionsList implements Runnable{
146145

147146
public threaded_application mainapp; // hold pointer to mainapp
148147

@@ -153,22 +152,23 @@ class saveConnectionsList extends AsyncTask<Void, Void, String> {
153152
private final String connected_ssid;
154153
private final String serviceType;
155154

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) {
157156
mainapp = myApp;
158157
connected_hostip = ip;
159158
connected_hostname = name;
160159
connected_port = port;
161160
connected_ssid = ssidName;
162161
webServerName = wsName;
163162
serviceType = sserviceType;
163+
164+
new Thread(this).start();
164165
}
165166

166-
@Override
167-
protected String doInBackground(Void... params) {
168167

168+
public void run() {
169169
String errMsg = "";
170170
//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;
172172

173173
foundDemoHost = false;
174174
boolean isBlankOrDemo = false;
@@ -177,14 +177,15 @@ protected String doInBackground(Void... params) {
177177
isDemo = true;
178178
foundDemoHost = true;
179179
}
180-
if ( ((webServerName.isEmpty()) || (connected_hostname.equals(webServerName)))
181-
|| (isDemo) ) {
180+
if (((webServerName.isEmpty()) || (connected_hostname.equals(webServerName)))
181+
|| (isDemo)) {
182182
isBlankOrDemo = true;
183183
}
184184

185185
//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+
}
188189
try {
189190
File connections_list_file = new File(context.getExternalFilesDir(null), "connections_list.txt");
190191
PrintWriter list_output = new PrintWriter(connections_list_file);
@@ -199,7 +200,7 @@ protected String doInBackground(Void... params) {
199200
}
200201

201202
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
203204
smrc = mainapp.getApplicationContext().getResources().getString(R.string.prefMaximumRecentConnectionsDefaultValue);
204205
}
205206
int mrc = 10;
@@ -210,19 +211,23 @@ protected String doInBackground(Void... params) {
210211
int clEntries = Math.min(connections_list.size(), mrc); //don't keep more entries than specified in preference
211212
for (int i = 0; i < clEntries; i++) { //loop thru entries from connections list, up to max in prefs
212213
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");
217218
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);
218222

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+
}
226231
}
227232
}
228233
list_output.flush();
@@ -241,30 +246,32 @@ protected String doInBackground(Void... params) {
241246
File connections_log_file = new File(context.getExternalFilesDir(null), connection_log_file_name);
242247
FileWriter fileWriter = new FileWriter(connections_log_file , true);
243248
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);
253250
log_output.close();
254251
bufferedWriter.close();
255252
fileWriter.close();
256253
} catch (IOException except) {
257254
errMsg = except.getMessage();
258255
}
259256
}
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+
}
260268

261-
return errMsg;
269+
log_output.flush();
270+
return log_output;
262271
}
263272

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())
268275
threaded_application.safeToast(mainapp.getResources().getString(R.string.toastConnectErrorSavingRecentConnection) + " " + errMsg, Toast.LENGTH_SHORT);
269276
}
270277
}

‎EngineDriver/src/main/java/jmri/enginedriver/import_export/ImportExportPreferences.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ public void loadRecentTurnoutsListFromFile() {
987987
String line = list_reader.readLine();
988988
int splitPos = line.indexOf(':');
989989
if (splitPos > 0) {
990-
Integer source = 0;
990+
Integer source;
991991
String addr, turnoutName ="", turnoutServer = "";
992992
try {
993993
addr = line.substring(0, splitPos);
@@ -1010,7 +1010,7 @@ public void loadRecentTurnoutsListFromFile() {
10101010
turnoutName = "";
10111011
source = -1;
10121012
}
1013-
if (addr.length() > 0) {
1013+
if (!addr.isEmpty()) {
10141014
recentTurnoutAddressList.add(addr);
10151015
recentTurnoutNameList.add(turnoutName);
10161016
recentTurnoutSourceList.add(source);
@@ -1068,7 +1068,7 @@ public void downloadRosterToRecents(Context context, SharedPreferences sharedPre
10681068

10691069
// parse address and length from string, e.g. 2591(L)
10701070
String[] ras = threaded_application.splitByString(rosterAddressString, "(");
1071-
if (ras[0].length() > 0) { //only process if address found
1071+
if (!ras[0].isEmpty()) { //only process if address found
10721072
locoAddressSize = (ras[1].charAt(0) == 'L') ? address_type.LONG : address_type.SHORT; // convert S/L to 0/1
10731073
try {
10741074
locoAddress = Integer.parseInt(ras[0]); // convert address to int

‎EngineDriver/src/main/java/jmri/enginedriver/intro/intro_activity.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
package jmri.enginedriver.intro;
2222

23+
import static jmri.enginedriver.threaded_application.context;
24+
2325
import android.Manifest;
2426
import android.annotation.SuppressLint;
2527
import android.content.Intent;
@@ -28,6 +30,7 @@
2830
import android.os.Bundle;
2931
import android.support.annotation.Nullable;
3032
import android.support.v4.app.Fragment;
33+
import android.support.v4.content.ContextCompat;
3134
import android.util.Log;
3235
import android.widget.Toast;
3336

@@ -42,10 +45,9 @@
4245
public class intro_activity extends AppIntro2 {
4346
private boolean introComplete = false;
4447
private SharedPreferences prefs;
45-
private String prefTheme = "";
46-
private String prefThrottleType = "";
48+
// private String prefThrottleType = "";
4749
private String originalPrefTheme = "";
48-
private String originalPrefThrottleType = "";
50+
// private String originalPrefThrottleType = "";
4951

5052
private threaded_application mainapp; //pointer back to application
5153

@@ -60,7 +62,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6062

6163
prefs = getSharedPreferences("jmri.enginedriver_preferences", 0);
6264
originalPrefTheme = prefs.getString("prefTheme", getApplicationContext().getResources().getString(R.string.prefThemeDefaultValue));
63-
originalPrefThrottleType = prefs.getString("prefThrottleScreenType", getApplicationContext().getResources().getString(R.string.prefThrottleScreenTypeDefault));
65+
// originalPrefThrottleType = prefs.getString("prefThrottleScreenType", getApplicationContext().getResources().getString(R.string.prefThrottleScreenTypeDefault));
6466

6567
mainapp.getFakeDeviceId(true); // force getting a new ID
6668

@@ -82,7 +84,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
8284
sliderPage.setTitle(getApplicationContext().getResources().getString(R.string.permissionsRequestTitle));
8385
sliderPage.setDescription(getApplicationContext().getResources().getString(R.string.permissionsPOST_NOTIFICATIONS));
8486
sliderPage.setImageDrawable(R.drawable.icon_vector);
85-
sliderPage.setBgColor(getResources().getColor(R.color.intro_background));
87+
sliderPage.setBgColor(ContextCompat.getColor(context, R.color.intro_background));
8688
addSlide(AppIntroFragment.newInstance(sliderPage));
8789
slideNumber = slideNumber + 1;
8890
askForPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, slideNumber);
@@ -123,7 +125,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
123125
sliderPage.setTitle(getApplicationContext().getResources().getString(R.string.permissionsRequestTitle));
124126
sliderPage.setDescription(getApplicationContext().getResources().getString(R.string.permissionsREAD_MEDIA_VISUAL_USER_SELECTED));
125127
sliderPage.setImageDrawable(R.drawable.icon_vector);
126-
sliderPage.setBgColor(getResources().getColor(R.color.intro_background));
128+
sliderPage.setBgColor(ContextCompat.getColor(context, R.color.intro_background));
127129
addSlide(AppIntroFragment.newInstance(sliderPage));
128130
slideNumber = slideNumber + 1;
129131
askForPermissions(new String[]{Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED}, slideNumber);
@@ -240,15 +242,17 @@ public void onDonePressed(Fragment currentFragment) {
240242
SharedPreferences prefsNoBackup = mainapp.getSharedPreferences("jmri.enginedriver_preferences_no_backup", 0);
241243
prefsNoBackup.edit().putString("prefRunIntro", threaded_application.INTRO_VERSION).commit();
242244

243-
prefTheme = prefs.getString("prefTheme", getApplicationContext().getResources().getString(R.string.prefThemeDefaultValue));
244-
prefThrottleType = prefs.getString("prefThrottleScreenType", getApplicationContext().getResources().getString(R.string.prefThrottleScreenTypeDefault));
245+
String prefTheme = prefs.getString("prefTheme", getApplicationContext().getResources().getString(R.string.prefThemeDefaultValue));
246+
// prefThrottleType = prefs.getString("prefThrottleScreenType", getApplicationContext().getResources().getString(R.string.prefThrottleScreenTypeDefault));
245247

246248
if (!prefTheme.equals(originalPrefTheme)) {
247249
// the theme has changed so need to restart the app.
248250
Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
249-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
250-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
251-
startActivity(intent);
251+
if (intent != null ) {
252+
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
253+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
254+
startActivity(intent);
255+
}
252256
finish();
253257
// Runtime.getRuntime().exit(0); // really force the kill
254258
}

0 commit comments

Comments
 (0)
Please sign in to comment.