~humpolec-team/humpolec/UbuntuInstaller-lp1262028

« back to all changes in this revision

Viewing changes to src/com/canonical/ubuntuinstaller/JsonChannelParser.java

  • Committer: Ondrej Kubik
  • Date: 2013-12-10 02:56:55 UTC
  • Revision ID: ondrej.kubik@canonical.com-20131210025655-5ybdg4j63b93g9y8
Adding channel selector
Channels are dynamic fetched from server
For selected channel latest version is selected and files to download are taken from json for given release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.canonical.ubuntuinstaller;
 
2
 
 
3
import java.util.Collections;
 
4
import java.util.Comparator;
 
5
import java.util.LinkedList;
 
6
import java.util.List;
 
7
 
 
8
import com.google.gson.Gson;
 
9
import com.google.gson.JsonArray;
 
10
import com.google.gson.JsonObject;
 
11
import com.google.gson.JsonParser;
 
12
 
 
13
public class JsonChannelParser {
 
14
        
 
15
        public final static String FULL_RELEASE = "full";
 
16
        public final static String DELTA_RELEASE = "delta";
 
17
        
 
18
        public enum ReleaseType {
 
19
                FULL, DELTA, UNKNOWN
 
20
        };
 
21
        
 
22
        
 
23
        // JSON supporting classes 
 
24
        public class File {
 
25
                String checksum;
 
26
                Integer order;
 
27
                String path;
 
28
                String signature;
 
29
                Integer size;
 
30
        }
 
31
 
 
32
        public class Image {
 
33
                String description;
 
34
                Integer version;
 
35
                String type;
 
36
                File[] files;
 
37
        }
 
38
 
 
39
        /**
 
40
         * Gather info on all available releases on server
 
41
         * @param jsonStr string holding json data
 
42
         * @param filter type of releases to add to the list
 
43
         * @return
 
44
         */
 
45
        static public List<Image> getAvailableReleases(String jsonStr, ReleaseType filter) {
 
46
                LinkedList<Image> releases = new LinkedList<Image>();
 
47
                
 
48
                JsonObject index =  new JsonParser().parse(jsonStr).getAsJsonObject();
 
49
                JsonArray images = index.get("images").getAsJsonArray();
 
50
                int size = images.size();
 
51
 
 
52
                for(int j = 0; j < size; j++) {
 
53
                        Image image = new Gson().fromJson(images.get(j), Image.class);
 
54
                        ReleaseType type = ReleaseType.UNKNOWN;
 
55
                        if (FULL_RELEASE.equals(image.type)) {
 
56
                                type = ReleaseType.FULL;
 
57
                        } else if (DELTA_RELEASE.equals(image.type)) {
 
58
                                type = ReleaseType.DELTA;
 
59
                        }
 
60
                        if (filter == type) {
 
61
                                releases.add(image);
 
62
                        }
 
63
                }
 
64
                // sort list
 
65
                Collections.sort(releases, imageComparator());
 
66
                return releases;
 
67
        }
 
68
 
 
69
        public static Comparator<Image> imageComparator() {
 
70
                Comparator<Image> imageComparator = new Comparator<Image>(){
 
71
                        @Override
 
72
                        public int compare(Image o1, Image o2) {
 
73
                                return o2.version - o1.version;
 
74
                        }
 
75
                };
 
76
                return imageComparator;
 
77
        }
 
78
        
 
79
        public static Comparator<File> fileComparator() {
 
80
                Comparator<File> imageComparator = new Comparator<File>(){
 
81
                        @Override
 
82
                        public int compare(File f1, File f2) {
 
83
                                return f1.order - f2.order;
 
84
                        }
 
85
                };
 
86
                return imageComparator;
 
87
        }
 
88
 
 
89
}