diff options
author | Fil <fil.bergamo@riseup.net> | 2017-03-11 12:40:52 +0100 |
---|---|---|
committer | Fil <fil.bergamo@riseup.net> | 2017-03-11 12:40:52 +0100 |
commit | 49f64a40a23c74510c1270d9609d05e1e316bc67 (patch) | |
tree | ebb1462801c7503ddc712205344121c71e9698d0 | |
parent | 98a80150b7c3ee2f47ec004676b62cdcc8b87afd (diff) | |
download | packages_apps_repwifi-49f64a40a23c74510c1270d9609d05e1e316bc67.zip packages_apps_repwifi-49f64a40a23c74510c1270d9609d05e1e316bc67.tar.gz packages_apps_repwifi-49f64a40a23c74510c1270d9609d05e1e316bc67.tar.bz2 |
Enable logging + bugfix + new features
13 files changed, 474 insertions, 118 deletions
diff --git a/app/src/fil/libre/repwifiapp/Commons.java b/app/src/fil/libre/repwifiapp/Commons.java index dcf8893..202e67f 100644 --- a/app/src/fil/libre/repwifiapp/Commons.java +++ b/app/src/fil/libre/repwifiapp/Commons.java @@ -20,19 +20,28 @@ package fil.libre.repwifiapp; -import java.io.File; +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import fil.libre.repwifiapp.helpers.Engine4p2; import fil.libre.repwifiapp.helpers.Engine6p0; import fil.libre.repwifiapp.helpers.IEngine; import fil.libre.repwifiapp.helpers.NetworkManager; +import fil.libre.repwifiapp.helpers.Utils; public abstract class Commons { - + private static Context currentContext; + public Context getContext(){ + return currentContext; + } + //------------- Enviromnet Constants ---------------------------------------- + public static final int EXCOD_ROOT_DISABLED = 255; + public static final int EXCOD_ROOT_DENIED = 1; public static final String v4p2 = "4.2"; public static final String v6p0 = "6.0"; public static final String SCAN_FILE_HDR = "bssid / frequency / signal level / flags / ssid"; @@ -57,8 +66,20 @@ public abstract class Commons { public static int colorThemeDark; public static int colorThemeLight; public static int colorBlack; + + public static int getLogPriority(){ + + SharedPreferences sets = getSettings(); + return Integer.parseInt(sets.getString("debug_priority","3")); + + } + + public static SharedPreferences getSettings(){ + return PreferenceManager.getDefaultSharedPreferences(currentContext); + } //---------------------------------------------------- + //------------- Activity Interaction ----------------- public static final String EXTRA_APINFO = "ExAPInfo"; public static final String EXTRA_APINFO_ARR = "ExAPInfoArr"; @@ -85,12 +106,7 @@ public abstract class Commons { //----------------- Application Files -------------------- private static String APP_DATA_FOLDER; - public static void setAppDataFolder(String path){ - File f = new File(path); - if (f.exists()){ - APP_DATA_FOLDER = path; - } - } + public static String getNetworkStorageFile(){ if (APP_DATA_FOLDER == null){ return null; @@ -123,10 +139,28 @@ public abstract class Commons { //----------- Initialization methods --------------------------- - public static void initObjects()throws Exception{ + public static boolean init(Context context){ + + currentContext = context; - initEngine(); - initNetworkStorage(); + try { + + + colorThemeDark = currentContext.getResources().getColor(R.color.ThemeDark); + colorThemeLight = currentContext.getResources().getColor(R.color.ThemeLight); + colorBlack = currentContext.getResources().getColor(R.color.black); + APP_DATA_FOLDER = currentContext.getExternalFilesDir(null).getAbsolutePath(); + + initEngine(); + initNetworkStorage(); + + + return true; + + } catch (Exception e) { + Utils.logError("Error initializing common resources.",e); + return false; + } } private static void initEngine() throws Exception{ diff --git a/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java b/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java index a9f010a..f9e7685 100644 --- a/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java @@ -57,6 +57,7 @@ public class LongTaskActivity extends Activity { } + @Override protected void onPostExecute(Object result) { taskCompleted(result, this.REQ_CODE); } diff --git a/app/src/fil/libre/repwifiapp/activities/MainActivity.java b/app/src/fil/libre/repwifiapp/activities/MainActivity.java index aa92767..abe7dc2 100644 --- a/app/src/fil/libre/repwifiapp/activities/MainActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/MainActivity.java @@ -36,10 +36,11 @@ import fil.libre.repwifiapp.helpers.RootCommand; import fil.libre.repwifiapp.helpers.Utils; import android.os.Bundle; +import android.R.integer; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.view.Menu; import android.view.MenuItem; @@ -49,36 +50,54 @@ import android.widget.Toast; public class MainActivity extends Activity{ + private AlertDialog diag; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - setImage(); - setupSharedResources(); - - RootCommand su = new RootCommand(null); - try { - su.execute(); - } catch (Exception e) { - Utils.logError("Error while trying to get first Super User access. Aborting.",e); - finish(); - } - - try { - Commons.initObjects(); - } catch (Exception e) { - Utils.logError("Error on creating engine. Aborting.",e); + if(! Commons.init(this)){ + Utils.logDebug("Failed to initialize Commons. Aborting."); finish(); + return; } + + setImage(); - - checkConnectionStatus(); + //checkConnectionStatus(); + + } + + @Override + public void onStart(){ + super.onStart(); + Utils.logDebug("Main onStart()"); - + checkConditions(); + + ConnectionStatus status = Commons.connectionEngine.getConnectionStatus(); + if (status != null && status.isConnected()){ + Utils.logDebug("Main about to launch status activity..."); + launchStatusActivity(status); + } + + Utils.logDebug("Main onStart() returning."); + } @Override + public void onStop(){ + super.onStop(); + + if (this.diag != null){ + this.diag.dismiss(); + } + + } + + + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); @@ -91,23 +110,19 @@ public class MainActivity extends Activity{ switch (item.getItemId()) { case R.id.menu_credits: launchCreditsActivity(); - return true; + break; + + case R.id.menu_config: + launchSettingsActivity(); + break; default: - return true; + break; } + + return true; } - @Override - public void onRestart(){ - super.onRestart(); - - ConnectionStatus status = Commons.connectionEngine.getConnectionStatus(); - if (status != null && status.isConnected()){ - launchStatusActivity(status); - } - - } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent){ @@ -134,7 +149,7 @@ public class MainActivity extends Activity{ break; case RequestCode.SELECT_CONN: - boolean rescan = (boolean)intent.getExtras().getBoolean(Commons.EXTRA_RESCAN); + boolean rescan = intent.getExtras().getBoolean(Commons.EXTRA_RESCAN); handleResultSelect(i, rescan); break; @@ -187,13 +202,7 @@ public class MainActivity extends Activity{ } - private void setupSharedResources(){ - Commons.colorThemeDark = getResources().getColor(R.color.ThemeDark); - Commons.colorThemeLight = getResources().getColor(R.color.ThemeLight); - Commons.colorBlack = getResources().getColor(R.color.black); - Commons.setAppDataFolder(getExternalFilesDir(null).getAbsolutePath()); - } - + private void handleResultSelect(AccessPointInfo i, boolean rescan){ if (rescan){ @@ -304,11 +313,15 @@ public class MainActivity extends Activity{ } private void launchCreditsActivity(){ - Intent intent = new Intent(this, CreditsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivityForResult(intent, RequestCode.NONE); - + } + + private void launchSettingsActivity() { + Intent intent = new Intent(this, SettingsActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); + startActivity(intent); } private void deleteNetwork(AccessPointInfo info){ @@ -345,8 +358,74 @@ public class MainActivity extends Activity{ } + private boolean checkConditions(){ + return (checkRootEnabled() && checkInterface()); + } + + private boolean checkInterface(){ + + boolean res = Commons.connectionEngine.isInterfaceAvailable(Commons.INTERFACE_NAME); + + if(res == false ){ + String msg = getResources().getString(R.string.msg_interface_not_found); + showMessage(msg); + } + + return res; + + } + + private boolean checkRootEnabled(){ + + boolean result = false; + String msg = "Unknown Root error"; + RootCommand su = new RootCommand(null); + + int excode = -1; + + try { + excode = su.execute(); + } catch (Exception e) { + Utils.logError("Error while trying to get first Super User access.",e); + excode = -1; + result = false; + } + + switch (excode) { + case 0: + result = true; + break; + + case Commons.EXCOD_ROOT_DENIED: + result = false; + msg = getResources().getString(R.string.msg_root_denied); + break; + + case Commons.EXCOD_ROOT_DISABLED: + result = false; + msg = getResources().getString(R.string.msg_root_disabled); + break; + + default: + result = false; + msg = "Unknown Root error.\nExit code " + excode; + break; + } + + if (!result){ + showMessage(msg); + } + + return result; + + } + private void doScan(){ + if (!checkConditions()){ + return; + } + Intent intent = new Intent(this, LongTaskActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.NETWORKS_GET); @@ -374,5 +453,22 @@ public class MainActivity extends Activity{ } } + + private void showMessage(String msg){ + + AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); + dlgAlert.setMessage(msg); + dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { + return; + } + }); + + dlgAlert.setCancelable(false); + this.diag = dlgAlert.create(); + this.diag.show(); + + } } diff --git a/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java b/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java index e43eee2..f0aa26d 100644 --- a/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java @@ -102,16 +102,21 @@ public class NetworkDetailsActivity extends Activity implements OnCheckedChangeL public void btnDeleteClick(View v){ + String msg = getResources().getString(R.string.msg_confirm_delete_network); + String yes = getResources().getString(R.string.yes); + String no = getResources().getString(R.string.no); AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); - dlgAlert.setMessage("Are you sure you want to delete this network?"); - dlgAlert.setPositiveButton("Yes",new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { + dlgAlert.setMessage(msg); + dlgAlert.setPositiveButton(yes,new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { returnResult(true); } }); - dlgAlert.setNegativeButton("NO",new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { + dlgAlert.setNegativeButton(no,new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { //nothing } }); diff --git a/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java b/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java index 275ce08..6731846 100644 --- a/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java @@ -192,7 +192,7 @@ public class SelectNetworkActivity extends Activity implements OnClickListener { private void addButtonForNetwork(AccessPointInfo info){ TableLayout s = (TableLayout)findViewById(R.id.table_networks); - TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT); + TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); TableRow row = new TableRow(this); TableRow.LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); rowParams.gravity = Gravity.FILL_HORIZONTAL; diff --git a/app/src/fil/libre/repwifiapp/activities/SettingsActivity.java b/app/src/fil/libre/repwifiapp/activities/SettingsActivity.java new file mode 100644 index 0000000..90629e3 --- /dev/null +++ b/app/src/fil/libre/repwifiapp/activities/SettingsActivity.java @@ -0,0 +1,35 @@ +package fil.libre.repwifiapp.activities; + +import java.util.List; + +import fil.libre.repwifiapp.R; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.preference.PreferenceFragment; + + +public class SettingsActivity extends PreferenceActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + } + + @Override + public void onBuildHeaders(List<Header> target) { + loadHeadersFromResource(R.xml.settings_headers, target); + } + + + public static class RepWifiFragment extends PreferenceFragment { + + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.settings); + } + + } + +} diff --git a/app/src/fil/libre/repwifiapp/helpers/Engine.java b/app/src/fil/libre/repwifiapp/helpers/Engine.java index cdddb0b..5a2f2b2 100644 --- a/app/src/fil/libre/repwifiapp/helpers/Engine.java +++ b/app/src/fil/libre/repwifiapp/helpers/Engine.java @@ -20,11 +20,15 @@ package fil.libre.repwifiapp.helpers; +import java.util.ArrayList; import fil.libre.repwifiapp.Commons; public abstract class Engine implements IEngine{ + public static final String DNS1 = "193.183.98.154"; + public static final String DNS2 = "87.98.175.85"; + protected String getCmdWpaSup(){ return "wpa_supplicant -B -dd -i" + Commons.INTERFACE_NAME + " -C\"" +Commons.SOCKET_DIR + "\" -P\"" + Commons.PID_FILE + "\""; } @@ -34,10 +38,7 @@ public abstract class Engine implements IEngine{ } protected abstract String getCmdWpaStart(); - - public static final String DNS1 = "193.183.98.154"; - public static final String DNS2 = "87.98.175.85"; - + public boolean deleteFileIfExists(String filePath){ if (filePath == null){ @@ -54,16 +55,21 @@ public abstract class Engine implements IEngine{ return false; } + //needs root (it only gets used by the 4p2 engine, working in /data/misc/wifi) return executeRootCmd("if [ -e \""+ filePath + "\" ]; then rm \"" + filePath + "\"; fi"); + } public boolean chmodFile(String filePath, String mod){ + //needs root (chmod) return executeRootCmd("chmod " + mod + " \"" + filePath + "\""); } @Override public boolean killPreviousConnections() { + //needs root (for killall) + Utils.logDebug("killing wpa_supplicant..:"); if (executeRootCmd("killall -SIGINT wpa_supplicant")){ Utils.logDebug("Killed wpa_supplicant"); @@ -86,6 +92,8 @@ public abstract class Engine implements IEngine{ @Override public boolean clearWorkingDir(){ + //needs root (to work within /data/misc/wifi) + Utils.logDebug("clearWorkingDir():"); if (executeRootCmd("rm -r " + Commons.SOCKET_DIR)){ @@ -122,6 +130,7 @@ public abstract class Engine implements IEngine{ Utils.logDebug("startWpaSupplicant():"); + //needs root (for wpa_supplicant) if (executeRootCmd(getCmdWpaSup())){ return true; }else{ @@ -138,6 +147,7 @@ public abstract class Engine implements IEngine{ killPreviousConnections(); + //is it really necessary??? --- Fil if (! clearWorkingDir()){ Utils.logError("Failed clearing dir"); return null; @@ -163,8 +173,8 @@ public abstract class Engine implements IEngine{ return null; } - //chmod 666 scan_file to make it readable - if (!chmodFile(Commons.getScanFile(), "666")){ + //chmod 664 scan_file to make it readable + if (!chmodFile(Commons.getScanFile(), "664")){ Utils.logError("failed chmodding scan_file"); return null; } @@ -174,7 +184,6 @@ public abstract class Engine implements IEngine{ Utils.logError("Unable to parse scan file into AccessPointInfo array"); } - return a; } @@ -185,6 +194,8 @@ public abstract class Engine implements IEngine{ @Override public boolean disconnect(){ + //needs root (for wpa_cli) + if (! isWpaSupplicantRunning()){ return true; } @@ -248,16 +259,99 @@ public abstract class Engine implements IEngine{ } + @Override + public boolean isInterfaceAvailable(String ifaceName){ + + String[]ifaces = getAvailableInterfaces(); + if(ifaces == null || ifaces.length == 0){ + return false; + } + + for(String name : ifaces){ + if (name.equals(ifaceName)){ + return true; + } + } + + return false; + } + + @Override + public String[] getAvailableInterfaces(){ + + try { + + ShellCommand cmd = new ShellCommand("ip link"); + if (cmd.execute() == 0){ + + String out = cmd.getOutput(); + if (out == null || out.contains("\n") == false){ + Utils.logDebug("No out from ip link"); + return null; + } + + ArrayList<String> list = new ArrayList<String>(); + + String[] lines = out.split("\n"); + for (String l : lines){ + + String[] fields = l.split(":"); + if (fields.length != 3){ + continue; + } + + String interfName = fields[1].trim(); + list.add(interfName); + + } + + String[] retArr = new String[list.size()]; + retArr = list.toArray(retArr); + + return retArr; + + } + else{ + return null; + } + + } catch (Exception e) { + Utils.logError("Error while querying ip link", e); + return null; + } + + } + public boolean runDhcpcd(){ + //needs root return executeRootCmd("dhcpcd " + Commons.INTERFACE_NAME); } public boolean interfaceUp(){ + //needs root (tested) return executeRootCmd("ifconfig " + Commons.INTERFACE_NAME + " up"); } + protected boolean executeCmd(String cmd){ + + try { + + ShellCommand c = new ShellCommand(cmd); + if ( c.execute() == 0){ + return true; + }else { + return false; + } + + } catch (Exception e) { + Utils.logError("Error executing \"" + cmd + "\"",e); + return false; + } + + } + protected boolean executeRootCmd(String cmd){ try { @@ -306,12 +400,14 @@ public abstract class Engine implements IEngine{ protected boolean scanNetworks(){ + //needs root (for wpa_supplicant and wpa_cli) return executeRootCmd("bash " + Commons.getScriptScan()); } protected boolean getScanResults(){ + //needs root (for wpa_supplicant and wpa_cli) return executeRootCmd("bash " + Commons.getScriptScanRes()); } @@ -336,11 +432,6 @@ public abstract class Engine implements IEngine{ "sleep 1s\n"; - //Try to create and chmod script scan - /* executeRootCmd("echo > " + Commons.getSCRIPT_SCAN()); - chmodFile(Commons.getSCRIPT_SCAN(), "666");*/ - - if (! Utils.writeFile(Commons.getScriptScan(),scan,true) ){ Exception e = Utils.getLastException(); @@ -351,9 +442,6 @@ public abstract class Engine implements IEngine{ return false; } - //Try to create and chmod script scanres - /*executeRootCmd("echo > " + Commons.getSCRIPT_SCANRES()); - chmodFile(Commons.getSCRIPT_SCANRES(), "666");*/ if (! Utils.writeFile(Commons.getScriptScanRes(),scanRes,true) ){ @@ -376,22 +464,5 @@ public abstract class Engine implements IEngine{ } - /*protected boolean createDhcpcdScritp(){ - - String scriptDhcp = "dhcpcd "+ Commons.INTERFACE_NAME + "\n" + - "sleep 3s\n"; - if (! Utils.writeFile(Commons.getScriptDhcpcd(),scriptDhcp,true) ){ - - Exception e = Utils.getLastException(); - if (e != null){ - Utils.logError("Error while writing dhcpcd script.",e); - } - - return false; - } - - return true; - - }*/ } diff --git a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java b/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java index 24096d2..9cbfeaf 100644 --- a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java +++ b/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java @@ -180,6 +180,7 @@ public class Engine6p0 extends Engine{ } private boolean destroyNetwork(){ + //needs root (tested) return executeRootCmd("ndc network destroy 1"); } @@ -187,6 +188,7 @@ public class Engine6p0 extends Engine{ try { + //needs root (wpa_cli) RootCommand su = new RootCommand(getCmdWpaCli() + " set_network " + networkID + " ssid '\"" + ssid + "\"'" ); if (su.execute() == 0){ String out = su.getOutput(); @@ -211,6 +213,8 @@ public class Engine6p0 extends Engine{ try { + //needs root (wpa_cli) + String cmdSetPass = null; if (info.needsPassword()){ cmdSetPass = getCmdWpaCli() + " set_network " + networkID + " psk '\"" + info.getPassword() + "\"'"; @@ -244,6 +248,7 @@ public class Engine6p0 extends Engine{ try { + //needs root (wpa_cli) RootCommand su = new RootCommand(getCmdWpaCli() + " select_network " + networkID); if (su.execute() == 0){ String out = su.getOutput(); @@ -268,6 +273,8 @@ public class Engine6p0 extends Engine{ try { + //needs root (wpa_cli) + RootCommand su = new RootCommand(getCmdWpaCli() + " enable_network " + networkID); if (su.execute() == 0){ String out = su.getOutput(); @@ -292,6 +299,8 @@ public class Engine6p0 extends Engine{ try { + //needs root (wpa_cli) + RootCommand su = new RootCommand(getCmdWpaCli() + " reassociate"); if (su.execute() == 0){ String out = su.getOutput(); @@ -316,14 +325,15 @@ public class Engine6p0 extends Engine{ try { - RootCommand su = new RootCommand("ip route show dev " + Commons.INTERFACE_NAME); - if (su.execute() != 0){ + //doesn't need root (tested) + ShellCommand cmd = new ShellCommand("ip route show dev " + Commons.INTERFACE_NAME); + if (cmd.execute() != 0){ Utils.logDebug("command failed show route"); return null; } //read command output - String out = su.getOutput(); + String out = cmd.getOutput(); if (out == null){ return null; } @@ -354,6 +364,7 @@ public class Engine6p0 extends Engine{ } private boolean clearAddrs(){ + //needs root (tested) return executeRootCmd("ndc interface clearaddrs " + Commons.INTERFACE_NAME); } diff --git a/app/src/fil/libre/repwifiapp/helpers/IEngine.java b/app/src/fil/libre/repwifiapp/helpers/IEngine.java index 67ffb86..1e33f1c 100644 --- a/app/src/fil/libre/repwifiapp/helpers/IEngine.java +++ b/app/src/fil/libre/repwifiapp/helpers/IEngine.java @@ -37,5 +37,8 @@ public interface IEngine { public ConnectionStatus getConnectionStatus(); - + public boolean isInterfaceAvailable(String ifaceName); + + public String[] getAvailableInterfaces(); + } diff --git a/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java b/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java index bc59862..33acbb7 100644 --- a/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java +++ b/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java @@ -109,32 +109,40 @@ public class NetworkManager { } } + + + if (save){ + //add the updated info to the storage + info.setLastTimeUsed(System.currentTimeMillis()); + newlist.add(info); + } for(AccessPointInfo old : existingNets){ if (old == null){ + //error while loading from file. skip. continue; } - - if (old.getBSSID().equals(info.getBSSID()) && old.getSSID().equals(info.getSSID())){ - - //found previous entry for this network, - //if the call is for saving, overwrite the entry with the new one, - //else omit the line, to remove network from the saved list. - if (save){ - info.setLastTimeUsed(System.currentTimeMillis()); - newlist.add(info); - } - - }else{ - //other network, keep it in the file - // only if it's not older than the max age for a network - if (! info.isOlderThan(NET_MAX_AGE)){ - newlist.add(old); - } + + // keep network only if it's not older than the max age for a network + else if (old.isOlderThan(NET_MAX_AGE)){ + //skip it + continue; } - + + else if (old.getBSSID().equals(info.getBSSID()) && old.getSSID().equals(info.getSSID())){ + //found previously saved entry for the same network we are managing + //skip it + continue; + } + + else{ + //old network info that can be kept in the storage + newlist.add(old); + } + } + AccessPointInfo[] newContents = new AccessPointInfo[newlist.size()]; newContents = newlist.toArray(newContents); @@ -240,7 +248,7 @@ public class NetworkManager { } String[] lines = Utils.readFileLines(_knownNetworksFile); - if (lines.length == 0){ + if (lines == null || lines.length == 0){ return null; } diff --git a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java index 3da4ae0..bcb6d0f 100644 --- a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java +++ b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java @@ -51,7 +51,7 @@ public class RootCommand { if ( this._cmdTxt != null ){ - Utils.logDebug("EXEC: " + this._cmdTxt); + Utils.logDebug("SU:EXEC: " + this._cmdTxt); this._cmdTxt += " > " + Commons.getTempOutFile(); diff --git a/app/src/fil/libre/repwifiapp/helpers/ShellCommand.java b/app/src/fil/libre/repwifiapp/helpers/ShellCommand.java new file mode 100644 index 0000000..e2004a7 --- /dev/null +++ b/app/src/fil/libre/repwifiapp/helpers/ShellCommand.java @@ -0,0 +1,85 @@ +package fil.libre.repwifiapp.helpers; + +import java.io.IOException; +import java.io.InputStream; + +public class ShellCommand { + + private String _cmdOut = ""; + private String _cmdTxt = ""; + + public ShellCommand(String commandText){ + this._cmdTxt = commandText; + } + + + public int execute() throws Exception{ + + if ( this._cmdTxt == null ){ + return -9; + } + + Utils.logDebug("EXEC: " + this._cmdTxt); + + Process cmd = Runtime.getRuntime().exec(this._cmdTxt); + + InputStream os = cmd.getInputStream(); + InputStream es = cmd.getErrorStream(); + + StringBuilder sb = new StringBuilder(); + + sb.append(getStringFromStream(es)); + sb.append(getStringFromStream(os)); + + int res = cmd.waitFor(); + + //re-read the output, in case it was empty when first tried + sb.append(getStringFromStream(es)); + sb.append(getStringFromStream(os)); + + this._cmdOut = sb.toString(); + + Utils.logDebug("EXITCODE: " + res); + Utils.logDebug("OUT: " + getOutput()); + + return res; + + } + + private String getStringFromStream(InputStream s) throws IOException{ + + StringBuilder sb = new StringBuilder(); + while ( (s.available() > 0) ) { + int b = s.read(); + if (b>=0){ + sb.append((char)b); + }else{ + break; + } + } + + return sb.toString(); + + } + + + public String getOutput(){ + + return this._cmdOut; + + /*String[] lastOut = Utils.readFileLines(Commons.getTempOutFile()); + if (lastOut == null){ + return this._cmdOut; + } + + String fout = ""; + + for (String s : lastOut){ + fout += s + "\n"; + } + + return fout;*/ + + } + +} diff --git a/app/src/fil/libre/repwifiapp/helpers/Utils.java b/app/src/fil/libre/repwifiapp/helpers/Utils.java index 2e5fa27..bb3c682 100644 --- a/app/src/fil/libre/repwifiapp/helpers/Utils.java +++ b/app/src/fil/libre/repwifiapp/helpers/Utils.java @@ -27,15 +27,21 @@ import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import fil.libre.repwifiapp.Commons; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; import android.util.Log; public class Utils { private static final long MILLIS_IN_DAY = 86400000; - public static final int logLevel = 1; - + public static final String APP_NAME = "RepWifi"; private static Exception _lastException = null; @@ -58,13 +64,14 @@ public class Utils { public static void logDebug(String msg, int level){ - if (level < logLevel){ + if (level < Commons.getLogPriority()){ return; } Log.d(APP_NAME,msg); } + public static boolean writeFile(String filePath, String text, boolean overwrite){ |