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

Commit aa3099e

Browse files
committed
Merge pull request #100 from tiwiz/autoupdate_v2
Autoupdate v2
2 parents 5cb4695 + ccf6519 commit aa3099e

File tree

6 files changed

+189
-52
lines changed

6 files changed

+189
-52
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public class CliMode {
3737
" -open\t open an archive with GUI \n" +
3838
" -export\t export to file \n" +
3939
" -methodcounts\t packages with method counts \n" +
40-
" -inspect experimental prints apk analysis" +
40+
" -inspect experimental prints apk analysis\n" +
41+
" -update\tupdates ClassyShark" +
4142
"\nwhere args is an optional classname\n";
4243

4344
private CliMode() {
4445
}
4546

4647
public static void with(List<String> args) {
47-
UpdateManager.getInstance().checkVersionConsole();
4848
if (args.size() < 2) {
4949
System.err.println("missing command line arguments " + "\n\n\n" + ERROR_MESSAGE);
5050
return;
@@ -71,6 +71,9 @@ public static void with(List<String> args) {
7171
case "-methodcounts":
7272
inspectPackages(args);
7373
break;
74+
case "-update":
75+
UpdateManager.getInstance().checkVersionConsole();
76+
break;
7477
default:
7578
System.err.println("wrong operand ==> " + operand + "\n\n\n" + ERROR_MESSAGE);
7679
}

ClassySharkWS/src/com/google/classyshark/updater/UpdateManager.java

+10-40
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,9 @@
1616

1717
package com.google.classyshark.updater;
1818

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-
19+
import com.google.classyshark.updater.networking.AbstractDownloader;
20+
import com.google.classyshark.updater.networking.CliDownloader;
21+
import com.google.classyshark.updater.networking.GuiDownloader;
2922

3023
/**
3124
* This class is the core point for the update process: based on the response
@@ -35,17 +28,9 @@
3528
*/
3629
public class UpdateManager{
3730
private static final UpdateManager instance = new UpdateManager();
38-
private final AbstractReleaseCallback releaseCallback;
39-
private final Release currentRelease = new Release();
40-
private boolean isGui = false;
4131

4232
private UpdateManager() {
43-
releaseCallback = new AbstractReleaseCallback() {
44-
@Override
45-
public void onReleaseReceived(Release release) {
46-
onReleaseResponse(release);
47-
}
48-
};
33+
4934
}
5035

5136
public static UpdateManager getInstance() {
@@ -61,29 +46,14 @@ public void checkVersionGui() {
6146
}
6247

6348
private void checkVersion(boolean isGui) {
64-
this.isGui = isGui;
65-
Call<Release> call = NetworkManager.getGitHubApi().getLatestRelease();
66-
call.enqueue(releaseCallback);
67-
}
68-
69-
private void onReleaseResponse(final Release release) {
70-
if (release.isNewerThan(currentRelease)) {
71-
new Thread(new Runnable() {
72-
@Override
73-
public void run() {
74-
obtainNew(release);
75-
SwingUtilities.invokeLater(new MessageRunnable(release.getReleaseName(), release.getChangelog(), isGui));
76-
}
77-
}).start();
78-
79-
}
49+
getDownloaderFrom(isGui).checkNewVersion();
8050
}
8151

82-
private void obtainNew(Release release) {
83-
try {
84-
FileUtils.downloadFileFrom(release);
85-
} catch (IOException e) {
86-
System.err.println("ERROR: " + e.getMessage());
52+
private AbstractDownloader getDownloaderFrom(boolean isGui) {
53+
if (isGui) {
54+
return GuiDownloader.getInstance();
55+
} else {
56+
return CliDownloader.getInstance();
8757
}
8858
}
8959
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 com.google.classyshark.updater.utils.FileUtils;
21+
import retrofit2.Call;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
26+
/**
27+
* This class is the skeleton of both the {@link GuiDownloader} and {@link CliDownloader}: it contains the core logic
28+
* needed to download the new JAR and a couple of callbacks that helps in managing the different outputs between the
29+
* two logics
30+
*/
31+
public abstract class AbstractDownloader extends AbstractReleaseCallback{
32+
private final Release current = new Release();
33+
34+
public void checkNewVersion() {
35+
Call<Release> call = NetworkManager.getGitHubApi().getLatestRelease();
36+
call.enqueue(this);
37+
}
38+
39+
@Override
40+
public void onReleaseReceived(final Release release) {
41+
if (release.isNewerThan(current)) {
42+
new Thread(new Runnable() {
43+
@Override
44+
public void run() {
45+
if (warnAboutNew(release)) {
46+
obtainNew(release);
47+
}
48+
}
49+
}).start();
50+
51+
}
52+
}
53+
54+
abstract boolean warnAboutNew(Release release);
55+
56+
private void obtainNew(Release release) {
57+
try {
58+
onReleaseDownloaded(FileUtils.downloadFileFrom(release), release);
59+
} catch (IOException e) {
60+
System.err.println("ERROR: " + e.getMessage());
61+
}
62+
}
63+
64+
abstract void onReleaseDownloaded(File file, Release release);
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
21+
import java.io.File;
22+
import java.util.Scanner;
23+
24+
/**
25+
* This class is the one taking care of downloading the update from the CLI, asking the user if they want to update
26+
* to the new version when one is found.
27+
*/
28+
public class CliDownloader extends AbstractDownloader{
29+
private static final AbstractDownloader instance = new CliDownloader();
30+
31+
private CliDownloader() {}
32+
33+
@Override
34+
boolean warnAboutNew(Release release) {
35+
String message = "New ClassyShark version available!\n" +
36+
release.getReleaseName() + "\n" +
37+
release.getChangelog() + "\n" +
38+
"Do you wish to download it? (y/N)";
39+
40+
System.out.println(message);
41+
Scanner scanner = new Scanner(System.in);
42+
43+
return scanner.next().equalsIgnoreCase("y");
44+
}
45+
46+
@Override
47+
void onReleaseDownloaded(File file, Release release) {
48+
49+
String message = "New ClassyShark version available offline!\n" +
50+
"The new release " +
51+
release.getReleaseName() +
52+
" has been downloaded to " +
53+
file.getAbsolutePath();
54+
55+
System.out.println(message);
56+
}
57+
58+
public static AbstractDownloader getInstance() {
59+
return instance;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
21+
import javax.swing.*;
22+
import java.io.File;
23+
24+
/**
25+
* This class is the one being called when ClassyShark shall be updated from the GUI.
26+
* Its callback is informing the user about the new version and the new changelog
27+
*/
28+
public class GuiDownloader extends AbstractDownloader{
29+
private final static AbstractDownloader instance = new GuiDownloader();
30+
31+
private GuiDownloader() {}
32+
33+
@Override
34+
boolean warnAboutNew(Release release) {
35+
return true;
36+
}
37+
38+
@Override
39+
void onReleaseDownloaded(File file, Release release) {
40+
SwingUtilities.invokeLater(new MessageRunnable(release.getReleaseName(), release.getChangelog()));
41+
}
42+
43+
public static AbstractDownloader getInstance() {
44+
return instance;
45+
}
46+
}

ClassySharkWS/src/com/google/classyshark/updater/networking/MessageRunnable.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818

1919
import javax.swing.*;
2020

21-
public class MessageRunnable implements Runnable {
21+
class MessageRunnable implements Runnable {
2222
private final String title;
2323
private final String changelog;
24-
private final boolean isGui;
2524

2625
private final String ICON_PATH = "/resources/ic_update.png";
2726

28-
public MessageRunnable(String title, String changelog, boolean gui) {
27+
MessageRunnable(String title, String changelog) {
2928
this.title = buildTitleFrom(title);
3029
this.changelog = buildChangelogFrom(changelog);
31-
this.isGui = gui;
3230
}
3331

3432
private String buildTitleFrom(String title) {
@@ -41,12 +39,6 @@ private String buildChangelogFrom(String changelog) {
4139

4240
@Override
4341
public void run() {
44-
if (isGui) {
45-
warnUserAboutNewRelease();
46-
}
47-
}
48-
49-
private void warnUserAboutNewRelease() {
5042
final Icon icon = new ImageIcon(getClass().getResource(ICON_PATH));
5143
JOptionPane.showConfirmDialog(null, changelog, title, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, icon);
5244
}

0 commit comments

Comments
 (0)