forked from dss16694/WechatFp
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
app/src/main/java/com/surcumference/fingerprint/util/LogcatManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.surcumference.fingerprint.util; | ||
|
||
import com.surcumference.fingerprint.util.log.L; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.lang.reflect.Field; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class LogcatManager { | ||
private Process process; | ||
private final File targetFile; | ||
private ScheduledExecutorService timeOutExecutor = Executors.newScheduledThreadPool(1); | ||
|
||
public LogcatManager(File targetFile) { | ||
this.targetFile = targetFile; | ||
} | ||
|
||
public void startLogging(long timeOutMS) { | ||
synchronized (LogcatManager.class) { | ||
try { | ||
stopLoggingInternal(); | ||
this.process = Runtime.getRuntime().exec("logcat"); | ||
Task.onBackground(() -> { | ||
PrintWriter pw = null; | ||
try { | ||
pw = new PrintWriter(targetFile); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
String line; | ||
while ((line = bufferedReader.readLine()) != null) { | ||
pw.println(line); | ||
} | ||
} catch (Exception e) { | ||
L.e(e); | ||
} finally { | ||
FileUtils.closeCloseable(pw); | ||
} | ||
}); | ||
timeOutExecutor.shutdown(); | ||
timeOutExecutor = Executors.newScheduledThreadPool(1); | ||
timeOutExecutor.schedule(() -> { | ||
stopLoggingInternal(); | ||
L.d("stopLogging on timeout"); | ||
}, timeOutMS, TimeUnit.MILLISECONDS); | ||
L.d("startLogging timeOutMS", timeOutMS); | ||
} catch (Exception e) { | ||
L.e(e); | ||
} | ||
} | ||
} | ||
|
||
public void stopLogging() { | ||
synchronized (LogcatManager.class) { | ||
stopLoggingInternal(); | ||
L.d("stopLogging"); | ||
} | ||
} | ||
|
||
private void stopLoggingInternal() { | ||
try { | ||
if (!isRunning()) { | ||
return; | ||
} | ||
int pid = getPid(process); | ||
if (pid > -1) { | ||
android.os.Process.killProcess(pid); | ||
} | ||
process.destroyForcibly(); | ||
} catch (Exception e) { | ||
L.e(e); | ||
} finally { | ||
timeOutExecutor.shutdown(); | ||
timeOutExecutor = Executors.newScheduledThreadPool(1); | ||
} | ||
} | ||
|
||
public boolean isRunning() { | ||
Process process = this.process; | ||
if (process == null) { | ||
return false; | ||
} | ||
return process.isAlive(); | ||
} | ||
|
||
public File getTargetFile() { | ||
return this.targetFile; | ||
} | ||
|
||
public static int getPid(Process p) { | ||
try { | ||
Field f = p.getClass().getDeclaredField("pid"); | ||
f.setAccessible(true); | ||
return f.getInt(p); | ||
} catch (Throwable e) { | ||
L.e(e); | ||
} | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters