added BroadcastReceiver in activity

This commit is contained in:
X1nto 2020-05-27 20:37:03 +04:00
parent 418ffe2fc9
commit 72aaab319e
3 changed files with 104 additions and 52 deletions

View File

@ -2,8 +2,6 @@ package com.vanced.manager.core.base;
import androidx.fragment.app.Fragment;
import com.vanced.manager.ui.MainActivity;
public class DummyJava extends Fragment {
}

View File

@ -1,18 +1,15 @@
package com.vanced.manager.core.installer
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller
import android.os.Build
import android.os.IBinder
import android.util.Log
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.Nullable
import androidx.appcompat.app.AlertDialog
import com.vanced.manager.R
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.vanced.manager.ui.MainActivity
class SplitInstallerService: Service() {
private val TAG = "VMInstall"
@ -33,58 +30,39 @@ class SplitInstallerService: Service() {
PackageInstaller.STATUS_SUCCESS -> {
Log.d(TAG, "Installation succeed")
getSharedPreferences("installPrefs", Context.MODE_PRIVATE).edit().putBoolean("isInstalling", false).apply()
launchVanced()
val mIntent = Intent(MainActivity.INSTALL_COMPLETED)
sendBroadcast(mIntent)
}
PackageInstaller.STATUS_FAILURE_ABORTED -> {
val mIntent = Intent(MainActivity.INSTALL_ABORTED)
sendBroadcast(mIntent)
}
PackageInstaller.STATUS_FAILURE_INVALID -> {
val mIntent = Intent(MainActivity.INSTALL_INVALID)
sendBroadcast(mIntent)
}
PackageInstaller.STATUS_FAILURE_CONFLICT -> {
val mIntent = Intent(MainActivity.INSTALL_CONFLICT)
sendBroadcast(mIntent)
}
PackageInstaller.STATUS_FAILURE_STORAGE -> {
val mIntent = Intent(MainActivity.INSTALL_STORAGE)
sendBroadcast(mIntent)
}
PackageInstaller.STATUS_FAILURE_BLOCKED -> {
val mIntent = Intent(MainActivity.INSTALL_BLOCKED)
sendBroadcast(mIntent)
}
PackageInstaller.STATUS_FAILURE_ABORTED -> alertBuilder("user aborted installation")
PackageInstaller.STATUS_FAILURE_INVALID -> alertBuilder("apk files are invalid")
PackageInstaller.STATUS_FAILURE_CONFLICT -> alertBuilder("app conflicts with already installed app")
PackageInstaller.STATUS_FAILURE_STORAGE -> alertBuilder("there was an error with storage.\n Hold up how is that even possible?")
else -> {
Log.d(TAG, "Installation failed")
alertBuilder("Installation failed")
val mIntent = Intent(MainActivity.INSTALL_FAILED)
sendBroadcast(mIntent)
}
}
stopSelf()
return START_NOT_STICKY
}
private fun alertBuilder(msg: String) {
val dialog = AlertDialog.Builder(applicationContext)
.setTitle("Error")
.setMessage("Operation failed because $msg")
.setPositiveButton(getString(R.string.close)) { dialog, _ -> dialog.dismiss() }
.create()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dialog.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
} else
dialog.window?.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
dialog.show()
}
private fun launchVanced() {
val intent = Intent()
intent.component = ComponentName("com.vanced.android.youtube", "com.vanced.android.youtube.HomeActivity")
val dialog = AlertDialog.Builder(applicationContext)
.setTitle("Success!")
.setMessage("Vanced has been successfully installed, do you want to launch it now?")
.setPositiveButton("Launch") {
_, _ -> startActivity(intent)
}
.setNegativeButton("Cancel") {
dialog, _ -> dialog.dismiss()
}
.create()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dialog.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
} else
dialog.window?.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
dialog.show()
}
@Nullable
override fun onBind(intent: Intent?): IBinder? {
return null

View File

@ -1,15 +1,16 @@
package com.vanced.manager.ui
import android.content.*
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceManager
import com.vanced.manager.R
import com.vanced.manager.core.Main
@ -38,9 +39,38 @@ class MainActivity : Main() {
setDisplayHomeAsUpEnabled(!isParent)
}
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
BLANK_INTENT
))
}
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
INSTALL_COMPLETED -> launchVanced()
else -> intent.action?.let { alertBuilder(it) }
}
}
}
override fun onDestroy() {
super.onDestroy()
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
}
override fun onPause() {
super.onPause()
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
}
override fun onResume() {
super.onResume()
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
BLANK_INTENT
))
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navHost = findNavController(R.id.bottom_nav_host)
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
@ -82,4 +112,50 @@ class MainActivity : Main() {
else -> toolbar.navigationIcon = null
}
}
private fun alertBuilder(msg: String) {
AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("Operation failed because $msg")
.setPositiveButton(getString(R.string.close)) { dialog, _ ->
run {
dialog.dismiss()
recreate()
}
}
.create()
.show()
}
private fun launchVanced() {
val intent = Intent()
intent.component = ComponentName("com.vanced.android.youtube", "com.vanced.android.youtube.HomeActivity")
AlertDialog.Builder(this)
.setTitle("Success!")
.setMessage("Vanced has been successfully installed, do you want to launch it now?")
.setPositiveButton("Launch") {
_, _ -> startActivity(intent)
}
.setNegativeButton("Cancel") {
dialog, _ ->
run {
dialog.dismiss()
recreate()
}
}
.create()
.show()
}
companion object {
const val BLANK_INTENT = "BLANK"
const val INSTALL_COMPLETED = "Installation completed"
const val INSTALL_ABORTED = "user aborted installation"
const val INSTALL_BLOCKED = "user blocked installation"
const val INSTALL_STORAGE = "there was an error with storage.\n Hold up how is that even possible?"
const val INSTALL_CONFLICT = "app conflicts with already installed app"
const val INSTALL_FAILED = "it just failed idk"
const val INSTALL_INVALID = "apk files are invalid"
}
}