Skip to content

Commit fd6514d

Browse files
committed
add Logging
1 parent 1562679 commit fd6514d

File tree

6 files changed

+101
-32
lines changed

6 files changed

+101
-32
lines changed

.idea/deploymentTargetDropDown.xml

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

.idea/gradle.xml

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

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

+18-13
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,34 @@
1313
import java.io.File;
1414

1515
public class AppStore {
16+
private LogDisplay logDisplay;
1617
private static final String TAG = "AppStore";
1718
private static final String DOWNLOADS_DIR = "apk_downloads";
1819

20+
public AppStore(LogDisplay logDisplay) {
21+
this.logDisplay = logDisplay;
22+
}
23+
1924
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);
2126
Handler mainHandler = new Handler(context.getMainLooper());
2227

2328
new Thread(() -> {
2429
try {
25-
Log.d(TAG, "Starting download...");
30+
logDisplay.log(TAG, "Starting download...");
2631
Uri contentUri = downloadApk(context, apkUrl, fileName);
27-
Log.d(TAG, "Download completed, contentUri: " + contentUri);
32+
logDisplay.log(TAG, "Download completed, contentUri: " + contentUri);
2833

2934
if (contentUri != null) {
3035
mainHandler.post(() -> {
31-
Log.d(TAG, "Initiating install process");
36+
logDisplay.log(TAG, "Initiating install process");
3237
installApk(context, contentUri);
3338
});
3439
} else {
35-
Log.e(TAG, "Download failed - contentUri is null");
40+
logDisplay.log(TAG, "Download failed - contentUri is null");
3641
}
3742
} catch (Exception e) {
38-
Log.e(TAG, "Error in download/install process", e);
43+
logDisplay.log(TAG, "Error in download/install process: " + e.getMessage());
3944
}
4045
}).start();
4146
}
@@ -47,14 +52,14 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
4752
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
4853
request.setTitle(fileName);
4954
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);
5257
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
5358

5459
final long downloadId = dm.enqueue(request);
55-
Log.d(TAG, "Download started with ID: " + downloadId);
60+
logDisplay.log(TAG, "Download started with ID: " + downloadId);
5661

57-
int maxAttempts = 30; // 30 seconds timeout
62+
int maxAttempts = 30;
5863
int attempts = 0;
5964

6065
while (attempts < maxAttempts) {
@@ -69,7 +74,7 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
6974
int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
7075
int reason = reasonIndex != -1 ? cursor.getInt(reasonIndex) : -1;
7176

72-
Log.d(TAG, "Download status: " + getStatusString(status) +
77+
logDisplay.log(TAG, "Download status: " + getStatusString(status) +
7378
", Reason: " + getReasonString(reason));
7479

7580
if (status == DownloadManager.STATUS_SUCCESSFUL) {
@@ -101,7 +106,7 @@ private Uri downloadApk(Context context, String apkUrl, String fileName) {
101106
throw new RuntimeException("Download timed out after " + maxAttempts + " seconds");
102107

103108
} catch (Exception e) {
104-
Log.e(TAG, "Error in downloadApk", e);
109+
logDisplay.log(TAG, "Error in downloadApk: " + e.getMessage());
105110
throw e;
106111
}
107112
}
@@ -154,7 +159,7 @@ private void installApk(Context context, Uri fileUri) {
154159
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155160
context.startActivity(intent);
156161
} catch (Exception e) {
157-
Log.e(TAG, "Error in installApk", e);
162+
logDisplay.log(TAG, "Error in installApk: " + e.getMessage());
158163
}
159164
}
160165
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.ludditeinstaller
2+
3+
import android.content.Context
4+
import android.os.Handler
5+
import android.os.Looper
6+
import android.util.Log
7+
import android.widget.TextView
8+
import androidx.appcompat.app.AppCompatActivity
9+
import java.text.SimpleDateFormat
10+
import java.util.*
11+
12+
class LogDisplay(private val context: Context, private val logTextView: TextView) {
13+
private val mainHandler = Handler(Looper.getMainLooper())
14+
private val dateFormat = SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault())
15+
16+
init {
17+
logTextView.setTextIsSelectable(true)
18+
}
19+
20+
fun log(tag: String, message: String) {
21+
val timestamp = dateFormat.format(Date())
22+
val logMessage = "[$timestamp] $tag: $message\n"
23+
24+
mainHandler.post {
25+
logTextView.append(logMessage)
26+
// Auto-scroll to bottom
27+
val scrollAmount = logTextView.layout?.getLineTop(logTextView.lineCount) ?: 0
28+
logTextView.scrollTo(0, scrollAmount)
29+
}
30+
31+
// Also log to system log
32+
Log.d(tag, message)
33+
}
34+
35+
fun clear() {
36+
mainHandler.post {
37+
logTextView.text = ""
38+
}
39+
}
40+
}

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,35 @@ import android.os.Bundle
44
import android.util.Log
55
import android.view.View
66
import android.widget.Button
7+
import android.widget.TextView
78
import androidx.appcompat.app.AppCompatActivity
89

910

1011
class MainActivity : AppCompatActivity() {
12+
private lateinit var logDisplay: LogDisplay
1113

1214
override fun onCreate(savedInstanceState: Bundle?) {
1315
super.onCreate(savedInstanceState)
1416
setContentView(R.layout.activity_main)
1517

18+
val logTextView = findViewById<TextView>(R.id.log_text_view)
19+
logDisplay = LogDisplay(this, logTextView)
20+
1621
val whatsappButton = findViewById<Button>(R.id.btn_whatsapp)
1722
whatsappButton.setOnClickListener { v: View? ->
18-
Log.d("MainActivity", "WhatsApp install button clicked")
19-
val appStore = AppStore()
23+
logDisplay.log("MainActivity", "WhatsApp install button clicked")
24+
val appStore = AppStore(logDisplay)
2025
appStore.downloadAndInstallApk(
2126
this,
2227
"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",
2328
"whatsapp.apk"
2429
)
2530
}
26-
}
2731

32+
// Optional: Add a clear logs button
33+
val clearButton = findViewById<Button>(R.id.btn_clear_logs)
34+
clearButton.setOnClickListener {
35+
logDisplay.clear()
36+
}
37+
}
2838
}

app/src/main/res/layout/activity_main.xml

+28-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,37 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:orientation="vertical"
44
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
5+
android:layout_height="match_parent"
6+
android:padding="16dp">
67

78
<Button
89
android:id="@+id/btn_whatsapp"
9-
android:layout_width="227dp"
10+
android:layout_width="match_parent"
1011
android:layout_height="wrap_content"
1112
android:text="Install Whatsapp" />
13+
14+
<Button
15+
android:id="@+id/btn_clear_logs"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"
18+
android:text="Clear Logs" />
19+
20+
<ScrollView
21+
android:layout_width="match_parent"
22+
android:layout_height="0dp"
23+
android:layout_weight="1"
24+
android:layout_marginTop="16dp"
25+
android:background="#F5F5F5">
26+
27+
<TextView
28+
android:id="@+id/log_text_view"
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"
31+
android:padding="8dp"
32+
android:textSize="12sp"
33+
android:fontFamily="monospace"
34+
android:textColor="#000000" />
35+
36+
</ScrollView>
37+
1238
</LinearLayout>

0 commit comments

Comments
 (0)