~humpolec-team/humpolec/UbuntuInstaller-refactor

« back to all changes in this revision

Viewing changes to src/com/canonical/ubuntu/installer/JsonChannelParser.java

  • Committer: Ondrej Kubik
  • Date: 2013-12-12 02:02:35 UTC
  • Revision ID: ondrej.kubik@canonical.com-20131212020235-ipkk7w5yh248anxr
New UI with two views
added progress bars for download and install
added uninstall
several usecases improvement

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package com.canonical.ubuntuinstaller;
 
1
package com.canonical.ubuntu.installer;
2
2
 
3
3
import java.util.Collections;
4
4
import java.util.Comparator;
11
11
import com.google.gson.JsonParser;
12
12
 
13
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
 
        }
 
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
88
 
89
89
}