code clean up

This commit is contained in:
HappyZ 2017-01-27 16:05:02 -08:00
parent 111c5b6116
commit 99d73ad3ea
9 changed files with 103 additions and 292 deletions

View File

@ -7,23 +7,25 @@ import android.telephony.TelephonyManager;
/**
* Created by yanzi on 7/10/15.
*
*/
public class Connectivity {
class Connectivity {
/**
* Get the network info
* @param context
* @return
* @param context:
* @return NetworkInfo
*/
public static NetworkInfo getNetworkInfo(Context context){
private static NetworkInfo getNetworkInfo(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
/**
* Check if there is any connectivity
* @param context
* @return
* @param context:
* @return boolean
*/
public static boolean isConnected(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
@ -32,8 +34,8 @@ public class Connectivity {
/**
* Check if there is any connectivity to a Wifi network
* @param context
* @return
* @param context:
* @return boolean
*/
public static boolean isConnectedWifi(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
@ -42,8 +44,8 @@ public class Connectivity {
/**
* Check if there is any connectivity to a mobile network
* @param context
* @return
* @param context:
* @return boolean
*/
public static boolean isConnectedMobile(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
@ -52,8 +54,8 @@ public class Connectivity {
/**
* Check if there is any connectivity to a mobile network
* @param context
* @return
* @param context:
* @return boolean
*/
public static boolean isConnectedLTE(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
@ -63,8 +65,8 @@ public class Connectivity {
/**
* Check if there is fast connectivity
* @param context
* @return
* @param context:
* @return boolean
*/
public static boolean isConnectedFast(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
@ -73,11 +75,11 @@ public class Connectivity {
/**
* Check if the connection is fast
* @param type
* @param subType
* @return
* @param type:
* @param subType:
* @return boolean
*/
public static boolean isConnectionFast(int type, int subType){
private static boolean isConnectionFast(int type, int subType){
if(type== ConnectivityManager.TYPE_WIFI){
return true;
}else if(type== ConnectivityManager.TYPE_MOBILE){

View File

@ -98,7 +98,7 @@ public class MainActivity extends Activity {
protected static boolean isRunning_RX_RawNormal = false;
/**
* Check whether a service is running
* @param serviceClass
* @param serviceClass:
* @return true/false
*/
protected boolean isServiceRunning(Class<?> serviceClass) {
@ -171,19 +171,17 @@ public class MainActivity extends Activity {
+ binaryFolderPath + "bigfile"
+ " count=1 bs=1 seek=$((2 * 1024 * 1024 * 1024 - 1)) "
+ "&& chmod 755 " + binaryFolderPath + "bigfile").waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(this, "Created a 2Gbits big file", Toast.LENGTH_LONG).show();
}
if (!missingFiles.equals("")) {
final String mFiles = missingFiles;
final String mFiles = "Failed to find following files:\n" + missingFiles;
myHandler.post(new Runnable() {
@Override
public void run() {
txt_results.setText("Failed to find following files:\n" + mFiles);
txt_results.setText(mFiles);
btn_startTransmit.setEnabled(false);
btn_startReceive.setEnabled(false);
}
@ -508,9 +506,7 @@ public class MainActivity extends Activity {
}
}
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
@ -523,9 +519,7 @@ public class MainActivity extends Activity {
Runtime.getRuntime().exec(
"su -c echo 1 > /sys/class/lcd/panel/lcd_power")
.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
// msg indicating all done
@ -585,7 +579,7 @@ public class MainActivity extends Activity {
intentSSLogger = new Intent(this, SSLogger.class);
// grab WiFi service and check if wifi is enabled
wm = (WifiManager) this.getSystemService(WIFI_SERVICE);
isUsingWifi = (wm.isWifiEnabled()) ? true : false;
isUsingWifi = wm.isWifiEnabled();
Utilities.getSelfIdentity(tcpdumpInterface, true);
// predefined selections
existedItems = new CharSequence[] {
@ -741,9 +735,7 @@ public class MainActivity extends Activity {
out.add(line);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
final CharSequence mTmp[] = out.toArray(new CharSequence[out.size()]);
@ -823,8 +815,7 @@ public class MainActivity extends Activity {
Toast.LENGTH_SHORT).show();
break;
case 3:
// TODO: wifi driver for android build not working, only for
// Samsung? Need to check.
// TODO: wifi driver for Nexus build not working
wifiDriverPID = isChecked ?
Utilities.getMyPID("dhd_dpc", true) : -1;
Toast.makeText(MainActivity.this,

View File

@ -12,7 +12,7 @@ import java.io.InputStreamReader;
* Updated on 01/27/17
*/
public class Thread_TX_CNormal implements Runnable {
class Thread_TX_CNormal implements Runnable {
private double sentBytes = 0.0;
private double duration = 0.0;
private double throughput = 0.0;

View File

@ -9,22 +9,26 @@ import java.io.InputStreamReader;
/**
* Created by yanzi on 9/18/15.
* Updated on 01/25/17
* Updated on 01/27/17
*/
public class Thread_TX_CNormalUDP implements Runnable {
class Thread_TX_CNormalUDP implements Runnable {
private double sentBytes = 0.0;
private double duration = 0.0;
private double throughput = 0.0;
@Override
public void run() {
// prevent multiple runs
if (MainActivity.isRunning_TX_NormalUDP)
return;
if (MainActivity.isVerbose)
Log.d("TX_NormalUDP", "Start TX Normal UDP");
MainActivity.isRunning_TX_NormalUDP = true;
// variables
Process proc;
String stdout;
BufferedReader stdout_buf, error_buf;
String[] commd = new String[3];
// get the right command
@ -40,6 +44,8 @@ public class Thread_TX_CNormalUDP implements Runnable {
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth));
Log.d("TX_NormalUDP", "Start TX Normal UDP");
try {
// run process
proc = Runtime.getRuntime().exec(commd);
@ -52,14 +58,13 @@ public class Thread_TX_CNormalUDP implements Runnable {
// read error
BufferedReader error_buf = new BufferedReader(new InputStreamReader(
error_buf = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
final String error = error_buf.readLine(); // only one line error
// read std out
BufferedReader stdout_buf = new BufferedReader(new InputStreamReader(
stdout_buf = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String stdout;
// get sent bytes
stdout = stdout_buf.readLine();
@ -92,44 +97,13 @@ public class Thread_TX_CNormalUDP implements Runnable {
stdout = stdout_buf.readLine();
throughput = Utilities.parseBinOutput(stdout);
}
//
// InputStream stdout = proc.getInputStream();
// byte[] buffer = new byte[20];
// int read;
// StringBuilder out = new StringBuilder();
// while(true){
// read = stdout.read(buffer);
// if(read<0){
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Failed in TX_NormalUDP\n");
// }
// });
// break;
// }
// out.append(new String(buffer, 0, read));
// if(read<20){
// break;
// }
// }
// final String mOut = out.toString().trim();
// MainActivity.reportedFinishTime = Double.parseDouble(mOut);
// if (MainActivity.isVerbose) {
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Time: " + mOut + "ms\n");
// }
// });
// }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
if (MainActivity.isVerbose)
Log.d("TX_NormalUDP", "Stop TX Normal UDP");
MainActivity.isRunning_TX_NormalUDP = false;
MainActivity.perProcPID = -1;
}

View File

@ -9,21 +9,25 @@ import java.io.InputStreamReader;
/**
* Created by yanzi on 9/19/15.
* Updated on 01/27/17
*/
public class Thread_TX_CRawNormal implements Runnable {
class Thread_TX_CRawNormal implements Runnable {
private double sentBytes = 0.0;
private double duration = 0.0;
private double throughput = 0.0;
@Override
public void run() {
// prevent multiple runs
if (MainActivity.isRunning_TX_RawNormal)
return;
if (MainActivity.isVerbose)
Log.d("TX_RawNormal", "Start TX RawNormal");
MainActivity.isRunning_TX_RawNormal = true;
// variables
Process proc;
String stdout;
BufferedReader stdout_buf, error_buf;
String[] commd = new String[3];
// get the right command
@ -38,6 +42,7 @@ public class Thread_TX_CRawNormal implements Runnable {
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth));
Log.d("TX_RawNormal", "Start TX RawNormal");
try {
// run process
proc = Runtime.getRuntime().exec(commd);
@ -49,14 +54,13 @@ public class Thread_TX_CRawNormal implements Runnable {
proc.waitFor();
// read error
BufferedReader error_buf = new BufferedReader(new InputStreamReader(
error_buf = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
final String error = error_buf.readLine(); // only one line error
// read std out
BufferedReader stdout_buf = new BufferedReader(new InputStreamReader(
stdout_buf = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String stdout;
// get sent bytes
stdout = stdout_buf.readLine();
@ -90,43 +94,12 @@ public class Thread_TX_CRawNormal implements Runnable {
throughput = Utilities.parseBinOutput(stdout);
}
// InputStream stdout = proc.getInputStream();
// byte[] buffer = new byte[20];
// int read;
// StringBuilder out = new StringBuilder();
// while(true){
// read = stdout.read(buffer);
// if(read<0){
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Failed in TX_RawNormal\n");
// }
// });
// break;
// }
// out.append(new String(buffer, 0, read));
// if(read<20){
// break;
// }
// }
// final String mOut = out.toString().trim();
// MainActivity.reportedFinishTime = Double.parseDouble(mOut);
// if (MainActivity.isVerbose) {
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Time: " + mOut + "ms\n");
// }
// });
// }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
if (MainActivity.isVerbose)
Log.d("TX_RawNormal", "Stop TX RawNormal");
MainActivity.isRunning_TX_RawNormal = false;
MainActivity.perProcPID = -1;
}

View File

@ -9,10 +9,10 @@ import java.io.InputStreamReader;
/**
* Created by yanzi on 9/18/15.
* Updated on 01/25/17
* Updated on 01/27/17
*/
public class Thread_TX_CRawSplice implements Runnable {
class Thread_TX_CRawSplice implements Runnable {
private double sentBytes = 0.0;
private double duration = 0.0;
private double throughput = 0.0;
@ -92,48 +92,12 @@ public class Thread_TX_CRawSplice implements Runnable {
throughput = Utilities.parseBinOutput(stdout);
}
// proc = Runtime.getRuntime().exec(commd);
// while (MainActivity.isLoggingPerProcPID && MainActivity.perProcPID == -1) {
// MainActivity.perProcPID = Utilities.getMyPID(MainActivity.binary_TX_RawSplice, false);
// }
// proc.waitFor();
// InputStream stdout = proc.getInputStream();
// byte[] buffer = new byte[20];
// int read;
// StringBuilder out = new StringBuilder();
// while(true){
// read = stdout.read(buffer);
// if(read<0){
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Failed in TX_RawSplice\n");
// }
// });
// break;
// }
// out.append(new String(buffer, 0, read));
// if(read<20){
// break;
// }
// }
// final String mOut = out.toString().trim();
// MainActivity.reportedFinishTime = Double.parseDouble(mOut);
// if (MainActivity.isVerbose) {
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Time: " + mOut + "ms\n");
// }
// });
// }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
if (MainActivity.isVerbose)
Log.d("TX_RawSplice", "Stop TX RawSplice");
MainActivity.isRunning_TX_RawSplice = false;
MainActivity.perProcPID = -1;
}

View File

@ -9,21 +9,25 @@ import java.io.InputStreamReader;
/**
* Created by yanzi on 9/19/15.
* Updated on 01/25/17
* Updated on 01/27/17
*/
public class Thread_TX_CSendfile implements Runnable {
class Thread_TX_CSendfile implements Runnable {
private double sentBytes = 0.0;
private double duration = 0.0;
private double throughput = 0.0;
@Override
public void run() {
// prevent multiple runs
if (MainActivity.isRunning_TX_Sendfile)
return;
if (MainActivity.isVerbose)
Log.d("TX_Sendfile", "Start TX Sendfile");
MainActivity.isRunning_TX_Sendfile = true;
// variables
Process proc;
String stdout;
BufferedReader stdout_buf, error_buf;
String[] commd = new String[3];
// get the right command
@ -39,6 +43,7 @@ public class Thread_TX_CSendfile implements Runnable {
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth));
Log.d("TX_Sendfile", "Start TX Sendfile");
try {
// run process
proc = Runtime.getRuntime().exec(commd);
@ -51,14 +56,13 @@ public class Thread_TX_CSendfile implements Runnable {
proc.waitFor();
// read error
BufferedReader error_buf = new BufferedReader(new InputStreamReader(
error_buf = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
final String error = error_buf.readLine(); // only one line error
// read std out
BufferedReader stdout_buf = new BufferedReader(new InputStreamReader(
stdout_buf = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String stdout;
// get sent bytes
stdout = stdout_buf.readLine();
@ -92,54 +96,12 @@ public class Thread_TX_CSendfile implements Runnable {
throughput = Utilities.parseBinOutput(stdout);
}
// commd[0] = "su";
// commd[1] = "-c";
// commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
// + MainActivity.binaryFolderPath + MainActivity.binary_TX_Sendfile + " "
// + MainActivity.bytes2send + " " + String.valueOf(MainActivity.currentBandwidth);
// try {
// proc = Runtime.getRuntime().exec(commd);
// while (MainActivity.isLoggingPerProcPID && MainActivity.perProcPID == -1) {
// MainActivity.perProcPID = Utilities.getMyPID(MainActivity.binary_TX_Sendfile, false);
// }
// proc.waitFor();
// InputStream stdout = proc.getInputStream();
// byte[] buffer = new byte[20];
// int read;
// StringBuilder out = new StringBuilder();
// while(true){
// read = stdout.read(buffer);
// if(read<0){
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Failed in TX_Sendfile\n");
// }
// });
// break;
// }
// out.append(new String(buffer, 0, read));
// if(read<20){
// break;
// }
// }
// final String mOut = out.toString().trim();
// MainActivity.reportedFinishTime = Double.parseDouble(mOut);
// if (MainActivity.isVerbose) {
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Time: " + mOut + "ms\n");
// }
// });
// }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
if (MainActivity.isVerbose)
Log.d("TX_Sendfile", "Stop TX Sendfile");
MainActivity.isRunning_TX_Sendfile = false;
MainActivity.perProcPID = -1;
}

View File

@ -9,23 +9,29 @@ import java.io.InputStreamReader;
/**
* Created by yanzi on 9/18/15.
* Updated on 01/25/17
* Updated on 01/27/17
*/
public class Thread_TX_CSplice implements Runnable {
class Thread_TX_CSplice implements Runnable {
private double sentBytes = 0.0;
private double duration = 0.0;
private double throughput = 0.0;
@Override
public void run() {
// prevent multiple runs
if (MainActivity.isRunning_TX_Splice)
return;
if (MainActivity.isVerbose)
Log.d("TX_Splice", "Start TX Splice");
MainActivity.isRunning_TX_Splice = true;
// variables
Process proc;
String stdout;
BufferedReader stdout_buf, error_buf;
String[] commd = new String[3];
MainActivity.isRunning_TX_Splice = true;
// get the right command
commd[0] = "su";
commd[1] = "-c";
@ -39,6 +45,7 @@ public class Thread_TX_CSplice implements Runnable {
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth));
Log.d("TX_Splice", "Start TX Splice");
try {
// run process
proc = Runtime.getRuntime().exec(commd);
@ -50,14 +57,13 @@ public class Thread_TX_CSplice implements Runnable {
proc.waitFor();
// read error
BufferedReader error_buf = new BufferedReader(new InputStreamReader(
error_buf = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
final String error = error_buf.readLine(); // only one line error
// read std out
BufferedReader stdout_buf = new BufferedReader(new InputStreamReader(
stdout_buf = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String stdout;
// get sent bytes
stdout = stdout_buf.readLine();
@ -91,54 +97,12 @@ public class Thread_TX_CSplice implements Runnable {
throughput = Utilities.parseBinOutput(stdout);
}
// commd[0] = "su";
// commd[1] = "-c";
// commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
// + MainActivity.binaryFolderPath + MainActivity.binary_TX_Splice + " "
// + MainActivity.bytes2send + " " + String.valueOf(MainActivity.currentBandwidth);
// try {
// proc = Runtime.getRuntime().exec(commd);
// while (MainActivity.isLoggingPerProcPID && MainActivity.perProcPID == -1) {
// MainActivity.perProcPID = Utilities.getMyPID(MainActivity.binary_TX_Splice, false);
// }
// proc.waitFor();
// InputStream stdout = proc.getInputStream();
// byte[] buffer = new byte[20];
// int read;
// StringBuilder out = new StringBuilder();
// while(true){
// read = stdout.read(buffer);
// if(read<0){
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Failed in TX_Splice\n");
// }
// });
// break;
// }
// out.append(new String(buffer, 0, read));
// if(read<20){
// break;
// }
// }
// final String mOut = out.toString().trim();
// MainActivity.reportedFinishTime = Double.parseDouble(mOut);
// if (MainActivity.isVerbose) {
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Time: " + mOut + "ms\n");
// }
// });
// }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
if (MainActivity.isVerbose)
Log.d("TX_Splice", "Stop TX Splice");
MainActivity.isRunning_TX_Splice = false;
MainActivity.perProcPID = -1;
}

View File

@ -12,18 +12,14 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
/**
@ -75,10 +71,7 @@ public class Utilities {
*/
static boolean canWriteOnExternalStorage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
return Environment.MEDIA_MOUNTED.equals(state);
}
/**
@ -258,9 +251,7 @@ public class Utilities {
*/
static boolean fileExist(String myFile) {
File file = new File(myFile);
if (file.exists() && file.isFile())
return true;
return false;
return file.exists() && file.isFile();
}
/**
@ -271,17 +262,7 @@ public class Utilities {
*/
static boolean dirExist(String myDirectory, boolean createIfNot) {
File file = new File(myDirectory);
if (file.exists() && file.isDirectory())
return true;
if (createIfNot) {
try{
file.mkdirs();
} catch (Exception e) {
return false;
}
return true;
}
return false;
return (file.exists() && file.isDirectory()) || (createIfNot && file.mkdirs());
}
/**
@ -417,7 +398,7 @@ public class Utilities {
* @param tmp: cpu usage
* @return long: used cpu usage
*/
static Long parseUsedCPU(String[] tmp, int offset) {
private static Long parseUsedCPU(String[] tmp, int offset) {
return (Long.parseLong(tmp[1 + offset])
+ Long.parseLong(tmp[2 + offset])
+ Long.parseLong(tmp[3 + offset])