aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/me/brysonsteck/wiimmfiwatcher/Updater.java
blob: 78e97c182400bbb3032fb723bf6c701a2b337fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package me.brysonsteck.wiimmfiwatcher;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Updater {

    public boolean outdated = false;
    public boolean failed = false;
    public String newestRelease;
    public String githubRelease;
    public String playStore = "https://play.google.com/store/apps/details?id=me.brysonsteck.wiimmfiwatcher";

    public Updater() {
        try {
            String json = urlReader();
            JsonElement root = new JsonParser().parse(json);
            JsonObject rootObj = root.getAsJsonObject();
            JsonElement softwareElement = rootObj.getAsJsonObject("wiimmfi-watcher");
            JsonObject softwareObj = softwareElement.getAsJsonObject();
            newestRelease = softwareObj.get("current-release").getAsString();
            newestRelease = newestRelease.replace("\"", "");
            githubRelease = softwareObj.get("github-release").getAsString();
            githubRelease = githubRelease.replace("\"", "");
        } catch (IOException e) {
            System.out.println("An error has occurred while attempting to check for updates.");
            e.printStackTrace();
        }
    }

    public String urlReader() throws IOException {
        URL website = new URL("https://brysonsteck.net/updates.json");
        URLConnection connection = website.openConnection();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        connection.getInputStream()));

        StringBuilder response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null)
            response.append(inputLine);

        in.close();

        return response.toString();

    }

    public void compareRelease(String deviceRelease) {
        if (newestRelease == null) {
            failed = true;
        }
        else if (!deviceRelease.equals(newestRelease)) {
            outdated = true;
        }
    }

    public boolean isOutdated() {
        return outdated;
    }

    public boolean hasFailed() { return failed; }

    public String getNewestRelease() {
        return newestRelease;
    }

    public String getGithubRelease() {
        return githubRelease;
    }
}