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

Temperature reading #53

Open
wants to merge 2 commits 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,11 +13,13 @@
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
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 All @@ -28,6 +30,7 @@
import com.sonelli.juicessh.performancemonitor.controllers.FreeRamController;
import com.sonelli.juicessh.performancemonitor.controllers.LoadAverageController;
import com.sonelli.juicessh.performancemonitor.controllers.NetworkUsageController;
import com.sonelli.juicessh.performancemonitor.controllers.TemperatureController;
import com.sonelli.juicessh.performancemonitor.helpers.PreferenceHelper;
import com.sonelli.juicessh.performancemonitor.loaders.ConnectionListLoader;
import com.sonelli.juicessh.performancemonitor.views.AutoResizeTextView;
Expand Down Expand Up @@ -55,6 +58,8 @@ public class MainActivity extends AppCompatActivity implements ActionBar.OnNavig
private Button connectButton;
private Button disconnectButton;

private LinearLayout temperatureLayout;

private ConnectionSpinnerAdapter spinnerAdapter;

// Controllers
Expand All @@ -63,13 +68,15 @@ public class MainActivity extends AppCompatActivity implements ActionBar.OnNavig
private BaseController cpuUsageController;
private BaseController diskUsageController;
private BaseController networkUsageController;
private BaseController temperatureController;

// Text displays
private AutoResizeTextView loadAverageTextView;
private AutoResizeTextView freeRamTextView;
private AutoResizeTextView cpuUsageTextView;
private AutoResizeTextView networkUsageTextView;
private AutoResizeTextView diskUsageTextView;
private AutoResizeTextView temperatureTextView;

// State
private volatile int sessionId;
Expand Down Expand Up @@ -101,13 +108,17 @@ protected void onCreate(Bundle savedInstanceState) {
this.cpuUsageTextView = (AutoResizeTextView) findViewById(R.id.cpu_usage);
this.networkUsageTextView = (AutoResizeTextView) findViewById(R.id.network_usage);
this.diskUsageTextView = (AutoResizeTextView) findViewById(R.id.disk_usage);
this.temperatureTextView = (AutoResizeTextView) findViewById(R.id.temperature);

this.connectButton = (Button) findViewById(R.id.connect_button);
Drawable drawable = getDrawable(R.drawable.login);
if (drawable != null) {
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.2),
(int)(drawable.getIntrinsicHeight()*0.2));
}

this.temperatureLayout = (LinearLayout) findViewById(R.id.temperatureLayout);

connectButton.setCompoundDrawables(drawable, null, null, null);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -170,6 +181,14 @@ public void run() {
}
});

if (preferenceHelper.getShowTemperatureFlag()) {
loadAverageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,36);
temperatureLayout.setVisibility(View.VISIBLE);
} else {
loadAverageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,55);
temperatureLayout.setVisibility(View.GONE);
}

}

@Override
Expand Down Expand Up @@ -239,6 +258,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
// assigning the keep screen on menu the value of its saved status
PreferenceHelper preferenceHelper = new PreferenceHelper(this);
menu.findItem(R.id.keep_screen_on).setChecked(preferenceHelper.getKeepScreenOnFlag());
menu.findItem(R.id.show_temperature).setChecked(preferenceHelper.getShowTemperatureFlag());

return true;
}
Expand Down Expand Up @@ -308,6 +328,13 @@ public void onSessionStarted(final int sessionId, final String sessionKey) {
.setTextview(networkUsageTextView)
.start();

this.temperatureController = new TemperatureController(this)
.setSessionId(sessionId)
.setSessionKey(sessionKey)
.setPluginClient(client)
.setTextview(temperatureTextView)
.start();

}

@Override
Expand Down Expand Up @@ -343,11 +370,16 @@ public void onSessionFinished() {
networkUsageController.stop();
}

if(temperatureController != null){
temperatureController.stop();
}

loadAverageTextView.setText("-");
freeRamTextView.setText("-");
cpuUsageTextView.setText("-");
networkUsageTextView.setText("-");
diskUsageTextView.setText("-");
temperatureTextView.setText("-");

disconnectButton.setVisibility(View.GONE);
disconnectButton.setEnabled(false);
Expand Down Expand Up @@ -386,6 +418,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return true;

case R.id.show_temperature:
item.setChecked(!item.isChecked());
PreferenceHelper preferenceHelper1 = new PreferenceHelper(this);
preferenceHelper1.setShowTemperatureFlag(item.isChecked());
if (item.isChecked()) {
loadAverageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,36);
temperatureLayout.setVisibility(View.VISIBLE);
} else {
loadAverageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,55);
temperatureLayout.setVisibility(View.GONE);
}
return true;

case R.id.rate_plugin:
String packageName = getResources().getString(R.string.app_package);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;

public class TemperatureController extends BaseController {

public static final String TAG = "TemperatureController";

public TemperatureController(Context context) {
super(context);
}

@Override
public BaseController start() {
super.start();

final Handler handler = new Handler();
handler.post(new Runnable() {

private int numOfLines;
private double sumOfTemps;

@Override
public void run() {
numOfLines = 0;
sumOfTemps = 0;
try {
getPluginClient().executeCommandOnSession(getSessionId(),getSessionKey(), "cat /sys/class/thermal/thermal_zone*/temp", new OnSessionExecuteListener() {
@Override
public void onCompleted(int exitCode) {
Log.d(TAG, "Exit code: " + exitCode);
switch(exitCode){
case 0:
sumOfTemps/=numOfLines; //calc avarage
sumOfTemps = (double)Math.round(sumOfTemps*10)/10; //one decimal place
setText(Double.toString(sumOfTemps) + "°C");
break;
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) {
Log.d(TAG, "Line: " + line);
//input is in millidegrees C
double temp = Double.parseDouble(line)/1000;
sumOfTemps+=temp;
numOfLines++;
}

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class PreferenceHelper {

private static final String KEEP_SCREEN_ON_KEY = "keep_screen_on_key";
private static final String SHOW_TEMPERATURE= "show_temperature_key";
private Context context;

public PreferenceHelper(Context context){
Expand All @@ -26,4 +27,18 @@ public boolean getKeepScreenOnFlag() {
PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getBoolean(KEEP_SCREEN_ON_KEY, false);
}

public boolean getShowTemperatureFlag(){
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getBoolean(SHOW_TEMPERATURE, false);
}

public void setShowTemperatureFlag(boolean flag) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(SHOW_TEMPERATURE, flag);
editor.apply();
}
}
6 changes: 6 additions & 0 deletions Plugin/src/main/res/drawable/ripple_temperature.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/trans_white">
<item android:drawable="@drawable/shape_temperature" />
</ripple>
6 changes: 6 additions & 0 deletions Plugin/src/main/res/drawable/shape_temperature.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="2dp" />
<solid android:color="@color/dashboard_orange" />
</shape>
Binary file added Plugin/src/main/res/drawable/temperature.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 52 additions & 1 deletion Plugin/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<RelativeLayout
android:id="@+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
Expand Down Expand Up @@ -123,7 +124,57 @@
android:clickable="true"
android:elevation="3dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@drawable/ripple_temperature"
android:visibility="visible"
android:id="@+id/temperatureLayout"
android:gravity="center" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="bottom|center_horizontal"
android:orientation="horizontal">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/temperature" />
<TextView
android:layout_marginStart="7dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:textSize="22sp"
android:fontFamily="sans-serif"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:text="@string/temperature" />
</LinearLayout>

<com.sonelli.juicessh.performancemonitor.views.AutoResizeTextView
android:layout_height="0dp"
android:layout_weight="0.75"
android:id="@+id/temperature"
android:fontFamily="sans-serif"
android:lines="1"
android:layout_width="match_parent"
android:textSize="36sp"
android:textColor="@android:color/white"
android:text="-"
android:gravity="center_horizontal|top" />


</LinearLayout>

<LinearLayout
android:clickable="true"
android:elevation="3dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
android:layout_width="0dp"
Expand Down Expand Up @@ -160,7 +211,7 @@
android:fontFamily="sans-serif"
android:lines="1"
android:layout_width="match_parent"
android:textSize="55sp"
android:textSize="36sp"
android:textColor="@android:color/white"
android:text="-"
android:gravity="center_horizontal|top" />
Expand Down
6 changes: 6 additions & 0 deletions Plugin/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
android:checkable="true"
compat:showAsAction="never" />

<item android:id="@+id/show_temperature"
android:title="@string/show_temperature"
android:orderInCategory="100"
android:checkable="true"
compat:showAsAction="never" />

<item android:id="@+id/fork_on_github"
android:title="@string/fork_me_on_github"
android:orderInCategory="100"
Expand Down
1 change: 1 addition & 0 deletions Plugin/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="dashboard_red">#ce432c</color>
<color name="dashboard_orange">#e64a19</color>
<color name="dashboard_green">#FF37BAB3</color>
<color name="dashboard_blue">#FF04B1C5</color>
<color name="dashboard_dark_orange">#FFF2663D</color>
Expand Down
2 changes: 2 additions & 0 deletions Plugin/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
<string name="use_dark_theme">Dark Theme</string>
<string name="use_byte">Bytes</string>
<string name="plugin_permissions_request">This plugin requires access to list JuiceSSH connections, and to connect to them.</string>
<string name="temperature">Temperature</string>
<string name="show_temperature">Show temperature</string>

</resources>