Skip to content

Commit 6213191

Browse files
Add permissions, webpage text update, update file name
1 parent 10d463a commit 6213191

File tree

9 files changed

+70
-37
lines changed

9 files changed

+70
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ captures/
1414
.cxx/
1515
*.apk
1616
output.json
17+
*/release
1718

1819
# IntelliJ
1920
*.iml

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
# LogcatLogger
2-
A utility app to save and download android device logs easily.
2+
3+
A utility app to save and download Android device logs easily
4+
5+
## Usage
6+
7+
Use ADB to grant `android.permission.READ_LOGS` to LogcatLogger.
8+
9+
If you don't grant permission, it collects logs from the LogcatLogger app only.
10+
11+
After permission is granted, it starts collecting all the logs.
12+
13+
### Steps
14+
15+
- Install App.
16+
- Connect android device to computer.
17+
- Run below command in Terminal or CMD.
18+
19+
```sh
20+
adb shell "pm grant com.appgambit.android_logger android.permission.READ_LOGS && am force-stop com.appgambit.android_logger"
21+
```
22+
23+
- Open app and it will starts collecting logs.

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111
minSdk 26
1212
targetSdk 34
1313
versionCode 1
14-
versionName "1.0"
14+
versionName "1.0.0"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
4+
<uses-permission android:name="android.permission.READ_LOGS"
5+
tools:ignore="ProtectedPermissions" />
6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
8+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
9+
<uses-permission android:name="android.permission.INTERNET" />
10+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
11+
<uses-permission android:name="android.permission.WAKE_LOCK" />
12+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
413

514
<application
615
android:allowBackup="true"
@@ -26,12 +35,5 @@
2635
<receiver android:name="com.appgambit.android_logger.LogCaptureService$LogDeletionReceiver" />
2736

2837
</application>
29-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
30-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
31-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
32-
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
33-
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
34-
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
35-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3638

3739
</manifest>

app/src/main/java/com/appgambit/android_logger/ForegroundLogService.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ public void run() {
7070
if (!root.exists()) {
7171
root.mkdirs();
7272
}
73+
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
74+
String newFileName = "log_" + sdf.format(new Date()) + ".txt";
7375

74-
File logFile = new File(root, "logg.txt");
76+
File logFile = new File(root, newFileName);
7577

7678
double fileSizeInBytes = 0;
7779
double fileSizeInKB = 0;
@@ -101,8 +103,8 @@ public void run() {
101103
fileSizeInMB = fileSizeInKB / 1024;
102104

103105
if (fileSizeInMB >= 5.0) {
104-
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
105-
String newFileName = "log_" + sdf.format(new Date()) + ".txt";
106+
sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
107+
newFileName = "log_" + sdf.format(new Date()) + ".txt";
106108
logFile = new File(root, newFileName);
107109

108110
fileSizeInBytes = 0;
@@ -203,7 +205,9 @@ private void deleteOldLogFiles(Context context) {
203205

204206
}
205207
public Notification createNotification() {
206-
String notificationChannelId = "LOGCAT LOGGER CHANNEL";
208+
String notificationChannelId = "LOGCAT_LOGGER_CHANNEL";
209+
String url = WebServerService.getUrl(this);
210+
Log.i(MainActivity.TAG, url);
207211

208212
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
209213
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
@@ -212,7 +216,7 @@ public Notification createNotification() {
212216
"Logcat logger notifications channel",
213217
NotificationManager.IMPORTANCE_HIGH
214218
);
215-
channel.setDescription("Service is running...");
219+
channel.setDescription("Use " + url + " to download logs.");
216220
channel.enableLights(true);
217221
channel.setLightColor(Color.RED);
218222
channel.enableVibration(true);
@@ -233,7 +237,7 @@ public Notification createNotification() {
233237

234238
return builder
235239
.setContentTitle("Logcat Logger Service")
236-
.setContentText("Service is running...")
240+
.setContentText("Use " + url + " to download logs.")
237241
.setContentIntent(pendingIntent)
238242
.setSmallIcon(R.mipmap.ic_launcher) // Replace with your icon resource
239243
.setTicker("Ticker text")

app/src/main/java/com/appgambit/android_logger/LogCaptureService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class LogCaptureService extends Service {
3333
private Handler handler = new Handler(Looper.getMainLooper());
3434

3535
// Define the number of days after which log files should be deleted.
36-
private static final int DELETE_LOGS_OLDER_THAN_DAYS = 2;
36+
static final int DELETE_LOGS_OLDER_THAN_DAYS = 2;
3737

3838
@Override
3939
public IBinder onBind(Intent intent) {
@@ -64,7 +64,7 @@ public void run() {
6464
double fileSizeInMB = 0;
6565

6666
while (!Thread.currentThread().isInterrupted()) {
67-
Process process = Runtime.getRuntime().exec("logcat -d");
67+
Process process = Runtime.getRuntime().exec("logcat");
6868

6969
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
7070
StringBuilder stringBuilder = new StringBuilder();

app/src/main/java/com/appgambit/android_logger/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class MainActivity extends AppCompatActivity {
1515
private TextView t1;
16-
16+
static String TAG = "LogcatLogger";
1717
@Override
1818
protected void onCreate(Bundle savedInstanceState) {
1919
super.onCreate(savedInstanceState);

app/src/main/java/com/appgambit/android_logger/WebServerService.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.appgambit.android_logger;
22

33
import android.app.Service;
4+
import android.content.Context;
45
import android.content.Intent;
56
import android.os.IBinder;
67
import android.os.Looper;
@@ -23,7 +24,7 @@
2324
import java.util.ArrayList;
2425

2526
public class WebServerService extends Service {
26-
private static final int SERVER_PORT = 12993;
27+
static final int SERVER_PORT = 12993;
2728
private ServerSocket serverSocket;
2829
private boolean isServerRunning;
2930
private Handler handler = new Handler(Looper.getMainLooper());
@@ -57,32 +58,34 @@ public void run() {
5758
handler.post(new Runnable() {
5859
@Override
5960
public void run() {
60-
String deviceIpAddress = getDeviceIpAddress();
61-
if (deviceIpAddress != null) {
62-
String urlToOpen = "http://" + deviceIpAddress + ":" + SERVER_PORT + "/";
63-
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlToOpen));
64-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
65-
startActivity(intent);
66-
} else {
67-
// Handle the case where you couldn't retrieve the device IP address
68-
}
61+
// String deviceIpAddress = getDeviceIpAddress(this);
62+
// if (deviceIpAddress != null) {
63+
// String urlToOpen = "http://" + deviceIpAddress + ":" + SERVER_PORT + "/";
64+
// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlToOpen));
65+
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
66+
// startActivity(intent);
67+
// } else {
68+
// // Handle the case where you couldn't retrieve the device IP address
69+
// }
6970
}
7071
});
7172
}
7273

73-
private String getDeviceIpAddress() {
74-
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
74+
static String getDeviceIpAddress(Context context) {
75+
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
7576
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
7677
if (ipAddress != 0) {
7778
return Formatter.formatIpAddress(ipAddress);
7879
} else {
7980
return null;
8081
}
8182
}
82-
83+
static String getUrl(Context context) {
84+
return "http://" + getDeviceIpAddress(context) + ":" + SERVER_PORT;
85+
}
8386
private void handleClientRequest(Socket clientSocket) {
8487
try {
85-
String deviceIpAddress = getDeviceIpAddress();
88+
String deviceIpAddress = getDeviceIpAddress(this);
8689
String request = getRequest(clientSocket);
8790
File folder = new File(getExternalFilesDir("Log_Datas").getAbsolutePath());
8891

@@ -172,7 +175,7 @@ private void serveFileNotFound(Socket clientSocket) {
172175

173176
private void serveFileList(Socket clientSocket, File[] files) {
174177
try {
175-
String deviceIpAddress = getDeviceIpAddress();
178+
String deviceIpAddress = getDeviceIpAddress(this);
176179
OutputStream os = clientSocket.getOutputStream();
177180
StringBuilder response = new StringBuilder();
178181

@@ -183,10 +186,12 @@ private void serveFileList(Socket clientSocket, File[] files) {
183186
response.append("<!DOCTYPE html>\n");
184187
response.append("<html>\n");
185188
response.append("<head>\n");
186-
response.append(" <title>File List</title>\n");
189+
response.append(" <title>Logcat Logger</title>\n");
187190
response.append("</head>\n");
188191
response.append("<body>\n");
189-
response.append(" <h1>File List</h1>\n");
192+
response.append(" <h1>Logcat Logger Service - Log Files</h1>\n");
193+
response.append(" <p>Click on the file link to download the log file.</p>\n");
194+
response.append(" <p>Logger Service automatically removes files older than " + LogCaptureService.DELETE_LOGS_OLDER_THAN_DAYS + " days.</p>\n");
190195
response.append(" <ul>\n");
191196

192197
for (File file : files) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
tools:context="com.appgambit.android_logger.com.appgambit.android_logger.MainActivity">
7+
tools:context="com.appgambit.android_logger.MainActivity">
88

99
<ScrollView
1010
android:layout_width="match_parent"
@@ -19,7 +19,7 @@
1919
android:id="@+id/t1"
2020
android:layout_width="wrap_content"
2121
android:layout_height="wrap_content"
22-
android:text="Hello World!" />
22+
android:text="" />
2323
</LinearLayout>
2424
</ScrollView>
2525

0 commit comments

Comments
 (0)