Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/PojavLauncher
Path: blob/v3_openjdk/app_pojavlauncher/src/main/java/com/kdt/LoggerView.java
2129 views
1
package com.kdt;
2
3
import android.content.Context;
4
import android.graphics.Typeface;
5
import android.util.AttributeSet;
6
import android.view.View;
7
import android.widget.ImageButton;
8
import android.widget.TextView;
9
import android.widget.ToggleButton;
10
11
import androidx.annotation.NonNull;
12
import androidx.annotation.Nullable;
13
import androidx.constraintlayout.widget.ConstraintLayout;
14
15
import net.kdt.pojavlaunch.Logger;
16
import net.kdt.pojavlaunch.R;
17
18
/**
19
* A class able to display logs to the user.
20
* It has support for the Logger class
21
*/
22
public class LoggerView extends ConstraintLayout {
23
private Logger.eventLogListener mLogListener;
24
private ToggleButton mLogToggle;
25
private DefocusableScrollView mScrollView;
26
private TextView mLogTextView;
27
28
29
public LoggerView(@NonNull Context context) {
30
this(context, null);
31
}
32
33
public LoggerView(@NonNull Context context, @Nullable AttributeSet attrs) {
34
super(context, attrs);
35
init();
36
}
37
38
@Override
39
public void setVisibility(int visibility) {
40
super.setVisibility(visibility);
41
// Triggers the log view shown state by default when viewing it
42
mLogToggle.setChecked(visibility == VISIBLE);
43
}
44
45
/**
46
* Inflate the layout, and add component behaviors
47
*/
48
private void init(){
49
inflate(getContext(), R.layout.view_logger, this);
50
mLogTextView = findViewById(R.id.content_log_view);
51
mLogTextView.setTypeface(Typeface.MONOSPACE);
52
//TODO clamp the max text so it doesn't go oob
53
mLogTextView.setMaxLines(Integer.MAX_VALUE);
54
mLogTextView.setEllipsize(null);
55
mLogTextView.setVisibility(GONE);
56
57
// Toggle log visibility
58
mLogToggle = findViewById(R.id.content_log_toggle_log);
59
mLogToggle.setOnCheckedChangeListener(
60
(compoundButton, isChecked) -> {
61
mLogTextView.setVisibility(isChecked ? VISIBLE : GONE);
62
if(isChecked) {
63
Logger.setLogListener(mLogListener);
64
}else{
65
mLogTextView.setText("");
66
Logger.setLogListener(null); // Makes the JNI code be able to skip expensive logger callbacks
67
// NOTE: was tested by rapidly smashing the log on/off button, no sync issues found :)
68
}
69
});
70
mLogToggle.setChecked(false);
71
72
// Remove the loggerView from the user View
73
ImageButton cancelButton = findViewById(R.id.log_view_cancel);
74
cancelButton.setOnClickListener(view -> LoggerView.this.setVisibility(GONE));
75
76
// Set the scroll view
77
mScrollView = findViewById(R.id.content_log_scroll);
78
mScrollView.setKeepFocusing(true);
79
80
//Set up the autoscroll switch
81
ToggleButton autoscrollToggle = findViewById(R.id.content_log_toggle_autoscroll);
82
autoscrollToggle.setOnCheckedChangeListener(
83
(compoundButton, isChecked) -> {
84
if(isChecked) mScrollView.fullScroll(View.FOCUS_DOWN);
85
mScrollView.setKeepFocusing(isChecked);
86
}
87
);
88
autoscrollToggle.setChecked(true);
89
90
// Listen to logs
91
mLogListener = text -> {
92
if(mLogTextView.getVisibility() != VISIBLE) return;
93
post(() -> {
94
mLogTextView.append(text + '\n');
95
if(mScrollView.isKeepFocusing()) mScrollView.fullScroll(View.FOCUS_DOWN);
96
});
97
98
};
99
}
100
101
}
102
103