Skip to content

Commit

Permalink
替换一些已弃用的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
AceDroidX committed Dec 28, 2024
1 parent 0d0b82d commit ad4b0ae
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 59 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/io/github/acedroidx/frp/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class AboutActivity : AppCompatActivity() {
get() {
val packageManager = packageManager
val packInfo = packageManager?.getPackageInfo(packageName, 0)
Log.d("wxxDebugAbout", packInfo!!.versionName)
return packInfo.versionName
return packInfo?.versionName ?: ""
}
}
43 changes: 19 additions & 24 deletions app/src/main/java/io/github/acedroidx/frp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ class MainActivity : AppCompatActivity() {
val binder = service as ShellService.LocalBinder
mService = binder.getService()
mBound = true
state_switch.isChecked = true
state_switch.isChecked = mService.getIsRunning()
}

override fun onServiceDisconnected(arg0: ComponentName) {
mBound = false
state_switch.isChecked = false
}
}

Expand All @@ -58,26 +57,33 @@ class MainActivity : AppCompatActivity() {
checkNotificationPermission()
createBGNotificationChannel()

mBound = isServiceRunning(ShellService::class.java)
state_switch = findViewById<SwitchCompat>(R.id.state_switch)
state_switch.isChecked = mBound
state_switch.setOnCheckedChangeListener { buttonView, isChecked -> if (isChecked) (startShell()) else (stopShell()) }
val editor = getSharedPreferences("data", AppCompatActivity.MODE_PRIVATE)
state_switch.isChecked = false
state_switch.setOnClickListener { if (state_switch.isChecked) (startShell()) else (stopShell()) }
val preferences = getSharedPreferences("data", AppCompatActivity.MODE_PRIVATE)
auto_start_switch = findViewById<SwitchCompat>(R.id.auto_start_switch)
auto_start_switch.isChecked = editor.getBoolean("auto_start", false)
auto_start_switch.setOnCheckedChangeListener { buttonView, isChecked ->
val editor = editor.edit()
auto_start_switch.isChecked = preferences.getBoolean("auto_start", false)
auto_start_switch.setOnCheckedChangeListener { _, isChecked ->
val editor = preferences.edit()
editor.putBoolean("auto_start", isChecked)
editor.apply();
}
if (mBound) {
if (!mBound) {
val intent = Intent(this, ShellService::class.java)
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}

setListener()
}

override fun onDestroy() {
super.onDestroy()
if (mBound) {
unbindService(connection)
mBound = false
}
}

private fun setListener() {
val configButton = findViewById<Button>(R.id.configButton)
configButton.setOnClickListener {
Expand Down Expand Up @@ -120,16 +126,15 @@ class MainActivity : AppCompatActivity() {

private fun startShell() {
val intent = Intent(this, ShellService::class.java)
intent.setAction(ShellServiceAction.START)
intent.putExtra("filename", BuildConfig.FrpcFileName)
startService(intent)
// Bind to LocalService
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}

private fun stopShell() {
val intent = Intent(this, ShellService::class.java)
unbindService(connection)
stopService(intent)
intent.setAction(ShellServiceAction.STOP)
startService(intent)
}

private fun checkNotificationPermission() {
Expand Down Expand Up @@ -173,14 +178,4 @@ class MainActivity : AppCompatActivity() {
notificationManager.createNotificationChannel(channel)
}
}

private fun isServiceRunning(serviceClass: Class<*>): Boolean {
val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
return true
}
}
return false
}
}
86 changes: 53 additions & 33 deletions app/src/main/java/io/github/acedroidx/frp/ShellService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class ShellService : Service() {
outputBuilder.clear()
}

fun getIsRunning(): Boolean {
return process_thread != null
}

// Binder given to clients
private val binder = LocalBinder()

Expand All @@ -50,45 +54,61 @@ class ShellService : Service() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
var filename = ""
if (process_thread != null) {
Log.w("adx", "process isn't null,service won't start")
Toast.makeText(this, "process isn't null,service won't start", Toast.LENGTH_SHORT)
.show()
return START_NOT_STICKY
}
if (intent != null) {
filename = intent.extras?.get("filename") as String
} else {
filename = "Error:filename unknown!!!"
Log.e("adx", filename)
Toast.makeText(this, filename, Toast.LENGTH_LONG).show()
stopSelf()
return START_NOT_STICKY
}
val ainfo =
packageManager.getApplicationInfo(packageName, PackageManager.GET_SHARED_LIBRARY_FILES)
Log.d("adx", "native library dir ${ainfo.nativeLibraryDir}")
try {
runCommand(
"${ainfo.nativeLibraryDir}/${filename} -c ${BuildConfig.ConfigFileName}",
arrayOf(""),
this.filesDir
)
} catch (e: Exception) {
Log.e("adx", e.stackTraceToString())
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
stopSelf()
return START_NOT_STICKY
when (intent?.action) {
ShellServiceAction.START -> {
if (process_thread != null) {
Log.w("adx", "process isn't null,service won't start")
Toast.makeText(
this, "process isn't null,service won't start", Toast.LENGTH_SHORT
).show()
return START_NOT_STICKY
}
val filename = intent.extras?.getString("filename")
if (filename == null) {
Log.w("adx", "filename is null,service won't start")
Toast.makeText(this, "filename is null,service won't start", Toast.LENGTH_SHORT)
.show()
return START_NOT_STICKY
}
val ainfo = packageManager.getApplicationInfo(
packageName, PackageManager.GET_SHARED_LIBRARY_FILES
)
Log.d("adx", "native library dir ${ainfo.nativeLibraryDir}")
try {
runCommand(
"${ainfo.nativeLibraryDir}/${filename} -c ${BuildConfig.ConfigFileName}",
arrayOf(""),
this.filesDir
)
} catch (e: Exception) {
Log.e("adx", e.stackTraceToString())
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
stopSelf()
return START_NOT_STICKY
}
Toast.makeText(this, "已启动frp服务", Toast.LENGTH_SHORT).show()
startForeground(1, showMotification());
}

ShellServiceAction.STOP -> {
process_thread?.interrupt()
process_thread = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_REMOVE)
} else {
@Suppress("DEPRECATION") stopForeground(true)
}
stopSelf()
Toast.makeText(this, "已关闭frp服务", Toast.LENGTH_SHORT).show()
}
}
Toast.makeText(this, "已启动frp服务", Toast.LENGTH_SHORT).show()
startForeground(1, showMotification());
return START_NOT_STICKY
}

override fun onDestroy() {
if (process_thread != null) Log.w("adx", "onDestroy: process_thread is not null")
process_thread?.interrupt()
Toast.makeText(this, "已关闭frp服务", Toast.LENGTH_SHORT).show()
process_thread = null
}

private fun runCommand(command: String, envp: Array<String>, dir: File) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.acedroidx.frp

object ShellServiceAction {
const val START = "io.github.acedroidx.frp.START"
const val STOP = "io.github.acedroidx.frp.STOP"
// const val PAUSE = "io.github.acedroidx.frp.PAUSE"
// const val CONTINUE = "io.github.acedroidx.frp.CONTINUE"
}

0 comments on commit ad4b0ae

Please sign in to comment.