|
28 | 28 | import java.text.DecimalFormat; |
29 | 29 | import java.util.ArrayList; |
30 | 30 | import java.util.Calendar; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.Comparator; |
31 | 33 | import java.util.Date; |
32 | 34 | import java.util.HashMap; |
33 | 35 | import java.util.Locale; |
@@ -62,14 +64,15 @@ class Tools { |
62 | 64 | static final DecimalFormat f0 = new DecimalFormat("#0.0"); |
63 | 65 | static final DecimalFormat f00 = new DecimalFormat("#0.00"); |
64 | 66 | static final DecimalFormat f000 = new DecimalFormat("#0.000"); |
65 | | - private static final String TAG_ENTITIES = "entities"; |
| 67 | + public static final String TAG_ENTITIES = "entities"; |
66 | 68 | private static final String TAG_ACTIVE = "active"; |
67 | 69 | private static final String TAG_FILLSTYLE = "fillstyle"; |
68 | 70 | private static final String TAG_PUBLIC = "public"; |
69 | 71 | private static final String TAG_STYLE = "style"; |
70 | 72 | private static final String TAG_YAXIS = "yaxis"; |
71 | 73 | private static final String TAG_CHILDREN = "children"; |
72 | 74 | private static final String BACKUP_FILENAME = "volkszaehler_settings_backup.txt"; |
| 75 | + private static final Object TAG_GROUP = "group"; |
73 | 76 | private static String unit = ""; |
74 | 77 |
|
75 | 78 | private static SharedPreferences getPrefs(Context context) { |
@@ -298,7 +301,7 @@ private static void getGroupChannels(JSONObject channel, HashMap<String, String> |
298 | 301 | } |
299 | 302 | catch (JSONException jex) |
300 | 303 | { |
301 | | - Log.d("getGroupChannels:", String.format("no children found for UUID: %s", channel)); |
| 304 | + Log.e("getGroupChannels:", String.format("no children found for UUID: %s", channel)); |
302 | 305 | jex.printStackTrace(); |
303 | 306 | } |
304 | 307 |
|
@@ -448,6 +451,7 @@ static boolean saveFile(Context context) { |
448 | 451 | fw.write("privateChannelUUIDs" + "=" + sharedPrefs.getString("privateChannelUUIDs", "") + "\n"); |
449 | 452 | fw.write("ZeroBasedYAxis" + "=" + (sharedPrefs.getBoolean("ZeroBasedYAxis", false) ? "true" : "false") + "\n"); |
450 | 453 | fw.write("autoReload" + "=" + (sharedPrefs.getBoolean("autoReload", false) ? "true" : "false")); |
| 454 | + fw.write("sortChannels" + "=" + (sharedPrefs.getBoolean("sortChannels", false) ? "true" : "false")); |
451 | 455 | } catch (IOException e) { |
452 | 456 | e.printStackTrace(); |
453 | 457 | return false; |
@@ -513,6 +517,13 @@ static boolean loadFile(Context context) { |
513 | 517 | continue; |
514 | 518 | } |
515 | 519 | PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("ZeroBasedYAxis", Boolean.parseBoolean(line)).commit(); |
| 520 | + } else if (line.startsWith("sortChannels")) { |
| 521 | + try { |
| 522 | + line = line.split("=")[1]; |
| 523 | + } catch (IndexOutOfBoundsException iobx) { |
| 524 | + continue; |
| 525 | + } |
| 526 | + PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("sortChannels", Boolean.parseBoolean(line)).commit(); |
516 | 527 | } else if (line.startsWith("autoReload")) { |
517 | 528 | try { |
518 | 529 | line = line.split("=")[1]; |
@@ -629,4 +640,46 @@ public static float determineStufe(float min, float max, float stufen, String va |
629 | 640 | float stufe = (Float.parseFloat(value) - min) / stufengroesse; |
630 | 641 | return Math.round(stufe); |
631 | 642 | } |
| 643 | + |
| 644 | + public static JSONObject sortJSONChannels(JSONObject JSONChannels, String sortWhat) |
| 645 | + { |
| 646 | + ArrayList<JSONObject> list = null; |
| 647 | + JSONArray what_Array = null; |
| 648 | + try { |
| 649 | + what_Array = (JSONArray) JSONChannels.get(sortWhat); |
| 650 | + list = new ArrayList<>(); |
| 651 | + for (int i = 0; i < what_Array.length(); i++) { |
| 652 | + JSONObject channel = (JSONObject) what_Array.get(i); |
| 653 | + if(TAG_GROUP.equals(channel.get(TAG_TYPE))) |
| 654 | + { |
| 655 | + if(channel.get(TAG_CHILDREN)!="" && channel.get(TAG_CHILDREN) != null) { |
| 656 | + channel = sortJSONChannels((JSONObject) channel,TAG_CHILDREN); |
| 657 | + } |
| 658 | + } |
| 659 | + list.add(channel); |
| 660 | + } |
| 661 | + Collections.sort(list, new MyJSONComparator()); |
| 662 | + JSONChannels.remove(sortWhat); |
| 663 | + JSONChannels.put(sortWhat, list); |
| 664 | + |
| 665 | + } catch (Exception e) { |
| 666 | + e.printStackTrace(); |
| 667 | + } |
| 668 | + return JSONChannels; |
| 669 | + } |
| 670 | +} |
| 671 | + |
| 672 | +class MyJSONComparator implements Comparator<JSONObject> { |
| 673 | + |
| 674 | + @Override |
| 675 | + public int compare(JSONObject jo1, JSONObject jo2) { |
| 676 | + String v1 = "", v2 = ""; |
| 677 | + try { |
| 678 | + v1 = (String) jo1.get(Tools.TAG_TITLE); |
| 679 | + v2 = (String) jo2.get(Tools.TAG_TITLE); |
| 680 | + } catch (JSONException e) { |
| 681 | + e.printStackTrace(); |
| 682 | + } |
| 683 | + return v1.compareTo(v2); |
| 684 | + } |
632 | 685 | } |
0 commit comments