Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 6aec59d

Browse files
committed
Merge pull request #99 from tiwiz/autoupdate
Autoupdate
2 parents 416b9e5 + 30fe686 commit 6aec59d

18 files changed

+554
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark;
18+
19+
/**
20+
* This file is the one containg the data for the current ClassyShark version
21+
*/
22+
public class Version {
23+
24+
public static final int MAJOR = 1;
25+
public static final int MINOR = 0;
26+
}

ClassySharkWS/src/com/google/classyshark/cli/CliMode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.classyshark.cli;
1818

19+
import com.google.classyshark.updater.UpdateManager;
20+
1921
import java.io.File;
2022
import java.util.List;
2123

@@ -42,6 +44,7 @@ private CliMode() {
4244
}
4345

4446
public static void with(List<String> args) {
47+
UpdateManager.getInstance().checkVersionConsole();
4548
if (args.size() < 2) {
4649
System.err.println("missing command line arguments " + "\n\n\n" + ERROR_MESSAGE);
4750
return;

ClassySharkWS/src/com/google/classyshark/gui/GuiMode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.classyshark.gui.panel.ClassySharkPanel;
2020
import com.google.classyshark.gui.theme.Theme;
2121
import com.google.classyshark.gui.theme.ThemeManager;
22+
import com.google.classyshark.updater.UpdateManager;
2223

2324
import javax.swing.JFrame;
2425
import javax.swing.UIManager;
@@ -38,6 +39,7 @@ private GuiMode() {
3839
}
3940

4041
public static void with(final List<String> argsAsArray) {
42+
UpdateManager.getInstance().checkVersionGui();
4143
javax.swing.SwingUtilities.invokeLater(new Runnable() {
4244
public void run() {
4345
buildAndShowClassyShark(argsAsArray);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.updater;
18+
19+
import com.google.classyshark.updater.models.Release;
20+
import com.google.classyshark.updater.networking.AbstractReleaseCallback;
21+
import com.google.classyshark.updater.networking.MessageRunnable;
22+
import com.google.classyshark.updater.networking.NetworkManager;
23+
import com.google.classyshark.updater.utils.FileUtils;
24+
import retrofit2.Call;
25+
26+
import javax.swing.*;
27+
import java.io.IOException;
28+
29+
30+
/**
31+
* This class is the core point for the update process: based on the response received from GitHub, it will download
32+
* the new release and, if ClassyShark has been started as desktop app, when the download finishes, it will show a
33+
* dialog containing the changelog of the new version
34+
*/
35+
public class UpdateManager{
36+
private static final UpdateManager instance = new UpdateManager();
37+
private final AbstractReleaseCallback releaseCallback;
38+
private final Release currentRelease = new Release();
39+
private boolean isGui = false;
40+
41+
private UpdateManager() {
42+
releaseCallback = new AbstractReleaseCallback() {
43+
@Override
44+
public void onReleaseReceived(Release release) {
45+
onReleaseResponse(release);
46+
}
47+
};
48+
}
49+
50+
public static UpdateManager getInstance() {
51+
return instance;
52+
}
53+
54+
public void checkVersionConsole() {
55+
checkVersion(false);
56+
}
57+
58+
public void checkVersionGui() {
59+
checkVersion(true);
60+
}
61+
62+
private void checkVersion(boolean isGui) {
63+
this.isGui = isGui;
64+
Call<Release> call = NetworkManager.getGitHubApi().getLatestRelease();
65+
call.enqueue(releaseCallback);
66+
}
67+
68+
private void onReleaseResponse(Release release) {
69+
if (release.isNewerThan(currentRelease)) {
70+
new Thread(new Runnable() {
71+
@Override
72+
public void run() {
73+
obtainNew(release);
74+
SwingUtilities.invokeLater(new MessageRunnable(release.getReleaseName(), release.getChangelog(), isGui));
75+
}
76+
}).start();
77+
78+
}
79+
}
80+
81+
private void obtainNew(Release release) {
82+
try {
83+
FileUtils.downloadFileFrom(release);
84+
} catch (IOException e) {
85+
System.err.println("ERROR: " + e.getMessage());
86+
}
87+
}
88+
89+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.updater.models;
18+
19+
20+
import com.google.classyshark.Version;
21+
import com.google.classyshark.updater.networking.GitHubApi;
22+
import com.google.gson.annotations.SerializedName;
23+
import com.sun.istack.internal.Nullable;
24+
25+
/**
26+
* This class represents the response that GitHub returns when queried with {@link GitHubApi#getLatestRelease()}.
27+
* Since we do not need all the fields from the response, we just add those we are gonna use.
28+
* At the moment, the {@link #preRelease} field is not used, but it will most probably be in the future in case we want to
29+
* only update to stable releases.
30+
*/
31+
public class Release {
32+
33+
private final String name;
34+
@SerializedName("prerelease")
35+
private final boolean preRelease;
36+
private final String body;
37+
private final ReleaseDownloadData[] assets;
38+
@SerializedName("created_at")
39+
private final String createdAt;
40+
41+
42+
private Release(String name, boolean preRelease, String body, ReleaseDownloadData[] assets, String createdAt) {
43+
this.name = name;
44+
this.preRelease = preRelease;
45+
this.body = body;
46+
this.assets = assets;
47+
this.createdAt = createdAt;
48+
}
49+
50+
public Release() {
51+
this(String.format("%d.%d", Version.MAJOR, Version.MINOR), false, "", null, "");
52+
}
53+
54+
@Override
55+
public boolean equals(Object other) {
56+
if (!(other instanceof Release)) {
57+
return false;
58+
}
59+
60+
return name.equals(((Release) other).name);
61+
}
62+
63+
private int getMajorVersion() {
64+
return getVersionField(0);
65+
}
66+
67+
private int getVersionField(int field) {
68+
String cleanedReleaseName = name.split("\\s")[0];
69+
return Integer.parseInt(cleanedReleaseName.split("\\.")[field]);
70+
}
71+
72+
private int getMinorVersion() {
73+
return getVersionField(1);
74+
}
75+
76+
@Override
77+
public int hashCode() {
78+
return name.hashCode();
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return "REL:\t" + name + "\nCHANGELOG:\n" + body;
84+
}
85+
86+
public boolean isNewerThan(Release other) {
87+
return getMajorVersion() > other.getMajorVersion() ||
88+
getMajorVersion() == other.getMajorVersion() && getMinorVersion() > other.getMinorVersion();
89+
}
90+
91+
@Nullable
92+
public String getDownloadURL() {
93+
if (assets != null && assets.length > 0) {
94+
return assets[0].getURL();
95+
} else {
96+
return null;
97+
}
98+
}
99+
100+
public String getReleaseName() {
101+
return name;
102+
}
103+
104+
public boolean isPreRelease() {
105+
return preRelease;
106+
}
107+
108+
public String getChangelog() {
109+
return body;
110+
}
111+
112+
public String getCreatedAt() {
113+
return createdAt;
114+
}
115+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.updater.models;
18+
19+
import com.google.gson.annotations.SerializedName;
20+
21+
class ReleaseDownloadData {
22+
23+
@SerializedName("browser_download_url")
24+
private final String browserDownloadUrl;
25+
26+
ReleaseDownloadData(String browserDownloadUrl) {
27+
this.browserDownloadUrl = browserDownloadUrl;
28+
}
29+
30+
@Override
31+
public boolean equals(Object other) {
32+
if (!(other instanceof ReleaseDownloadData)) {
33+
return false;
34+
}
35+
36+
return browserDownloadUrl.equals(((ReleaseDownloadData) other).browserDownloadUrl);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return browserDownloadUrl.hashCode();
42+
}
43+
44+
String getURL() {
45+
return browserDownloadUrl;
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.updater.networking;
18+
19+
import com.google.classyshark.updater.models.Release;
20+
import retrofit2.Call;
21+
import retrofit2.Callback;
22+
import retrofit2.Response;
23+
24+
public abstract class AbstractReleaseCallback implements Callback<Release>{
25+
26+
@Override
27+
public void onResponse(Call<Release> call, Response<Release> response) {
28+
onReleaseReceived(response.body());
29+
}
30+
31+
public abstract void onReleaseReceived(Release release);
32+
33+
@Override
34+
public void onFailure(Call<Release> call, Throwable throwable) {
35+
System.err.println("ERROR: " + throwable.getMessage());
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.updater.networking;
18+
19+
20+
import com.google.classyshark.updater.models.Release;
21+
import retrofit2.Call;
22+
import retrofit2.http.GET;
23+
24+
/**
25+
* This class is the one taking care of representing the API needed in order to retrieve the latest release data from
26+
* GitHub.
27+
*/
28+
public interface GitHubApi {
29+
30+
String ENDPOINT = "https://api.github.com/";
31+
32+
@GET("repos/google/android-classyshark/releases/latest")
33+
Call<Release> getLatestRelease();
34+
}

0 commit comments

Comments
 (0)