From fc9deccad74d3f5ef118045461ae9cb0227ff84c Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sun, 24 Apr 2022 14:33:28 +0200 Subject: [PATCH] Fix some AndroidStudio warnings --- .../btools/routingapp/BRouterActivity.java | 52 ++++---- .../java/btools/routingapp/BRouterView.java | 113 +++++++++--------- 2 files changed, 75 insertions(+), 90 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index e4862da..d26ce2e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -14,6 +14,7 @@ import android.os.PowerManager.WakeLock; import android.os.StatFs; import android.widget.EditText; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.os.EnvironmentCompat; @@ -22,6 +23,7 @@ import java.io.File; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -65,7 +67,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private Set selectedVias; private List nogoList; private String maptoolDirCandidate; - private final Set dialogIds = new HashSet(); + private final Set dialogIds = new HashSet<>(); private String errorMessage; private String title; private int wpCount; @@ -74,7 +76,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat * Called when the activity is first created. */ @Override - @SuppressWarnings("deprecation") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -94,7 +95,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } @Override - @SuppressWarnings("deprecation") protected Dialog onCreateDialog(int id) { AlertDialog.Builder builder; builder = new AlertDialog.Builder(this); @@ -372,11 +372,10 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private boolean[] getCheckedBooleanArray(int size) { boolean[] checked = new boolean[size]; - for (int i = 0; i < checked.length; i++) checked[i] = true; + Arrays.fill(checked, true); return checked; } - @SuppressWarnings("deprecation") public void selectProfile(String[] items) { availableProfiles = items; @@ -384,7 +383,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(DIALOG_MAINACTION_ID); } - @SuppressWarnings("deprecation") public void startDownloadManager() { if (!mBRouterView.hasUpToDateLookups()) { showDialog(DIALOG_OLDDATAHINT_ID); @@ -393,19 +391,18 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } } - @SuppressWarnings("deprecation") public void selectBasedir(ArrayList items, String defaultBasedir, String message) { this.defaultbasedir = defaultBasedir; this.message = message; availableBasedirs = items; - ArrayList dirFreeSizes = new ArrayList(); + ArrayList dirFreeSizes = new ArrayList<>(); for (File f : items) { long size = 0L; try { StatFs stat = new StatFs(f.getAbsolutePath()); size = (long) stat.getAvailableBlocks() * stat.getBlockSize(); } catch (Exception e) { /* ignore */ } - dirFreeSizes.add(Long.valueOf(size)); + dirFreeSizes.add(size); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -426,7 +423,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(DIALOG_SELECTBASEDIR_ID); } - @SuppressWarnings("deprecation") public void selectRoutingModes(String[] modes, boolean[] modesChecked, String message) { routingModes = modes; routingModesChecked = modesChecked; @@ -434,28 +430,23 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(DIALOG_ROUTINGMODES_ID); } - @SuppressWarnings("deprecation") public void showModeConfigOverview(String message) { this.message = message; showDialog(DIALOG_MODECONFIGOVERVIEW_ID); } - @SuppressWarnings("deprecation") public void selectVias(String[] items) { availableVias = items; - selectedVias = new HashSet(availableVias.length); - for (String via : items) - selectedVias.add(via); + selectedVias = new HashSet<>(availableVias.length); + Collections.addAll(selectedVias, items); showDialog(DIALOG_VIASELECT_ID); } - @SuppressWarnings("deprecation") public void selectWaypoint(String[] items) { availableWaypoints = items; showNewDialog(DIALOG_PICKWAYPOINT_ID); } - @SuppressWarnings("deprecation") public void showWaypointDatabaseHelp() { if (mBRouterView.canAccessSdCard) { showNewDialog(DIALOG_SHOW_WP_HELP_ID); @@ -464,38 +455,33 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } } - @SuppressWarnings("deprecation") public void showRepeatTimeoutHelp() { showNewDialog(DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID); } - @SuppressWarnings("deprecation") public void showWpDatabaseScanSuccess(String bestGuess) { maptoolDirCandidate = bestGuess; showNewDialog(DIALOG_SHOW_WP_SCANRESULT_ID); } - @SuppressWarnings("deprecation") public void selectNogos(List nogoList) { this.nogoList = nogoList; showDialog(DIALOG_NOGOSELECT_ID); } private void showNewDialog(int id) { - if (dialogIds.contains(Integer.valueOf(id))) { + if (dialogIds.contains(id)) { removeDialog(id); } - dialogIds.add(Integer.valueOf(id)); + dialogIds.add(id); showDialog(id); } - @SuppressWarnings("deprecation") public void showErrorMessage(String msg) { errorMessage = msg; showNewDialog(DIALOG_EXCEPTION_ID); } - @SuppressWarnings("deprecation") public void showResultMessage(String title, String msg, int wpCount) { errorMessage = msg; this.title = title; @@ -535,16 +521,18 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat public ArrayList getStorageDirectories() { ArrayList list = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - list = new ArrayList(Arrays.asList(getExternalMediaDirs())); + list = new ArrayList<>(Arrays.asList(getExternalMediaDirs())); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - list = new ArrayList(Arrays.asList(getExternalFilesDirs(null))); + list = new ArrayList<>(Arrays.asList(getExternalFilesDirs(null))); } - ArrayList res = new ArrayList(); + ArrayList res = new ArrayList<>(); - for (File f : list) { - if (f != null) { - if (getStorageState(f).equals(Environment.MEDIA_MOUNTED)) - res.add(f); + if (list != null) { + for (File f : list) { + if (f != null) { + if (getStorageState(f).equals(Environment.MEDIA_MOUNTED)) + res.add(f); + } } } @@ -577,7 +565,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 0) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index df787c3..3edae99 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -66,7 +66,6 @@ public class BRouterView extends View { private File tracksDir; private File segmentDir; private File profileDir; - private String profilePath; private String profileName; private String sourceHint; private boolean waitingForSelection = false; @@ -109,8 +108,7 @@ public class BRouterView extends View { // don't ask twice String version = "v" + getContext().getString(R.string.app_version); File vFile = new File(brd, "profiles2/" + version); - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q - && vFile.exists()) { + if (vFile.exists()) { startSetup(baseDir, false); return; } @@ -120,11 +118,10 @@ public class BRouterView extends View { waitingForSelection = true; waitingForMigration = true; oldMigrationPath = brd.getAbsolutePath(); - return; } else { startSetup(baseDir, false); - return; } + return; } } String message = baseDir == null ? "(no basedir configured previously)" : "(previous basedir " + baseDir @@ -212,7 +209,7 @@ public class BRouterView extends View { wpList = cor.waypoints; nogoList = cor.nogopoints; - nogoVetoList = new ArrayList(); + nogoVetoList = new ArrayList<>(); sourceHint = "(dev/trgt=" + deviceLevel + "/" + targetSdkVersion + " coordinate-source: " + cor.basedir + cor.rootdir + ")"; @@ -248,15 +245,17 @@ public class BRouterView extends View { } String[] fileNames = profileDir.list(); - ArrayList profiles = new ArrayList(); + ArrayList profiles = new ArrayList<>(); boolean lookupsFound = false; - for (String fileName : fileNames) { - if (fileName.endsWith(".brf")) { - profiles.add(fileName.substring(0, fileName.length() - 4)); + if (fileNames != null) { + for (String fileName : fileNames) { + if (fileName.endsWith(".brf")) { + profiles.add(fileName.substring(0, fileName.length() - 4)); + } + if (fileName.equals("lookups.dat")) + lookupsFound = true; } - if (fileName.equals("lookups.dat")) - lookupsFound = true; } // add a "last timeout" dummy profile @@ -298,24 +297,26 @@ public class BRouterView extends View { private void moveFolders(String oldMigrationPath, String basedir) { File oldDir = new File(oldMigrationPath); File[] oldFiles = oldDir.listFiles(); - for (File f : oldFiles) { - if (f.isDirectory()) { - int index = f.getAbsolutePath().lastIndexOf("/"); - String tmpdir = basedir + f.getAbsolutePath().substring(index); - moveFolders(f.getAbsolutePath(), tmpdir); - } else { - if (!f.getName().startsWith("v1.6")) { - moveFile(oldMigrationPath, f.getName(), basedir); + if (oldFiles != null) { + for (File f : oldFiles) { + if (f.isDirectory()) { + int index = f.getAbsolutePath().lastIndexOf("/"); + String tmpdir = basedir + f.getAbsolutePath().substring(index); + moveFolders(f.getAbsolutePath(), tmpdir); + } else { + if (!f.getName().startsWith("v1.6")) { + moveFile(oldMigrationPath, f.getName(), basedir); + } } - } + } } } private void moveFile(String inputPath, String inputFile, String outputPath) { - InputStream in = null; - OutputStream out = null; + InputStream in; + OutputStream out; try { //create output directory if it doesn't exist @@ -334,12 +335,10 @@ public class BRouterView extends View { out.write(buffer, 0, read); } in.close(); - in = null; // write the output file out.flush(); out.close(); - out = null; // delete the original file new File(inputPath + "/" + inputFile).delete(); @@ -365,7 +364,7 @@ public class BRouterView extends View { } public void updateViaList(Set selectedVias) { - ArrayList filtered = new ArrayList(wpList.size()); + ArrayList filtered = new ArrayList<>(wpList.size()); for (OsmNodeNamed n : wpList) { String name = n.name; if ("from".equals(name) || "to".equals(name) || selectedVias.contains(name)) @@ -442,7 +441,7 @@ public class BRouterView extends View { private List readWpList(BufferedReader br, boolean isNogo) throws Exception { int cnt = Integer.parseInt(br.readLine()); - List res = new ArrayList(cnt); + List res = new ArrayList<>(cnt); for (int i = 0; i < cnt; i++) { OsmNodeNamed wp = OsmNodeNamed.decodeNogo(br.readLine()); wp.isNogo = isNogo; @@ -471,7 +470,7 @@ public class BRouterView extends View { rawTrackPath = modesDir + "/remote_rawtrack.dat"; } - profilePath = profileDir + "/" + profile + ".brf"; + String profilePath = profileDir + "/" + profile + ".brf"; profileName = profile; if (needsViaSelection) { @@ -490,15 +489,15 @@ public class BRouterView extends View { } if (needsWaypointSelection) { - String msg; + StringBuilder msg; if (wpList.size() == 0) { - msg = "Expecting waypoint selection\n" + sourceHint; + msg = new StringBuilder("Expecting waypoint selection\n" + sourceHint); } else { - msg = "current waypoint selection:\n"; + msg = new StringBuilder("current waypoint selection:\n"); for (int i = 0; i < wpList.size(); i++) - msg += (i > 0 ? "->" : "") + wpList.get(i).name; + msg.append(i > 0 ? "->" : "").append(wpList.get(i).name); } - ((BRouterActivity) getContext()).showResultMessage("Select Action", msg, wpList.size()); + ((BRouterActivity) getContext()).showResultMessage("Select Action", msg.toString(), wpList.size()); return; } @@ -578,7 +577,7 @@ public class BRouterView extends View { File vtag = new File(path, versionTag); try { exists = !vtag.createNewFile(); - } catch (IOException io) { + } catch (IOException ignored) { } // well.. } @@ -599,7 +598,7 @@ public class BRouterView extends View { } String name = ze.getName(); File outfile = new File(path, name); - if (!outfile.exists()) { + if (!outfile.exists() && outfile.getParentFile() != null) { outfile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(outfile); @@ -647,7 +646,7 @@ public class BRouterView extends View { int ir = (int) (n.radius * scaleMeter2Pixel); if (ir > minradius) { Paint paint = new Paint(); - paint.setColor(Color.RED); + paint.setColor(color); paint.setStyle(Paint.Style.STROKE); canvas.drawCircle((float) x, (float) y, (float) ir, paint); } @@ -705,7 +704,7 @@ public class BRouterView extends View { cr = null; try { Thread.sleep(2000); - } catch (InterruptedException ie) { + } catch (InterruptedException ignored) { } ((BRouterActivity) getContext()).showErrorMessage(t.toString()); waitingForSelection = true; @@ -715,7 +714,7 @@ public class BRouterView extends View { private void showDatabaseScanning(Canvas canvas) { try { Thread.sleep(100); - } catch (InterruptedException ie) { + } catch (InterruptedException ignored) { } Paint paint1 = new Paint(); paint1.setColor(Color.WHITE); @@ -768,7 +767,7 @@ public class BRouterView extends View { try { Thread.sleep(sleeptime); - } catch (InterruptedException ie) { + } catch (InterruptedException ignored) { } lastTs = System.currentTimeMillis(); @@ -776,9 +775,6 @@ public class BRouterView extends View { if (cr != null) { if (cr.getErrorMessage() != null) { ((BRouterActivity) getContext()).showErrorMessage(cr.getErrorMessage()); - cr = null; - waitingForSelection = true; - return; } else { String memstat = memoryClass + "mb pathPeak " + ((cr.getPathPeak() + 500) / 1000) + "k"; String result = "version = BRouter-" + getContext().getString(R.string.app_version) + "\n" + "mem = " + memstat + "\ndistance = " + cr.getDistance() / 1000. + " km\n" + "filtered ascend = " + cr.getAscend() @@ -796,10 +792,10 @@ public class BRouterView extends View { title += " / " + cr.getAlternativeIndex() + ". Alternative"; ((BRouterActivity) getContext()).showResultMessage(title, result, rawTrackPath == null ? -1 : -3); - cr = null; - waitingForSelection = true; - return; } + cr = null; + waitingForSelection = true; + return; } else if (System.currentTimeMillis() > lastDataTime) { System.exit(0); } @@ -812,17 +808,18 @@ public class BRouterView extends View { paintPosition(openSet[si], openSet[si + 1], 0xffffff, 1); } // paint nogos on top (red) + int minradius = 4; for (int ngi = 0; ngi < nogoList.size(); ngi++) { OsmNodeNamed n = nogoList.get(ngi); int color = 0xff0000; - paintPosition(n.ilon, n.ilat, color, 4); + paintPosition(n.ilon, n.ilat, color, minradius); } // paint start/end/vias on top (yellow/green/blue) for (int wpi = 0; wpi < wpList.size(); wpi++) { OsmNodeNamed n = wpList.get(wpi); int color = wpi == 0 ? 0xffff00 : wpi < wpList.size() - 1 ? 0xff : 0xff00; - paintPosition(n.ilon, n.ilat, color, 4); + paintPosition(n.ilon, n.ilat, color, minradius); } canvas.drawBitmap(imgPixels, 0, imgw, (float) 0., (float) 0., imgw, imgh, false, null); @@ -831,10 +828,10 @@ public class BRouterView extends View { for (int ngi = 0; ngi < nogoList.size(); ngi++) { OsmNodeNamed n = nogoList.get(ngi); if (n instanceof OsmNogoPolygon) { - paintPolygon(canvas, (OsmNogoPolygon) n, 4); + paintPolygon(canvas, (OsmNogoPolygon) n, minradius); } else { - int color = 0xff0000; - paintCircle(canvas, n, color, 4); + int color = Color.RED; + paintCircle(canvas, n, color, minradius); } } @@ -857,7 +854,7 @@ public class BRouterView extends View { File basedir = Environment.getExternalStorageDirectory(); try { File bd2 = new File(basedir, "external_sd"); - ArrayList basedirGuesses = new ArrayList(); + ArrayList basedirGuesses = new ArrayList<>(); basedirGuesses.add(basedir.getAbsolutePath()); if (bd2.exists()) { @@ -865,7 +862,7 @@ public class BRouterView extends View { basedirGuesses.add(basedir.getAbsolutePath()); } - ArrayList rl = new ArrayList(); + ArrayList rl = new ArrayList<>(); for (String bdg : basedirGuesses) { rl.add(new CoordinateReaderOsmAnd(bdg)); rl.add(new CoordinateReaderLocus(bdg)); @@ -897,7 +894,7 @@ public class BRouterView extends View { if (rawTrack != null) { try { rawTrack.writeBinary(rawTrackPath); - } catch (Exception e) { + } catch (Exception ignored) { } } else { new File(rawTrackPath).delete(); @@ -916,7 +913,7 @@ public class BRouterView extends View { public void configureService(String[] routingModes, boolean[] checkedModes) { // read in current config - TreeMap map = new TreeMap(); + TreeMap map = new TreeMap<>(); BufferedReader br = null; String modesFile = modesDir + "/serviceconfig.dat"; try { @@ -928,12 +925,12 @@ public class BRouterView extends View { ServiceModeConfig smc = new ServiceModeConfig(line); map.put(smc.mode, smc); } - } catch (Exception e) { + } catch (Exception ignored) { } finally { if (br != null) try { br.close(); - } catch (Exception ee) { + } catch (Exception ignored) { } } @@ -961,12 +958,12 @@ public class BRouterView extends View { bw.write('\n'); msg.append(smc).append('\n'); } - } catch (Exception e) { + } catch (Exception ignored) { } finally { if (bw != null) try { bw.close(); - } catch (Exception ee) { + } catch (Exception ignored) { } } ((BRouterActivity) getContext()).showModeConfigOverview(msg.toString());