13
13
import java .io .File ;
14
14
15
15
public class AppStore {
16
+ private LogDisplay logDisplay ;
16
17
private static final String TAG = "AppStore" ;
17
18
private static final String DOWNLOADS_DIR = "apk_downloads" ;
18
19
20
+ public AppStore (LogDisplay logDisplay ) {
21
+ this .logDisplay = logDisplay ;
22
+ }
23
+
19
24
public void downloadAndInstallApk (Context context , String apkUrl , String fileName ) {
20
- Log . d (TAG , "Starting download and install process for: " + apkUrl );
25
+ logDisplay . log (TAG , "Starting download and install process for: " + apkUrl );
21
26
Handler mainHandler = new Handler (context .getMainLooper ());
22
27
23
28
new Thread (() -> {
24
29
try {
25
- Log . d (TAG , "Starting download..." );
30
+ logDisplay . log (TAG , "Starting download..." );
26
31
Uri contentUri = downloadApk (context , apkUrl , fileName );
27
- Log . d (TAG , "Download completed, contentUri: " + contentUri );
32
+ logDisplay . log (TAG , "Download completed, contentUri: " + contentUri );
28
33
29
34
if (contentUri != null ) {
30
35
mainHandler .post (() -> {
31
- Log . d (TAG , "Initiating install process" );
36
+ logDisplay . log (TAG , "Initiating install process" );
32
37
installApk (context , contentUri );
33
38
});
34
39
} else {
35
- Log . e (TAG , "Download failed - contentUri is null" );
40
+ logDisplay . log (TAG , "Download failed - contentUri is null" );
36
41
}
37
42
} catch (Exception e ) {
38
- Log . e (TAG , "Error in download/install process" , e );
43
+ logDisplay . log (TAG , "Error in download/install process: " + e . getMessage () );
39
44
}
40
45
}).start ();
41
46
}
@@ -47,14 +52,14 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
47
52
DownloadManager .Request request = new DownloadManager .Request (Uri .parse (apkUrl ));
48
53
request .setTitle (fileName );
49
54
request .setDestinationInExternalFilesDir (context , DOWNLOADS_DIR , fileName );
50
- request .setAllowedOverMetered (true ); // Allow download over mobile network
51
- request .setAllowedOverRoaming (true ); // Allow download when roaming
55
+ request .setAllowedOverMetered (true );
56
+ request .setAllowedOverRoaming (true );
52
57
request .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE );
53
58
54
59
final long downloadId = dm .enqueue (request );
55
- Log . d (TAG , "Download started with ID: " + downloadId );
60
+ logDisplay . log (TAG , "Download started with ID: " + downloadId );
56
61
57
- int maxAttempts = 30 ; // 30 seconds timeout
62
+ int maxAttempts = 30 ;
58
63
int attempts = 0 ;
59
64
60
65
while (attempts < maxAttempts ) {
@@ -69,7 +74,7 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
69
74
int reasonIndex = cursor .getColumnIndex (DownloadManager .COLUMN_REASON );
70
75
int reason = reasonIndex != -1 ? cursor .getInt (reasonIndex ) : -1 ;
71
76
72
- Log . d (TAG , "Download status: " + getStatusString (status ) +
77
+ logDisplay . log (TAG , "Download status: " + getStatusString (status ) +
73
78
", Reason: " + getReasonString (reason ));
74
79
75
80
if (status == DownloadManager .STATUS_SUCCESSFUL ) {
@@ -101,7 +106,7 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
101
106
throw new RuntimeException ("Download timed out after " + maxAttempts + " seconds" );
102
107
103
108
} catch (Exception e ) {
104
- Log . e (TAG , "Error in downloadApk" , e );
109
+ logDisplay . log (TAG , "Error in downloadApk: " + e . getMessage () );
105
110
throw e ;
106
111
}
107
112
}
@@ -154,7 +159,7 @@ private void installApk(Context context, Uri fileUri) {
154
159
intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
155
160
context .startActivity (intent );
156
161
} catch (Exception e ) {
157
- Log . e (TAG , "Error in installApk" , e );
162
+ logDisplay . log (TAG , "Error in installApk: " + e . getMessage () );
158
163
}
159
164
}
160
165
}
0 commit comments