Skip to content

Instantly share code, notes, and snippets.

@paramsen
Last active November 11, 2020 22:14
Show Gist options
  • Save paramsen/f04dd5661ba7da4a39063cf87069b4a6 to your computer and use it in GitHub Desktop.
Save paramsen/f04dd5661ba7da4a39063cf87069b4a6 to your computer and use it in GitHub Desktop.
Android View for printing private logs in realtime, supports lifecycle methods (start->stop)
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.annotation.LayoutRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.atomic.AtomicBoolean;
import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Completable;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
/**
* View for printing private logs in realtime.
* Uses RxJava and ButterKnife. Inflates itself.
*
* Scrolls to bottom on new log
*
* @author Pär Amsen 05/2017
*/
public class LogView extends FrameLayout {
@BindView(R.id.logs)
TextView logs;
@BindView(R.id.scrollView)
ScrollView scrollView;
private AtomicBoolean started;
private StringBuilder sb;
public LogView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public void init(Context context) {
started = new AtomicBoolean(false);
sb = new StringBuilder();
inflateCustomView(context, this, R.layout.view_log, activity -> ButterKnife.bind(this));
}
public void start() {
Completable.create(sub -> {
started.set(true);
try {
Runtime.getRuntime().exec("logcat -c");
Process pq = Runtime.getRuntime().exec("logcat v main");
BufferedReader stream = new BufferedReader(new InputStreamReader(pq.getInputStream()));
String log = "";
while (started.get() && (log = stream.readLine()) != null) {
final String temp = log;
RxUtils.doOnMain(() -> log(truncate(temp)));
}
sub.onCompleted();
} catch (Exception e) {
sub.onError(e);
}
}).subscribeOn(Schedulers.newThread()).subscribe(Throwable::printStackTrace, RxUtils::ignore);
}
public String truncate(String log) {
return log.replaceFirst("([\\d-\\s:.]*)", "");
}
public void log(String log) {
sb.append(log).append("\n");
logs.setText(sb.toString());
scrollView.smoothScrollTo(0, logs.getBottom());
}
public void stop() {
started.set(false);
}
public static void inflateCustomView(Context context, ViewGroup root, @LayoutRes int layout, Action1<Activity> onInflated) {
if (root.isInEditMode()) {
((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layout, root, true);
return;
}
Activity activity = getActivityFromContextWrapper(context);
activity.getLayoutInflater().cloneInContext(context).inflate(layout, root, true);
onInflated.call(activity);
}
public static Activity getActivityFromContextWrapper(Context context) {
Activity activity = null;
if (context instanceof Activity) {
activity = (Activity) context;
} else if (context instanceof ContextWrapper && ((ContextWrapper) context).getBaseContext() instanceof Activity) {
activity = (Activity) ((ContextWrapper) context).getBaseContext();
} else if (context instanceof ContextWrapper && ((ContextWrapper) context).getBaseContext() instanceof ContextWrapper) {
activity = getActivityFromContextWrapper(((ContextWrapper) context).getBaseContext());
}
if (activity == null) {
getActivityFromContextWrapper(context);
}
return activity;
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/logs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="@string/long_text"
android:textSize="8sp"/>
</ScrollView>
@BachegaGB
Copy link

Do you have any sample project that implements this? im having some difficulties

@BrokenTeeth
Copy link

BrokenTeeth commented Nov 11, 2020

can you check implements library name?

how do i add implementation library at my android project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment