mirror of
https://github.com/Team-xManager/xManager.git
synced 2024-12-17 15:58:26 +01:00
Add files via upload
This commit is contained in:
parent
955fe00fa3
commit
567afbfc1b
54
app/src/main/java/com/xc3fff0e/xmanager/xManager.java
Normal file
54
app/src/main/java/com/xc3fff0e/xmanager/xManager.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.xc3fff0e.xmanager;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.Application;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Process;
|
||||
import android.util.Log;
|
||||
|
||||
public class xManager extends Application {
|
||||
|
||||
private static Context mApplicationContext;
|
||||
private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
|
||||
|
||||
public static Context getContext() {
|
||||
return mApplicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
mApplicationContext = getApplicationContext();
|
||||
this.uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||
|
||||
Thread.setDefaultUncaughtExceptionHandler(
|
||||
new Thread.UncaughtExceptionHandler() {
|
||||
@Override
|
||||
public void uncaughtException(Thread thread, Throwable throwable) {
|
||||
Intent intent = new Intent(getApplicationContext(), DebugActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
intent.putExtra("error", Log.getStackTraceString(throwable));
|
||||
|
||||
PendingIntent pendingIntent =
|
||||
PendingIntent.getActivity(
|
||||
getApplicationContext(),
|
||||
11111,
|
||||
intent,
|
||||
PendingIntent.FLAG_ONE_SHOT
|
||||
);
|
||||
|
||||
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, pendingIntent);
|
||||
|
||||
xManagerLogger.broadcastLog(Log.getStackTraceString(throwable));
|
||||
Process.killProcess(Process.myPid());
|
||||
System.exit(1);
|
||||
|
||||
uncaughtExceptionHandler.uncaughtException(thread, throwable);
|
||||
}
|
||||
});
|
||||
xManagerLogger.startLogging();
|
||||
super.onCreate();
|
||||
}
|
||||
}
|
71
app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java
Normal file
71
app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java
Normal file
@ -0,0 +1,71 @@
|
||||
package com.xc3fff0e.xmanager;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
public class xManagerLogger {
|
||||
|
||||
private static Thread loggerThread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
isRunning = true;
|
||||
|
||||
try {
|
||||
Runtime.getRuntime().exec("logcat -c");
|
||||
Process process = Runtime.getRuntime().exec("logcat");
|
||||
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||
String logTxt = bufferedReader.readLine();
|
||||
do {
|
||||
broadcastLog(logTxt);
|
||||
} while (isRunning && ((logTxt = bufferedReader.readLine()) != null));
|
||||
|
||||
// Thread got stopped, restart if not stopping wantedly
|
||||
if (isRunning) {
|
||||
broadcastLog("Logger got killed. Restarting.");
|
||||
startLogging();
|
||||
} else {
|
||||
broadcastLog("Logger stopped.");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
broadcastLog(e.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static volatile boolean isRunning = false;
|
||||
|
||||
public static void startLogging() {
|
||||
if (!isRunning) {
|
||||
loggerThread.start();
|
||||
} else {
|
||||
throw new IllegalStateException("Logger already running");
|
||||
}
|
||||
}
|
||||
|
||||
public static void broadcastLog(String log) {
|
||||
Context context = xManager.getContext();
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.setAction("com.xc3fff0e.xmanager.ACTION_NEW_DEBUG_LOG");
|
||||
intent.putExtra("log", log);
|
||||
intent.putExtra("packageName", context.getPackageName());
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
public static void stopLogging() {
|
||||
if (isRunning) {
|
||||
isRunning = false;
|
||||
broadcastLog("Stopping logger by user request.");
|
||||
} else {
|
||||
throw new IllegalStateException("Logger not running");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user