Skip to content

Commit

Permalink
💥 重写启动核心的逻辑,进行了多项优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Szzrain committed Apr 10, 2023
1 parent 6316fb6 commit 5551043
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 190 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ android {

defaultConfig {
applicationId "com.sealdice.dice"
minSdk 26
minSdk 23
//noinspection ExpiredTargetSdkVersion
targetSdk 28
versionCode 27
versionName "v0.4.7-rc2"
versionCode 28
versionName "v0.4.8-rc"
buildConfigField "String", "DOCUMENTS_AUTHORITY", "\"com.sealdice.dice.authorities\""
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
Expand Down Expand Up @@ -48,10 +48,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0-alpha08'
implementation 'androidx.navigation:navigation-ui-ktx:2.6.0-alpha08'
implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0-alpha09'
implementation 'androidx.navigation:navigation-ui-ktx:2.6.0-alpha09'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
implementation 'androidx.core:core:1.9.0'
implementation 'androidx.core:core:1.10.0'
implementation 'androidx.preference:preference:1.2.0'
implementation 'androidx.lifecycle:lifecycle-service:2.6.1'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
</intent-filter>
</provider>
<service android:name=".MediaService"/>
<service android:name=".NotificationService"/>
<service
android:name=".ProcessService"
android:foregroundServiceType="location"/>
<service android:name=".WakeLockService"/>
<service android:name=".FloatWindowService"/>
<service android:name=".HeartbeatService"/>
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/sealdice/dice/DimWakeLockService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sealdice.dice

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.os.PowerManager

class DimWakeLockService : Service(){
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
val wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "DIM:LocationManagerService")
wakeLock.acquire()
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}
202 changes: 116 additions & 86 deletions app/src/main/java/com/sealdice/dice/FirstFragment.kt

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions app/src/main/java/com/sealdice/dice/MediaService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class MediaService : Service() {

// declaring object of MediaPlayer
private lateinit var player: MediaPlayer

// execution of service will start
// on calling this method
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
override fun onCreate() {
super.onCreate()
val base64 =
"AAAAGGZ0eXBtcDQyAAAAAG1wNDFpc29tAAAAKHV1aWRcpwj7Mo5CBahhZQ7KCpWWAAAADDEwLjAuMTgzNjMuMAAAAG5tZGF0AAAAAAAAABAnDEMgBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBAIBDSX5AAAAAAAAB9Pp9Pp9Pp9Pp9Pp9Pp9Pp9Pp9Pp9Pp9Pp9AAAC/m1vb3YAAABsbXZoZAAAAADeilCc3opQnAAAu4AAAAIRAAEAAAEAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAHBdHJhawAAAFx0a2hkAAAAAd6KUJzeilCcAAAAAgAAAAAAAAIRAAAAAAAAAAAAAAAAAQAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAABXW1kaWEAAAAgbWRoZAAAAADeilCc3opQnAAAu4AAAAIRVcQAAAAAAC1oZGxyAAAAAAAAAABzb3VuAAAAAAAAAAAAAAAAU291bmRIYW5kbGVyAAAAAQhtaW5mAAAAEHNtaGQAAAAAAAAAAAAAACRkaW5mAAAAHGRyZWYAAAAAAAAAAQAAAAx1cmwgAAAAAQAAAMxzdGJsAAAAZHN0c2QAAAAAAAAAAQAAAFRtcDRhAAAAAAAAAAEAAAAAAAAAAAACABAAAAAAu4AAAAAAADBlc2RzAAAAAAOAgIAfAAAABICAgBRAFQAGAAACM2gAAjNoBYCAgAIRkAYBAgAAABhzdHRzAAAAAAAAAAEAAAABAAACEQAAABxzdHNjAAAAAAAAAAEAAAABAAAAAQAAAAEAAAAYc3RzegAAAAAAAAAAAAAAAQAAAF4AAAAUc3RjbwAAAAAAAAABAAAAUAAAAMl1ZHRhAAAAkG1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAG1kaXIAAAAAAAAAAAAAAAAAAAAAY2lsc3QAAAAeqW5hbQAAABZkYXRhAAAAAQAAAADlvZXpn7MAAAAcqWRheQAAABRkYXRhAAAAAQAAAAAyMDIyAAAAIWFBUlQAAAAZZGF0YQAAAAEAAAAA5b2V6Z+z5py6AAAAMVh0cmEAAAApAAAAD1dNL0VuY29kaW5nVGltZQAAAAEAAAAOABUA2rD/dVfYAQ=="

Expand All @@ -37,6 +37,9 @@ class MediaService : Service() {
// value as true to play
// the audio on loop
player.isLooping = true
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// starting the process
player.start()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class NotificationActivity : AppCompatActivity() {
return when (item.itemId) {
android.R.id.home -> {
onBackPressedDispatcher.onBackPressed()
finish()
true
}
else -> super.onOptionsItemSelected(item)
Expand Down
29 changes: 0 additions & 29 deletions app/src/main/java/com/sealdice/dice/NotificationService.kt

This file was deleted.

104 changes: 104 additions & 0 deletions app/src/main/java/com/sealdice/dice/ProcessService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.sealdice.dice

import android.app.*
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import androidx.preference.PreferenceManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader


class ProcessService : Service(){
private val binder = MyBinder()
private var processBuilder: ProcessBuilder = ProcessBuilder("sh").redirectErrorStream(true)
private lateinit var process: Process
private var isRunning = false
private var shellLogs = ""
inner class MyBinder : Binder() {
fun getService(): ProcessService = this@ProcessService
}
fun getShellLogs(): String {
return shellLogs
}
fun stopProcess() {
isRunning = false
Thread.sleep(5)
process.destroy()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Build.VERSION.SDK_INT >= 26) {
val ns: String = Context.NOTIFICATION_SERVICE
val mNotificationManager = getSystemService(ns) as NotificationManager
val notificationChannel = NotificationChannel("sealdice","SealDice", NotificationManager.IMPORTANCE_HIGH)
mNotificationManager.createNotificationChannel(notificationChannel)
val pendingIntent = PendingIntent.getActivity(applicationContext, 0, Intent(applicationContext, NotificationActivity::class.java), PendingIntent.FLAG_MUTABLE)
val notification: Notification = Notification.Builder(this,"sealdice")
.setContentTitle("SealDice is running")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build()
startForeground(1, notification)
}
else {
val pendingIntent = PendingIntent.getActivity(applicationContext, 0, Intent(applicationContext, NotificationActivity::class.java), PendingIntent.FLAG_MUTABLE)
val notification: Notification = Notification.Builder(this)
.setContentTitle("SealDice is running")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build()
startForeground(1, notification)
}
if (!isRunning) {
isRunning = true
process = processBuilder.directory(File(this.filesDir.absolutePath)).start()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val args = sharedPreferences.getString("launch_args", "")
val cmd = "cd sealdice&&./sealdice-core $args"
GlobalScope.launch(context = Dispatchers.IO) {
val os = process.outputStream
os.write(cmd.toByteArray())
os.flush()
os.close()
val data = process.inputStream
val ir = BufferedReader(InputStreamReader(data))
while (isRunning) {
var line: String?
try {
line = ir.readLine()
} catch (e: Exception) {
break
}
while (line != null && isRunning) {
shellLogs += line
shellLogs += "\n"
try {
line = ir.readLine()
} catch (e: Exception) {
break
}
}
Thread.sleep(1000)
}
}
}
return START_STICKY
}
fun isRunning(): Boolean {
return isRunning
}
override fun onBind(p0: Intent?): IBinder {
return binder
}
override fun onDestroy() {
super.onDestroy()
stopProcess()
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/sealdice/dice/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity


class SettingsActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -19,6 +20,7 @@ class SettingsActivity : AppCompatActivity() {
return when (item.itemId) {
android.R.id.home -> {
onBackPressedDispatcher.onBackPressed()
finish()
true
}
else -> super.onOptionsItemSelected(item)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/sealdice/dice/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SeekBarPreference
import androidx.preference.SwitchPreferenceCompat


class SettingsFragment : PreferenceFragmentCompat() {
Expand All @@ -15,5 +16,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
setPreferencesFromResource(R.xml.prefrences, rootKey)
sharedPreferences = preferenceScreen.sharedPreferences!!
val mySeekBarPreference : SeekBarPreference? = findPreference<SeekBarPreference>("launch_waiting_time")
// val switchPreference: SwitchPreferenceCompat = findPreference("my_switch_preference")
// switchPreference.setTitleTextColor(resources.getColor(android.R.color.my_color))
}
}
38 changes: 29 additions & 9 deletions app/src/main/java/com/sealdice/dice/UpdateService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand All @@ -21,6 +23,7 @@ class UpdateService : Service() {
private val UPDATE_URL = "https://get.sealdice.com/seal/version/android"
private val NOTIFICATION_ID = 2

@OptIn(DelicateCoroutinesApi::class)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
GlobalScope.launch(context = Dispatchers.IO) {
checkForUpdates()
Expand Down Expand Up @@ -61,14 +64,33 @@ class UpdateService : Service() {
private fun showUpdateNotification(version: String) {
// Create a notification channel for Android Oreo and higher
val notificationManager = getSystemService(NotificationManager::class.java)
val channel = NotificationChannel(
"update_channel",
"Update Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
if (Build.VERSION.SDK_INT >= 26) {
val channel = NotificationChannel(
"update_channel",
"Update Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
} else {
val builder = NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("SealDice 检测到更新")
.setContentText("点击此处下载新版本 $version")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Create a pending intent for the update dialog
val uri = Uri.parse("https://d.catlevel.com/seal/android/latest")
val intent = Intent()
intent.action = "android.intent.action.VIEW"
intent.data = uri
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(pendingIntent)
// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder.build())
return
}
// Create a notification builder
val builder = NotificationCompat.Builder(this, "update_channel")
val builder = NotificationCompat.Builder(this,"update_channel")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("SealDice 检测到更新")
.setContentText("点击此处下载新版本 $version")
Expand All @@ -85,6 +107,4 @@ class UpdateService : Service() {
notificationManager.notify(NOTIFICATION_ID, builder.build())
}

// ...

}
Loading

0 comments on commit 5551043

Please sign in to comment.