diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/activities')
10 files changed, 1274 insertions, 1039 deletions
diff --git a/app/src/fil/libre/repwifiapp/activities/CreditsActivity.java b/app/src/fil/libre/repwifiapp/activities/CreditsActivity.java index 4a0cb4c..80c15e6 100644 --- a/app/src/fil/libre/repwifiapp/activities/CreditsActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/CreditsActivity.java @@ -21,7 +21,6 @@ package fil.libre.repwifiapp.activities; import fil.libre.repwifiapp.R; - import android.os.Bundle; import android.app.Activity; import android.text.Html; @@ -32,25 +31,24 @@ import android.widget.TextView; public class CreditsActivity extends Activity { - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_credits); - - String content = getResources().getString(R.string.credit_text); - TextView tv = (TextView)findViewById(R.id.txt_credits); - tv.setMovementMethod(new ScrollingMovementMethod()); - tv.setText(Html.fromHtml(content)); - tv.setMovementMethod(LinkMovementMethod.getInstance()); - - } - - @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_credits, menu); - return true; - } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_credits); + + String content = getResources().getString(R.string.credit_text); + TextView tv = (TextView) findViewById(R.id.txt_credits); + tv.setMovementMethod(new ScrollingMovementMethod()); + tv.setText(Html.fromHtml(content)); + tv.setMovementMethod(LinkMovementMethod.getInstance()); + + } + + @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_credits, menu); + return true; + } } diff --git a/app/src/fil/libre/repwifiapp/activities/InputPasswordActivity.java b/app/src/fil/libre/repwifiapp/activities/InputPasswordActivity.java index 239c082..6f04d1c 100644 --- a/app/src/fil/libre/repwifiapp/activities/InputPasswordActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/InputPasswordActivity.java @@ -20,11 +20,10 @@ package fil.libre.repwifiapp.activities; - +import fil.libre.repwifiapp.ActivityLauncher; import fil.libre.repwifiapp.Commons; import fil.libre.repwifiapp.R; import fil.libre.repwifiapp.helpers.AccessPointInfo; - import android.app.Activity; import android.content.Intent; import android.os.Bundle; @@ -37,79 +36,79 @@ import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.TextView; +public class InputPasswordActivity extends Activity implements OnCheckedChangeListener { + + AccessPointInfo apinfo = null; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + + setContentView(R.layout.activity_input_password); + + CheckBox c = (CheckBox) findViewById(R.id.chk_show_pass); + c.setOnCheckedChangeListener(this); + + setTitle("Input password"); + + TextView v = (TextView) findViewById(R.id.txt_insert_pass); + + // get the network to set password to: + this.apinfo = (AccessPointInfo) getIntent().getSerializableExtra( + ActivityLauncher.EXTRA_APINFO); + v.append(" " + apinfo.getSsid()); + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // disable menu + return true; + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + + if (buttonView == findViewById(R.id.chk_show_pass)) { + chkShowPassChanged(); + } + + } + + public void onBtnNextClick(View v) { + + EditText txpass = (EditText) findViewById(R.id.txt_password); + String pass = txpass.getText().toString(); + + if (pass.length() == 0) { + Commons.showMessage("Password can't be empty!", this); + } + + this.apinfo.setPassword(pass); + + Intent intent = new Intent(); + intent.putExtra(ActivityLauncher.EXTRA_APINFO, this.apinfo); + setResult(RESULT_OK, intent); + + finish(); + + } + + public void chkShowPassChanged() { + + CheckBox c = (CheckBox) findViewById(R.id.chk_show_pass); + EditText txtPass = (EditText) findViewById(R.id.txt_password); + + if (c.isChecked()) { + txtPass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); + txtPass.setSelection(txtPass.getText().length()); + + } else { + txtPass.setInputType(129); + txtPass.setSelection(txtPass.getText().length()); + + } -public class InputPasswordActivity extends Activity implements OnCheckedChangeListener{ - - AccessPointInfo apinfo = null; - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - - setContentView(R.layout.activity_input_password); - - CheckBox c = (CheckBox)findViewById(R.id.chk_show_pass); - c.setOnCheckedChangeListener(this); - - setTitle("Input password"); - - TextView v = (TextView)findViewById(R.id.txt_insert_pass); - - //get the network to set password to: - this.apinfo = (AccessPointInfo)getIntent().getSerializableExtra(Commons.EXTRA_APINFO); - v.append(" " + apinfo.getSSID()); - - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - //disable menu - return true; - } - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - - if(buttonView == findViewById(R.id.chk_show_pass)){ - chkShowPassChanged(); - } - - } - - public void onBtnNextClick(View v){ - - EditText txpass = (EditText)findViewById(R.id.txt_password); - String pass = txpass.getText().toString(); - - if (pass.length()>0){ - - this.apinfo.setPassword(pass); - - Intent intent = new Intent(); - intent.putExtra(Commons.EXTRA_APINFO, this.apinfo); - setResult(RESULT_OK, intent); - - finish(); - - } - - } - - public void chkShowPassChanged(){ - - CheckBox c = (CheckBox)findViewById(R.id.chk_show_pass); - EditText txtPass = (EditText)findViewById(R.id.txt_password); - - if (c.isChecked()){ - txtPass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); - txtPass.setSelection(txtPass.getText().length()); - - }else{ - txtPass.setInputType(129); - txtPass.setSelection(txtPass.getText().length()); - - } - - } + } } diff --git a/app/src/fil/libre/repwifiapp/activities/InputSsidActivity.java b/app/src/fil/libre/repwifiapp/activities/InputSsidActivity.java new file mode 100644 index 0000000..4f3f74c --- /dev/null +++ b/app/src/fil/libre/repwifiapp/activities/InputSsidActivity.java @@ -0,0 +1,115 @@ +// +// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net> +// +// This file is part of RepWifiApp. +// +// RepWifiApp is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// RepWifiApp is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with RepWifiApp. If not, see <http://www.gnu.org/licenses/>. +// +// ******************************************************************** + +package fil.libre.repwifiapp.activities; + +import fil.libre.repwifiapp.ActivityLauncher; +import fil.libre.repwifiapp.ActivityLauncher.RequestCode; +import fil.libre.repwifiapp.Commons; +import fil.libre.repwifiapp.R; +import fil.libre.repwifiapp.helpers.AccessPointInfo; +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.Menu; +import android.view.View; +import android.widget.EditText; + +public class InputSsidActivity extends Activity { + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + + setContentView(R.layout.activity_input_ssid); + setTitle("Insert Network's parameters"); + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // disable menu + return true; + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + + if (intent == null) { + return; + } + + if (resultCode != RESULT_OK) { + return; + } + + switch (requestCode) { + + case RequestCode.SELECT_CONN: + + if (intent.hasExtra(ActivityLauncher.EXTRA_APINFO)) { + Bundle xtras = intent.getExtras(); + AccessPointInfo i = (AccessPointInfo) xtras + .getSerializable(ActivityLauncher.EXTRA_APINFO); + returnAccessPointInfo(i); + } + + break; + + default: + break; + } + + } + + public void onBtnNextClick(View v) { + + EditText txssid = (EditText) findViewById(R.id.txt_ssid); + String ssid = txssid.getText().toString(); + + if (ssid.length() == 0) { + Commons.showMessage("Network name can't be empty!", this); + return; + } + + AccessPointInfo apinfo = new AccessPointInfo(ssid, Commons.BSSID_NOT_AVAILABLE, "WPA2", + null, null); + + returnAccessPointInfo(apinfo); + + } + + public void onBtnSelectClick(View v) { + ActivityLauncher launcher = new ActivityLauncher(this); + launcher.launchSelectActivity(null, true, true); + } + + private void returnAccessPointInfo(AccessPointInfo apinfo) { + + apinfo.setHidden(true); + + Intent intent = new Intent(); + intent.putExtra(ActivityLauncher.EXTRA_APINFO, apinfo); + setResult(RESULT_OK, intent); + + finish(); + } + +}
\ No newline at end of file diff --git a/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java b/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java index 63063d8..e0b4184 100644 --- a/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java @@ -1,176 +1,184 @@ package fil.libre.repwifiapp.activities; - +import fil.libre.repwifiapp.ActivityLauncher; import fil.libre.repwifiapp.Commons; import fil.libre.repwifiapp.R; import fil.libre.repwifiapp.helpers.AccessPointInfo; import fil.libre.repwifiapp.helpers.ConnectionStatus; import fil.libre.repwifiapp.helpers.Utils; - import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; +import android.view.View; +import android.widget.ProgressBar; import android.widget.TextView; import android.app.Activity; import android.content.Intent; public class LongTaskActivity extends Activity { + private class Task extends AsyncTask<Object, Object, Object> { + + private int REQ_CODE; + + public Task(int reqCode, Object input) { + this.REQ_CODE = reqCode; + } + + @Override + protected Object doInBackground(Object... params) { + + Object ret = null; + + switch (this.REQ_CODE) { + + case ActivityLauncher.RequestCode.CONNECT: + + ret = Commons.connectionEngine.connect((AccessPointInfo) params[0]); + break; + + case ActivityLauncher.RequestCode.NETWORKS_GET: + + ret = Commons.connectionEngine.getAvailableNetworks(); + break; + + case ActivityLauncher.RequestCode.STATUS_GET: + + ret = Commons.connectionEngine.getConnectionStatus(); + break; + + default: - private class Task extends AsyncTask<Object, Object, Object>{ + break; - private int REQ_CODE; + } - public Task(int reqCode, Object input){ - this.REQ_CODE = reqCode; - } + return ret; - @Override - protected Object doInBackground(Object... params) { + } - Object ret = null; + @Override + protected void onPostExecute(Object result) { + taskCompleted(result, this.REQ_CODE); + } - switch (this.REQ_CODE){ + } - case Commons.RequestCode.CONNECT: + private AccessPointInfo currentNetwork = null; - ret = Commons.connectionEngine.connect((AccessPointInfo)params[0]); - break; + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_long_task); - case Commons.RequestCode.NETWORKS_GET: + toggleProgbar(Commons.isProgbarEnabled()); + startTask(); - ret = Commons.connectionEngine.getAvailableNetworks(); - break; + } - case Commons.RequestCode.STATUS_GET: + @Override + public boolean onCreateOptionsMenu(Menu menu) { + return true; + } - ret = Commons.connectionEngine.getConnectionStatus(); - break; + private void startTask() { - default: + // retrieve the request code: + Intent intent = getIntent(); + if (!intent.hasExtra(ActivityLauncher.EXTRA_REQCODE)) { + this.setResult(RESULT_CANCELED); + finish(); + } - break; + Object input = null; + int reqCode = intent.getExtras().getInt(ActivityLauncher.EXTRA_REQCODE); - } + switch (reqCode) { - return ret; + case ActivityLauncher.RequestCode.CONNECT: + // Extract AccessPointInfo + input = intent.getExtras().getSerializable(ActivityLauncher.EXTRA_APINFO); + currentNetwork = (AccessPointInfo) input; + setTitle("Connecting to " + currentNetwork.getSsid() + "..."); + setMessage("Connecting to " + currentNetwork.getSsid() + "..."); + break; - } + case ActivityLauncher.RequestCode.NETWORKS_GET: + setTitle("Scanning..."); + setMessage("Scanning for Networks..."); - @Override - protected void onPostExecute(Object result) { - taskCompleted(result, this.REQ_CODE); - } + case ActivityLauncher.RequestCode.STATUS_GET: + setTitle("Checking status..."); + setMessage("Checking status..."); - } + default: + setTitle("Please wait..."); + setMessage("Please wait..."); + break; + } - private AccessPointInfo currentNetwork = null; - - @Override - public void onCreate(Bundle savedInstanceState){ - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_long_task); - - startTask(); + Task task = new Task(reqCode, input); + task.execute(input); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu){ - return true; - } + } - private void startTask() { + private void taskCompleted(Object result, int reqCode) { - //retrieve the request code: - Intent intent = getIntent(); - if (! intent.hasExtra(Commons.EXTRA_REQCODE)){ - this.setResult(RESULT_CANCELED); - finish(); - } + Utils.logDebug("Finished long task reqCode: " + reqCode, 1); - Object input = null; - int reqCode = intent.getExtras().getInt(Commons.EXTRA_REQCODE); - - switch (reqCode) { - - case Commons.RequestCode.CONNECT: - setTitle("Connecting..."); - setMessage("Connecting..."); - - //Extract AccessPointInfo - input = intent.getExtras().getSerializable(Commons.EXTRA_APINFO); - currentNetwork = (AccessPointInfo)input; - break; - - case Commons.RequestCode.NETWORKS_GET: - setTitle("Scanning..."); - setMessage("Scanning for Networks..."); - - case Commons.RequestCode.STATUS_GET: - setTitle("Checking status..."); - setMessage("Checking status..."); + // Return to caller: + Intent intent = this.getIntent(); - default: - setTitle("Please wait..."); - setMessage("Please wait..."); - break; - } - - Task task = new Task(reqCode, input); - task.execute(input); + switch (reqCode) { - } + case ActivityLauncher.RequestCode.CONNECT: - private void taskCompleted(Object result, int reqCode){ + intent.putExtra(ActivityLauncher.EXTRA_BOOLEAN, (Boolean) result); + intent.putExtra(ActivityLauncher.EXTRA_APINFO, this.currentNetwork); + break; - Utils.logDebug("Finished long task reqCode: "+ reqCode,1); - - //Return to caller: - Intent intent = this.getIntent(); + case ActivityLauncher.RequestCode.NETWORKS_GET: + intent.putExtra(ActivityLauncher.EXTRA_APINFO_ARR, (AccessPointInfo[]) result); + break; - switch (reqCode){ + case ActivityLauncher.RequestCode.STATUS_GET: - case Commons.RequestCode.CONNECT: + intent.putExtra(ActivityLauncher.EXTRA_CONSTATUS, (ConnectionStatus) result); + break; - intent.putExtra(Commons.EXTRA_BOOLEAN, (Boolean)result); - intent.putExtra(Commons.EXTRA_APINFO, this.currentNetwork); - break; + default: - case Commons.RequestCode.NETWORKS_GET: + Utils.logDebug("Task terminating in null: ", 1); + break; - intent.putExtra(Commons.EXTRA_APINFO_ARR, (AccessPointInfo[])result); - break; + } - case Commons.RequestCode.STATUS_GET: + this.setResult(RESULT_OK, intent); + finish(); - intent.putExtra(Commons.EXTRA_CONSTATUS, (ConnectionStatus)result); - break; + } - default: + private void setMessage(String msg) { + TextView txt = (TextView) findViewById(R.id.txt_msg); + txt.setText(msg); + } - Utils.logDebug("Task terminating in null: ",1); - break; + private void toggleProgbar(boolean enable) { - } + ProgressBar pb = (ProgressBar) findViewById(R.id.progbar); - this.setResult(RESULT_OK, intent); - finish(); - - } - - - private void setMessage(String msg) { - TextView txt = (TextView)findViewById(R.id.txt_msg); - txt.setText(msg); - } - - @Override - public void onBackPressed() { - //suppress back button - } + if (enable) { + pb.setVisibility(View.VISIBLE); + } else { + pb.setVisibility(View.INVISIBLE); + } + } + @Override + public void onBackPressed() { + // suppress back button + } } diff --git a/app/src/fil/libre/repwifiapp/activities/MainActivity.java b/app/src/fil/libre/repwifiapp/activities/MainActivity.java index aa179fb..6e667bf 100644 --- a/app/src/fil/libre/repwifiapp/activities/MainActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/MainActivity.java @@ -21,447 +21,447 @@ package fil.libre.repwifiapp.activities; import java.io.IOException; +import java.net.SocketException; +import fil.libre.repwifiapp.ActivityLauncher; import fil.libre.repwifiapp.Commons; import fil.libre.repwifiapp.R; -import fil.libre.repwifiapp.Commons.RequestCode; +import fil.libre.repwifiapp.ActivityLauncher.RequestCode; import fil.libre.repwifiapp.helpers.AccessPointInfo; import fil.libre.repwifiapp.helpers.ConnectionStatus; import fil.libre.repwifiapp.helpers.NetworkManager; import fil.libre.repwifiapp.helpers.RootCommand; import fil.libre.repwifiapp.helpers.Utils; import android.os.Bundle; -import android.app.Activity; -import android.app.AlertDialog; -import android.content.DialogInterface; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.graphics.drawable.Drawable; -import android.view.Menu; -import android.view.MenuItem; +import android.hardware.usb.UsbManager; +import android.util.Log; import android.view.View; import android.widget.ImageView; 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); - - if(! Commons.init(this)){ - Utils.logDebug("Failed to initialize Commons. Aborting."); - finish(); - return; - } - - setImage(); - - //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); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle item selection - switch (item.getItemId()) { - case R.id.menu_credits: - launchCreditsActivity(); - break; - - case R.id.menu_config: - launchSettingsActivity(); - break; - - default: - break; - } - - return true; - } - - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent intent){ - - Utils.logDebug("Main onActivityResult(): ",1); - - if (intent == null){ - return; - } - - if (resultCode != RESULT_OK){ - return; - } - - AccessPointInfo i = null; - if (intent.hasExtra(Commons.EXTRA_APINFO)){ - i = (AccessPointInfo)intent.getExtras().getSerializable(Commons.EXTRA_APINFO); - } - - switch (requestCode) { - - case RequestCode.PASS_INPUT: - handleResultSetPass(i); - break; - - case RequestCode.SELECT_CONN: - boolean rescan = intent.getExtras().getBoolean(Commons.EXTRA_RESCAN); - handleResultSelect(i, rescan); - break; - - case RequestCode.CONNECT: - boolean conres = intent.getExtras().getBoolean(Commons.EXTRA_BOOLEAN); - handleFinishedConnecting(conres, i); - break; - - case RequestCode.STATUS_GET: - ConnectionStatus status = (ConnectionStatus)intent.getExtras().getSerializable(Commons.EXTRA_CONSTATUS); - handleResultGetStatus(status); - break; - - case RequestCode.NETWORKS_GET: - AccessPointInfo[] nets = (AccessPointInfo[])intent.getExtras().getSerializable(Commons.EXTRA_APINFO_ARR); - launchSelectActivity(nets, true); - - case RequestCode.STATUS_SHOW: - //do nothing - break; - - case RequestCode.SELECT_DETAILS: - launchDetailsActivity(i); - break; - - case RequestCode.DETAILS_SHOW: - boolean del = intent.getExtras().getBoolean(Commons.EXTRA_DELETE); - if (del){ deleteNetwork(i); } - break; - - default: - - break; - - } - - - } - - private void setImage(){ - - ImageView img = (ImageView)findViewById(R.id.img_logo); - - try { - Drawable d = Drawable.createFromStream(getAssets().open("repwifi-logo-0.png"),null); - img.setImageDrawable(d); - } catch (IOException e) { - Utils.logError("Error while loading logo image",e); - } - - } - - - private void handleResultSelect(AccessPointInfo i, boolean rescan){ - - if (rescan){ - - doScan(); - - }else if (i != null){ - - if (i.needsPassword()){ - - //try to fetch network's password from storage - AccessPointInfo fromStorage = Commons.storage.getSavedNetwork(i); - if (fromStorage == null){ - - launchPasswordActivity(i); - return; - - }else{ - //use fetched network - i = fromStorage; - } - - } - - connectToNetwork(i); - } - - } - - private void handleResultSetPass(AccessPointInfo i){ - connectToNetwork(i); - } - - private void handleResultGetStatus(ConnectionStatus status){ - if (status != null && status.isConnected()){ - launchStatusActivity(status); - } - } - - private void handleFinishedConnecting(boolean connectionResult, AccessPointInfo info){ - - if(connectionResult && info.needsPassword()){ - - //Save network - if (Commons.storage.save(info)){ - Toast toast2 = Toast.makeText(getApplicationContext(), "Network Saved!",Toast.LENGTH_LONG); - toast2.show(); - - }else { - Toast toast2 = Toast.makeText(getApplicationContext(), "FAILED to save network!",Toast.LENGTH_LONG); - toast2.show(); - } - - checkConnectionStatus(); - - }else{ - //alert that connection failed - Toast toast = Toast.makeText(getApplicationContext(), "FAILED to connect!", Toast.LENGTH_LONG); - toast.show(); - } - } - - private void launchPasswordActivity(AccessPointInfo info){ - - Intent intent = new Intent(); - intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - intent.setClass(getApplicationContext(), InputPasswordActivity.class); - intent.putExtra(Commons.EXTRA_APINFO, info); - - startActivityForResult(intent, RequestCode.PASS_INPUT); - - } - - private void launchStatusActivity(ConnectionStatus status){ - - Intent intent = new Intent(); - intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - intent.putExtra(Commons.EXTRA_CONSTATUS, status); - intent.setClass(getApplicationContext(), ShowStatusActivity.class); - startActivityForResult(intent, RequestCode.STATUS_SHOW); - - } - - private void launchSelectActivity(AccessPointInfo[] nets,boolean forConnection){ - - Intent intent = new Intent(this, SelectNetworkActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - intent.putExtra(Commons.EXTRA_APINFO_ARR, nets); - - if (forConnection){ - intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.SELECT_CONN); - startActivityForResult(intent, RequestCode.SELECT_CONN); - } - else{ - intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.SELECT_DETAILS); - startActivityForResult(intent, RequestCode.SELECT_DETAILS); - } - - } - - private void launchDetailsActivity(AccessPointInfo info){ - - Intent intent = new Intent(this, NetworkDetailsActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - intent.putExtra(Commons.EXTRA_APINFO, info); - startActivityForResult(intent, RequestCode.DETAILS_SHOW); - - } - - 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){ - - NetworkManager manager = new NetworkManager(Commons.getNetworkStorageFile()); - String msg = ""; - if (manager.remove(info)){ - msg = "Network info deleted!"; - }else{ - msg = "FAILED to delete network info!"; - } - - Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG); - toast.show(); - - } - - private void connectToNetwork(AccessPointInfo info){ - - Intent intent = new Intent(this, LongTaskActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.CONNECT); - intent.putExtra(Commons.EXTRA_APINFO, info); - startActivityForResult(intent, RequestCode.CONNECT); - - } - - private void checkConnectionStatus(){ - - Intent intent = new Intent(this, LongTaskActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.STATUS_GET); - startActivityForResult(intent, RequestCode.STATUS_GET); - - } - - 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); - startActivityForResult(intent, RequestCode.NETWORKS_GET); - - } - - public void btnScanClick(View v){ - - doScan(); - - } - - public void btnManageClick(View v){ - - NetworkManager manager = new NetworkManager(Commons.getNetworkStorageFile()); - AccessPointInfo[] infos = manager.getKnownNetworks(); - - if (infos == null || infos.length == 0){ - Toast toast = Toast.makeText(this, "No saved network", Toast.LENGTH_LONG); - toast.show(); - } - else{ - launchSelectActivity(infos, false); - } - - } - - 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(); - - } - +public class MainActivity extends MenuEnabledActivity { + + private ActivityLauncher launcher = new ActivityLauncher(this); + private BroadcastReceiver detachReceiver; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + if (!Commons.init(this)) { + Utils.logDebug("Failed to initialize Commons. Aborting."); + finish(); + return; + } + + setImage(); + setUsbDeviceMonitor(); + } + + @Override + public void onStart() { + super.onStart(); + Utils.logDebug("Main onStart()"); + + Commons.updateNotification(this); + + if (handleIntent()) { + // app called for a specific intent. + // avoid any other task. + Log.d("RepWifi", "handleIntent returned true"); + return; + } + + checkConditions(); + + ConnectionStatus status = Commons.connectionEngine.getConnectionStatus(); + if (status != null && status.isConnected()) { + Utils.logDebug("Main about to launch status activity..."); + launcher.launchStatusActivity(status); + } + + Utils.logDebug("Main onStart() returning."); + + } + + private boolean handleIntent() { + + Intent i = getIntent(); + if (i != null && i.hasExtra(ActivityLauncher.EXTRA_REQCODE)) { + + switch (i.getIntExtra(ActivityLauncher.EXTRA_REQCODE, -1)) { + + case RequestCode.NONE: + moveTaskToBack(true); + + break; + + default: + break; + } + + return true; + + } else { + // no intent to handle. + return false; + } + + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + + Utils.logDebug("Main onActivityResult(): ", 1); + + if (intent == null) { + return; + } + + if (resultCode != RESULT_OK) { + return; + } + + AccessPointInfo i = null; + if (intent.hasExtra(ActivityLauncher.EXTRA_APINFO)) { + Bundle xtras = intent.getExtras(); + i = (AccessPointInfo) xtras.getSerializable(ActivityLauncher.EXTRA_APINFO); + } + + switch (requestCode) { + + case RequestCode.PASS_INPUT: + handleResultSetPass(i); + break; + + case RequestCode.SELECT_CONN: + boolean rescan = intent.getExtras().getBoolean(ActivityLauncher.EXTRA_RESCAN); + handleResultSelect(i, rescan); + break; + + case RequestCode.CONNECT: + boolean conres = intent.getExtras().getBoolean(ActivityLauncher.EXTRA_BOOLEAN); + handleFinishedConnecting(conres, i); + break; + + case RequestCode.STATUS_GET: + ConnectionStatus status = (ConnectionStatus) intent.getExtras().getSerializable( + ActivityLauncher.EXTRA_CONSTATUS); + handleResultGetStatus(status); + break; + + case RequestCode.NETWORKS_GET: + AccessPointInfo[] nets = (AccessPointInfo[]) intent.getExtras().getSerializable( + ActivityLauncher.EXTRA_APINFO_ARR); + launcher.launchSelectActivity(nets, true, false); + + case RequestCode.STATUS_SHOW: + // do nothing + break; + + case RequestCode.SELECT_DETAILS: + launcher.launchDetailsActivity(i); + break; + + case RequestCode.DETAILS_SHOW: + boolean del = intent.getExtras().getBoolean(ActivityLauncher.EXTRA_DELETE); + if (del) { + deleteNetwork(i); + } + break; + + case RequestCode.CONNECT_HIDDEN: + + if (i != null) { + handleResultSelect(i, false); + } + break; + + default: + + break; + + } + + } + + private void setImage() { + + ImageView img = (ImageView) findViewById(R.id.img_logo); + + try { + Drawable d = Drawable.createFromStream(getAssets().open("repwifi-logo-0.png"), null); + img.setImageDrawable(d); + } catch (IOException e) { + Utils.logError("Error while loading logo image", e); + } + + } + + private void setUsbDeviceMonitor() { + detachReceiver = new BroadcastReceiver() { + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { + handleUsbEvent(true); + } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { + handleUsbEvent(false); + } + } + }; + + IntentFilter filter = new IntentFilter(); + filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); + registerReceiver(detachReceiver, filter); + + IntentFilter filt2 = new IntentFilter(); + filt2.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); + registerReceiver(detachReceiver, filt2); + } + + private boolean checkConditions() { + return (checkRootEnabled() && checkInterface(true)); + } + + private boolean checkInterface(boolean alert) { + + boolean res = false; + String msg = ""; + + try { + res = Commons.connectionEngine.isInterfaceAvailable(Commons.INTERFACE_NAME); + } catch (SocketException e) { + Utils.logError("SocketException during isInterfaceAvailable()", e); + msg = "Error while retrieving interface list!"; + res = false; + } + + if (res == false && alert) { + msg = getResources().getString(R.string.msg_interface_not_found); + Commons.showMessage(msg, this); + } + + 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) { + Commons.showMessage(msg, this); + } + + return result; + + } + + private void handleResultSelect(AccessPointInfo i, boolean rescan) { + + if (rescan) { + + doScan(); + + } else if (i != null) { + + if (i.needsPassword()) { + + // try to fetch network's password from storage + AccessPointInfo fromStorage = Commons.storage.getSavedNetwork(i); + if (fromStorage == null) { + + launcher.launchPasswordActivity(i); + return; + + } else { + // use fetched network + i = fromStorage; + } + + } + + launcher.launchLongTaskActivityConnect(i); + } + + } + + private void handleResultSetPass(AccessPointInfo i) { + launcher.launchLongTaskActivityConnect(i); + } + + private void handleResultGetStatus(ConnectionStatus status) { + if (status != null && status.isConnected()) { + launcher.launchStatusActivity(status); + } + } + + private void handleFinishedConnecting(boolean connectionResult, AccessPointInfo info) { + + if (connectionResult && info.needsPassword()) { + + ConnectionStatus status = Commons.connectionEngine.getConnectionStatus(); + if (status != null) { + // update APinfo with the right BSSID + info.setBssid(status.BSSID); + } + + // Save network + if (Commons.storage.save(info)) { + Toast toast2 = Toast.makeText(getApplicationContext(), "Network Saved!", + Toast.LENGTH_LONG); + toast2.show(); + + } else { + Toast toast2 = Toast.makeText(getApplicationContext(), "FAILED to save network!", + Toast.LENGTH_LONG); + toast2.show(); + } + + // show status + launcher.launchStatusActivity(status); + + } else { + // alert that connection failed + Toast toast = Toast.makeText(getApplicationContext(), "FAILED to connect!", + Toast.LENGTH_LONG); + toast.show(); + } + } + + private void deleteNetwork(AccessPointInfo info) { + + NetworkManager manager = new NetworkManager(Commons.getNetworkStorageFile()); + String msg = ""; + if (manager.remove(info)) { + msg = "Network info deleted!"; + } else { + msg = "FAILED to delete network info!"; + } + + Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG); + toast.show(); + + } + + private void handleUsbEvent(boolean detached) { + + if (detached && !checkInterface(false)) { + // device disconnected, update the status bar: + Commons.updateNotification(this); + + } else if (Commons.isAutoConnectEnabled()) { + + try { + + // waits for a maximum of WAIT_ON_USB_ATTACHED milliseconds + // to let the interface being registered. + int msWaited = 0; + while (msWaited < Commons.WAIT_ON_USB_ATTACHED) { + + Thread.sleep(100); + msWaited += 100; + + if (checkInterface(false)) { + autoConnect(); + return; + } + } + + } catch (InterruptedException e) { + // ignores and exits; + return; + } + + } + + } + + private void autoConnect() { + + try { + + AccessPointInfo[] nets = Commons.connectionEngine.getAvailableNetworks(); + if (nets == null || nets.length == 0) { + return; + } + + for (AccessPointInfo i : nets) { + + if (Commons.storage.isKnown(i)) { + launcher.launchLongTaskActivityConnect(i); + return; + } + + } + + // if no network is known, shows available networks to the user. + launcher.launchSelectActivity(nets, true, false); + + } catch (Exception e) { + Utils.logError("Error while autoconnecting", e); + Commons.showMessage("An error occured while trying to auto-connect", this); + } + + } + + private void doScan() { + if (checkConditions()) { + launcher.launchLongTaskActivityScan(); + } + } + + public void btnScanClick(View v) { + doScan(); + } + + public void btnHiddenClick(View v) { + + if (checkConditions()) { + launcher.launchInputSsidActivity(); + } + + } + + public void btnManageClick(View v) { + launcher.launchSelectActivity(null, false, true); + } + } diff --git a/app/src/fil/libre/repwifiapp/activities/MenuEnabledActivity.java b/app/src/fil/libre/repwifiapp/activities/MenuEnabledActivity.java new file mode 100644 index 0000000..0edfcf0 --- /dev/null +++ b/app/src/fil/libre/repwifiapp/activities/MenuEnabledActivity.java @@ -0,0 +1,49 @@ +package fil.libre.repwifiapp.activities; + +import fil.libre.repwifiapp.R; +import android.app.Activity; +import android.content.Intent; +import android.view.Menu; +import android.view.MenuItem; + +public class MenuEnabledActivity extends Activity { + + @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); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle item selection + int id = item.getItemId(); + switch (id) { + case R.id.menu_credits: + launchCreditsActivity(); + break; + + case R.id.menu_config: + launchSettingsActivity(); + break; + + default: + break; + } + + return true; + } + + private void launchCreditsActivity() { + Intent intent = new Intent(this, CreditsActivity.class); + // intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); + startActivity(intent); + } + + private void launchSettingsActivity() { + Intent intent = new Intent(this, SettingsActivity.class); + // intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); + startActivity(intent); + } +} diff --git a/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java b/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java index 6d7ed56..325d546 100644 --- a/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/NetworkDetailsActivity.java @@ -21,12 +21,9 @@ package fil.libre.repwifiapp.activities; import java.util.Date; - - -import fil.libre.repwifiapp.Commons; +import fil.libre.repwifiapp.ActivityLauncher; import fil.libre.repwifiapp.R; import fil.libre.repwifiapp.helpers.AccessPointInfo; - import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; @@ -42,111 +39,112 @@ import android.widget.CompoundButton.OnCheckedChangeListener; public class NetworkDetailsActivity extends Activity implements OnCheckedChangeListener { - private AccessPointInfo currentNetwor; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_network_details); - - CheckBox c = (CheckBox)findViewById(R.id.chk_show_pass_details); - c.setOnCheckedChangeListener(this); - - Intent intent = getIntent(); - if (! intent.hasExtra(Commons.EXTRA_APINFO)){ - this.setResult(RESULT_CANCELED); - this.finish(); - return; - } - - this.currentNetwor = (AccessPointInfo)intent.getExtras().getSerializable(Commons.EXTRA_APINFO); - if (this.currentNetwor == null){ - this.setResult(RESULT_CANCELED); - this.finish(); - return; - } - - loadNetwork(false); - - } - - @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_manage_networks, menu); - return true; - } - - private void loadNetwork(boolean showPassword){ - - setTitle(this.currentNetwor.getSSID()); - - TextView v = (TextView)findViewById(R.id.txt_net_details); - v.setText("SSID: " + this.currentNetwor.getSSID()); - v.append("\nBSSID: " + this.currentNetwor.getBSSID()); - - long lastused = this.currentNetwor.getLastTimeUsed(); - - if (lastused > 0){ - Date ts = new Date(lastused); - String formstring = "dd-MMM-yyyy kk:mm:ss"; - v.append("\nLast Used: " + DateFormat.format(formstring, ts)); - } - - if (showPassword){ - v.append("\n\nPassword:\n" + this.currentNetwor.getPassword()); - } - else{ - v.append("\n\n\n"); - } - - } - - 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(msg); - dlgAlert.setPositiveButton(yes,new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int whichButton) { - returnResult(true); - } - }); - dlgAlert.setNegativeButton(no,new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int whichButton) { - return; - } - }); - - dlgAlert.setCancelable(true); - dlgAlert.create().show(); - - } - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - - if (buttonView == findViewById(R.id.chk_show_pass_details)){ - loadNetwork(isChecked); - } - - } - - private void returnResult(boolean delete){ - - Intent i = new Intent(); - i.putExtra(Commons.EXTRA_DELETE, delete); - i.putExtra(Commons.EXTRA_APINFO, this.currentNetwor); - this.setResult(RESULT_OK,i); - finish(); - - } - - - + private AccessPointInfo currentNetwork; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_network_details); + + CheckBox c = (CheckBox) findViewById(R.id.chk_show_pass_details); + c.setOnCheckedChangeListener(this); + + Intent intent = getIntent(); + if (!intent.hasExtra(ActivityLauncher.EXTRA_APINFO)) { + this.setResult(RESULT_CANCELED); + this.finish(); + return; + } + + this.currentNetwork = (AccessPointInfo) intent.getExtras().getSerializable( + ActivityLauncher.EXTRA_APINFO); + if (this.currentNetwork == null) { + this.setResult(RESULT_CANCELED); + this.finish(); + return; + } + + loadNetwork(false); + + } + + @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_manage_networks, menu); + return true; + } + + private void loadNetwork(boolean showPassword) { + + setTitle(this.currentNetwork.getSsid()); + + TextView v = (TextView) findViewById(R.id.txt_net_details); + v.setText("SSID: " + this.currentNetwork.getSsid()); + v.append("\nBSSID: " + this.currentNetwork.getBssid()); + + long lastused = this.currentNetwork.getLastTimeUsed(); + + if (lastused > 0) { + Date ts = new Date(lastused); + String formstring = "dd-MMM-yyyy kk:mm:ss"; + v.append("\nLast Used: " + DateFormat.format(formstring, ts)); + } + + if (showPassword) { + v.append("\n\nPassword:\n" + this.currentNetwork.getPassword()); + } else { + v.append("\n\n\n"); + } + + } + + 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, + R.style.Theme_RepWifiDialogTheme); + 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() { + + @Override + public void onClick(DialogInterface dialog, int whichButton) { + return; + } + }); + + dlgAlert.setCancelable(true); + dlgAlert.create().show(); + + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + + if (buttonView == findViewById(R.id.chk_show_pass_details)) { + loadNetwork(isChecked); + } + + } + + private void returnResult(boolean delete) { + + Intent i = new Intent(); + i.putExtra(ActivityLauncher.EXTRA_DELETE, delete); + i.putExtra(ActivityLauncher.EXTRA_APINFO, this.currentNetwork); + this.setResult(RESULT_OK, i); + finish(); + + } + } diff --git a/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java b/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java index 9b07246..341fb68 100644 --- a/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java @@ -20,6 +20,7 @@ package fil.libre.repwifiapp.activities; +import fil.libre.repwifiapp.ActivityLauncher; import fil.libre.repwifiapp.Commons; import fil.libre.repwifiapp.R; import fil.libre.repwifiapp.helpers.AccessPointInfo; @@ -40,178 +41,180 @@ import android.widget.TextView; public class SelectNetworkActivity extends Activity implements OnClickListener { - private AccessPointInfo[] aps; - - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_select_network); - - setTitle("Select network"); - - getNetworks(); - - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - //Suppress menu button - return true; - } - - private void writeOut(String msg) { - - TextView v = (TextView)findViewById(R.id.txt_selnets); - v.setText(msg); - - } - - private void getNetworks(){ - - Intent intent = getIntent(); - if(! intent.hasExtra(Commons.EXTRA_APINFO_ARR)){ - this.setResult(RESULT_CANCELED); - finish(); - return; - } - AccessPointInfo[] nets = (AccessPointInfo[])intent.getExtras().getSerializable(Commons.EXTRA_APINFO_ARR); - if (nets == null){ - this.setResult(RESULT_CANCELED); - finish(); - return; - } - - int reqCode = intent.getExtras().getInt(Commons.EXTRA_REQCODE); - - this.aps = nets; - - if (reqCode == Commons.RequestCode.SELECT_CONN){ - showNetworksForConnection(nets); - } - else{ - showNetworksForManagement(nets); - } - - } - - public void btnScanClick(View v){ - returnResults(null, true); - } - - @Override + private AccessPointInfo[] aps; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_select_network); + + setTitle("Select network"); + + getNetworks(); + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Suppress menu button + return true; + } + + private void writeOut(String msg) { + + TextView v = (TextView) findViewById(R.id.txt_selnets); + v.setText(msg); + + } + + private void getNetworks() { + + Intent intent = getIntent(); + if (!intent.hasExtra(ActivityLauncher.EXTRA_APINFO_ARR)) { + this.setResult(RESULT_CANCELED); + finish(); + return; + } + AccessPointInfo[] nets = (AccessPointInfo[]) intent.getExtras().getSerializable( + ActivityLauncher.EXTRA_APINFO_ARR); + if (nets == null) { + this.setResult(RESULT_CANCELED); + finish(); + return; + } + + int reqCode = intent.getExtras().getInt(ActivityLauncher.EXTRA_REQCODE); + + this.aps = nets; + + if (reqCode == ActivityLauncher.RequestCode.SELECT_CONN) { + showNetworksForConnection(nets); + } else { + showNetworksForManagement(nets); + } + + } + + public void btnScanClick(View v) { + returnResults(null, true); + } + + @Override public void onClick(View v) { - if (v instanceof NetworkButton){ - networkNameClick((NetworkButton)v); - } + if (v instanceof NetworkButton) { + networkNameClick((NetworkButton) v); + } + + } + + public void networkNameClick(NetworkButton b) { + + for (AccessPointInfo i : this.aps) { + + if (i.getBssid().equals(b.getNetworkBSSID())) { + + returnResults(i, false); + + } + + } + } + + private void returnResults(AccessPointInfo i, boolean rescan) { + + Intent intent = new Intent(); + intent.putExtra(ActivityLauncher.EXTRA_APINFO, i); + intent.putExtra(ActivityLauncher.EXTRA_RESCAN, rescan); + setResult(RESULT_OK, intent); + finish(); + + } + + private void showNetworksForConnection(AccessPointInfo[] info) { + + if (info == null) { + Utils.logError("Unable to retrieve network list!"); + writeOut("Unable to retrieve network list!"); + return; + } + + if (info.length == 0) { + writeOut("No network found."); + toggleBtnRescan(true); + return; + } + + writeOut("Select the network you want to connect to:"); + toggleBtnRescan(false); + + for (AccessPointInfo i : info) { + + addButtonForNetwork(i); + + } + + } + + private void showNetworksForManagement(AccessPointInfo[] info) { + + if (info == null || info.length == 0) { + return; + } + + writeOut("Select network info to manage:"); + toggleBtnRescan(false); + + for (AccessPointInfo i : info) { + + addButtonForNetwork(i); + + } + + } + + private void toggleBtnRescan(boolean enable) { + + Button b = (Button) findViewById(R.id.btn_rescan); + if (enable) { + b.setVisibility(View.VISIBLE); + } else { + b.setVisibility(View.INVISIBLE); + } + } + + private void addButtonForNetwork(AccessPointInfo info) { + + TableLayout s = (TableLayout) findViewById(R.id.table_networks); + 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; + row.setPadding(10, 10, 10, 10); + row.setLayoutParams(rowParams); + row.setGravity(Gravity.FILL_HORIZONTAL); + row.setLayoutParams(rowParams); + + NetworkButton button = new NetworkButton(this, info.getBssid()); + + TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, + LayoutParams.WRAP_CONTENT); + button.setLayoutParams(params); + button.setBackground(getResources().getDrawable(R.drawable.repwifi_button)); + button.setTextColor(Commons.colorThemeLight); + button.setTextSize(20); + button.setPadding(25, 10, 25, 10); + button.setGravity(Gravity.CENTER_HORIZONTAL); + button.setText(info.getSsid(20)); + button.setOnClickListener(this); + + row.addView(button, params); + row.setGravity(Gravity.CENTER_HORIZONTAL); + s.addView(row, tableParams); + s.setGravity(Gravity.FILL_HORIZONTAL); } - - public void networkNameClick(NetworkButton b){ - - for(AccessPointInfo i : this.aps){ - - if (i.getBSSID().equals(b.getNetworkBSSID())){ - - returnResults(i,false); - - } - - } - } - - private void returnResults(AccessPointInfo i, boolean rescan){ - - Intent intent = new Intent(); - intent.putExtra(Commons.EXTRA_APINFO, i); - intent.putExtra(Commons.EXTRA_RESCAN, rescan); - setResult(RESULT_OK, intent); - finish(); - - } - - private void showNetworksForConnection(AccessPointInfo[] info) { - - if (info == null){ - Utils.logError("Unable to retrieve network list!"); - writeOut("Unable to retrieve network list!"); - return; - } - - if (info.length == 0){ - writeOut("No network found."); - toggleBtnRescan(true); - return; - } - - writeOut("Select the network you want to connect to:"); - toggleBtnRescan(false); - - for (AccessPointInfo i : info){ - - addButtonForNetwork(i); - - } - - } - - private void showNetworksForManagement(AccessPointInfo[] info){ - - if (info == null || info.length == 0){ - return; - } - - writeOut("Select network info to manage:"); - toggleBtnRescan(false); - - for (AccessPointInfo i : info){ - - addButtonForNetwork(i); - - } - - } - - private void toggleBtnRescan(boolean enable) { - - Button b = (Button)findViewById(R.id.btn_rescan); - if (enable){ - b.setVisibility(View.VISIBLE); - } - else{ - b.setVisibility(View.INVISIBLE); - } - } - - private void addButtonForNetwork(AccessPointInfo info){ - - TableLayout s = (TableLayout)findViewById(R.id.table_networks); - 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; - row.setPadding(10, 10, 10, 10); - row.setLayoutParams(rowParams); - row.setGravity(Gravity.FILL_HORIZONTAL); - row.setLayoutParams(rowParams); - - NetworkButton button = new NetworkButton(this, info.getBSSID()); - - TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); - button.setLayoutParams(params); - button.setBackgroundColor(Commons.colorThemeDark); - button.setTextColor(Commons.colorThemeLight); - button.setTextSize(20); - button.setPadding(10, 10, 10, 10); - button.setGravity(Gravity.CENTER_HORIZONTAL); - button.setText(info.getSSID()); - button.setOnClickListener(this); - - row.addView(button,params); - s.addView(row,tableParams); - s.setGravity(Gravity.FILL_HORIZONTAL); - - } } diff --git a/app/src/fil/libre/repwifiapp/activities/SettingsActivity.java b/app/src/fil/libre/repwifiapp/activities/SettingsActivity.java index 90629e3..6f14748 100644 --- a/app/src/fil/libre/repwifiapp/activities/SettingsActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/SettingsActivity.java @@ -1,35 +1,101 @@ package fil.libre.repwifiapp.activities; +import org.apache.http.conn.util.InetAddressUtils; import java.util.List; - +import fil.libre.repwifiapp.Commons; import fil.libre.repwifiapp.R; import android.os.Bundle; +import android.preference.EditTextPreference; +import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; - public class SettingsActivity extends PreferenceActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - } - - @Override + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(R.style.Theme_SettingsTheme); + 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); - } - - } + + public static class DebugSettingFragment extends PreferenceFragment { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.debug_settings); + + setConfirmKillBackend(); + + } + + private void setConfirmKillBackend() { + + Preference pref = getPreferenceScreen().findPreference("pref_kill_backend"); + pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference p) { + Commons.killBackEnd(getActivity(), false); + return true; + } + }); + + } + + } + + public static class GeneralSettingFragment extends PreferenceFragment { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.general_settings); + + setValidationListener("dns1"); + setValidationListener("dns2"); + setConfirmRestore(); + + } + + private void setValidationListener(String prefName) { + EditTextPreference edit_Pref = (EditTextPreference) getPreferenceScreen() + .findPreference(prefName); + + edit_Pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + // put validation here.. + if (((String) newValue).isEmpty() + || InetAddressUtils.isIPv4Address((String) newValue)) { + return true; + } else { + Commons.showMessage("ERROR:\nWrong IP format!", getActivity()); + return false; + } + } + }); + } + + private void setConfirmRestore() { + + Preference pref = getPreferenceScreen().findPreference("pref_restore_default"); + pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference p) { + Commons.resetSettingsDefault(getActivity(), false); + return true; + } + }); + + } + } } diff --git a/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java b/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java index 2674737..0fb8992 100644 --- a/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java @@ -18,114 +18,113 @@ // // ******************************************************************** - - package fil.libre.repwifiapp.activities; - +import fil.libre.repwifiapp.ActivityLauncher; import fil.libre.repwifiapp.Commons; import fil.libre.repwifiapp.R; import fil.libre.repwifiapp.helpers.ConnectionStatus; import fil.libre.repwifiapp.helpers.Utils; - import android.os.Bundle; -import android.app.Activity; -import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; -public class ShowStatusActivity extends Activity { - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_show_status); - showStatus(); - - } - - @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_show_status, menu); - return true; - } - - @Override - public void onRestart(){ - super.onRestart(); - showStatus(); - } - - private void setMessage(String msg){ - TextView view = (TextView)findViewById(R.id.txt_status); - view.setText(msg); - } - - private void showStatus(){ - - ConnectionStatus status = Commons.connectionEngine.getConnectionStatus(); - if (status == null){ - this.finish(); - - }else if (status.isConnected()){ - Utils.logDebug("StatusActivity isConnected,showing buttons"); - setMessage("Connected to " + status.SSID + "\n\n" + "IP Address: " + status.IP + "\n"); - toggleBtnDisconnect(true); - - - }else{ - Utils.logDebug("StatusActivity status Else"); - setMessage("Status:\n" + status.status); - toggleBtnDisconnect(false); - - } - } - - private void toggleBtnDisconnect(boolean enable){ - - Button b = (Button)findViewById(R.id.btn_disconnect); - Button bk = (Button)findViewById(R.id.btn_back); - b.setEnabled(enable); - bk.setEnabled(! enable); - - if (enable){ - b.setVisibility(View.VISIBLE); - bk.setVisibility(View.INVISIBLE); - }else{ - b.setVisibility(View.INVISIBLE); - bk.setVisibility(View.VISIBLE); - } - - } - - public void onBtnDisconnectClick(View v){ - - boolean res = Commons.connectionEngine.disconnect(); - String msg = ""; - if (res){ - msg = "Disconnected."; - } - else{ - msg = "FAILED to disconnect!"; - } - - Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT); - toast.show(); - - showStatus(); - - } - - public void onBtnMainClick(View v){ - finish(); - } - - @Override - public void onBackPressed() { - moveTaskToBack(true); - } - +public class ShowStatusActivity extends MenuEnabledActivity { + + private ConnectionStatus status; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_show_status); + + if (getIntent().hasExtra(ActivityLauncher.EXTRA_CONSTATUS)) { + this.status = (ConnectionStatus) getIntent().getSerializableExtra( + ActivityLauncher.EXTRA_CONSTATUS); + } + + showStatus(false); + + } + + @Override + public void onRestart() { + super.onRestart(); + showStatus(true); + } + + private void setMessage(String msg) { + TextView view = (TextView) findViewById(R.id.txt_status); + view.setText(msg); + } + + private void showStatus(boolean refresh) { + + if (refresh || status == null) { + this.status = Commons.connectionEngine.getConnectionStatus(); + } + + if (status == null) { + this.finish(); + + } else if (this.status.isConnected()) { + Utils.logDebug("StatusActivity isConnected,showing buttons"); + setMessage("Connected to " + status.SSID + "\n\n" + "IP Address: " + status.IP + "\n"); + toggleBtnDisconnect(true); + + } else { + Utils.logDebug("StatusActivity status Else"); + setMessage("Status:\n" + status.status); + toggleBtnDisconnect(false); + } + + Commons.updateNotification(this); + + } + + private void toggleBtnDisconnect(boolean enable) { + + Button b = (Button) findViewById(R.id.btn_disconnect); + Button bk = (Button) findViewById(R.id.btn_back); + b.setEnabled(enable); + bk.setEnabled(!enable); + + if (enable) { + b.setVisibility(View.VISIBLE); + bk.setVisibility(View.INVISIBLE); + } else { + b.setVisibility(View.INVISIBLE); + bk.setVisibility(View.VISIBLE); + } + + } + + public void onBtnDisconnectClick(View v) { + + boolean res = Commons.connectionEngine.disconnect(); + String msg = ""; + if (res) { + msg = "Disconnected."; + } else { + msg = "FAILED to disconnect!"; + } + + Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT); + toast.show(); + + showStatus(true); + + } + + public void onBtnMainClick(View v) { + finish(); + } + + @Override + public void onBackPressed() { + moveTaskToBack(true); + } + } |