@@ -72,7 +72,18 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
72
72
try {
73
73
DownloadManager .Request request = new DownloadManager .Request (Uri .parse (apkUrl ));
74
74
request .setTitle (fileName );
75
+
76
+ // Ensure the download directory exists
77
+ File downloadDir = context .getExternalFilesDir (DOWNLOADS_DIR );
78
+ if (downloadDir != null && !downloadDir .exists ()) {
79
+ downloadDir .mkdirs ();
80
+ logDisplay .log (TAG , "Created download directory: " + downloadDir .getAbsolutePath ());
81
+ }
82
+
83
+ // Set the destination (external app files directory)
75
84
request .setDestinationInExternalFilesDir (context , DOWNLOADS_DIR , fileName );
85
+ logDisplay .log (TAG , "Download destination: " + context .getExternalFilesDir (DOWNLOADS_DIR ).getAbsolutePath () + "/" + fileName );
86
+
76
87
request .setAllowedOverMetered (true );
77
88
request .setAllowedOverRoaming (true );
78
89
request .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE );
@@ -103,7 +114,45 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
103
114
if (uriIndex != -1 ) {
104
115
String uriString = cursor .getString (uriIndex );
105
116
cursor .close ();
106
- return Uri .parse (uriString );
117
+
118
+ Uri downloadedUri = Uri .parse (uriString );
119
+ logDisplay .log (TAG , "Downloaded file URI: " + downloadedUri .toString ());
120
+
121
+ // If it's a content URI, try to get the actual file path
122
+ if (downloadedUri .getScheme () != null && downloadedUri .getScheme ().equals ("content" )) {
123
+ logDisplay .log (TAG , "Downloaded file is a content URI" );
124
+
125
+ // Try to get the actual file path using the download manager
126
+ try {
127
+ DownloadManager .Query fileQuery = new DownloadManager .Query ();
128
+ fileQuery .setFilterById (downloadId );
129
+ Cursor filePathCursor = dm .query (fileQuery );
130
+
131
+ if (filePathCursor .moveToFirst ()) {
132
+ int fileNameIdx = filePathCursor .getColumnIndex (DownloadManager .COLUMN_LOCAL_FILENAME );
133
+ if (fileNameIdx != -1 ) {
134
+ String filePath = filePathCursor .getString (fileNameIdx );
135
+ filePathCursor .close ();
136
+
137
+ if (filePath != null ) {
138
+ File file = new File (filePath );
139
+ logDisplay .log (TAG , "Actual file path: " + file .getAbsolutePath () + ", exists: " + file .exists ());
140
+ if (file .exists ()) {
141
+ return Uri .fromFile (file );
142
+ }
143
+ }
144
+ } else {
145
+ filePathCursor .close ();
146
+ }
147
+ } else {
148
+ filePathCursor .close ();
149
+ }
150
+ } catch (Exception e ) {
151
+ logDisplay .log (TAG , "Error getting file path: " + e .getMessage ());
152
+ }
153
+ }
154
+
155
+ return downloadedUri ;
107
156
}
108
157
} else if (status == DownloadManager .STATUS_FAILED ) {
109
158
cursor .close ();
@@ -169,16 +218,47 @@ private String getReasonString(int reason) {
169
218
170
219
private void installApk (Context context , Uri fileUri ) {
171
220
try {
221
+ logDisplay .log (TAG , "Preparing installation from URI: " + fileUri );
172
222
File file = new File (fileUri .getPath ());
173
- Uri contentUri = FileProvider .getUriForFile (context ,
174
- context .getPackageName () + ".provider" ,
175
- file );
176
-
177
- Intent intent = new Intent ("com.luddite.app.store.INSTALL_PACKAGE" );
178
- intent .setDataAndType (contentUri , "application/vnd.android.package-archive" );
179
- intent .addFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
180
- intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
181
- context .startActivity (intent );
223
+
224
+ logDisplay .log (TAG , "File exists: " + file .exists () + ", path: " + file .getAbsolutePath ());
225
+
226
+ // Check if the file is a direct path or content URI
227
+ Uri contentUri ;
228
+ if (fileUri .getScheme () != null && fileUri .getScheme ().equals ("content" )) {
229
+ contentUri = fileUri ;
230
+ logDisplay .log (TAG , "Using direct content URI: " + contentUri );
231
+ } else {
232
+ contentUri = FileProvider .getUriForFile (context ,
233
+ context .getPackageName () + ".provider" ,
234
+ file );
235
+ logDisplay .log (TAG , "Created content URI using FileProvider: " + contentUri );
236
+ }
237
+
238
+ // First try the custom intent
239
+ try {
240
+ Intent customIntent = new Intent ("com.luddite.app.store.INSTALL_PACKAGE" );
241
+ customIntent .setDataAndType (contentUri , "application/vnd.android.package-archive" );
242
+ customIntent .addFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
243
+ customIntent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
244
+ logDisplay .log (TAG , "Attempting to start custom intent: com.luddite.app.store.INSTALL_PACKAGE" );
245
+ context .startActivity (customIntent );
246
+ } catch (Exception e ) {
247
+ logDisplay .log (TAG , "Custom intent failed: " + e .getMessage () + ". Trying standard install intent..." );
248
+
249
+ // Fallback to standard package installer
250
+ Intent standardIntent = new Intent (Intent .ACTION_VIEW );
251
+ standardIntent .setDataAndType (contentUri , "application/vnd.android.package-archive" );
252
+ standardIntent .addFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
253
+ standardIntent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
254
+ logDisplay .log (TAG , "Starting standard install intent: ACTION_VIEW" );
255
+ context .startActivity (standardIntent );
256
+ }
257
+
258
+ // Notify completion - we consider it complete when installation dialog starts
259
+ if (callback != null ) {
260
+ callback .onDownloadComplete ();
261
+ }
182
262
} catch (Exception e ) {
183
263
logDisplay .log (TAG , "Error in installApk: " + e .getMessage ());
184
264
if (callback != null ) {
0 commit comments