mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat: downloads resume where they stopped, and fall back to another source (#623)
This commit is contained in:
1 parent
5e57d78b5d
commit
7c90879b39
53 files changed
+784
-221
No files matched your search
@@ -20,6 +20,7 @@ class App : Application() {
|
||||
com.droiddeck.launcher.session.QuestExperimental.initialize(this)
|
||||
com.droiddeck.launcher.session.SessionPrefs.settleGpuDriverMode(this)
|
||||
com.droiddeck.launcher.update.AppUpdates.init(this)
|
||||
com.droiddeck.launcher.core.DownloadSource.init(this)
|
||||
GpuClockPin.clearLeftover(this)
|
||||
BwrapSpawner.start(this)
|
||||
com.droiddeck.launcher.frontend.GameFileSync.start(this)
|
||||
|
||||
@@ -47,6 +47,7 @@ import com.droiddeck.launcher.session.SessionLogShare
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
import com.droiddeck.launcher.session.SteamRepair
|
||||
import com.droiddeck.launcher.core.CpuCores
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import com.droiddeck.launcher.core.PhantomProcessLimit
|
||||
import com.droiddeck.launcher.core.PhantomProcessStatus
|
||||
import com.droiddeck.launcher.core.WirelessAdbFix
|
||||
@@ -155,6 +156,9 @@ class MainActivity : ComponentActivity() {
|
||||
private var stretch16x9 by mutableStateOf(false)
|
||||
private var orientation by mutableStateOf(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
|
||||
private var launcherFullscreen by mutableStateOf(true)
|
||||
private var downloadSource by mutableStateOf(DownloadSource.MODE_AUTO)
|
||||
private var downloadMirror by mutableStateOf("")
|
||||
private var downloadPrefix by mutableStateOf("")
|
||||
private var animationsEnabled by mutableStateOf(true)
|
||||
private var gameStoresEnabled by mutableStateOf(true)
|
||||
private var gameStoresSpeedTier by mutableStateOf("fast")
|
||||
@@ -515,6 +519,10 @@ class MainActivity : ComponentActivity() {
|
||||
orientation = SessionPrefs.orientation(this)
|
||||
requestedOrientation = orientation
|
||||
launcherFullscreen = SessionPrefs.launcherFullscreen(this)
|
||||
downloadSource = DownloadSource.mode(this)
|
||||
downloadMirror = DownloadSource.mirror(this)
|
||||
downloadPrefix = DownloadSource.custom(this)
|
||||
DownloadSource.probe()
|
||||
animationsEnabled = SessionPrefs.animationsEnabled(this)
|
||||
gameStoresEnabled = SessionPrefs.gameStoresEnabled(this)
|
||||
gameStoresSpeedTier = SessionPrefs.gameStoresSpeedTier(this)
|
||||
@@ -571,6 +579,9 @@ class MainActivity : ComponentActivity() {
|
||||
showPhantomGate = showPhantomGate,
|
||||
orientation = orientation,
|
||||
launcherFullscreen = launcherFullscreen,
|
||||
downloadSource = downloadSource,
|
||||
downloadMirror = downloadMirror,
|
||||
downloadPrefix = downloadPrefix,
|
||||
animationsEnabled = animationsEnabled,
|
||||
gameStoresEnabled = gameStoresEnabled,
|
||||
gameStoresSpeedTier = gameStoresSpeedTier,
|
||||
@@ -750,6 +761,18 @@ class MainActivity : ComponentActivity() {
|
||||
animationsEnabled = on
|
||||
com.droiddeck.launcher.ui.Motion.refresh(this)
|
||||
},
|
||||
onDownloadSource = { mode ->
|
||||
DownloadSource.setMode(this, mode)
|
||||
downloadSource = mode
|
||||
},
|
||||
onDownloadMirror = { host ->
|
||||
DownloadSource.setMirror(this, host)
|
||||
downloadMirror = host
|
||||
},
|
||||
onDownloadPrefix = { prefix ->
|
||||
DownloadSource.setCustom(this, prefix)
|
||||
downloadPrefix = DownloadSource.custom(this)
|
||||
},
|
||||
onHomeApp = { manageHomeApp() },
|
||||
onHomeScreen = { on ->
|
||||
HomeApp.setHomeScreenEnabled(this, on)
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
package com.droiddeck.launcher.core
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
object DownloadSource {
|
||||
const val MODE_AUTO = "auto"
|
||||
const val MODE_DIRECT = "direct"
|
||||
const val MODE_MIRROR = "mirror"
|
||||
const val MODE_CUSTOM = "custom"
|
||||
|
||||
private const val PREFS = "download-source"
|
||||
private const val KEY_MODE = "mode"
|
||||
private const val KEY_MIRROR = "mirror"
|
||||
private const val KEY_CUSTOM = "custom"
|
||||
private const val KEY_FASTEST = "fastest"
|
||||
|
||||
private const val PROBE = "https://raw.githubusercontent.com/Droid-Deck/DroidDeck/main/README.md"
|
||||
private const val PROBE_TIMEOUT_MS = 4_000
|
||||
private const val PROBE_COOLDOWN_MS = 30_000L
|
||||
private const val UA = "DroidDeck-Android"
|
||||
|
||||
private val MODES = listOf(MODE_AUTO, MODE_DIRECT, MODE_MIRROR, MODE_CUSTOM)
|
||||
|
||||
private val GITHUB_HOSTS = listOf(
|
||||
"github.com",
|
||||
"raw.githubusercontent.com",
|
||||
"api.github.com",
|
||||
)
|
||||
|
||||
private val CATALOGS = listOf(
|
||||
"https://raw.githubusercontent.com/Droid-Deck/DroidDeck-CI/",
|
||||
"https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/",
|
||||
)
|
||||
|
||||
data class Mirror(val host: String, val api: Boolean)
|
||||
|
||||
val MIRRORS = listOf(
|
||||
Mirror("gh-proxy.com", true),
|
||||
Mirror("gh-proxy.org", true),
|
||||
Mirror("ghproxy.net", false),
|
||||
Mirror("ghfast.top", false),
|
||||
)
|
||||
|
||||
@Volatile
|
||||
private var app: Context? = null
|
||||
|
||||
@Volatile
|
||||
private var lastGood: String? = null
|
||||
|
||||
private val probing = AtomicBoolean(false)
|
||||
private val probedAt = AtomicLong(0L)
|
||||
|
||||
fun init(context: Context) {
|
||||
app = context.applicationContext
|
||||
lastGood = prefs()?.getString(KEY_FASTEST, null)
|
||||
}
|
||||
|
||||
fun probe() {
|
||||
if (app == null || prefs()?.getString(KEY_MODE, MODE_AUTO) == MODE_DIRECT) return
|
||||
if (!probing.compareAndSet(false, true)) return
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - probedAt.get() < PROBE_COOLDOWN_MS) {
|
||||
probing.set(false)
|
||||
return
|
||||
}
|
||||
probedAt.set(now)
|
||||
Thread {
|
||||
try {
|
||||
val times = ConcurrentHashMap<String, Long>()
|
||||
val sources = listOf<String?>(null) + MIRRORS.map { it.host }
|
||||
val latch = CountDownLatch(sources.size)
|
||||
for (mirror in sources) {
|
||||
Thread {
|
||||
try {
|
||||
val ms = latency(mirror?.let { "https://$it/$PROBE" } ?: PROBE)
|
||||
if (ms > 0) times[mirror ?: ""] = ms
|
||||
} finally {
|
||||
latch.countDown()
|
||||
}
|
||||
}.apply { isDaemon = true }.start()
|
||||
}
|
||||
latch.await(PROBE_TIMEOUT_MS * 3L, TimeUnit.MILLISECONDS)
|
||||
val best = times.minByOrNull { it.value }?.key ?: return@Thread
|
||||
lastGood = best
|
||||
prefs()?.edit()?.putString(KEY_FASTEST, best)?.apply()
|
||||
} finally {
|
||||
probing.set(false)
|
||||
}
|
||||
}.apply { isDaemon = true }.start()
|
||||
}
|
||||
|
||||
private fun latency(url: String): Long {
|
||||
val started = System.nanoTime()
|
||||
var connection: HttpURLConnection? = null
|
||||
return try {
|
||||
val c = (URL(url).openConnection() as HttpURLConnection).apply {
|
||||
connectTimeout = PROBE_TIMEOUT_MS
|
||||
readTimeout = PROBE_TIMEOUT_MS
|
||||
instanceFollowRedirects = true
|
||||
setRequestProperty("Range", "bytes=0-1023")
|
||||
setRequestProperty("User-Agent", UA)
|
||||
}
|
||||
connection = c
|
||||
if (c.responseCode / 100 != 2) 0L else {
|
||||
c.inputStream.use { it.read() }
|
||||
(System.nanoTime() - started) / 1_000_000
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
0L
|
||||
} finally {
|
||||
runCatching { connection?.disconnect() }
|
||||
}
|
||||
}
|
||||
|
||||
fun isGithub(url: String): Boolean = GITHUB_HOSTS.any { url.startsWith("https://$it/") }
|
||||
|
||||
/** Catalogs carry the digests everything else is checked against, so a mirror must never supply one. */
|
||||
fun isCatalog(url: String): Boolean = CATALOGS.any { url.startsWith(it) }
|
||||
|
||||
fun candidates(target: String): List<String> {
|
||||
// GitHub API metadata carries executable URLs and digests; never proxy it.
|
||||
if (isCatalog(target) || target.startsWith("https://api.github.com/")) return listOf(target)
|
||||
val prefs = prefs() ?: return listOf(target)
|
||||
if (!isGithub(target)) return listOf(target)
|
||||
val api = target.startsWith("https://api.github.com/")
|
||||
val proxies = MIRRORS.filter { !api || it.api }.map { it.proxy(target) }
|
||||
val ordered = when (prefs.getString(KEY_MODE, MODE_AUTO) ?: MODE_AUTO) {
|
||||
MODE_DIRECT -> listOf(target)
|
||||
MODE_MIRROR -> listOfNotNull(picked(prefs, api)?.proxy(target)) + listOf(target) + quickest(proxies)
|
||||
MODE_CUSTOM -> listOfNotNull(customProxy(prefs, target)) + listOf(target) + quickest(proxies)
|
||||
else -> if (api) listOf(target) else listOf(target) + quickest(proxies)
|
||||
}
|
||||
return ordered.distinct()
|
||||
}
|
||||
|
||||
private fun quickest(sources: List<String>): List<String> =
|
||||
sources.sortedBy { if (host(it) == lastGood) 0 else 1 }
|
||||
|
||||
fun succeeded(source: String) {
|
||||
lastGood = host(source)
|
||||
}
|
||||
|
||||
fun failed() {
|
||||
lastGood = null
|
||||
prefs()?.edit()?.remove(KEY_FASTEST)?.apply()
|
||||
}
|
||||
|
||||
fun mode(context: Context): String = prefs(context)?.getString(KEY_MODE, MODE_AUTO) ?: MODE_AUTO
|
||||
|
||||
fun setMode(context: Context, mode: String) {
|
||||
prefs(context)?.edit()?.putString(KEY_MODE, mode.takeIf { it in MODES } ?: MODE_AUTO)?.apply()
|
||||
}
|
||||
|
||||
fun mirror(context: Context): String {
|
||||
val host = prefs(context)?.getString(KEY_MIRROR, "") ?: ""
|
||||
return MIRRORS.firstOrNull { it.host == host }?.host ?: MIRRORS.first().host
|
||||
}
|
||||
|
||||
fun setMirror(context: Context, host: String) {
|
||||
prefs(context)?.edit()?.putString(KEY_MIRROR, host.takeIf { h -> MIRRORS.any { it.host == h } } ?: "")?.apply()
|
||||
}
|
||||
|
||||
fun custom(context: Context): String = prefs(context)?.getString(KEY_CUSTOM, "") ?: ""
|
||||
|
||||
fun setCustom(context: Context, prefix: String) {
|
||||
prefs(context)?.edit()?.putString(KEY_CUSTOM, normalizePrefix(prefix))?.apply()
|
||||
}
|
||||
|
||||
fun reset(context: Context) {
|
||||
lastGood = null
|
||||
prefs(context)?.edit()?.clear()?.apply()
|
||||
}
|
||||
|
||||
private fun prefs(): SharedPreferences? = app?.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
|
||||
private fun prefs(context: Context): SharedPreferences? =
|
||||
context.applicationContext.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
|
||||
private fun Mirror.proxy(target: String): String = "https://$host/$target"
|
||||
|
||||
private fun host(url: String): String =
|
||||
MIRRORS.firstOrNull { url.startsWith("https://${it.host}/") }?.host ?: ""
|
||||
|
||||
private fun picked(prefs: SharedPreferences, api: Boolean): Mirror? {
|
||||
val chosen = MIRRORS.firstOrNull { it.host == (prefs.getString(KEY_MIRROR, "") ?: "") } ?: MIRRORS.firstOrNull()
|
||||
return if (api && chosen != null && !chosen.api) MIRRORS.firstOrNull { it.api } else chosen
|
||||
}
|
||||
|
||||
private fun custom(prefs: SharedPreferences): String = prefs.getString(KEY_CUSTOM, "") ?: ""
|
||||
|
||||
private fun customProxy(prefs: SharedPreferences, target: String): String? =
|
||||
normalizePrefix(custom(prefs)).takeIf { it.isNotEmpty() }?.let { "$it/$target" }
|
||||
|
||||
private fun normalizePrefix(value: String): String {
|
||||
val trimmed = value.trim().trimEnd('/')
|
||||
if (trimmed.isEmpty() || trimmed.startsWith("http://")) return ""
|
||||
return if (trimmed.startsWith("https://")) trimmed else "https://$trimmed"
|
||||
}
|
||||
}
|
||||
@@ -6,69 +6,201 @@ import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public final class Downloader {
|
||||
private static final String TAG = "Downloader";
|
||||
private static final int TIMEOUT_MS = 30_000;
|
||||
private static final int CONNECT_TIMEOUT_MS = 10_000;
|
||||
private static final int STALL_TIMEOUT_MS = 15_000;
|
||||
private static final int HTTP_RANGE_NOT_SATISFIABLE = 416;
|
||||
|
||||
private Downloader() {}
|
||||
|
||||
public static String downloadString(String url) {
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = open(url);
|
||||
if (connection.getResponseCode() / 100 != 2) {
|
||||
Log.w(TAG, url + " -> HTTP " + connection.getResponseCode());
|
||||
return null;
|
||||
List<String> sources = DownloadSource.INSTANCE.candidates(url);
|
||||
for (int i = 0; i < sources.size(); i++) {
|
||||
if (i > 0) Log.i(TAG, "retrying " + url + " via " + sources.get(i));
|
||||
Fetch fetch = fetch(sources.get(i));
|
||||
if (fetch.body != null) {
|
||||
DownloadSource.INSTANCE.succeeded(sources.get(i));
|
||||
return fetch.body;
|
||||
}
|
||||
try (InputStream in = connection.getInputStream();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
FileUtils.copy(in, out);
|
||||
return new String(out.toByteArray(), StandardCharsets.UTF_8);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "GET " + url, e);
|
||||
return null;
|
||||
} finally {
|
||||
if (connection != null) connection.disconnect();
|
||||
if (fetch.fatal && sources.get(i).equals(url)) return null;
|
||||
}
|
||||
DownloadSource.INSTANCE.failed();
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Fetches without the mirror list, for a URL whose body is a digest the payload is checked against. */
|
||||
public static String downloadDirect(String url) {
|
||||
return fetch(url).body;
|
||||
}
|
||||
|
||||
public static boolean downloadFile(String url, File destination, boolean resume,
|
||||
java.util.function.Consumer<Float> progress) {
|
||||
return downloadFile(url, destination, resume, null, progress);
|
||||
}
|
||||
|
||||
/** Verify each candidate before accepting it, so a bad mirror can fall back. */
|
||||
public static boolean downloadFile(String url, File destination, boolean resume, String expectedSha256,
|
||||
java.util.function.Consumer<Float> progress) {
|
||||
if (expectedSha256 != null && !expectedSha256.matches("(?i)[0-9a-f]{64}"))
|
||||
throw new IllegalArgumentException("Expected SHA-256 must be 64 hex characters");
|
||||
return downloadCandidates(url, DownloadSource.INSTANCE.candidates(url), destination, resume, expectedSha256, progress);
|
||||
}
|
||||
|
||||
// Package-private for the local multi-source HTTP test.
|
||||
static boolean downloadCandidates(String original, List<String> sources, File destination, boolean resume,
|
||||
String expectedSha256, java.util.function.Consumer<Float> progress) {
|
||||
Identity identity = loadIdentity(destination, resume, expectedSha256);
|
||||
for (int i = 0; i < sources.size(); i++) {
|
||||
String source = sources.get(i);
|
||||
if (i > 0) Log.i(TAG, "retrying " + original + " via " + source);
|
||||
Outcome outcome = attempt(source, destination, resume, identity, expectedSha256, progress);
|
||||
if (outcome == Outcome.DONE) {
|
||||
if (expectedSha256 != null && !expectedSha256.equalsIgnoreCase(Hashes.sha256(destination))) {
|
||||
Log.w(TAG, "checksum mismatch from " + source + "; trying another source");
|
||||
if (!discardPartial(destination, identity)) return false;
|
||||
continue;
|
||||
}
|
||||
resumeFile(destination).delete();
|
||||
DownloadSource.INSTANCE.succeeded(source);
|
||||
if (progress != null) progress.accept(1f);
|
||||
return true;
|
||||
}
|
||||
if (outcome == Outcome.FATAL && source.equals(original)) return false;
|
||||
}
|
||||
DownloadSource.INSTANCE.failed();
|
||||
return false;
|
||||
}
|
||||
|
||||
private enum Outcome { DONE, RETRY, FATAL }
|
||||
|
||||
private static final class Identity {
|
||||
private String source;
|
||||
private String etag;
|
||||
}
|
||||
|
||||
private static File resumeFile(File dest) {
|
||||
return new File(dest.getPath() + ".resume.properties");
|
||||
}
|
||||
|
||||
private static boolean strongETag(String value) {
|
||||
return value != null && !value.isEmpty() && !value.startsWith("W/");
|
||||
}
|
||||
|
||||
/** Only reuse saved bytes when we persisted a strong validator and their source. */
|
||||
private static Identity loadIdentity(File dest, boolean resume, String expectedSha256) {
|
||||
Identity identity = new Identity();
|
||||
File marker = resumeFile(dest);
|
||||
if (!resume || !dest.isFile() || dest.length() == 0) {
|
||||
marker.delete();
|
||||
return identity;
|
||||
}
|
||||
try (FileInputStream in = new FileInputStream(marker)) {
|
||||
Properties p = new Properties();
|
||||
p.load(in);
|
||||
String source = p.getProperty("source");
|
||||
String etag = p.getProperty("etag");
|
||||
String expected = p.getProperty("sha256", "");
|
||||
if (source != null && source.startsWith("http") && strongETag(etag)
|
||||
&& expected.equals(expectedSha256 == null ? "" : expectedSha256.toLowerCase(java.util.Locale.ROOT))) {
|
||||
identity.source = source;
|
||||
identity.etag = etag;
|
||||
return identity;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Legacy partials or interrupted metadata writes have no trustworthy identity.
|
||||
}
|
||||
if (!discardPartial(dest, identity)) Log.w(TAG, "could not discard unidentified partial " + dest);
|
||||
return identity;
|
||||
}
|
||||
|
||||
private static boolean discardPartial(File dest, Identity identity) {
|
||||
identity.source = null;
|
||||
identity.etag = null;
|
||||
File marker = resumeFile(dest);
|
||||
return (!dest.exists() || dest.delete()) && (!marker.exists() || marker.delete());
|
||||
}
|
||||
|
||||
private static void saveIdentity(File dest, Identity identity, String expectedSha256) {
|
||||
File marker = resumeFile(dest);
|
||||
if (!strongETag(identity.etag)) {
|
||||
marker.delete();
|
||||
return;
|
||||
}
|
||||
Properties p = new Properties();
|
||||
p.setProperty("source", identity.source);
|
||||
p.setProperty("etag", identity.etag);
|
||||
p.setProperty("sha256", expectedSha256 == null ? "" : expectedSha256.toLowerCase(java.util.Locale.ROOT));
|
||||
try (FileOutputStream out = new FileOutputStream(marker)) {
|
||||
p.store(out, "DroidDeck partial download validator");
|
||||
} catch (IOException e) {
|
||||
marker.delete(); // No valid metadata means no future resume.
|
||||
Log.w(TAG, "cannot persist download validator", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Outcome attempt(String url, File destination, boolean resume, Identity identity,
|
||||
String expectedSha256, java.util.function.Consumer<Float> progress) {
|
||||
HttpURLConnection connection = null;
|
||||
long have = resume && destination.isFile() ? destination.length() : 0;
|
||||
try {
|
||||
connection = open(url);
|
||||
if (have > 0) connection.setRequestProperty("Range", "bytes=" + have + "-");
|
||||
if (have > 0 && (!url.equals(identity.source) || !strongETag(identity.etag))) {
|
||||
// A new source or unvalidated bytes must restart from byte zero.
|
||||
if (!discardPartial(destination, identity)) return Outcome.RETRY;
|
||||
have = 0;
|
||||
}
|
||||
connection = open(url, STALL_TIMEOUT_MS);
|
||||
if (have > 0) {
|
||||
connection.setRequestProperty("Range", "bytes=" + have + "-");
|
||||
connection.setRequestProperty("If-Range", identity.etag);
|
||||
}
|
||||
int code = connection.getResponseCode();
|
||||
boolean appending = code == HttpURLConnection.HTTP_PARTIAL;
|
||||
if (appending && !rangeStartsAt(connection.getHeaderField("Content-Range"), have)) {
|
||||
// A 206 that does not begin where the kept bytes end would corrupt the file if appended.
|
||||
if (have > 0 && code == HTTP_RANGE_NOT_SATISFIABLE) {
|
||||
connection.disconnect();
|
||||
destination.delete();
|
||||
return downloadFile(url, destination, false, progress);
|
||||
if (!discardPartial(destination, identity)) return Outcome.RETRY;
|
||||
return attempt(url, destination, false, identity, expectedSha256, progress);
|
||||
}
|
||||
if (code / 100 != 2) {
|
||||
if (have > 0) {
|
||||
connection.disconnect();
|
||||
destination.delete();
|
||||
return downloadFile(url, destination, false, progress);
|
||||
}
|
||||
Log.w(TAG, url + " -> HTTP " + code);
|
||||
return false;
|
||||
return code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE
|
||||
? Outcome.FATAL : Outcome.RETRY;
|
||||
}
|
||||
boolean appending = code == HttpURLConnection.HTTP_PARTIAL;
|
||||
if (appending && !rangeStartsAt(connection.getHeaderField("Content-Range"), have)) {
|
||||
connection.disconnect();
|
||||
if (!discardPartial(destination, identity)) return Outcome.RETRY;
|
||||
return attempt(url, destination, false, identity, expectedSha256, progress);
|
||||
}
|
||||
String etag = connection.getHeaderField("ETag");
|
||||
if (have > 0 && appending && (!strongETag(etag) || !etag.equals(identity.etag))) {
|
||||
connection.disconnect();
|
||||
if (!discardPartial(destination, identity)) return Outcome.RETRY;
|
||||
return attempt(url, destination, false, identity, expectedSha256, progress);
|
||||
}
|
||||
if (!appending && have > 0) {
|
||||
// If-Range may legitimately return full 200 when representation changed.
|
||||
if (!discardPartial(destination, identity)) return Outcome.RETRY;
|
||||
have = 0;
|
||||
}
|
||||
if (!appending) have = 0;
|
||||
long length = connection.getContentLengthLong();
|
||||
long total = length < 0 ? -1 : length + have;
|
||||
long total = appending ? rangeTotal(connection.getHeaderField("Content-Range")) : -1;
|
||||
if (total < 0) total = length < 0 ? -1 : length + have;
|
||||
File parent = destination.getParentFile();
|
||||
if (parent != null && !parent.isDirectory())
|
||||
parent.mkdirs();
|
||||
if (parent != null && !parent.isDirectory()) parent.mkdirs();
|
||||
identity.source = url;
|
||||
identity.etag = strongETag(etag) ? etag : null;
|
||||
saveIdentity(destination, identity, expectedSha256);
|
||||
long written = have;
|
||||
try (InputStream in = new BufferedInputStream(connection.getInputStream(), 1 << 16);
|
||||
OutputStream out = new FileOutputStream(destination, appending)) {
|
||||
@@ -83,11 +215,11 @@ public final class Downloader {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (progress != null) progress.accept(1f);
|
||||
return total <= 0 || written >= total;
|
||||
if (total > 0 && written < total) return Outcome.RETRY;
|
||||
return Outcome.DONE;
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "download " + url, e);
|
||||
return false;
|
||||
return Outcome.RETRY;
|
||||
} finally {
|
||||
if (connection != null) connection.disconnect();
|
||||
}
|
||||
@@ -100,10 +232,49 @@ public final class Downloader {
|
||||
return m.matches() && Long.parseLong(m.group(1)) == start;
|
||||
}
|
||||
|
||||
private static HttpURLConnection open(String url) throws java.io.IOException {
|
||||
/** The complete length in a `Content-Range` header, or -1 when the header leaves it unknown. */
|
||||
static long rangeTotal(String contentRange) {
|
||||
if (contentRange == null) return -1;
|
||||
java.util.regex.Matcher m = java.util.regex.Pattern.compile("^\\s*bytes\\s+\\d+-\\d+/(\\d+)\\s*$").matcher(contentRange);
|
||||
return m.matches() ? Long.parseLong(m.group(1)) : -1;
|
||||
}
|
||||
|
||||
private static final class Fetch {
|
||||
final String body;
|
||||
final boolean fatal;
|
||||
|
||||
Fetch(String body, boolean fatal) {
|
||||
this.body = body;
|
||||
this.fatal = fatal;
|
||||
}
|
||||
}
|
||||
|
||||
private static Fetch fetch(String url) {
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = open(url, STALL_TIMEOUT_MS);
|
||||
int code = connection.getResponseCode();
|
||||
if (code / 100 != 2) {
|
||||
Log.w(TAG, url + " -> HTTP " + code);
|
||||
return new Fetch(null, code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE);
|
||||
}
|
||||
try (InputStream in = connection.getInputStream();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
FileUtils.copy(in, out);
|
||||
return new Fetch(new String(out.toByteArray(), StandardCharsets.UTF_8), false);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "GET " + url, e);
|
||||
return new Fetch(null, false);
|
||||
} finally {
|
||||
if (connection != null) connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
private static HttpURLConnection open(String url, int readTimeoutMs) throws java.io.IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setConnectTimeout(TIMEOUT_MS);
|
||||
connection.setReadTimeout(TIMEOUT_MS);
|
||||
connection.setConnectTimeout(CONNECT_TIMEOUT_MS);
|
||||
connection.setReadTimeout(readTimeoutMs);
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
connection.setRequestProperty("User-Agent", "DroidDeck-Android");
|
||||
return connection;
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.frontend
|
||||
import android.content.Context
|
||||
import android.graphics.BitmapFactory
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
@@ -199,24 +200,62 @@ object CoverArt {
|
||||
setRequestProperty("User-Agent", "DroidDeck")
|
||||
}
|
||||
|
||||
private fun exists(url: String): Boolean = try {
|
||||
val c = open(url, "HEAD"); val ok = c.responseCode == 200; c.disconnect(); ok
|
||||
} catch (e: Exception) { false }
|
||||
private fun <T> through(url: String, attempt: (String, Boolean) -> T?): T? {
|
||||
for (source in DownloadSource.candidates(url)) {
|
||||
val result = attempt(source, source == url)
|
||||
if (result != null) {
|
||||
DownloadSource.succeeded(source)
|
||||
return result
|
||||
}
|
||||
}
|
||||
DownloadSource.failed()
|
||||
return null
|
||||
}
|
||||
|
||||
private fun get(url: String): String? = try {
|
||||
private fun exists(url: String): Boolean = through(url) { source, original -> head(source, original) } ?: false
|
||||
|
||||
private fun head(url: String, original: Boolean): Boolean? = try {
|
||||
val c = open(url, "HEAD")
|
||||
val code = c.responseCode
|
||||
c.disconnect()
|
||||
when {
|
||||
code == 200 -> true
|
||||
code == 404 || code == 410 -> if (original) false else null
|
||||
else -> null
|
||||
}
|
||||
} catch (e: Exception) { null }
|
||||
|
||||
private fun get(url: String): String? = through(url) { source, original -> text(source, original) }
|
||||
|
||||
private fun text(url: String, original: Boolean): String? = try {
|
||||
val c = open(url)
|
||||
if (c.responseCode != 200) { c.disconnect(); null } else c.inputStream.bufferedReader().use { it.readText() }
|
||||
val code = c.responseCode
|
||||
when {
|
||||
code == 200 -> c.inputStream.bufferedReader().use { it.readText() }.takeIf { it.isNotEmpty() }
|
||||
code == 404 || code == 410 -> { c.disconnect(); if (original) "" else null }
|
||||
else -> { c.disconnect(); null }
|
||||
}
|
||||
} catch (e: Exception) { Log.w(TAG, "$url: $e"); null }
|
||||
|
||||
private fun download(url: String, target: File): Boolean = try {
|
||||
private fun download(url: String, target: File): Boolean = through(url) { source, original -> fetch(source, target, original) } ?: false
|
||||
|
||||
private fun fetch(url: String, target: File, original: Boolean): Boolean? = try {
|
||||
val c = open(url)
|
||||
if (c.responseCode != 200) { c.disconnect(); false } else {
|
||||
target.parentFile?.mkdirs()
|
||||
val tmp = File(target.path + ".part")
|
||||
c.inputStream.use { input -> tmp.outputStream().use { input.copyTo(it) } }
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeFile(tmp.path, bounds)
|
||||
if (bounds.outWidth > 0 && tmp.renameTo(target)) true else { tmp.delete(); false }
|
||||
when (val code = c.responseCode) {
|
||||
200 -> {
|
||||
target.parentFile?.mkdirs()
|
||||
val tmp = File(target.path + ".part")
|
||||
c.inputStream.use { input -> tmp.outputStream().use { input.copyTo(it) } }
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeFile(tmp.path, bounds)
|
||||
when {
|
||||
bounds.outWidth > 0 && tmp.renameTo(target) -> true
|
||||
original -> { tmp.delete(); false }
|
||||
else -> { tmp.delete(); null }
|
||||
}
|
||||
}
|
||||
404, 410 -> { c.disconnect(); if (original) false else null }
|
||||
else -> { c.disconnect(); null }
|
||||
}
|
||||
} catch (e: Exception) { Log.w(TAG, "$url: $e"); false }
|
||||
} catch (e: Exception) { Log.w(TAG, "$url: $e"); null }
|
||||
}
|
||||
@@ -1,18 +1,21 @@
|
||||
package com.droiddeck.launcher.gpu
|
||||
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import com.droiddeck.launcher.core.Downloader
|
||||
import com.droiddeck.launcher.core.Hashes
|
||||
import com.droiddeck.launcher.R
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.core.FileUtils
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
object TurnipReleases {
|
||||
private const val TAG = "TurnipReleases"
|
||||
private const val PREFS = "turnip_releases"
|
||||
private const val KEY_RELEASE = "latest"
|
||||
private const val KEY_DOWNLOADS = "downloads"
|
||||
@@ -104,7 +107,24 @@ object TurnipReleases {
|
||||
}
|
||||
|
||||
private fun get(context: Context, url: String): String {
|
||||
val c = URL(url).openConnection() as HttpURLConnection
|
||||
val sources = DownloadSource.candidates(url)
|
||||
var last: IOException? = null
|
||||
for ((index, source) in sources.withIndex()) {
|
||||
try {
|
||||
val body = get(context, source, url)
|
||||
DownloadSource.succeeded(source)
|
||||
return body
|
||||
} catch (e: IOException) {
|
||||
last = e
|
||||
if (index < sources.lastIndex) Log.i(TAG, "retrying $url via $source")
|
||||
}
|
||||
}
|
||||
DownloadSource.failed()
|
||||
throw last ?: IOException(context.getString(R.string.turnip_github_http, 0))
|
||||
}
|
||||
|
||||
private fun get(context: Context, source: String, url: String): String {
|
||||
val c = URL(source).openConnection() as HttpURLConnection
|
||||
c.connectTimeout = 15_000
|
||||
c.readTimeout = 20_000
|
||||
c.setRequestProperty("Accept", "application/vnd.github+json")
|
||||
@@ -160,39 +180,13 @@ object TurnipReleases {
|
||||
fun download(context: Context, asset: Asset, progress: (Int) -> Unit): File {
|
||||
if (asset.sha256.isEmpty()) throw IOException(context.getString(R.string.turnip_list_predates_checksums))
|
||||
val target = File(context.cacheDir, asset.name)
|
||||
val c = URL(asset.url).openConnection() as HttpURLConnection
|
||||
c.connectTimeout = 15_000
|
||||
c.readTimeout = 60_000
|
||||
c.setRequestProperty("User-Agent", "DroidDeck-app")
|
||||
c.instanceFollowRedirects = true
|
||||
try {
|
||||
if (c.responseCode != 200) throw IOException(context.getString(R.string.turnip_download_http, c.responseCode))
|
||||
val total = c.contentLengthLong.takeIf { it > 0 } ?: asset.size
|
||||
c.inputStream.use { input ->
|
||||
FileOutputStream(target).use { out ->
|
||||
val buf = ByteArray(1 shl 16)
|
||||
var done = 0L
|
||||
var last = -1
|
||||
while (true) {
|
||||
val r = input.read(buf)
|
||||
if (r <= 0) break
|
||||
out.write(buf, 0, r)
|
||||
done += r
|
||||
val pct = if (total > 0) (done * 100 / total).toInt() else 0
|
||||
if (pct != last) { last = pct; progress(pct) }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!asset.sha256.equals(Hashes.sha256(target), ignoreCase = true)) {
|
||||
throw IOException(context.getString(R.string.turnip_checksum_mismatch))
|
||||
}
|
||||
return target
|
||||
} catch (e: Exception) {
|
||||
val ok = Downloader.downloadFile(asset.url, target, true, asset.sha256) { f -> progress(if (f < 0) -1 else (f * 100).toInt()) }
|
||||
if (!ok) throw IOException(context.getString(R.string.turnip_download_failed))
|
||||
if (!asset.sha256.equals(Hashes.sha256(target), ignoreCase = true)) {
|
||||
FileUtils.delete(target)
|
||||
throw e
|
||||
} finally {
|
||||
c.disconnect()
|
||||
throw IOException(context.getString(R.string.turnip_checksum_mismatch))
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
private fun downloads(context: Context): JSONObject =
|
||||
|
||||
@@ -108,11 +108,11 @@ object DeckyManager {
|
||||
target.parentFile?.mkdirs()
|
||||
val temp = File(target.parentFile, "PluginLoader.download")
|
||||
val downloading = context.getString(R.string.user_apps_downloading, release.tag)
|
||||
val ok = Downloader.downloadFile(release.url, temp, false) { f -> progress(downloading, if (f < 0) -1 else (f * 100).toInt().coerceIn(0, 100)) }
|
||||
val ok = Downloader.downloadFile(release.url, temp, true, release.digest?.substringAfter(':')) { f -> progress(downloading, if (f < 0) -1 else (f * 100).toInt().coerceIn(0, 100)) }
|
||||
if (!ok) { temp.delete(); return context.getString(R.string.deckymgr_download_failed) }
|
||||
if (temp.length() != release.size) { temp.delete(); return context.getString(R.string.deckymgr_size_mismatch) }
|
||||
val expected = release.digest?.substringAfter(':') ?: release.shaUrl?.let { checksumUrl ->
|
||||
val checksumBody = Downloader.downloadString(checksumUrl) ?: run { temp.delete(); return context.getString(R.string.deckymgr_checksum_download_failed) }
|
||||
val checksumBody = Downloader.downloadDirect(checksumUrl) ?: run { temp.delete(); return context.getString(R.string.deckymgr_checksum_download_failed) }
|
||||
Regex("(?i)\\b[0-9a-f]{64}\\b").find(checksumBody)?.value
|
||||
} ?: run { temp.delete(); return context.getString(R.string.deckymgr_checksum_missing) }
|
||||
if (!expected.matches(Regex("(?i)[0-9a-f]{64}"))) { temp.delete(); return context.getString(R.string.deckymgr_checksum_invalid) }
|
||||
@@ -236,7 +236,7 @@ object DeckyManager {
|
||||
downloadingBinary = true
|
||||
progress(downloading, -1)
|
||||
val downloaded = try {
|
||||
Downloader.downloadFile(url, temp, false) { fraction ->
|
||||
Downloader.downloadFile(url, temp, true, hash) { fraction ->
|
||||
progress(downloading, if (fraction < 0) -1 else (fraction * 100).toInt().coerceIn(0, 100))
|
||||
}
|
||||
} finally {
|
||||
|
||||
@@ -90,7 +90,7 @@ object DesktopCatalog {
|
||||
try {
|
||||
val downloading = context.getString(R.string.user_apps_downloading, entry.name)
|
||||
listener?.onProgress(LinuxRuntimeInstaller.Step.DOWNLOADING, downloading, 0)
|
||||
val ok = Downloader.downloadFile(entry.url, download, true) { f ->
|
||||
val ok = Downloader.downloadFile(entry.url, download, true, entry.sha256) { f ->
|
||||
listener?.onProgress(LinuxRuntimeInstaller.Step.DOWNLOADING, downloading, if (f < 0) -1 else Math.round(f * 100f))
|
||||
}
|
||||
if (!ok) return context.getString(R.string.user_apps_download_failed)
|
||||
|
||||
@@ -261,7 +261,7 @@ public final class LinuxRuntimeInstaller {
|
||||
emptyTrash(LinuxRuntime.rootDir(context).getParentFile());
|
||||
String downloading = context.getString(R.string.rtinst_downloading);
|
||||
if (listener != null) listener.onProgress(Step.DOWNLOADING, downloading, 0);
|
||||
boolean ok = Downloader.downloadFile(release.url, archive, true, (fraction) -> {
|
||||
boolean ok = Downloader.downloadFile(release.url, archive, true, release.sha256, (fraction) -> {
|
||||
if (listener != null) {
|
||||
listener.onProgress(Step.DOWNLOADING, downloading,
|
||||
fraction < 0 ? -1 : Math.round(fraction * 100f));
|
||||
|
||||
@@ -929,8 +929,7 @@ object ComponentsManager {
|
||||
require(Hashes.isGithubSha256(item.digest)) { context.getString(R.string.cmgr_list_predates_checksums) }
|
||||
val dest = File(packagesDir(context), safeName(item.file))
|
||||
val part = File(dest.parentFile, dest.name + ".part")
|
||||
part.delete()
|
||||
check(Downloader.downloadFile(item.url, part, false) { f -> progress(if (f < 0) -1 else (f * 100).toInt().coerceIn(0, 100)) }) { context.getString(R.string.user_apps_download_failed) }
|
||||
check(Downloader.downloadFile(item.url, part, true, item.digest.substringAfter(':')) { f -> progress(if (f < 0) -1 else (f * 100).toInt().coerceIn(0, 100)) }) { context.getString(R.string.user_apps_download_failed) }
|
||||
if (!Hashes.sha256(part).equals(item.digest.substringAfter(':'), true)) { part.delete(); error(context.getString(R.string.cmgr_checksum_mismatch)) }
|
||||
val info = runCatching { packageInfo(context, part) }.getOrElse { part.delete(); throw it }
|
||||
synchronized(lock) { part.renameTo(dest) }
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.session
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import com.droiddeck.launcher.core.FileUtils
|
||||
import com.droiddeck.launcher.core.Hashes
|
||||
import com.github.luben.zstd.ZstdInputStream
|
||||
@@ -620,16 +621,27 @@ object EsyncPacks {
|
||||
val label = context.getString(com.droiddeck.launcher.R.string.esync_fetching)
|
||||
onProgress?.invoke(label, -1)
|
||||
var reported = -1
|
||||
val fetched = FileOutputStream(part).use { output ->
|
||||
fetch(entry.asset.url, entry.asset.size, output) { done ->
|
||||
val percent = (done * 100 / entry.asset.size).toInt().coerceIn(0, 100)
|
||||
if (percent != reported) {
|
||||
reported = percent
|
||||
onProgress?.invoke(label, percent)
|
||||
val sources = DownloadSource.candidates(entry.asset.url)
|
||||
var fetched = false
|
||||
for ((index, source) in sources.withIndex()) {
|
||||
if (index > 0) Log.i(TAG, "retrying ${entry.asset.url} via $source")
|
||||
reported = -1
|
||||
fetched = FileOutputStream(part).use { output ->
|
||||
fetch(source, entry.asset.size, output) { done ->
|
||||
val percent = (done * 100 / entry.asset.size).toInt().coerceIn(0, 100)
|
||||
if (percent != reported) {
|
||||
reported = percent
|
||||
onProgress?.invoke(label, percent)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fetched) {
|
||||
DownloadSource.succeeded(source)
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!fetched) {
|
||||
DownloadSource.failed()
|
||||
SessionEvents.record("sync.pack", mapOf("id" to entry.id, "source" to "download", "result" to "failed", "reason" to "download"))
|
||||
return false
|
||||
}
|
||||
@@ -836,8 +848,17 @@ object EsyncPacks {
|
||||
}
|
||||
|
||||
private fun fetchBytes(url: String, limit: Long): ByteArray? {
|
||||
val out = ByteArrayOutputStream()
|
||||
return if (fetch(url, limit, out, null)) out.toByteArray() else null
|
||||
val sources = DownloadSource.candidates(url)
|
||||
for ((index, source) in sources.withIndex()) {
|
||||
if (index > 0) Log.i(TAG, "retrying $url via $source")
|
||||
val out = ByteArrayOutputStream()
|
||||
if (fetch(source, limit, out, null)) {
|
||||
DownloadSource.succeeded(source)
|
||||
return out.toByteArray()
|
||||
}
|
||||
}
|
||||
DownloadSource.failed()
|
||||
return null
|
||||
}
|
||||
|
||||
private fun fetch(url: String, limit: Long, out: OutputStream, progress: ((Long) -> Unit)?): Boolean =
|
||||
|
||||
@@ -167,7 +167,7 @@ object ProtonExtras {
|
||||
onProgress(downloading, 0)
|
||||
var downloaded = false
|
||||
for (attempt in 0 until 5) {
|
||||
downloaded = Downloader.downloadFile(asset.url, archive, true) { fraction ->
|
||||
downloaded = Downloader.downloadFile(asset.url, archive, true, asset.sha256) { fraction ->
|
||||
onProgress(downloading, if (fraction < 0) -1 else (fraction * 100f).toInt().coerceIn(0, 100))
|
||||
}
|
||||
if (downloaded) break
|
||||
@@ -192,7 +192,7 @@ object ProtonExtras {
|
||||
val verified = when {
|
||||
asset.sha256 != null -> asset.sha256.equals(Hashes.sha256(archive), ignoreCase = true)
|
||||
asset.sha512 != null -> {
|
||||
val expected = Downloader.downloadString(asset.sha512)?.let { Regex("(?i)\\b[0-9a-f]{128}\\b").find(it)?.value }
|
||||
val expected = Downloader.downloadDirect(asset.sha512)?.let { Regex("(?i)\\b[0-9a-f]{128}\\b").find(it)?.value }
|
||||
?: return context.getString(R.string.pextra_checksum_unreadable)
|
||||
expected.equals(Hashes.sha512(archive), ignoreCase = true)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.session
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import com.droiddeck.launcher.core.GameEnvironment
|
||||
import com.droiddeck.launcher.input.ControllerPrefs
|
||||
import com.droiddeck.launcher.runtime.LinuxRuntime
|
||||
@@ -41,6 +42,7 @@ object SettingsReset {
|
||||
context.getSharedPreferences("frame_gen", Context.MODE_PRIVATE).edit().clear().commit()
|
||||
context.getSharedPreferences("steam_touch", Context.MODE_PRIVATE).edit()
|
||||
.apply { STEAM_TOUCH_KEYS.forEach { remove(it) } }.commit()
|
||||
DownloadSource.reset(context)
|
||||
com.droiddeck.launcher.agent.AgentAccess.setCommandsEnabled(context, false)
|
||||
|
||||
if (gameProfiles) {
|
||||
|
||||
@@ -77,6 +77,7 @@ import com.droiddeck.launcher.frontend.Library
|
||||
import com.droiddeck.launcher.gpu.FrameGen
|
||||
import com.droiddeck.launcher.gpu.Lossless
|
||||
import com.droiddeck.launcher.input.SecondScreenDisplay
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import com.droiddeck.launcher.core.PhantomProcessLimit
|
||||
import com.droiddeck.launcher.core.PhantomProcessStatus
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
@@ -131,6 +132,9 @@ class FrontEndState(
|
||||
val showPhantomGate: Boolean = false,
|
||||
val orientation: Int = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
|
||||
val launcherFullscreen: Boolean = true,
|
||||
val downloadSource: String = DownloadSource.MODE_AUTO,
|
||||
val downloadMirror: String = "",
|
||||
val downloadPrefix: String = "",
|
||||
val animationsEnabled: Boolean = true,
|
||||
val gameStoresEnabled: Boolean = true,
|
||||
val gameStoresSpeedTier: String = "fast",
|
||||
@@ -198,6 +202,9 @@ class FrontEndActions(
|
||||
val onOrientation: (Int) -> Unit = {},
|
||||
val onLauncherFullscreen: (Boolean) -> Unit = {},
|
||||
val onAnimationsEnabled: (Boolean) -> Unit = {},
|
||||
val onDownloadSource: (String) -> Unit = {},
|
||||
val onDownloadMirror: (String) -> Unit = {},
|
||||
val onDownloadPrefix: (String) -> Unit = {},
|
||||
val onGameStoresEnabled: (Boolean) -> Unit = {},
|
||||
val onGameStoresSpeedTier: (String) -> Unit = {},
|
||||
val onStoresOpenTab: (String) -> Unit = {},
|
||||
|
||||
@@ -75,6 +75,7 @@ import androidx.compose.ui.layout.boundsInRoot
|
||||
import androidx.compose.ui.layout.onGloballyPositioned
|
||||
import com.droiddeck.launcher.core.AppLanguage
|
||||
import com.droiddeck.launcher.core.DeviceSupport
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import com.droiddeck.launcher.core.PhantomProcessLimit
|
||||
import com.droiddeck.launcher.core.PhantomProcessStatus
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
@@ -166,7 +167,14 @@ internal fun SetupPanel(
|
||||
val signedIn = s.offlineAccount != null
|
||||
var showLimitDetails by rememberSaveable { mutableStateOf(false) }
|
||||
var editSgdbKey by remember { mutableStateOf(false) }
|
||||
var editDownloadPrefix by remember { mutableStateOf(false) }
|
||||
if (editSgdbKey) SgdbKeyDialog(s.sgdbUserKey, onSave = { a.onSgdbKey(it); editSgdbKey = false }, onDismiss = { editSgdbKey = false })
|
||||
|
||||
if (editDownloadPrefix) TextEntryDialog(
|
||||
stringResource(R.string.setup_download_prefix), s.downloadPrefix,
|
||||
onDismiss = { editDownloadPrefix = false },
|
||||
onSave = { a.onDownloadPrefix(it); editDownloadPrefix = false },
|
||||
)
|
||||
val tabs = listOf(stringResource(R.string.setup_tab_overview), stringResource(R.string.setup_tab_controller), stringResource(R.string.setup_tab_launcher), stringResource(R.string.setup_tab_storage), stringResource(R.string.setup_tab_diagnostics))
|
||||
var tab by rememberSaveable { mutableStateOf(0) }
|
||||
val chipFocus = remember { List(tabs.size) { FocusRequester() } }
|
||||
@@ -390,10 +398,39 @@ internal fun SetupPanel(
|
||||
)
|
||||
}
|
||||
}
|
||||
val download: @Composable () -> Unit = {
|
||||
SettingsGroup(stringResource(R.string.setup_group_download)) {
|
||||
ChoiceRow(
|
||||
host, "download-source", stringResource(R.string.setup_download_source), null,
|
||||
listOf(
|
||||
DownloadSource.MODE_AUTO to stringResource(R.string.setup_download_source_auto),
|
||||
DownloadSource.MODE_DIRECT to stringResource(R.string.setup_download_source_direct),
|
||||
DownloadSource.MODE_MIRROR to stringResource(R.string.setup_download_source_mirror),
|
||||
DownloadSource.MODE_CUSTOM to stringResource(R.string.setup_download_source_custom),
|
||||
),
|
||||
s.downloadSource, onPick = a.onDownloadSource,
|
||||
)
|
||||
if (s.downloadSource == DownloadSource.MODE_MIRROR) {
|
||||
ChoiceRow(
|
||||
host, "download-mirror", stringResource(R.string.setup_download_mirror), null,
|
||||
DownloadSource.MIRRORS.map { it.host to it.host },
|
||||
s.downloadMirror, onPick = a.onDownloadMirror,
|
||||
)
|
||||
}
|
||||
if (s.downloadSource == DownloadSource.MODE_CUSTOM) {
|
||||
ActionRow(
|
||||
stringResource(R.string.setup_download_prefix),
|
||||
s.downloadPrefix.takeIf { it.isNotEmpty() },
|
||||
stringResource(R.string.mode_sgdb_edit),
|
||||
onClick = { editDownloadPrefix = true },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wide) Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.Top) {
|
||||
Column(Modifier.weight(1f)) { look() }
|
||||
Column(Modifier.weight(1f)) { device(); art() }
|
||||
} else { look(); device(); art() }
|
||||
Column(Modifier.weight(1f)) { device(); art(); download() }
|
||||
} else { look(); device(); art(); download() }
|
||||
}
|
||||
STORAGE_TAB -> StoragePanel(s, a)
|
||||
4 -> {
|
||||
|
||||
@@ -3,8 +3,10 @@ package com.droiddeck.launcher.update
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.BuildConfig
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.core.DownloadSource
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import java.io.IOException
|
||||
@@ -13,6 +15,7 @@ import java.net.URL
|
||||
import java.security.MessageDigest
|
||||
|
||||
object AppUpdates {
|
||||
private const val TAG = "AppUpdates"
|
||||
private const val REPO = "Droid-Deck/DroidDeck"
|
||||
private const val CI_REPO = "Droid-Deck/DroidDeck-CI"
|
||||
private const val CATALOG_URL = "https://raw.githubusercontent.com/$CI_REPO/main/catalog.json"
|
||||
@@ -353,7 +356,24 @@ object AppUpdates {
|
||||
}
|
||||
|
||||
private fun get(context: Context, url: String): String {
|
||||
val c = URL(url).openConnection() as HttpURLConnection
|
||||
val sources = DownloadSource.candidates(url)
|
||||
var last: IOException? = null
|
||||
for ((index, source) in sources.withIndex()) {
|
||||
try {
|
||||
val body = get(context, source, url)
|
||||
DownloadSource.succeeded(source)
|
||||
return body
|
||||
} catch (e: IOException) {
|
||||
last = e
|
||||
if (index < sources.lastIndex) Log.i(TAG, "retrying $url via $source")
|
||||
}
|
||||
}
|
||||
DownloadSource.failed()
|
||||
throw last ?: IOException(context.getString(R.string.appupd_http, 0))
|
||||
}
|
||||
|
||||
private fun get(context: Context, source: String, url: String): String {
|
||||
val c = URL(source).openConnection() as HttpURLConnection
|
||||
c.connectTimeout = 15_000
|
||||
c.readTimeout = 20_000
|
||||
c.setRequestProperty("Accept", "application/json")
|
||||
|
||||
@@ -10,13 +10,11 @@ import android.os.Build
|
||||
import android.provider.Settings
|
||||
import com.droiddeck.launcher.BuildConfig
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.core.Downloader
|
||||
import com.droiddeck.launcher.core.FileUtils
|
||||
import com.droiddeck.launcher.core.Hashes
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.security.MessageDigest
|
||||
|
||||
object SelfInstaller {
|
||||
@@ -30,32 +28,12 @@ object SelfInstaller {
|
||||
|
||||
fun download(context: Context, apk: AppUpdates.Apk, progress: (Int) -> Unit): File {
|
||||
val dir = File(context.cacheDir, "updates")
|
||||
FileUtils.delete(dir)
|
||||
dir.listFiles()?.forEach { if (it.name != apk.name) FileUtils.delete(it) }
|
||||
dir.mkdirs()
|
||||
val target = File(dir, apk.name)
|
||||
val c = URL(apk.url).openConnection() as HttpURLConnection
|
||||
c.connectTimeout = 15_000
|
||||
c.readTimeout = 60_000
|
||||
c.setRequestProperty("User-Agent", "DroidDeck-app")
|
||||
c.instanceFollowRedirects = true
|
||||
val ok = Downloader.downloadFile(apk.url, target, true, apk.sha256) { f -> progress(if (f < 0) -1 else (f * 100).toInt()) }
|
||||
if (!ok) throw IOException(context.getString(R.string.selfinst_download_failed))
|
||||
try {
|
||||
if (c.responseCode != 200) throw IOException(context.getString(R.string.selfinst_http, c.responseCode))
|
||||
val total = c.contentLengthLong.takeIf { it > 0 } ?: apk.size
|
||||
c.inputStream.use { input ->
|
||||
FileOutputStream(target).use { out ->
|
||||
val buf = ByteArray(1 shl 16)
|
||||
var done = 0L
|
||||
var last = -1
|
||||
while (true) {
|
||||
val r = input.read(buf)
|
||||
if (r <= 0) break
|
||||
out.write(buf, 0, r)
|
||||
done += r
|
||||
val pct = if (total > 0) (done * 100 / total).toInt() else -1
|
||||
if (pct != last) { last = pct; progress(pct) }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (apk.size > 0 && target.length() != apk.size) {
|
||||
throw IOException(context.getString(R.string.selfinst_size_mismatch))
|
||||
}
|
||||
@@ -63,13 +41,11 @@ object SelfInstaller {
|
||||
throw IOException(context.getString(R.string.selfinst_checksum_mismatch))
|
||||
}
|
||||
validateDownloaded(context, target, apk)
|
||||
return target
|
||||
} catch (e: Exception) {
|
||||
FileUtils.delete(target)
|
||||
throw e
|
||||
} finally {
|
||||
c.disconnect()
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
private fun validateDownloaded(context: Context, file: File, apk: AppUpdates.Apk) {
|
||||
|
||||
Reference in new issue
Block a user