Skip to content

Commit 1562679

Browse files
committed
whatsapp works now
1 parent 1d5ab55 commit 1562679

File tree

5 files changed

+158
-26
lines changed

5 files changed

+158
-26
lines changed

.idea/deploymentTargetDropDown.xml

+14-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
android:grantUriPermissions="true">
2323
<meta-data
2424
android:name="android.support.FILE_PROVIDER_PATHS"
25-
android:resource="@xml/provider_paths" />
25+
android:resource="@xml/file_paths" />
2626
</provider>
2727
<activity
2828
android:name=".MainActivity"

app/src/main/java/com/example/ludditeinstaller/AppStore.java

+132-23
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,156 @@
55
import android.content.Intent;
66
import android.database.Cursor;
77
import android.net.Uri;
8+
import android.os.Handler;
9+
import android.util.Log;
10+
11+
import androidx.core.content.FileProvider;
12+
13+
import java.io.File;
814

915
public class AppStore {
16+
private static final String TAG = "AppStore";
1017
private static final String DOWNLOADS_DIR = "apk_downloads";
1118

1219
public void downloadAndInstallApk(Context context, String apkUrl, String fileName) {
13-
Uri contentUri = downloadApk(context, apkUrl, fileName);
14-
installApk(context, contentUri);
20+
Log.d(TAG, "Starting download and install process for: " + apkUrl);
21+
Handler mainHandler = new Handler(context.getMainLooper());
22+
23+
new Thread(() -> {
24+
try {
25+
Log.d(TAG, "Starting download...");
26+
Uri contentUri = downloadApk(context, apkUrl, fileName);
27+
Log.d(TAG, "Download completed, contentUri: " + contentUri);
28+
29+
if (contentUri != null) {
30+
mainHandler.post(() -> {
31+
Log.d(TAG, "Initiating install process");
32+
installApk(context, contentUri);
33+
});
34+
} else {
35+
Log.e(TAG, "Download failed - contentUri is null");
36+
}
37+
} catch (Exception e) {
38+
Log.e(TAG, "Error in download/install process", e);
39+
}
40+
}).start();
1541
}
1642

1743
private Uri downloadApk(Context context, String apkUrl, String fileName) {
1844
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
1945

20-
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
21-
request.setTitle(fileName);
22-
request.setDestinationInExternalFilesDir(context, DOWNLOADS_DIR, fileName);
46+
try {
47+
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
48+
request.setTitle(fileName);
49+
request.setDestinationInExternalFilesDir(context, DOWNLOADS_DIR, fileName);
50+
request.setAllowedOverMetered(true); // Allow download over mobile network
51+
request.setAllowedOverRoaming(true); // Allow download when roaming
52+
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
53+
54+
final long downloadId = dm.enqueue(request);
55+
Log.d(TAG, "Download started with ID: " + downloadId);
56+
57+
int maxAttempts = 30; // 30 seconds timeout
58+
int attempts = 0;
2359

24-
long downloadId = dm.enqueue(request);
60+
while (attempts < maxAttempts) {
61+
DownloadManager.Query query = new DownloadManager.Query();
62+
query.setFilterById(downloadId);
63+
Cursor cursor = dm.query(query);
2564

26-
DownloadManager.Query query = new DownloadManager.Query();
27-
query.setFilterById(downloadId);
28-
Cursor cursor = dm.query(query);
65+
if (cursor.moveToFirst()) {
66+
int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
67+
if (statusIndex != -1) {
68+
int status = cursor.getInt(statusIndex);
69+
int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
70+
int reason = reasonIndex != -1 ? cursor.getInt(reasonIndex) : -1;
2971

30-
Uri fileUri = null;
31-
if (cursor != null && cursor.moveToFirst()) {
32-
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
33-
if (columnIndex != -1) {
34-
String uriString = cursor.getString(columnIndex);
35-
if (uriString != null) {
36-
fileUri = Uri.parse(uriString);
72+
Log.d(TAG, "Download status: " + getStatusString(status) +
73+
", Reason: " + getReasonString(reason));
74+
75+
if (status == DownloadManager.STATUS_SUCCESSFUL) {
76+
int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
77+
if (uriIndex != -1) {
78+
String uriString = cursor.getString(uriIndex);
79+
cursor.close();
80+
return Uri.parse(uriString);
81+
}
82+
} else if (status == DownloadManager.STATUS_FAILED) {
83+
cursor.close();
84+
throw new RuntimeException("Download failed with reason: " + getReasonString(reason));
85+
}
86+
}
87+
}
88+
cursor.close();
89+
90+
attempts++;
91+
try {
92+
Thread.sleep(1000);
93+
} catch (InterruptedException e) {
94+
Thread.currentThread().interrupt();
95+
throw new RuntimeException("Download interrupted", e);
3796
}
3897
}
39-
cursor.close();
98+
99+
// If we get here, download timed out
100+
dm.remove(downloadId);
101+
throw new RuntimeException("Download timed out after " + maxAttempts + " seconds");
102+
103+
} catch (Exception e) {
104+
Log.e(TAG, "Error in downloadApk", e);
105+
throw e;
106+
}
107+
}
108+
private String getStatusString(int status) {
109+
switch (status) {
110+
case DownloadManager.STATUS_FAILED:
111+
return "STATUS_FAILED";
112+
case DownloadManager.STATUS_PAUSED:
113+
return "STATUS_PAUSED";
114+
case DownloadManager.STATUS_PENDING:
115+
return "STATUS_PENDING";
116+
case DownloadManager.STATUS_RUNNING:
117+
return "STATUS_RUNNING";
118+
case DownloadManager.STATUS_SUCCESSFUL:
119+
return "STATUS_SUCCESSFUL";
120+
default:
121+
return "STATUS_UNKNOWN_" + status;
40122
}
123+
}
41124

42-
return fileUri;
125+
private String getReasonString(int reason) {
126+
switch (reason) {
127+
case DownloadManager.ERROR_CANNOT_RESUME: return "ERROR_CANNOT_RESUME";
128+
case DownloadManager.ERROR_DEVICE_NOT_FOUND: return "ERROR_DEVICE_NOT_FOUND";
129+
case DownloadManager.ERROR_FILE_ALREADY_EXISTS: return "ERROR_FILE_ALREADY_EXISTS";
130+
case DownloadManager.ERROR_FILE_ERROR: return "ERROR_FILE_ERROR";
131+
case DownloadManager.ERROR_HTTP_DATA_ERROR: return "ERROR_HTTP_DATA_ERROR";
132+
case DownloadManager.ERROR_INSUFFICIENT_SPACE: return "ERROR_INSUFFICIENT_SPACE";
133+
case DownloadManager.ERROR_TOO_MANY_REDIRECTS: return "ERROR_TOO_MANY_REDIRECTS";
134+
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE: return "ERROR_UNHANDLED_HTTP_CODE";
135+
case DownloadManager.ERROR_UNKNOWN: return "ERROR_UNKNOWN";
136+
case DownloadManager.PAUSED_QUEUED_FOR_WIFI: return "PAUSED_QUEUED_FOR_WIFI";
137+
case DownloadManager.PAUSED_UNKNOWN: return "PAUSED_UNKNOWN";
138+
case DownloadManager.PAUSED_WAITING_FOR_NETWORK: return "PAUSED_WAITING_FOR_NETWORK";
139+
case DownloadManager.PAUSED_WAITING_TO_RETRY: return "PAUSED_WAITING_TO_RETRY";
140+
default: return "UNKNOWN_REASON_" + reason;
141+
}
43142
}
44143

45-
private void installApk(Context context, Uri contentUri) {
46-
Intent intent = new Intent("com.luddite.app.store.INSTALL_PACKAGE");
47-
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
48-
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
49-
context.startActivity(intent);
144+
private void installApk(Context context, Uri fileUri) {
145+
try {
146+
File file = new File(fileUri.getPath());
147+
Uri contentUri = FileProvider.getUriForFile(context,
148+
context.getPackageName() + ".provider",
149+
file);
150+
151+
Intent intent = new Intent("com.luddite.app.store.INSTALL_PACKAGE");
152+
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
153+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
154+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155+
context.startActivity(intent);
156+
} catch (Exception e) {
157+
Log.e(TAG, "Error in installApk", e);
158+
}
50159
}
51160
}

app/src/main/java/com/example/ludditeinstaller/MainActivity.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.example.ludditeinstaller
22

33
import android.os.Bundle
4+
import android.util.Log
45
import android.view.View
56
import android.widget.Button
67
import androidx.appcompat.app.AppCompatActivity
8+
9+
710
class MainActivity : AppCompatActivity() {
811

912
override fun onCreate(savedInstanceState: Bundle?) {
@@ -12,10 +15,11 @@ class MainActivity : AppCompatActivity() {
1215

1316
val whatsappButton = findViewById<Button>(R.id.btn_whatsapp)
1417
whatsappButton.setOnClickListener { v: View? ->
18+
Log.d("MainActivity", "WhatsApp install button clicked")
1519
val appStore = AppStore()
1620
appStore.downloadAndInstallApk(
1721
this,
18-
"https://scontent.whatsapp.net/v/t61.25591-34/10000000_1970768216771235_41723970822000500_n.apk/WhatsApp.apk?ccb=1-7&_nc_sid=c49adc&_nc_ohc=u9etM8GIfIQQ7kNvgFekAK4&_nc_zt=3&_nc_ht=scontent.whatsapp.net&_nc_gid=A3SkJFb7EO9ccdL7SQXdTBj&oh=01_Q5AaIJ2vg-4Wn9WNeSRX7mcnOC1uRQ7AjDyBvzeYaFflNEYH&oe=6793E8F8",
22+
"https://scontent.whatsapp.net/v/t61.25591-34/10000000_1970768216771235_41723970822000500_n.apk/WhatsApp.apk?ccb=1-7&_nc_sid=c49adc&_nc_ohc=u9etM8GIfIQQ7kNvgFekAK4&_nc_zt=3&_nc_ht=scontent.whatsapp.net&_nc_gid=AKHGXCfvpKZ1237Zq0Xs5kW&oh=01_Q5AaIGUofr6QbnvMxDVrTOAUzp7cD0C0ZiIv22leRtpHBFhE&oe=6794C9F8",
1923
"whatsapp.apk"
2024
)
2125
}

app/src/main/res/xml/file_paths.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<paths xmlns:android="http://schemas.android.com/apk/res/android">
3+
<external-files-path
4+
name="apks"
5+
path="apk_downloads" />
6+
</paths>

0 commit comments

Comments
 (0)