mirror of
https://github.com/amir1376/ab-download-manager.git
synced 2025-02-20 11:43:24 +08:00
improve start on boot (#313)
This commit is contained in:
parent
3a9d1c22f2
commit
8a24d5e06d
@ -9,6 +9,7 @@ import com.abdownloadmanager.desktop.utils.*
|
||||
import com.abdownloadmanager.desktop.utils.singleInstance.*
|
||||
import com.abdownloadmanager.integration.Integration
|
||||
import com.abdownloadmanager.utils.DownloadSystem
|
||||
import com.sun.jna.platform.win32.Advapi32Util
|
||||
import ir.amirab.util.platform.Platform
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okio.Path.Companion.toOkioPath
|
||||
|
@ -196,10 +196,8 @@ val startUpModule = module {
|
||||
single {
|
||||
Startup.getStartUpManagerForDesktop(
|
||||
name = AppInfo.name,
|
||||
path = AppInfo.exeFile?.let { exeFile ->
|
||||
"$exeFile ${AppArguments.Args.BACKGROUND}"
|
||||
},
|
||||
jar = false,
|
||||
path = AppInfo.exeFile,
|
||||
args = listOf(AppArguments.Args.BACKGROUND),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -3,4 +3,6 @@ plugins {
|
||||
}
|
||||
dependencies {
|
||||
implementation(project(":shared:utils"))
|
||||
// for windows, we use registry
|
||||
implementation(libs.jna.platform)
|
||||
}
|
@ -1,11 +1,22 @@
|
||||
package ir.amirab.util.startup
|
||||
|
||||
abstract class AbstractStartupManager(
|
||||
val isJar: Boolean,
|
||||
val name: String,
|
||||
val path: String,
|
||||
val args: List<String>,
|
||||
) {
|
||||
@Throws(Exception::class)
|
||||
abstract fun install()
|
||||
abstract fun uninstall()
|
||||
|
||||
fun getExecutableWithArgs(): String {
|
||||
return buildList {
|
||||
add(path.quoted())
|
||||
addAll(args)
|
||||
}.joinToString(" ")
|
||||
}
|
||||
|
||||
private fun String.quoted(): String {
|
||||
return "\"$this\""
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,11 @@ package ir.amirab.util.startup
|
||||
class HeadlessStartup(
|
||||
name: String,
|
||||
path: String,
|
||||
isJar: Boolean = false,
|
||||
args: List<String>,
|
||||
) : AbstractStartupManager(
|
||||
name = name,
|
||||
path = path,
|
||||
isJar = isJar
|
||||
args = args,
|
||||
) {
|
||||
@Throws(Exception::class)
|
||||
override fun install() {
|
||||
|
@ -7,9 +7,11 @@ import java.io.PrintWriter
|
||||
class MacOSStartup(
|
||||
name: String,
|
||||
path: String,
|
||||
isJar: Boolean,
|
||||
args: List<String>,
|
||||
) : AbstractStartupManager(
|
||||
path = path, name = name, isJar = isJar,
|
||||
path = path,
|
||||
name = name,
|
||||
args = args,
|
||||
) {
|
||||
private fun getFile(): File {
|
||||
if (!launchAgentsDir.exists()) {
|
||||
@ -28,11 +30,7 @@ class MacOSStartup(
|
||||
out.println("\t<string>" + super.name + "</string>")
|
||||
out.println("\t<key>ProgramArguments</key>")
|
||||
out.println("\t<array>")
|
||||
if (isJar) {
|
||||
out.println("\t\t<string>java</string>")
|
||||
out.println("\t\t<string>-jar</string>")
|
||||
}
|
||||
out.println("\t\t<string>" + super.path + "</string>")
|
||||
out.println("\t\t<string>" + getExecutableWithArgs() + "</string>")
|
||||
out.println("\t</array>")
|
||||
out.println("\t<key>RunAtLoad</key>")
|
||||
out.println("\t<true/>")
|
||||
|
@ -7,11 +7,14 @@ object Startup {
|
||||
* Add file to startup
|
||||
* @param name Name of key/file
|
||||
* @param path Path to file
|
||||
* @param jar If file should be executed by the JVM
|
||||
* @throws Exception
|
||||
*/
|
||||
@Throws(Exception::class)
|
||||
fun getStartUpManagerForDesktop(name: String, path: String?, jar: Boolean = false): AbstractStartupManager {
|
||||
fun getStartUpManagerForDesktop(
|
||||
name: String,
|
||||
path: String?,
|
||||
args: List<String>,
|
||||
): AbstractStartupManager {
|
||||
if (path==null){
|
||||
//there is no installation path provided so we use no-op
|
||||
return noImplStartUpManager()
|
||||
@ -20,18 +23,19 @@ object Startup {
|
||||
val startup=when (os) {
|
||||
Platform.Desktop.Linux -> {
|
||||
if (Utils.isHeadless) {
|
||||
HeadlessStartup(name, path, jar)
|
||||
HeadlessStartup(name, path, args)
|
||||
} else {
|
||||
UnixXDGStartup(name, path, jar)
|
||||
UnixXDGStartup(name, path, args)
|
||||
}
|
||||
}
|
||||
Platform.Desktop.MacOS -> MacOSStartup(name, path, jar)
|
||||
Platform.Desktop.Windows -> WindowsStartup(name, path, jar)
|
||||
|
||||
Platform.Desktop.MacOS -> MacOSStartup(name, path, args)
|
||||
Platform.Desktop.Windows -> WindowsStartup(name, path, args)
|
||||
Platform.Android -> error("this code should not be called in android")
|
||||
}
|
||||
return startup
|
||||
}
|
||||
private fun noImplStartUpManager(): HeadlessStartup {
|
||||
return HeadlessStartup("","",false)
|
||||
return HeadlessStartup("", "", emptyList())
|
||||
}
|
||||
}
|
||||
|
@ -5,38 +5,32 @@ import java.io.FileWriter
|
||||
import java.io.PrintWriter
|
||||
|
||||
class UnixXDGStartup(
|
||||
name:String,
|
||||
name: String,
|
||||
path: String,
|
||||
isJar:Boolean=false
|
||||
args: List<String>,
|
||||
) : AbstractStartupManager(
|
||||
name = name,
|
||||
path = path,
|
||||
isJar = isJar
|
||||
args = args,
|
||||
) {
|
||||
|
||||
private fun getAutoStartFile(): File {
|
||||
if (!autostartDir.exists()) {
|
||||
autostartDir.mkdirs()
|
||||
}
|
||||
return File(autostartDir, super.name + ".desktop")
|
||||
return File(autostartDir, this.name + ".desktop")
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
override fun install() {
|
||||
val out = PrintWriter(FileWriter(getAutoStartFile()))
|
||||
out.println("[Desktop Entry]")
|
||||
out.println("Type=Application")
|
||||
out.println("Name=" + super.name)
|
||||
if (isJar) {
|
||||
out.println("Exec=java -jar '" + super.path + "'")
|
||||
} else {
|
||||
out.println("Exec=" + super.path)
|
||||
}
|
||||
out.println("Name=" + this.name)
|
||||
out.println("Exec=" + getExecutableWithArgs())
|
||||
out.println("Terminal=false")
|
||||
out.println("NoDisplay=true")
|
||||
out.close()
|
||||
|
||||
val cmd = arrayOf("chmod", "+x", super.path)
|
||||
Runtime.getRuntime().exec(cmd)
|
||||
}
|
||||
|
||||
override fun uninstall() {
|
||||
|
@ -7,11 +7,6 @@ import java.io.File
|
||||
import java.io.InputStreamReader
|
||||
|
||||
object Utils {
|
||||
val jarFile: File
|
||||
get() = File(
|
||||
Utils::class.java.protectionDomain.codeSource.location.path.replace("%20", " ").replace("file:", "")
|
||||
)
|
||||
|
||||
@get:Throws(Exception::class)
|
||||
val isRoot: Boolean
|
||||
get() = Platform.getCurrentPlatform() !== Platform.Desktop.Windows && BufferedReader(
|
||||
|
@ -1,41 +1,37 @@
|
||||
package ir.amirab.util.startup
|
||||
|
||||
import com.sun.jna.platform.win32.Advapi32Util
|
||||
import com.sun.jna.platform.win32.WinReg
|
||||
|
||||
class WindowsStartup(
|
||||
name: String,
|
||||
path: String,
|
||||
isJar: Boolean = false,
|
||||
args: List<String>,
|
||||
) : AbstractStartupManager(
|
||||
name = name,
|
||||
path = path,
|
||||
isJar = isJar
|
||||
args = args
|
||||
) {
|
||||
|
||||
private fun getData(): String {
|
||||
|
||||
return if (isJar) {
|
||||
val javaHome = System.getProperty("java.home") + "\\bin\\javaw.exe"
|
||||
"$javaHome -jar \"$path\""
|
||||
} else {
|
||||
super.path
|
||||
}
|
||||
}
|
||||
@Throws(Exception::class)
|
||||
override fun install() {
|
||||
val data=getData()
|
||||
|
||||
Runtime.getRuntime().exec(
|
||||
arrayOf("reg", "add", "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\", "/v", super.name, "/t",
|
||||
"REG_SZ", "/d", data, "/f"
|
||||
)
|
||||
val data = getExecutableWithArgs()
|
||||
Advapi32Util.registrySetStringValue(
|
||||
WinReg.HKEY_CURRENT_USER,
|
||||
"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
|
||||
this.name,
|
||||
data
|
||||
)
|
||||
}
|
||||
|
||||
override fun uninstall() {
|
||||
Runtime.getRuntime().exec(
|
||||
arrayOf(
|
||||
"reg", "delete", "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\",
|
||||
"/v", super.name, "/f",
|
||||
)
|
||||
try {
|
||||
Advapi32Util.registryDeleteValue(
|
||||
WinReg.HKEY_CURRENT_USER,
|
||||
"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
|
||||
this.name,
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user