forked from Sonelli/juicessh-performancemonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskUsageController.java
76 lines (57 loc) · 2.53 KB
/
DiskUsageController.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
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 DiskUsageController extends BaseController {
public static final String TAG = "DiskUsageController";
public DiskUsageController(Context context) {
super(context);
}
@Override
public BaseController start() {
super.start();
// Work out the free disk space percentage on the / disk
final Pattern diskUsagePattern = Pattern.compile("([0-9.]+%)"); // Heavy cpu so do out of loops.
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
try {
getPluginClient().executeCommandOnSession(getSessionId(), getSessionKey(), "df | grep ' /$'", new OnSessionExecuteListener() {
@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 diskUsageMatcher = diskUsagePattern.matcher(line);
if(diskUsageMatcher.find()){
setText(diskUsageMatcher.group(1));
}
}
@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;
}
}