Live updates working.

This commit is contained in:
GaidamakUA 2016-01-15 14:25:22 +02:00
parent bc5b9cff61
commit 01a368b3d0
3 changed files with 67 additions and 39 deletions

View file

@ -10,10 +10,8 @@ import android.widget.ImageView;
import net.osmand.plus.IconsCache; import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.OsmandActionBarActivity;
/**
* Created by GaidamakUA on 1/12/16.
*/
public class BaseOsmAndFragment extends Fragment { public class BaseOsmAndFragment extends Fragment {
private IconsCache iconsCache; private IconsCache iconsCache;
@ -21,6 +19,10 @@ public class BaseOsmAndFragment extends Fragment {
return (OsmandApplication) getActivity().getApplication(); return (OsmandApplication) getActivity().getApplication();
} }
protected OsmandActionBarActivity getMyActivity() {
return (OsmandActionBarActivity) getActivity();
}
protected IconsCache getIconsCache() { protected IconsCache getIconsCache() {
if (iconsCache == null) { if (iconsCache == null) {
iconsCache = getMyApplication().getIconsCache(); iconsCache = getMyApplication().getIconsCache();

View file

@ -1,18 +1,19 @@
package net.osmand.plus.download; package net.osmand.plus.download;
import java.util.ArrayList; import android.annotation.SuppressLint;
import java.util.Collections; import android.content.Context;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import net.osmand.OsmAndCollator; import net.osmand.OsmAndCollator;
import net.osmand.map.OsmandRegions; import net.osmand.map.OsmandRegions;
import net.osmand.map.WorldRegion; import net.osmand.map.WorldRegion;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import android.annotation.SuppressLint;
import android.content.Context; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
public class DownloadResourceGroup { public class DownloadResourceGroup {
@ -333,4 +334,7 @@ public class DownloadResourceGroup {
return parentGroup.getUniqueId() + "#" + id; return parentGroup.getUniqueId() + "#" + id;
} }
public String getId() {
return id;
}
} }

View file

@ -12,17 +12,18 @@ import android.widget.ArrayAdapter;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.plus.OsmandApplication; import net.osmand.map.WorldRegion;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.download.DownloadResourceGroup;
import net.osmand.plus.download.DownloadResources;
import net.osmand.plus.liveupdates.network.GetJsonAsyncTask; import net.osmand.plus.liveupdates.network.GetJsonAsyncTask;
import net.osmand.plus.liveupdates.network.Protocol; import net.osmand.plus.liveupdates.network.Protocol;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -36,7 +37,8 @@ import java.util.Locale;
*/ */
public class ReportsFragment extends BaseOsmAndFragment { public class ReportsFragment extends BaseOsmAndFragment {
public static final String TITLE = "Report"; public static final String TITLE = "Report";
public static final String TOTAL_CHANGES_BY_MONTH_URL = "http://builder.osmand.net/reports/total_changes_by_month.php?month="; public static final String TOTAL_CHANGES_BY_MONTH_URL_PATTERN = "http://download.osmand.net/" +
"reports/query_report.php?report=total_changes_by_month&month=%s&region=%s";
private TextView contributorsTextView; private TextView contributorsTextView;
private TextView editsTextView; private TextView editsTextView;
@ -44,6 +46,7 @@ public class ReportsFragment extends BaseOsmAndFragment {
private Spinner montReportsSpinner; private Spinner montReportsSpinner;
private Spinner regionReportsSpinner; private Spinner regionReportsSpinner;
private MonthsForReportsAdapter monthsForReportsAdapter; private MonthsForReportsAdapter monthsForReportsAdapter;
private RegionsForReportsAdapter regionsForReportsAdapter;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -54,9 +57,7 @@ public class ReportsFragment extends BaseOsmAndFragment {
montReportsSpinner.setAdapter(monthsForReportsAdapter); montReportsSpinner.setAdapter(monthsForReportsAdapter);
regionReportsSpinner = (Spinner) view.findViewById(R.id.regionReportsSpinner); regionReportsSpinner = (Spinner) view.findViewById(R.id.regionReportsSpinner);
ArrayAdapter<String> regionsForReportsAdapter = regionsForReportsAdapter = new RegionsForReportsAdapter(getMyActivity());
new ArrayAdapter<String>(getActivity(), R.layout.reports_for_spinner_item,
android.R.id.text1, getCountriesList(getMyApplication()));
regionsForReportsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); regionsForReportsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
regionReportsSpinner.setAdapter(regionsForReportsAdapter); regionReportsSpinner.setAdapter(regionsForReportsAdapter);
@ -86,10 +87,11 @@ public class ReportsFragment extends BaseOsmAndFragment {
return view; return view;
} }
private void requestAndUpdateUi() { public void requestAndUpdateUi() {
int monthItemPosition = montReportsSpinner.getSelectedItemPosition(); int monthItemPosition = montReportsSpinner.getSelectedItemPosition();
String monthUrlString = monthsForReportsAdapter.getQueryString(monthItemPosition); String monthUrlString = monthsForReportsAdapter.getQueryString(monthItemPosition);
String regionUrlString = regionReportsSpinner.getSelectedItem().toString(); int regionItemPosition = regionReportsSpinner.getSelectedItemPosition();
String regionUrlString = regionsForReportsAdapter.getQueryString(regionItemPosition);
GetJsonAsyncTask.OnResponseListener<Protocol.TotalChangesByMonthResponse> onResponseListener = GetJsonAsyncTask.OnResponseListener<Protocol.TotalChangesByMonthResponse> onResponseListener =
new GetJsonAsyncTask.OnResponseListener<Protocol.TotalChangesByMonthResponse>() { new GetJsonAsyncTask.OnResponseListener<Protocol.TotalChangesByMonthResponse>() {
@Override @Override
@ -110,7 +112,8 @@ public class ReportsFragment extends BaseOsmAndFragment {
GetJsonAsyncTask<Protocol.TotalChangesByMonthResponse> totalChangesByMontAsyncTask = GetJsonAsyncTask<Protocol.TotalChangesByMonthResponse> totalChangesByMontAsyncTask =
new GetJsonAsyncTask<>(Protocol.TotalChangesByMonthResponse.class); new GetJsonAsyncTask<>(Protocol.TotalChangesByMonthResponse.class);
totalChangesByMontAsyncTask.setOnResponseListener(onResponseListener); totalChangesByMontAsyncTask.setOnResponseListener(onResponseListener);
totalChangesByMontAsyncTask.execute(TOTAL_CHANGES_BY_MONTH_URL + monthUrlString); String finalUrl = String.format(TOTAL_CHANGES_BY_MONTH_URL_PATTERN, monthUrlString, regionUrlString);
totalChangesByMontAsyncTask.execute(finalUrl);
} }
private static class MonthsForReportsAdapter extends ArrayAdapter<String> { private static class MonthsForReportsAdapter extends ArrayAdapter<String> {
@ -125,13 +128,13 @@ public class ReportsFragment extends BaseOsmAndFragment {
Calendar startDate = Calendar.getInstance(); Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.MONTH, Calendar.JUNE); startDate.set(Calendar.MONTH, Calendar.JUNE);
startDate.set(Calendar.YEAR, 2015); startDate.set(Calendar.YEAR, 2015);
startDate.set(Calendar.DAY_OF_MONTH, 1);
startDate.set(Calendar.HOUR_OF_DAY, 0);
Calendar endDate = Calendar.getInstance(); Calendar endDate = Calendar.getInstance();
endDate.set(Calendar.DAY_OF_MONTH, endDate.getActualMaximum(Calendar.DAY_OF_MONTH));
endDate.set(Calendar.HOUR_OF_DAY, endDate.getActualMaximum(Calendar.HOUR_OF_DAY));
while (startDate.before(endDate)) { while (startDate.before(endDate)) {
queryString.add(queryFormat.format(startDate.getTime())); queryString.add(queryFormat.format(endDate.getTime()));
add(humanFormat.format(startDate.getTime())); add(humanFormat.format(endDate.getTime()));
startDate.add(Calendar.MONTH, 1); endDate.add(Calendar.MONTH, -1);
} }
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
} }
@ -141,24 +144,43 @@ public class ReportsFragment extends BaseOsmAndFragment {
} }
} }
private static List<String> getCountriesList(OsmandApplication ctx) { private static class RegionsForReportsAdapter extends ArrayAdapter<String> {
DownloadResources root = ctx.getDownloadThread().getIndexes(); ArrayList<String> queryRegionNames = new ArrayList<>();
ArrayList<String> namesList = new ArrayList<>();
processGroup(root, namesList, ctx); public RegionsForReportsAdapter(final OsmandActionBarActivity context) {
return namesList; super(context, R.layout.reports_for_spinner_item, android.R.id.text1);
WorldRegion root = context.getMyApplication().getRegions().getWorldRegion();
ArrayList<WorldRegion> groups = new ArrayList<>();
processGroup(root, groups, context);
Collections.sort(groups, new Comparator<WorldRegion>() {
@Override
public int compare(WorldRegion lhs, WorldRegion rhs) {
return lhs.getLocaleName().compareTo(rhs.getLocaleName());
}
});
for (WorldRegion group : groups) {
add(group.getLocaleName());
queryRegionNames.add(group.getRegionDownloadName());
}
} }
private static void processGroup(DownloadResourceGroup group, List<String> nameList, public String getQueryString(int position) {
return queryRegionNames.get(position);
}
private static void processGroup(WorldRegion group,
List<WorldRegion> nameList,
Context context) { Context context) {
if (group.getType().isScreen() && group.getParentGroup() != null if (group.isRegionMapDownload()) {
&& group.getParentGroup().getParentGroup() != null) { nameList.add(group);
nameList.add(group.getName(context));
} }
if (group.getGroups() != null) { if (group.getSubregions() != null) {
for (DownloadResourceGroup g : group.getGroups()) { for (WorldRegion g : group.getSubregions()) {
processGroup(g, nameList, context); processGroup(g, nameList, context);
} }
} }
} }
}
} }