11package navjot .valorant .valorantagentradomizer ;
22
3+ import android .Manifest ;
34import android .app .Activity ;
45import android .app .AlertDialog ;
56import android .content .Context ;
7+ import android .content .SharedPreferences ;
8+ import android .content .pm .PackageManager ;
9+ import android .content .pm .PermissionInfo ;
610import android .content .res .Configuration ;
7- import android .net .ConnectivityManager ;
811import android .os .Bundle ;
12+ import android .os .Environment ;
913import android .view .View ;
1014import android .view .animation .Animation ;
1115import android .view .animation .AnimationUtils ;
1216import 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 ;
1423import androidx .annotation .NonNull ;
1524import androidx .annotation .Nullable ;
25+ import androidx .core .app .ActivityCompat ;
26+ import androidx .core .content .ContextCompat ;
27+ import androidx .core .content .PermissionChecker ;
1628import androidx .core .content .res .ResourcesCompat ;
1729import androidx .recyclerview .widget .DefaultItemAnimator ;
1830import androidx .recyclerview .widget .GridLayoutManager ;
1931import 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 ;
2435import java .util .ArrayList ;
25- import java .util .concurrent .Executor ;
2636import java .util .concurrent .ExecutorService ;
2737import java .util .concurrent .Executors ;
2838
29- import javax .net .ssl .HttpsURLConnection ;
30-
3139import navjot .valorant .valorantagentradomizer .UIElements .modernAgentHolder .AgentData ;
3240import navjot .valorant .valorantagentradomizer .UIElements .modernAgentHolder .ModernAgentAdapter ;
41+ import navjot .valorant .valorantagentradomizer .update .Updatable ;
42+ import navjot .valorant .valorantagentradomizer .update .Update ;
3343import navjot .valorant .valorantagentradomizer .valorant .AgentFlag ;
3444
3545public 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+ }
0 commit comments