From c04f785dde8f8ec7c97ac65e2958a0a6ce359ec2 Mon Sep 17 00:00:00 2001 From: mayankmetha Date: Sun, 4 Aug 2019 10:46:47 +0530 Subject: [PATCH] Staging v1.4 --- app/build.gradle | 2 +- .../java/com/mayank/rucky/MainActivity.java | 149 +++++++++++++++--- .../mayank/rucky/RootSettingsFragment.java | 5 +- release/genSHA512.sh | 4 + release/version | 2 +- 5 files changed, 132 insertions(+), 30 deletions(-) create mode 100644 release/genSHA512.sh diff --git a/app/build.gradle b/app/build.gradle index e525b83b..2106b7a4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { minSdkVersion 19 targetSdkVersion 28 versionCode 1 - versionName "1.3" + versionName "1.4" } buildTypes { release { diff --git a/app/src/main/java/com/mayank/rucky/MainActivity.java b/app/src/main/java/com/mayank/rucky/MainActivity.java index 73db09a7..95ba6e54 100644 --- a/app/src/main/java/com/mayank/rucky/MainActivity.java +++ b/app/src/main/java/com/mayank/rucky/MainActivity.java @@ -69,8 +69,8 @@ public class MainActivity extends AppCompatActivity { BufferedReader dis; public static String getSHA512; public static String genSHA512; - static private Boolean root = false; - static private String libPath = null; + private Boolean root = false; + static private int downloadId = 0; @Override protected void onCreate(Bundle savedInstanceState)throws NullPointerException { @@ -84,7 +84,20 @@ protected void onCreate(Bundle savedInstanceState)throws NullPointerException { setSupportActionBar(toolbar); if (savedInstanceState == null) { permission(); - supportedFiles(); + if(root) { + supportedFiles(); + } else { + AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); + builder.setTitle("Root Access Required!"); + builder.setCancelable(false); + builder.setNegativeButton("Exit", ((dialog, which) -> { + moveTaskToBack(true); + android.os.Process.killProcess(android.os.Process.myPid()); + System.exit(1); + })); + AlertDialog rootMissing = builder.create(); + rootMissing.show(); + } } try { PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0); @@ -757,6 +770,7 @@ void updater(int mode) { .setCancelable(false) .setPositiveButton("Download & Install", (dialog, id) -> { getDownloadHash(); + downloadId = 1; Uri dl = Uri.parse("https://github.com/mayankmetha/Rucky/releases/download/"+newVersion+"/rucky.apk"); download(dl); }) @@ -829,7 +843,8 @@ public void onReceive(Context context, Intent intent) { dlStatus = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); long refId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); if (refId == downloadRef && dlStatus == DownloadManager.STATUS_SUCCESSFUL) { - generateHash(); + if(downloadId == 1) generateHash(); + else if(downloadId == 2) generateDependencyHash(); } } } @@ -907,11 +922,7 @@ void generateHash() { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); alertBuilder.setMessage("Update file corrupted!") .setCancelable(false) - .setPositiveButton("RETRY AGAIN", (dialogInterface, i) -> { - Uri dl = Uri.parse("https://github.com/mayankmetha/Rucky/releases/download/"+newVersion+"/rucky.apk"); - download(dl); - }) - .setNegativeButton("TRY LATER", (dialog, which) -> { + .setNegativeButton("TRY AGAIN LATER", (dialog, which) -> { }); AlertDialog alert = alertBuilder.create(); alert.show(); @@ -945,21 +956,9 @@ private void permission() { String rootCheck = dis.readLine(); if(rootCheck.contains("uid=0")) root = true; } - } catch (Exception e) { + } catch (IOException e) { e.printStackTrace(); } - if(!root) { - AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); - builder.setTitle("Root Access Required!"); - builder.setCancelable(false); - builder.setPositiveButton("Exit", (dialog, which) -> { - moveTaskToBack(true); - android.os.Process.killProcess(android.os.Process.myPid()); - System.exit(1); - }); - AlertDialog rootExit = builder.create(); - rootExit.show(); - } } private void supportedFiles() { @@ -992,11 +991,18 @@ private void supportedFiles() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Dependency file missing"); builder.setCancelable(false); - builder.setPositiveButton("Exit", (dialog, which) -> { + builder.setPositiveButton("Download & Install", (dialog, which) -> { + String arch = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? Build.SUPPORTED_ABIS[0] : Build.CPU_ABI; + downloadDependenciesHash(arch); + downloadId = 2; + Uri dl = Uri.parse("https://raw.githubusercontent.com/mayankmetha/Rucky/master/release/" + arch + "/rucky-hid"); + downloadDependencies(dl); + }); + builder.setNegativeButton("Exit", ((dialog, which) -> { moveTaskToBack(true); android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); - }); + })); AlertDialog fileMissing = builder.create(); fileMissing.show(); } else { @@ -1016,4 +1022,99 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis } } } + + private void downloadDependenciesHash(String arch) { + final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + assert conMgr != null; + final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); + if (activeNetwork != null && activeNetwork.isConnected()) { + try { + Notification.Builder updateNotify; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + updateNotify = new Notification.Builder(this, CHANNEL_ID); + } else { + updateNotify = new Notification.Builder(this); + } + updateNotify.setContentTitle("Verifying dependencies") + .setContentText("Please Wait...") + .setSmallIcon(R.drawable.ic_notification) + .setAutoCancel(true); + getManager().notify(4,updateNotify.build()); + URL url = new URL("https://raw.githubusercontent.com/mayankmetha/Rucky/master/release/"+arch+"/rucky-hid.sha512"); + new fetchHash().execute(url); + getManager().cancel(4); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } else { + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); + alertBuilder.setMessage("Please check the network connection") + .setCancelable(false) + .setPositiveButton("OK", (dialogInterface, i) -> { + }); + AlertDialog alert = alertBuilder.create(); + alert.show(); + } + } + + private void downloadDependencies(Uri uri) { + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); + alertBuilder.setMessage("Please leave the app open") + .setCancelable(false) + .setPositiveButton("OK", (dialogInterface, i) -> { + }); + AlertDialog alert = alertBuilder.create(); + alert.show(); + File fDel = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/rucky-hid"); + if (fDel.exists()) { + fDel.delete(); + } + downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); + DownloadManager.Request req = new DownloadManager.Request(uri); + req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); + req.setAllowedOverRoaming(true); + req.setTitle("rucky-hid"); + req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"/rucky-hid"); + req.setVisibleInDownloadsUi(true); + DownloadManager.Query q = new DownloadManager.Query(); + q.setFilterById(DownloadManager.STATUS_FAILED | DownloadManager.STATUS_SUCCESSFUL | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING); + Cursor c = downloadManager.query(q); + while (c.moveToNext()) { + downloadManager.remove(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID))); + } + downloadRef = downloadManager.enqueue(req); + IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); + registerReceiver(downloadBR, filter); + } + + void generateDependencyHash() { + File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/rucky-hid"); + try { + genSHA512 = Files.asByteSource(file).hash(Hashing.sha512()).toString(); + } catch (IOException e) { + e.printStackTrace(); + } + if(getSHA512.equals(genSHA512)) { + installDependency(); + } else { + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); + alertBuilder.setMessage("Dependency file corrupted!") + .setCancelable(false) + .setNegativeButton("TRY AGAIN LATER", (dialog, which) -> { + }); + AlertDialog alert = alertBuilder.create(); + alert.show(); + } + } + + private void installDependency() { + File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/rucky-hid"); + String path = file.getAbsolutePath(); + try { + dos.writeBytes("mv "+path+" /data/local/tmp/rucky-hid;chmod 755 /data/local/tmp/rucky-hid\n"); + dos.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/mayank/rucky/RootSettingsFragment.java b/app/src/main/java/com/mayank/rucky/RootSettingsFragment.java index 084a3acb..1b55cdc9 100644 --- a/app/src/main/java/com/mayank/rucky/RootSettingsFragment.java +++ b/app/src/main/java/com/mayank/rucky/RootSettingsFragment.java @@ -86,10 +86,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { Preference versionPreference = findPreference("version"); assert versionPreference != null; versionPreference.setSummary(Double.toString(currentVersion)); - String currentArch; - if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) - currentArch = Build.SUPPORTED_ABIS[0]; - else currentArch = Build.CPU_ABI; + String currentArch = Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP?Build.SUPPORTED_ABIS[0]:Build.CPU_ABI; Preference archPreference = findPreference("arch"); assert archPreference != null; archPreference.setSummary(currentArch); diff --git a/release/genSHA512.sh b/release/genSHA512.sh new file mode 100644 index 00000000..5c69ab07 --- /dev/null +++ b/release/genSHA512.sh @@ -0,0 +1,4 @@ +openssl sha512 ./arm64-v8a/rucky-hid | cut -d"=" -f2 | cut -d" " -f2 > ./arm64-v8a/rucky-hid.sha512 +openssl sha512 ./armeabi-v7a/rucky-hid | cut -d"=" -f2 | cut -d" " -f2 > ./armeabi-v7a/rucky-hid.sha512 +openssl sha512 ./x86/rucky-hid | cut -d"=" -f2 | cut -d" " -f2 > ./x86/rucky-hid.sha512 +openssl sha512 ./x86_64/rucky-hid | cut -d"=" -f2 | cut -d" " -f2 > ./x86_64/rucky-hid.sha512 diff --git a/release/version b/release/version index a58941b0..840ca8cb 100644 --- a/release/version +++ b/release/version @@ -1 +1 @@ -1.3 \ No newline at end of file +1.4 \ No newline at end of file