Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to change Disk Usage partition #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sonelli.juicessh.performancemonitor.R;
Expand Down Expand Up @@ -48,6 +49,9 @@ public class MainActivity extends ActionBarActivity implements ActionBar.OnNavig

private ConnectionSpinnerAdapter spinnerAdapter;

// Containers
private LinearLayout diskUsageContainer;

// Controllers
private BaseController loadAverageController;
private BaseController freeRamController;
Expand Down Expand Up @@ -94,6 +98,17 @@ protected void onCreate(Bundle savedInstanceState) {
this.networkUsageTextView = (AutoResizeTextView) findViewById(R.id.network_usage);
this.diskUsageTextView = (AutoResizeTextView) findViewById(R.id.disk_usage);

this.diskUsageContainer = (LinearLayout) findViewById(R.id.disk_usage_container);

this.diskUsageContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(diskUsageController != null) {
((DiskUsageController)diskUsageController).choosePartition();
}
}
});

this.connectButton = (Button) findViewById(R.id.connect_button);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class BaseController {
private String sessionKey;
private PluginClient client;
private TextView textView;
private WeakReference<Context> context;
protected WeakReference<Context> context;

private AtomicBoolean isRunning = new AtomicBoolean(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,91 @@
package com.sonelli.juicessh.performancemonitor.controllers;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DiskUsageController extends BaseController {

public static final String TAG = "DiskUsageController";

private String partition;

public DiskUsageController(Context context) {
super(context);
partition = "/";
}

public void choosePartition() {
final ArrayList<String> partitions = new ArrayList<String>();

final Pattern partitionNamePattern = Pattern.compile("(/[\\w/]*$)");

final Handler handler = new Handler();

final boolean wasRunning = isRunning();

handler.post(new Runnable() {
@Override
public void run() {
try {
if(isRunning())
stop();

getPluginClient().executeCommandOnSession(getSessionId(), getSessionKey(), "df | sed '1 d'", new OnSessionExecuteListener() {
@Override
public void onCompleted(int exitCode) {
switch(exitCode) {
case 0:
if(context.get() != null) {
final String[] partitionsArray = (String[]) partitions.toArray(new String[0]);
AlertDialog.Builder builder = new AlertDialog.Builder(context.get());
builder.setTitle("Choose a partition")
.setItems(partitionsArray, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
partition = partitionsArray[which];
dialogInterface.cancel();
if(wasRunning)
start();
}
})
.show();
}
break;
case 127:
default:
Log.d(TAG, "Could not load available partitions");
break;
}
}

@Override
public void onOutputLine(String line) {
Matcher partitionNameMatcher = partitionNamePattern.matcher(line);
if(partitionNameMatcher.find())
partitions.add(partitionNameMatcher.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");
}
}
});
}

@Override
Expand All @@ -28,13 +97,18 @@ public BaseController start() {
final Pattern diskUsagePattern = Pattern.compile("([0-9.]+%)"); // Heavy cpu so do out of loops.

final Handler handler = new Handler();

final String command = "df | grep ' " + partition + "$'";

Log.d(TAG, command);

handler.post(new Runnable() {
@Override
public void run() {

try {

getPluginClient().executeCommandOnSession(getSessionId(), getSessionKey(), "df | grep ' /$'", new OnSessionExecuteListener() {
getPluginClient().executeCommandOnSession(getSessionId(), getSessionKey(), command, new OnSessionExecuteListener() {
@Override
public void onCompleted(int exitCode) {
switch(exitCode){
Expand Down
1 change: 1 addition & 0 deletions Plugin/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
</LinearLayout>

<LinearLayout
android:id="@+id/disk_usage_container"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
Expand Down