@@ -69,8 +69,8 @@ public class MainActivity extends AppCompatActivity {
6969 BufferedReader dis ;
7070 public static String getSHA512 ;
7171 public static String genSHA512 ;
72- static private Boolean root = false ;
73- static private String libPath = null ;
72+ private Boolean root = false ;
73+ static private int downloadId = 0 ;
7474
7575 @ Override
7676 protected void onCreate (Bundle savedInstanceState )throws NullPointerException {
@@ -84,7 +84,20 @@ protected void onCreate(Bundle savedInstanceState)throws NullPointerException {
8484 setSupportActionBar (toolbar );
8585 if (savedInstanceState == null ) {
8686 permission ();
87- supportedFiles ();
87+ if (root ) {
88+ supportedFiles ();
89+ } else {
90+ AlertDialog .Builder builder = new AlertDialog .Builder (MainActivity .this );
91+ builder .setTitle ("Root Access Required!" );
92+ builder .setCancelable (false );
93+ builder .setNegativeButton ("Exit" , ((dialog , which ) -> {
94+ moveTaskToBack (true );
95+ android .os .Process .killProcess (android .os .Process .myPid ());
96+ System .exit (1 );
97+ }));
98+ AlertDialog rootMissing = builder .create ();
99+ rootMissing .show ();
100+ }
88101 }
89102 try {
90103 PackageInfo pInfo = this .getPackageManager ().getPackageInfo (getPackageName (), 0 );
@@ -757,6 +770,7 @@ void updater(int mode) {
757770 .setCancelable (false )
758771 .setPositiveButton ("Download & Install" , (dialog , id ) -> {
759772 getDownloadHash ();
773+ downloadId = 1 ;
760774 Uri dl = Uri .parse ("https://github.com/mayankmetha/Rucky/releases/download/" +newVersion +"/rucky.apk" );
761775 download (dl );
762776 })
@@ -829,7 +843,8 @@ public void onReceive(Context context, Intent intent) {
829843 dlStatus = c .getInt (c .getColumnIndex (DownloadManager .COLUMN_STATUS ));
830844 long refId = intent .getLongExtra (DownloadManager .EXTRA_DOWNLOAD_ID , -1 );
831845 if (refId == downloadRef && dlStatus == DownloadManager .STATUS_SUCCESSFUL ) {
832- generateHash ();
846+ if (downloadId == 1 ) generateHash ();
847+ else if (downloadId == 2 ) generateDependencyHash ();
833848 }
834849 }
835850 }
@@ -907,11 +922,7 @@ void generateHash() {
907922 AlertDialog .Builder alertBuilder = new AlertDialog .Builder (this );
908923 alertBuilder .setMessage ("Update file corrupted!" )
909924 .setCancelable (false )
910- .setPositiveButton ("RETRY AGAIN" , (dialogInterface , i ) -> {
911- Uri dl = Uri .parse ("https://github.com/mayankmetha/Rucky/releases/download/" +newVersion +"/rucky.apk" );
912- download (dl );
913- })
914- .setNegativeButton ("TRY LATER" , (dialog , which ) -> {
925+ .setNegativeButton ("TRY AGAIN LATER" , (dialog , which ) -> {
915926 });
916927 AlertDialog alert = alertBuilder .create ();
917928 alert .show ();
@@ -945,21 +956,9 @@ private void permission() {
945956 String rootCheck = dis .readLine ();
946957 if (rootCheck .contains ("uid=0" )) root = true ;
947958 }
948- } catch (Exception e ) {
959+ } catch (IOException e ) {
949960 e .printStackTrace ();
950961 }
951- if (!root ) {
952- AlertDialog .Builder builder = new AlertDialog .Builder (MainActivity .this );
953- builder .setTitle ("Root Access Required!" );
954- builder .setCancelable (false );
955- builder .setPositiveButton ("Exit" , (dialog , which ) -> {
956- moveTaskToBack (true );
957- android .os .Process .killProcess (android .os .Process .myPid ());
958- System .exit (1 );
959- });
960- AlertDialog rootExit = builder .create ();
961- rootExit .show ();
962- }
963962 }
964963
965964 private void supportedFiles () {
@@ -992,11 +991,18 @@ private void supportedFiles() {
992991 AlertDialog .Builder builder = new AlertDialog .Builder (MainActivity .this );
993992 builder .setTitle ("Dependency file missing" );
994993 builder .setCancelable (false );
995- builder .setPositiveButton ("Exit" , (dialog , which ) -> {
994+ builder .setPositiveButton ("Download & Install" , (dialog , which ) -> {
995+ String arch = Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ? Build .SUPPORTED_ABIS [0 ] : Build .CPU_ABI ;
996+ downloadDependenciesHash (arch );
997+ downloadId = 2 ;
998+ Uri dl = Uri .parse ("https://raw.githubusercontent.com/mayankmetha/Rucky/master/release/" + arch + "/rucky-hid" );
999+ downloadDependencies (dl );
1000+ });
1001+ builder .setNegativeButton ("Exit" , ((dialog , which ) -> {
9961002 moveTaskToBack (true );
9971003 android .os .Process .killProcess (android .os .Process .myPid ());
9981004 System .exit (1 );
999- });
1005+ })) ;
10001006 AlertDialog fileMissing = builder .create ();
10011007 fileMissing .show ();
10021008 } else {
@@ -1016,4 +1022,99 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
10161022 }
10171023 }
10181024 }
1025+
1026+ private void downloadDependenciesHash (String arch ) {
1027+ final ConnectivityManager conMgr = (ConnectivityManager ) getSystemService (Context .CONNECTIVITY_SERVICE );
1028+ assert conMgr != null ;
1029+ final NetworkInfo activeNetwork = conMgr .getActiveNetworkInfo ();
1030+ if (activeNetwork != null && activeNetwork .isConnected ()) {
1031+ try {
1032+ Notification .Builder updateNotify ;
1033+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
1034+ updateNotify = new Notification .Builder (this , CHANNEL_ID );
1035+ } else {
1036+ updateNotify = new Notification .Builder (this );
1037+ }
1038+ updateNotify .setContentTitle ("Verifying dependencies" )
1039+ .setContentText ("Please Wait..." )
1040+ .setSmallIcon (R .drawable .ic_notification )
1041+ .setAutoCancel (true );
1042+ getManager ().notify (4 ,updateNotify .build ());
1043+ URL url = new URL ("https://raw.githubusercontent.com/mayankmetha/Rucky/master/release/" +arch +"/rucky-hid.sha512" );
1044+ new fetchHash ().execute (url );
1045+ getManager ().cancel (4 );
1046+ } catch (MalformedURLException e ) {
1047+ e .printStackTrace ();
1048+ }
1049+ } else {
1050+ AlertDialog .Builder alertBuilder = new AlertDialog .Builder (this );
1051+ alertBuilder .setMessage ("Please check the network connection" )
1052+ .setCancelable (false )
1053+ .setPositiveButton ("OK" , (dialogInterface , i ) -> {
1054+ });
1055+ AlertDialog alert = alertBuilder .create ();
1056+ alert .show ();
1057+ }
1058+ }
1059+
1060+ private void downloadDependencies (Uri uri ) {
1061+ AlertDialog .Builder alertBuilder = new AlertDialog .Builder (this );
1062+ alertBuilder .setMessage ("Please leave the app open" )
1063+ .setCancelable (false )
1064+ .setPositiveButton ("OK" , (dialogInterface , i ) -> {
1065+ });
1066+ AlertDialog alert = alertBuilder .create ();
1067+ alert .show ();
1068+ File fDel = new File (Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ) + "/rucky-hid" );
1069+ if (fDel .exists ()) {
1070+ fDel .delete ();
1071+ }
1072+ downloadManager = (DownloadManager ) getSystemService (DOWNLOAD_SERVICE );
1073+ DownloadManager .Request req = new DownloadManager .Request (uri );
1074+ req .setAllowedNetworkTypes (DownloadManager .Request .NETWORK_MOBILE | DownloadManager .Request .NETWORK_WIFI );
1075+ req .setAllowedOverRoaming (true );
1076+ req .setTitle ("rucky-hid" );
1077+ req .setDestinationInExternalPublicDir (Environment .DIRECTORY_DOWNLOADS ,"/rucky-hid" );
1078+ req .setVisibleInDownloadsUi (true );
1079+ DownloadManager .Query q = new DownloadManager .Query ();
1080+ q .setFilterById (DownloadManager .STATUS_FAILED | DownloadManager .STATUS_SUCCESSFUL | DownloadManager .STATUS_PAUSED | DownloadManager .STATUS_PENDING | DownloadManager .STATUS_RUNNING );
1081+ Cursor c = downloadManager .query (q );
1082+ while (c .moveToNext ()) {
1083+ downloadManager .remove (c .getLong (c .getColumnIndex (DownloadManager .COLUMN_ID )));
1084+ }
1085+ downloadRef = downloadManager .enqueue (req );
1086+ IntentFilter filter = new IntentFilter (DownloadManager .ACTION_DOWNLOAD_COMPLETE );
1087+ registerReceiver (downloadBR , filter );
1088+ }
1089+
1090+ void generateDependencyHash () {
1091+ File file = new File (Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ) + "/rucky-hid" );
1092+ try {
1093+ genSHA512 = Files .asByteSource (file ).hash (Hashing .sha512 ()).toString ();
1094+ } catch (IOException e ) {
1095+ e .printStackTrace ();
1096+ }
1097+ if (getSHA512 .equals (genSHA512 )) {
1098+ installDependency ();
1099+ } else {
1100+ AlertDialog .Builder alertBuilder = new AlertDialog .Builder (this );
1101+ alertBuilder .setMessage ("Dependency file corrupted!" )
1102+ .setCancelable (false )
1103+ .setNegativeButton ("TRY AGAIN LATER" , (dialog , which ) -> {
1104+ });
1105+ AlertDialog alert = alertBuilder .create ();
1106+ alert .show ();
1107+ }
1108+ }
1109+
1110+ private void installDependency () {
1111+ File file = new File (Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ) + "/rucky-hid" );
1112+ String path = file .getAbsolutePath ();
1113+ try {
1114+ dos .writeBytes ("mv " +path +" /data/local/tmp/rucky-hid;chmod 755 /data/local/tmp/rucky-hid\n " );
1115+ dos .flush ();
1116+ } catch (IOException e ) {
1117+ e .printStackTrace ();
1118+ }
1119+ }
10191120}
0 commit comments