Skip to content

Commit f961e89

Browse files
committed
fix: Mobile Data warning fix
1 parent 15b8aad commit f961e89

5 files changed

Lines changed: 70 additions & 33 deletions

File tree

‎app/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ android {
1515
minSdk 28
1616
targetSdk 36
1717
versionCode 1
18-
versionName "0.7"
18+
versionName "0.5"
1919
buildConfigField "boolean", "IS_BETA", "true"
2020

2121
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

‎app/src/main/java/com/drivehub/kamera/NetworkStateHelper.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ static Transport getActiveTransport(Context context) {
3737
return Transport.UNKNOWN;
3838
}
3939
}
40+
41+
static boolean isActiveNetworkMetered(Context context) {
42+
try {
43+
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
44+
return cm != null && cm.isActiveNetworkMetered();
45+
} catch (Throwable ignored) {
46+
return false;
47+
}
48+
}
4049
}

‎app/src/main/java/com/drivehub/kamera/OtaApkVerifier.java‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.drivehub.kamera;
22

3+
import android.content.Context;
4+
import android.net.Uri;
5+
36
import java.io.File;
47
import java.io.FileInputStream;
8+
import java.io.InputStream;
59
import java.security.MessageDigest;
610
import java.util.Locale;
711

@@ -11,18 +15,15 @@ private OtaApkVerifier() {
1115
}
1216

1317
// Verification is intentionally isolated so download integrity stays testable without UI code.
14-
static VerificationResult verifyDownloadedApk(File apkFile, OtaUpdateManager.UpdateInfo info) {
18+
static VerificationResult verifyDownloadedApk(Context context, Uri apkUri, OtaUpdateManager.UpdateInfo info) {
1519
boolean success;
1620
String computed = "";
1721
String message;
18-
try {
19-
if (apkFile == null || !apkFile.exists()) {
20-
throw new IllegalStateException("Downloaded APK not found");
21-
}
22+
try (InputStream in = openApkStream(context, apkUri)) {
2223
if (info == null || info.expectedSha256 == null || info.expectedSha256.isEmpty()) {
2324
throw new IllegalStateException("Missing expected SHA-256");
2425
}
25-
computed = computeSha256(apkFile);
26+
computed = computeSha256(in);
2627
success = computed.equalsIgnoreCase(info.expectedSha256);
2728
message = success ? "OK" : "SHA256 mismatch";
2829
} catch (Exception e) {
@@ -32,14 +33,41 @@ static VerificationResult verifyDownloadedApk(File apkFile, OtaUpdateManager.Upd
3233
return new VerificationResult(success, computed, message);
3334
}
3435

36+
private static InputStream openApkStream(Context context, Uri apkUri) throws Exception {
37+
if (context == null) {
38+
throw new IllegalStateException("Context missing");
39+
}
40+
if (apkUri == null) {
41+
throw new IllegalStateException("Downloaded APK not found");
42+
}
43+
String scheme = apkUri.getScheme();
44+
if ("file".equalsIgnoreCase(scheme)) {
45+
File apkFile = new File(apkUri.getPath());
46+
if (!apkFile.exists()) {
47+
throw new IllegalStateException("Downloaded APK not found");
48+
}
49+
return new FileInputStream(apkFile);
50+
}
51+
52+
InputStream stream = context.getContentResolver().openInputStream(apkUri);
53+
if (stream == null) {
54+
throw new IllegalStateException("Downloaded APK not found");
55+
}
56+
return stream;
57+
}
58+
3559
private static String computeSha256(File file) throws Exception {
36-
MessageDigest digest = MessageDigest.getInstance("SHA-256");
3760
try (FileInputStream in = new FileInputStream(file)) {
38-
byte[] buffer = new byte[8192];
39-
int read;
40-
while ((read = in.read(buffer)) != -1) {
41-
digest.update(buffer, 0, read);
42-
}
61+
return computeSha256(in);
62+
}
63+
}
64+
65+
private static String computeSha256(InputStream in) throws Exception {
66+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
67+
byte[] buffer = new byte[8192];
68+
int read;
69+
while ((read = in.read(buffer)) != -1) {
70+
digest.update(buffer, 0, read);
4371
}
4472
byte[] hash = digest.digest();
4573
StringBuilder sb = new StringBuilder(hash.length * 2);

‎app/src/main/java/com/drivehub/kamera/OtaController.java‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
import androidx.appcompat.app.AppCompatActivity;
1919
import androidx.core.content.ContextCompat;
20-
import androidx.core.content.FileProvider;
21-
22-
import java.io.File;
2320
import java.util.Locale;
2421

2522
final class OtaController {
@@ -115,7 +112,11 @@ private void maybeStartDownload(OtaUpdateManager.UpdateInfo info) {
115112
return;
116113
}
117114
NetworkStateHelper.Transport transport = NetworkStateHelper.getActiveTransport(activity);
118-
if (transport == NetworkStateHelper.Transport.CELLULAR) {
115+
boolean warnForMeteredDownload = transport == NetworkStateHelper.Transport.CELLULAR
116+
|| (NetworkStateHelper.isActiveNetworkMetered(activity)
117+
&& transport != NetworkStateHelper.Transport.WIFI
118+
&& transport != NetworkStateHelper.Transport.ETHERNET);
119+
if (warnForMeteredDownload) {
119120
OtaDialogs.showConfirmDialog(
120121
activity,
121122
activity.getString(R.string.ota_dialog_mobile_warning_message, info.latestVersion),
@@ -261,9 +262,9 @@ private void startVerification(long downloadId, TextView statusView, View instal
261262
if (installButton != null) installButton.setVisibility(View.GONE);
262263
if (statusView != null) statusView.setText(R.string.ota_progress_verifying);
263264

264-
File apkFile;
265+
Uri apkUri;
265266
try {
266-
apkFile = resolveApkFile(downloadId);
267+
apkUri = resolveDownloadedApkUri(downloadId);
267268
} catch (Exception e) {
268269
verificationInFlight = false;
269270
if (statusView != null) {
@@ -273,7 +274,7 @@ private void startVerification(long downloadId, TextView statusView, View instal
273274
return;
274275
}
275276

276-
OtaUpdateManager.verifyDownloadedApk(apkFile, activeDownloadInfo, (success, computedSha256, message) -> {
277+
OtaUpdateManager.verifyDownloadedApk(activity, apkUri, activeDownloadInfo, (success, computedSha256, message) -> {
277278
if (downloadId != verificationDownloadId) return;
278279
verificationInFlight = false;
279280
verificationPassed = success;
@@ -296,15 +297,10 @@ private void startVerification(long downloadId, TextView statusView, View instal
296297

297298
private void installUpdate(long downloadId) {
298299
try {
299-
File apkFile = resolveApkFile(downloadId);
300-
if (apkFile == null || !apkFile.exists()) {
300+
Uri apkUri = resolveDownloadedApkUri(downloadId);
301+
if (apkUri == null) {
301302
throw new IllegalStateException("Downloaded APK not found");
302303
}
303-
Uri apkUri = FileProvider.getUriForFile(
304-
activity,
305-
BuildConfig.APPLICATION_ID + ".fileprovider",
306-
apkFile
307-
);
308304
Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
309305
installIntent.setData(apkUri);
310306
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
@@ -327,16 +323,18 @@ private void installUpdate(long downloadId) {
327323
}
328324
}
329325

330-
private File resolveApkFile(long downloadId) throws Exception {
326+
private Uri resolveDownloadedApkUri(long downloadId) throws Exception {
331327
DownloadManager dm = activity.getSystemService(DownloadManager.class);
332328
if (dm == null) return null;
329+
Uri downloadedUri = dm.getUriForDownloadedFile(downloadId);
330+
if (downloadedUri != null) {
331+
return downloadedUri;
332+
}
333333
try (Cursor cursor = dm.query(new DownloadManager.Query().setFilterById(downloadId))) {
334334
if (cursor == null || !cursor.moveToFirst()) return null;
335335
String localUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
336336
if (localUri == null || localUri.isEmpty()) return null;
337-
Uri uri = Uri.parse(localUri);
338-
if (!"file".equalsIgnoreCase(uri.getScheme())) return null;
339-
return new File(uri.getPath());
337+
return Uri.parse(localUri);
340338
}
341339
}
342340

‎app/src/main/java/com/drivehub/kamera/OtaUpdateManager.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ static long enqueueDownload(Context context, UpdateInfo info) {
148148
return dm.enqueue(request);
149149
}
150150

151-
static void verifyDownloadedApk(File apkFile, UpdateInfo info, VerifyCallback callback) {
151+
static void verifyDownloadedApk(Context context, Uri apkUri, UpdateInfo info, VerifyCallback callback) {
152+
final Context appContext = context.getApplicationContext();
152153
EXECUTOR.execute(() -> {
153-
OtaApkVerifier.VerificationResult result = OtaApkVerifier.verifyDownloadedApk(apkFile, info);
154+
OtaApkVerifier.VerificationResult result =
155+
OtaApkVerifier.verifyDownloadedApk(appContext, apkUri, info);
154156
MAIN_HANDLER.post(() -> callback.onResult(result.success, result.computedSha256, result.message));
155157
});
156158
}

0 commit comments

Comments
 (0)