Commit 1ebdcb2e 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 c5a9796f
......@@ -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;
......@@ -224,7 +225,7 @@ public class CheckSettingsTask extends AsyncTask<Void, Void, JSONArray>
// Servers
final Set<String> serverSet;
final JSONArray servers = resultListItem.getJSONArray("servers");
final JSONArray servers = resultListItem.optJSONArray("servers");
if (servers == null || servers.length() == 0) {
Log.i(getClass().getSimpleName(), "No servers found in the response.");
......
......@@ -581,8 +581,6 @@ public class ControlServerConnection
requestData.put("time", time);
requestData.put("client_secret", ClientSecretHelper.prepareClientSecret(getUUID(), clientName, clientVersionName, clientVersionCode, time, BuildConfig.CLIENT_SECRET));
requestData.put("user_server_selection", true); //TODO - Is Developer Options Enabled
final int tcAcceptedVersion = ConfigHelper.getTCAcceptedVersion(context);
requestData.put("terms_and_conditions_accepted_version", tcAcceptedVersion);
if (tcAcceptedVersion > 0) // for server backward compatibility
......
......@@ -482,7 +482,7 @@ public class InformationCollector
Server server;
if((server = ConfigHelper.getUserSelectedServer(ctx)) != null) {
object.put("user_server_selection", true);
object.put("prefer_server", server);
object.put("prefer_server", server.getUuid());
}
}
......
......@@ -55,7 +55,7 @@ public class RegistrationResource extends ServerResource
String name;
int port;
String address;
String key;
String secret;
String type;
}
......@@ -448,7 +448,7 @@ public class RegistrationResource extends ServerResource
else
{
final String data = testUuid + "_" + testSlot;
final String hmac = Helperfunctions.calculateHMAC(server.key, data);
final String hmac = Helperfunctions.calculateHMAC(server.secret, data);
if (hmac.length() == 0)
errorList.addError("ERROR_TEST_TOKEN");
final String token = data + "_" + hmac;
......@@ -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";
......@@ -633,7 +634,7 @@ public class RegistrationResource extends ServerResource
result.address = address;
result.port = rs.getInt(ssl ? "port_ssl" : "port");
result.name = rs.getString("name") + " (" + rs.getString("city") + ")";
result.key = rs.getString("key");
result.secret = rs.getString("secret");
result.type = rs.getString("server_type");
return result;
......
......@@ -630,7 +630,7 @@ public class ResultResource extends ServerResource
private String selectSecretKeyByServerToken(String serverToken) {
final String sql =
"SELECT ts.key FROM test_server ts, test t " +
"SELECT ts.secret FROM test_server ts, test t " +
"WHERE t.server_id = ts.uid " +
"and t.token = ?";
......@@ -640,11 +640,11 @@ public class ResultResource extends ServerResource
try (ResultSet rs = ps.executeQuery()) {
if(!rs.next()) {
System.out.println("Couldn't find key for serverToken: " + serverToken);
System.out.println("Couldn't find secret for serverToken: " + serverToken);
return null;
}
String secret = rs.getString("key");
String secret = rs.getString("secret");
return secret;
}
}
......
......@@ -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