Commit 481645c0 authored by Drahomír Karchňák's avatar Drahomír Karchňák

Request new settings on preferences change. Fixed CPU and Mem usage on Android...

Request new settings on preferences change. Fixed CPU and Mem usage on Android O. (Disabled them on Android O, so it's not spamming exceptions all over the place, because u cant ready /proc/* files anymore.)
parent ad694a36
......@@ -9,6 +9,11 @@
android:id="@+id/server_select_list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:smoothScrollbar="false"
android:persistentDrawingCache="scrolling"
android:cacheColorHint="@android:color/transparent"
android:layout_above="@+id/server_select_auto_button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
......
......@@ -15,6 +15,9 @@
*******************************************************************************/
package at.alladin.rmbt.android.impl;
import android.util.Log;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -25,7 +28,7 @@ import cz.nic.netmetr.R;
public class CpuStatAndroidImpl extends CpuStat {
public static enum CpuMemClassificationEnum {
public enum CpuMemClassificationEnum {
LOW(0, 50, R.color.cpu_classification_green),
MID(50, 75, R.color.cpu_classification_yellow),
......@@ -36,7 +39,7 @@ public class CpuStatAndroidImpl extends CpuStat {
protected float max;
protected int resId;
private CpuMemClassificationEnum(float min, float max, int resId) {
CpuMemClassificationEnum(float min, float max, int resId) {
this.min = min;
this.max = max;
this.resId = resId;
......@@ -55,9 +58,7 @@ public class CpuStatAndroidImpl extends CpuStat {
}
/**
*
* @param bitPerSecond
* @return
* @param value - bitPerSecond
*/
public static CpuMemClassificationEnum classify(float value) {
for (CpuMemClassificationEnum e : CpuMemClassificationEnum.values()) {
......@@ -75,12 +76,30 @@ public class CpuStatAndroidImpl extends CpuStat {
private final static Pattern CPU_PATTERN = Pattern.compile("cpu[^0-9]([\\s0-9]*)");
private final static Pattern CPU_CORE_PATTERN = Pattern.compile("cpu([0-9]+)([\\s0-9]*)");
private static boolean canReadProcFiles = true;
public CpuUsage getCurrentCpuUsage(boolean getByCore) {
CpuUsage cpuUsage = new CpuUsage();
String stat = ToolUtils.readFromProc(PROC_PATH + "stat");
if (!canReadProcFiles) {
cpuUsage.getCoreUsageList().add(new CoreUsage(0, 0, 0, 0, 0, 0, 0, 0));
return cpuUsage;
}
String stat = null;
try {
stat = ToolUtils.readFromProc(PROC_PATH + "stat");
} catch (IOException e) {
//Welcome to Android Oreo...
e.printStackTrace();
Log.i(getClass().getSimpleName(), "Can't read /proc/* files. We are probably running Android O.");
canReadProcFiles = false;
cpuUsage.getCoreUsageList().add(new CoreUsage(0, 0, 0, 0, 0 ,0, 0, 0));
return cpuUsage;
}
if (getByCore) {
Matcher m = CPU_CORE_PATTERN.matcher(stat);
while(m.find()) {
......
......@@ -15,6 +15,9 @@
*******************************************************************************/
package at.alladin.rmbt.android.impl;
import android.util.Log;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
......@@ -60,7 +63,17 @@ public class MemInfoAndroidImpl implements MemInfo {
public synchronized void update() {
memoryMap = new HashMap<String, Long>();
String memInfo = ToolUtils.readFromProc("/proc/meminfo");
String memInfo = null;
try {
memInfo = ToolUtils.readFromProc("/proc/meminfo");
} catch (IOException e) {
//Welcome to Android Oreo...
e.printStackTrace();
Log.i(getClass().getSimpleName(), "Can't read /proc/* files. We are probably running Android O.");
return;
}
Matcher m = MEMINFO_PATTERN.matcher(memInfo);
while (m.find()) {
String type = m.group(1);
......
......@@ -141,10 +141,11 @@ import cz.nic.netmetr.R;
* @author
*
*/
public class RMBTMainActivity extends Activity implements MapProperties, RMBTLoopService.RMBTLoopServiceListener
{
public class RMBTMainActivity extends Activity implements MapProperties, RMBTLoopService.RMBTLoopServiceListener {
private static final String BUG_REPORT_MANIFEST_PROPERTY = "cz.nic.labs.rmbt.android.test.RMBTBugReport";
protected static final int REQUEST_PREFERENCES_ACTIVITY_RESULT = 1;
/**
*
*/
......@@ -686,7 +687,24 @@ public class RMBTMainActivity extends Activity implements MapProperties, RMBTLoo
unregisterReceiver(mNetworkStateIntentReceiver);
unregisterReceiver(testFinishedReciever);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_PREFERENCES_ACTIVITY_RESULT) {
//Has data and re-init key is true
if( data != null &&
data.hasExtra(RMBTPreferenceActivity.RESULT_REINIT_APP_EXTRA_KEY) &&
data.getBooleanExtra(RMBTPreferenceActivity.RESULT_REINIT_APP_EXTRA_KEY, false))
{
Log.i(getClass().getSimpleName(), "Preferences activity requested re-init of application.");
//Force check settings, show loading dialog and wait for result!
checkSettings(true, null);
waitForSettings(false, false, true);
}
}
}
public void setOverlayVisibility(boolean isVisible) {
final LinearLayout overlay = (LinearLayout) findViewById(R.id.overlay);
......@@ -1329,7 +1347,7 @@ public class RMBTMainActivity extends Activity implements MapProperties, RMBTLoo
*
*/
public void showSettings() {
startActivity(new Intent(this, RMBTPreferenceActivity.class));
startActivityForResult(new Intent(this, RMBTPreferenceActivity.class), REQUEST_PREFERENCES_ACTIVITY_RESULT);
}
/**
......
......@@ -38,8 +38,9 @@ import at.alladin.rmbt.android.util.ConfigHelper;
import at.alladin.rmbt.android.util.Server;
import cz.nic.netmetr.R;
public class RMBTPreferenceActivity extends PreferenceActivity
{
public class RMBTPreferenceActivity extends PreferenceActivity {
public static final String RESULT_REINIT_APP_EXTRA_KEY = "reinit_app";
protected static final int REQUEST_NDT_CHECK = 1;
protected static final int REQUEST_IC_CHECK = 2;
protected static final int REQUEST_SERVER_SELECT = 3;
......@@ -47,7 +48,6 @@ public class RMBTPreferenceActivity extends PreferenceActivity
protected Method mLoadHeaders = null;
protected Method mHasHeaders = null;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
......@@ -222,6 +222,10 @@ public class RMBTPreferenceActivity extends PreferenceActivity
else if (requestCode == REQUEST_SERVER_SELECT) {
refreshServerSelectionState(false);
}
Intent i = new Intent();
i.putExtra(RESULT_REINIT_APP_EXTRA_KEY, true);
setResult(RESULT_OK, i);
}
private void refreshServerSelectionState(boolean setClickListener) {
......
......@@ -84,7 +84,8 @@ public class CheckSettingsTask extends AsyncTask<Void, Void, JSONArray>
JSONArray resultList = null;
serverConn = new ControlServerConnection(activity.getApplicationContext());
Log.i(getClass().getSimpleName(), "Requesting new settings!");
resultList = serverConn.requestSettings();
return resultList;
......
......@@ -583,9 +583,10 @@ public class RegistrationResource extends ServerResource
final String sql = "SELECT * FROM test_server"
+ " WHERE active"
+ " AND (server_type = ?)"
+ " AND ( ? = ANY (countries) OR 'any' = ANY (countries))"
+ " ORDER BY 'any' != ANY (countries) DESC,"
+ " AND server_type = ?"
+ " AND (country = ? OR country = 'any' OR country IS NULL)"
+ " ORDER BY"
+ " (country != 'any' AND country IS NOT NULL) DESC,"
+ " priority,"
+ " random() * weight DESC"
+ " LIMIT 1";
......
......@@ -18,18 +18,18 @@ package at.alladin.rmbt.util.tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class ToolUtils {
/**
*
* @param path
*
* @param file
* @return
*/
public static String readFromProc(String file) {
public static String readFromProc(String file) throws IOException {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
......@@ -43,9 +43,6 @@ public class ToolUtils {
sb.append("\n");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (br != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment