Skip to content

Commit

Permalink
BuildPlacesTask
Browse files Browse the repository at this point in the history
refactors "add world places" to use `place_group` resources (#785)
  • Loading branch information
forrestguice committed Apr 6, 2024
1 parent 26049e8 commit 685606a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public void onClick(View v)
{
BuildPlacesTask task = new BuildPlacesTask(getActivity());
task.setTaskListener(buildPlacesListener);
task.execute();
task.execute(false, null, new String[0]); // TODO: selectable
}
};
private final BuildPlacesTask.TaskListener buildPlacesListener = new BuildPlacesTask.TaskListener()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Copyright (C) 2014-2018 Forrest Guice
Copyright (C) 2014-2024 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
Expand All @@ -18,7 +18,6 @@

package com.forrestguice.suntimeswidget.getfix;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
Expand All @@ -44,6 +43,7 @@
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
Expand Down Expand Up @@ -116,6 +116,51 @@ private void addPlacesFromRes(Context context, @NonNull ArrayList<Location> loca
}
}

private void addPlacesFromGroup(Context context, @NonNull String[] groups, @NonNull ArrayList<Location> locations)
{
if (groups.length == 0) {
addPlacesFromGroup(context, (String) null, locations);
} else {
for (String group : groups) {
addPlacesFromGroup(context, group, locations);
}
}
}

private void addPlacesFromGroup(Context context, @Nullable String fromGroup, @NonNull ArrayList<Location> locations)
{
Resources r = context.getResources();
int groupID = (fromGroup == null) ? 0
: r.getIdentifier(fromGroup, "array", context.getPackageName());

if (fromGroup == null || (fromGroup.startsWith("place_group_") && groupID != 0))
{
String[] groups = fromGroup != null
? r.getStringArray(groupID)
: r.getStringArray(R.array.place_groups);

for (String groupItem : groups)
{
String[] parts = groupItem.split(",");
if (parts.length > 0) {
addPlacesFromGroup(context, parts[0].trim(), locations); // recursive call
}
}

} else if (groupID != 0) {
ArrayList<String> items = new ArrayList<>(Arrays.asList(r.getStringArray(groupID))); // base case
if (items.size() > 0)
{
for (String item : items) {
Location location = csvItemToLocation(item);
if (location != null) {
locations.add(location);
}
}
}
}
}

private void addPlacesFromUri(Context context, @NonNull Uri uri, @NonNull ArrayList<Location> locations)
{
try {
Expand All @@ -128,37 +173,8 @@ private void addPlacesFromUri(Context context, @NonNull Uri uri, @NonNull ArrayL
String line = reader.readLine();
while (line != null)
{
String[] parts = line.split(",");
if (parts.length < 3) {
Log.e("BuildPlacesTask", "Ignoring malformed line; " + line);
line = reader.readLine();
continue;
}

String label = parts[0];
if (label.startsWith("\"")) {
label = label.substring(1);
}
if (label.endsWith("\"")) {
label = label.substring(0, label.length()-1);
}

String lat, lon;
String alt = "0";
try {
lat = "" + Double.parseDouble(parts[1]);
lon = "" + Double.parseDouble(parts[2]);
if (parts.length >= 4) {
alt = "" + Double.parseDouble(parts[3]);
}
} catch (NumberFormatException e) {
Log.e("BuildPlacesTask", "Ignoring line " + line + " .. " + e);
line = reader.readLine();
continue;
}

Location location = new Location(label, lat, lon, alt);
if (!locations.contains(location)) {
Location location = csvItemToLocation(line);
if (location != null && !locations.contains(location)) {
locations.add(location);
}
line = reader.readLine();
Expand All @@ -174,7 +190,47 @@ private void addPlacesFromUri(Context context, @NonNull Uri uri, @NonNull ArrayL
}
}

private int buildPlaces(@Nullable Uri uri)
@Nullable
private Location csvItemToLocation(String csv_item)
{
String[] parts = csv_item.split(",");
if (parts.length < 3) {
Log.e("BuildPlacesTask", "Ignoring malformed line; " + csv_item);
return null;
}

String label = parts[0];
if (label.startsWith("\"")) {
label = label.substring(1);
}
if (label.endsWith("\"")) {
label = label.substring(0, label.length()-1);
}

String lat, lon;
String alt = "0";
try {
lat = "" + Double.parseDouble(parts[1]);
lon = "" + Double.parseDouble(parts[2]);
if (parts.length >= 4) {
alt = "" + Double.parseDouble(parts[3]);
}
} catch (NumberFormatException e) {
Log.e("BuildPlacesTask", "Ignoring line " + csv_item + " .. " + e);
return null;
}

return new Location(label, lat, lon, alt);
}

/**
* Pass a URI to build from file, groups[] to build from resources, or null for both to build
* from internal locales.
* @param uri optional source uri; null to skip
* @param groups optional group list; null to skip, or pass an empty list to add all
* @return the number of items added to the database
*/
private int buildPlaces(@Nullable Uri uri, @Nullable String[] groups)
{
int result = 0;
ArrayList<Location> locations = new ArrayList<>();
Expand All @@ -184,6 +240,8 @@ private int buildPlaces(@Nullable Uri uri)

if (uri != null) {
addPlacesFromUri(context, uri, locations);
} else if (groups != null) {
addPlacesFromGroup(context, groups, locations);
} else {
addPlacesFromRes(context, locations);
}
Expand Down Expand Up @@ -234,8 +292,13 @@ protected Integer doInBackground(Object... params)
param_source = (Uri)params[1];
}

String[] param_groups = null;
if (params.length > 2) {
param_groups = (String[])params[2];
}

int result = param_clearPlaces ? clearPlaces()
: buildPlaces(param_source);
: buildPlaces(param_source, param_groups);

long endTime = System.currentTimeMillis();
while ((endTime - startTime) < MIN_WAIT_TIME || isPaused)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ public void addWorldPlaces(Context context)
{
BuildPlacesTask task = new BuildPlacesTask(context);
task.setTaskListener(buildPlacesListener);
task.execute();
task.execute(false, null, new String[0]); // TODO: selectable
}
private BuildPlacesTask.TaskListener buildPlacesListener = new BuildPlacesTask.TaskListener()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public boolean onPreferenceClick(Preference preference)
{
buildPlacesTask = new BuildPlacesTask(myParent);
buildPlacesTask.setTaskListener(buildPlacesListener);
buildPlacesTask.execute();
buildPlacesTask.execute(false, null, new String[0]); // TODO: selectable
return true;
}
return false;
Expand Down

0 comments on commit 685606a

Please sign in to comment.