forked from Sonelli/juicessh-performancemonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreeRamController.java
122 lines (100 loc) · 5.01 KB
/
FreeRamController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.sonelli.juicessh.performancemonitor.controllers;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import com.sonelli.juicessh.performancemonitor.R;
import com.sonelli.juicessh.pluginlibrary.exceptions.ServiceNotConnectedException;
import com.sonelli.juicessh.pluginlibrary.listeners.OnSessionExecuteListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FreeRamController extends BaseController {
public static final String TAG = "FreeRamController";
public FreeRamController(Context context) {
super(context);
}
@Override
public BaseController start() {
super.start();
// Compile the regex patterns outside of the menu_main loop (cpu heavy)
final Pattern buffersPattern = Pattern.compile("^Buffers:\\s*([0-9]+)");
final Pattern freePattern = Pattern.compile("^MemFree:\\s*([0-9]+)");
final Pattern cachedPattern = Pattern.compile("^Cached:\\s*([0-9]+)");
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
try {
getPluginClient().executeCommandOnSession(getSessionId(), getSessionKey(), "cat /proc/meminfo", new OnSessionExecuteListener() {
private int buffers = -1;
private int free = -1;
private int cached = -1;
@Override
public void onCompleted(int exitCode) {
switch(exitCode){
case 127:
setText(getString(R.string.error));
Log.d(TAG, "Tried to run a command but the command was not found on the server");
break;
}
}
@Override
public void onOutputLine(String line) {
Matcher buffersMatcher = buffersPattern.matcher(line);
Matcher freeMatcher = freePattern.matcher(line);
Matcher cachedMatcher = cachedPattern.matcher(line);
if(buffersMatcher.find()){
buffers = Integer.valueOf(buffersMatcher.group(1));
if(buffers > -1 && free > -1 && cached > -1){
long kb = free + buffers + cached;
if(kb > 1048576){
setText(kb / 1024 / 1024 + " GB");
} else if (kb > 1024){
setText(kb / 1024 + " MB");
} else {
setText(kb + " KB");
}
}
}
if(freeMatcher.find()){
free = Integer.valueOf(freeMatcher.group(1));
if(buffers > -1 && free > -1 && cached > -1){
long kb = free + buffers + cached;
if(kb > 1048576){
setText(kb / 1024 / 1024 + " GB");
} else if (kb > 1024){
setText(kb / 1024 + " MB");
} else {
setText(kb + " KB");
}
}
}
if(cachedMatcher.find()){
cached = Integer.valueOf(cachedMatcher.group(1));
if(buffers > -1 && free > -1 && cached > -1){
long kb = free + buffers + cached;
if(kb > 1048576){
setText(kb / 1024 / 1024 + " GB");
} else if (kb > 1024){
setText(kb / 1024 + " MB");
} else {
setText(kb + " KB");
}
}
}
}
@Override
public void onError(int error, String reason) {
toast(reason);
}
});
} catch (ServiceNotConnectedException e){
Log.d(TAG, "Tried to execute a command but could not connect to JuiceSSH plugin service");
}
if(isRunning()){
handler.postDelayed(this, INTERVAL_SECONDS * 1000L);
}
}
});
return this;
}
}