Skip to content

Commit 2a629a0

Browse files
Merge pull request #3 from NavjotSRakhra/Update
App can now check for updates and download and install them
2 parents 381b123 + 11d7cd1 commit 2a629a0

11 files changed

Lines changed: 279 additions & 68 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ solve that problem with some extra quality of life features. I will try to make
88
it better over time by keeping it up to date and fixing any bugs that I notice
99
or are brought to my attention.
1010
## How to download
11-
[Click here](https://github.com/NavjotSRakhra/ValorantAgentRandomizer/releases/download/v1.1.3/Valorant.Agent.Randomizer.v1.1.3.apk) to download the latest version Or to use this application you simply have to download it from the **Releases** section
11+
[Click here](https://github.com/NavjotSRakhra/ValorantAgentRandomizer/releases/download/v1.1.4/Valorant.Agent.Randomizer.v1.1.4.apk) to download the latest version Or to use this application you simply have to download it from the **Releases** section
1212
on Github and download the latest version.
1313
### Application requirements
1414
The app requires a minimum of Android API 26. This means the app will run on

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "navjot.valorant.valorantagentradomizer"
1010
minSdk 26
1111
targetSdk 31
12-
versionCode 9
13-
versionName "1.1.3"
12+
versionCode 11
13+
versionName "1.1.4"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
}

app/release/app-release.apk

4.91 KB
Binary file not shown.

app/release/output-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"type": "SINGLE",
1212
"filters": [],
1313
"attributes": [],
14-
"versionCode": 9,
15-
"versionName": "1.1.3",
14+
"versionCode": 11,
15+
"versionName": "1.1.4",
1616
"outputFile": "app-release.apk"
1717
}
1818
],

app/src/main/AndroidManifest.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,33 @@
33
package="navjot.valorant.valorantagentradomizer">
44

55
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6+
<uses-permission android:name="android.permission.INTERNET" />
7+
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
8+
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
9+
610
<application
711
android:allowBackup="true"
812
android:icon="@mipmap/ic_launcher"
913
android:label="@string/app_name"
1014
android:roundIcon="@mipmap/ic_launcher_round"
1115
android:supportsRtl="true"
12-
android:theme="@style/Theme.ValorantAgentRadomizer" >
16+
android:theme="@style/Theme.ValorantAgentRadomizer">
17+
<provider
18+
android:authorities="${applicationId}.provider"
19+
android:name="androidx.core.content.FileProvider"
20+
android:exported="false"
21+
android:grantUriPermissions="true">
22+
<meta-data
23+
android:name="android.support.FILE_PROVIDER_PATHS"
24+
android:resource="@xml/provided_paths"/>
25+
</provider>
1326
<activity
1427
android:name=".MainActivity"
15-
android:label="@string/app_name"
1628
android:exported="true">
1729
<intent-filter>
1830
<action android:name="android.intent.action.MAIN" />
1931
<category android:name="android.intent.category.LAUNCHER" />
2032
</intent-filter>
2133
</activity>
2234
</application>
23-
<uses-permission android:name="android.permission.INTERNET"/>
2435
</manifest>

app/src/main/java/navjot/valorant/valorantagentradomizer/MainActivity.java

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
11
package navjot.valorant.valorantagentradomizer;
22

3+
import android.Manifest;
34
import android.app.Activity;
45
import android.app.AlertDialog;
56
import android.content.Context;
7+
import android.content.SharedPreferences;
8+
import android.content.pm.PackageManager;
9+
import android.content.pm.PermissionInfo;
610
import android.content.res.Configuration;
7-
import android.net.ConnectivityManager;
811
import android.os.Bundle;
12+
import android.os.Environment;
913
import android.view.View;
1014
import android.view.animation.Animation;
1115
import android.view.animation.AnimationUtils;
1216
import android.widget.Toast;
1317

18+
import androidx.activity.result.ActivityResult;
19+
import androidx.activity.result.ActivityResultCallback;
20+
import androidx.activity.result.ActivityResultCaller;
21+
import androidx.activity.result.ActivityResultLauncher;
22+
import androidx.activity.result.contract.ActivityResultContracts;
1423
import androidx.annotation.NonNull;
1524
import androidx.annotation.Nullable;
25+
import androidx.core.app.ActivityCompat;
26+
import androidx.core.content.ContextCompat;
27+
import androidx.core.content.PermissionChecker;
1628
import androidx.core.content.res.ResourcesCompat;
1729
import androidx.recyclerview.widget.DefaultItemAnimator;
1830
import androidx.recyclerview.widget.GridLayoutManager;
1931
import androidx.recyclerview.widget.RecyclerView;
2032

21-
import java.io.IOException;
22-
import java.io.InputStream;
23-
import java.net.URL;
33+
import java.io.FileNotFoundException;
34+
import java.security.Permission;
2435
import java.util.ArrayList;
25-
import java.util.concurrent.Executor;
2636
import java.util.concurrent.ExecutorService;
2737
import java.util.concurrent.Executors;
2838

29-
import javax.net.ssl.HttpsURLConnection;
30-
3139
import navjot.valorant.valorantagentradomizer.UIElements.modernAgentHolder.AgentData;
3240
import navjot.valorant.valorantagentradomizer.UIElements.modernAgentHolder.ModernAgentAdapter;
41+
import navjot.valorant.valorantagentradomizer.update.Updatable;
42+
import navjot.valorant.valorantagentradomizer.update.Update;
3343
import navjot.valorant.valorantagentradomizer.valorant.AgentFlag;
3444

3545
public class MainActivity extends Activity {
@@ -41,10 +51,7 @@ public class MainActivity extends Activity {
4151
private static AgentFlag agentFlags;
4252
private Animation scaleUp, scaleDown;
4353
private static final String buildVersionName = BuildConfig.VERSION_NAME;
44-
private static final String latestReleaseURL = "https://api.github.com/repos/NavjotSRakhra/ValorantAgentRandomizer/releases/latest";
45-
private static String latestBuildVersion = "@";
46-
private static final ExecutorService executorService = Executors.newFixedThreadPool(1);
47-
private static String latestReleaseResponse = "@";
54+
private static final ExecutorService executorService = Executors.newFixedThreadPool(2);
4855

4956
@Override
5057
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -60,61 +67,48 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6067
findViewById(R.id.generate).setOnClickListener(this::generateButtonListener);
6168
findViewById(R.id.helpFloatingActionButton).setOnClickListener(this::helpFloatingActionButtonListener);
6269

63-
checkUpdates(executorService, this::update);
70+
Updatable checkUpdatable = new Updatable(this, buildVersionName, "NavjotSRakhra", "ValorantAgentRandomizer");
71+
checkUpdatable.checkUpdates(executorService, this::newUpdateAvailable);
6472
}
6573

66-
private void update(String updateLink) {
67-
68-
}
69-
70-
private void checkUpdates(Executor executor, OnUpdatable actionIfUpdatable) {
71-
executor.execute(() -> {
72-
latestBuildVersion = getLatestBuildVersion();
73-
if (isUpdatable()) {
74-
String updateLink = getUpdateLink();
75-
actionIfUpdatable.onComplete(updateLink);
76-
}
74+
private void newUpdateAvailable(String updateLink) {
75+
SharedPreferences pref = this.getSharedPreferences("download_prompt", Context.MODE_PRIVATE);
76+
int n = pref.getInt("down", 1);
77+
if (n == 0)
78+
return;
79+
runOnUiThread(() -> {
80+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
81+
builder.setTitle(R.string.new_version_available).setMessage(R.string.download_prompt).setPositiveButton(R.string.yes, (dialogInterface, i) -> {
82+
Update updater = new Update(this, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), updateLink);
83+
executorService.execute(() -> downloadAndInstall(updater));
84+
}).setNegativeButton(R.string.no, null).setNeutralButton(R.string.dont_prompt_again, (dialogInterface, i) -> {
85+
pref.edit().putInt("down", 0).apply();
86+
}).show();
7787
});
7888
}
7989

80-
private String getUpdateLink() {
81-
String temp = latestReleaseResponse.substring(latestReleaseResponse.indexOf("browser_download_url") + ("browser_download_url\": ".length()));
82-
temp = temp.substring(0, temp.indexOf("\""));
83-
return temp;
84-
}
85-
86-
private boolean isUpdatable() {
87-
System.out.println(latestBuildVersion + "|" + buildVersionName);
88-
return !latestBuildVersion.equals(buildVersionName);
89-
}
90-
91-
private String getLatestBuildVersion() {
92-
try {
93-
94-
if (((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().isAvailable()) {
95-
int c;
96-
97-
URL url = new URL(MainActivity.latestReleaseURL);
98-
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
99-
InputStream in = httpsURLConnection.getInputStream();
100-
101-
StringBuilder stringBuilder = new StringBuilder();
102-
103-
while (-1 != (c = in.read())) {
104-
stringBuilder.appendCodePoint(c);
90+
private void downloadAndInstall(Update update) {
91+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.MANAGE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
92+
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.MANAGE_EXTERNAL_STORAGE}, 231);
93+
}
94+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.MANAGE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
95+
String fileName = this.getSharedPreferences("downloadedFileName", Context.MODE_PRIVATE).getString("fileName", "n");
96+
System.out.println(this.getSharedPreferences("downloadedFileName", MODE_PRIVATE).getAll());
97+
System.out.println(fileName);
98+
if (fileName.equals("n")) {
99+
update.download();
100+
}
101+
try {
102+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.REQUEST_INSTALL_PACKAGES) == PackageManager.PERMISSION_DENIED) {
103+
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 232);
105104
}
106-
in.close();
107-
httpsURLConnection.disconnect();
108-
109-
latestReleaseResponse = stringBuilder.toString();
110-
String tag_name = latestReleaseResponse.substring(latestReleaseResponse.indexOf("tag_name") + ("tag_name\": ".length()));
111-
return tag_name.substring(1, tag_name.indexOf('\"'));
105+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.REQUEST_INSTALL_PACKAGES) == PackageManager.PERMISSION_DENIED) {
106+
update.install();
107+
}
108+
} catch (FileNotFoundException e) {
109+
e.printStackTrace();
112110
}
113-
114-
} catch (IOException e) {
115-
System.out.println(e.getMessage());
116111
}
117-
return buildVersionName;
118112
}
119113

120114
@Override
@@ -201,5 +195,4 @@ public void helpFloatingActionButtonListener(View v) {
201195
public static AgentFlag getAgentFlags() {
202196
return agentFlags;
203197
}
204-
}
205-
//Updated java version from java 8 to java 11
198+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package navjot.valorant.valorantagentradomizer.update;
2+
3+
import android.content.Context;
4+
import android.net.ConnectivityManager;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.net.URL;
9+
import java.util.concurrent.Executor;
10+
11+
import javax.net.ssl.HttpsURLConnection;
12+
13+
import navjot.valorant.valorantagentradomizer.OnUpdatable;
14+
15+
public class Updatable {
16+
private String latestBuildVersion, latestReleaseResponse;
17+
private final String buildVersionName, latestReleaseURL;
18+
private final Context context;
19+
20+
public Updatable(Context context, String currentBuildVersionName, String userName, String repoName) {
21+
this.context = context;
22+
this.buildVersionName = currentBuildVersionName;
23+
this.latestReleaseURL = "https://api.github.com/repos/" + userName + "/" + repoName + "/releases/latest";
24+
}
25+
26+
public void checkUpdates(Executor executor, OnUpdatable actionIfUpdatable) {
27+
executor.execute(() -> {
28+
latestBuildVersion = getLatestBuildVersion();
29+
if (isUpdatable()) {
30+
String updateLink = getUpdateLink();
31+
actionIfUpdatable.onComplete(updateLink);
32+
}else {
33+
Update.deleteFile(context);
34+
}
35+
});
36+
}
37+
38+
private String getUpdateLink() {
39+
String temp = latestReleaseResponse.substring(latestReleaseResponse.indexOf("browser_download_url") + ("browser_download_url\": ".length()));
40+
temp = temp.substring(0, temp.indexOf("\""));
41+
return temp;
42+
}
43+
44+
private boolean isUpdatable() {
45+
return !latestBuildVersion.equals(buildVersionName);
46+
}
47+
48+
private String getLatestBuildVersion() {
49+
try {
50+
51+
if (((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().isAvailable()) {
52+
int c;
53+
54+
URL url = new URL(latestReleaseURL);
55+
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
56+
InputStream in = httpsURLConnection.getInputStream();
57+
58+
StringBuilder stringBuilder = new StringBuilder();
59+
60+
while (-1 != (c = in.read())) {
61+
stringBuilder.appendCodePoint(c);
62+
}
63+
in.close();
64+
httpsURLConnection.disconnect();
65+
66+
latestReleaseResponse = stringBuilder.toString();
67+
String tag_name = latestReleaseResponse.substring(latestReleaseResponse.indexOf("tag_name") + ("tag_name\": ".length()));
68+
return tag_name.substring(1, tag_name.indexOf('\"'));
69+
}
70+
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
}
74+
return buildVersionName;
75+
}
76+
}

0 commit comments

Comments
 (0)