Skip to content

Commit

Permalink
Added a help message and deletion confirmation. Moved the "New Config…
Browse files Browse the repository at this point in the history
…" option from the list to a menu. Added more input validation and caught a couple more potential null pointer exceptions.
  • Loading branch information
jp-bennett committed Jun 7, 2015
1 parent 93d0d02 commit 07c47f8
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 66 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="biz.incomsystems.fwknop2.HelpActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
</activity>
<activity
android:name=".ConfigDetailActivity"
android:label="@string/title_config_detail"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
Expand Down Expand Up @@ -126,12 +125,16 @@ public void onActivityCreated(Bundle savedInstanceState) {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.mainmenu, menu);
inflater.inflate(R.menu.detail_menu, menu);

}

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.qr_code) {
if (id == R.id.detail_help) {
Intent detailIntent = new Intent(getActivity(), HelpActivity.class);
startActivity(detailIntent);
} else if (id == R.id.qr_code) {
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes
Expand All @@ -141,8 +144,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri);
startActivity(marketIntent);
}
}
if (id == R.id.save) {
} else if (id == R.id.save) {
InetAddressValidator ipValidate = new InetAddressValidator();
Context context = getActivity(); // We know we will use a toast, so set it up now
int duration = Toast.LENGTH_LONG;
Expand All @@ -156,16 +158,22 @@ public boolean onOptionsItemSelected(MenuItem item) {

//The following is all input validation


try {
if ((Integer.parseInt(txt_server_port.getText().toString()) > 0) && (Integer.parseInt(txt_server_port.getText().toString()) < 65535)) { // check for valid port
txt_server_port.setText(String.valueOf(Integer.parseInt(txt_server_port.getText().toString())));
} else txt_server_port.setText(String.valueOf(62201));
} else {
txt_server_port.setText(String.valueOf(62201));
}
} catch (NumberFormatException ex) {
txt_server_port.setText(String.valueOf(62201));
}
if (txt_NickName.getText().toString().equalsIgnoreCase("")) { // Need to create a new Nick
toast.setText("You Must choose a unique Nickname."); // choosing a used nick will just overwrite it. So really
toast.show();
} else if (!(txt_ports.getText().toString().matches("tcp/\\d.*") || txt_ports.getText().toString().matches("udp/\\d.*"))) {
toast.setText("Access ports must be in the form of 'tcp/22'");
toast.show();
} else if (spn_allowip.getSelectedItem().toString().equalsIgnoreCase("Allow IP") && (!ipValidate.isValid(txt_allowIP.getText().toString()))){ //Have to have a valid ip to allow, if using allow ip
toast.setText("You Must supply a valid IP address to 'Allow IP'.");
toast.show();
Expand Down Expand Up @@ -219,7 +227,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else if (spn_ssh.getSelectedItem().toString().equalsIgnoreCase("Juicessh")) {
config.SSH_CMD = "juice:" + juice_adapt.getConnectionName(spn_juice.getSelectedItemPosition());
config.juice_uuid = juice_adapt.getConnectionId(spn_juice.getSelectedItemPosition());
Log.v("fwknop2", "Connection Name is: " + juice_adapt.getConnectionName(spn_juice.getSelectedItemPosition()));
} else {
config.juice_uuid = UUID.fromString("00000000-0000-0000-0000-000000000000");
config.SSH_CMD = "";
Expand All @@ -235,20 +242,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
if(activity instanceof ConfigListActivity) {
ConfigListActivity myactivity = (ConfigListActivity) activity;
myactivity.onItemSaved();

} else {
ConfigDetailActivity myactivity = (ConfigDetailActivity) activity;
myactivity.onBackPressed();
}
}
} else {
return false;
}
return super.onOptionsItemSelected(item);
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // This handles the qrcode results
Log.v("fwknop2", "Detail fragment activity result");
if (requestCode == 0) {

if (resultCode == Activity.RESULT_OK) {

String contents = data.getStringExtra("SCAN_RESULT");
for (String stanzas: contents.split(" ")){
String[] tmp = stanzas.split(":");
Expand Down Expand Up @@ -287,8 +297,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
View rootView = inflater.inflate(R.layout.fragment_config_detail, container, false);
active_Nick = getArguments().getString("item_id");
myJuice = new PluginContract();
// do something here to get the list of nicks


//Handlers for the input fields
txt_NickName = (TextView) rootView.findViewById(R.id.NickName);
Expand Down Expand Up @@ -317,8 +325,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
juice_adapt = new ConnectionSpinnerAdapter(getActivity());
spn_juice.setAdapter(juice_adapt);


//connectionListLoader.setOnLoadedListener(connectionListLoader.OnLoadedListener());
spn_allowip = (Spinner) rootView.findViewById(R.id.allowip);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.spinner_options, android.R.layout.simple_spinner_item);
Expand Down Expand Up @@ -348,7 +354,6 @@ public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)

if (parent.getItemAtPosition(pos).toString().equalsIgnoreCase("None")) {
lay_sshcmd.setVisibility(View.GONE);
spn_juice.setVisibility(View.GONE);
Expand Down Expand Up @@ -433,7 +438,7 @@ public void onNothingSelected(AdapterView<?> parent) {}
});

//Below is the loading of a saved config
if (active_Nick.equalsIgnoreCase("New Config")) {
if (active_Nick.equalsIgnoreCase("")) {
txt_NickName.setText("");
config.SSH_CMD = "";
txt_HMAC.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/biz/incomsystems/fwknop2/ConfigListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
*/
package biz.incomsystems.fwknop2;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import java.util.List;

Expand All @@ -44,7 +45,7 @@
public class ConfigListActivity extends FragmentActivity
implements ConfigListFragment.Callbacks {
ConfigDetailFragment fragment;
private boolean mTwoPane; // Whether in two-pane mode.
public boolean mTwoPane; // Whether in two-pane mode.

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -64,12 +65,18 @@ protected void onCreate(Bundle savedInstanceState) {
.findFragmentById(R.id.config_list))
.setActivateOnItemClick(true);
}
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean haveWeShownPreferences = prefs.getBoolean("HaveShownPrefs", false);

if (!haveWeShownPreferences) {
Intent detailIntent = new Intent(this, HelpActivity.class);
startActivity(detailIntent);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v("fwknop2", "onActivityResult in activity");
for (Fragment fragment : getSupportFragmentManager().getFragments())
// this overcomes what may be a bug in the android framework. Pushes the result into fragment so it can get to the nested class.
{
Expand Down
73 changes: 61 additions & 12 deletions app/src/main/java/biz/incomsystems/fwknop2/ConfigListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package biz.incomsystems.fwknop2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -45,6 +47,7 @@
*/
public class ConfigListFragment extends ListFragment {
public ArrayAdapter customAdapter;
private AdapterView.AdapterContextMenuInfo info;
ArrayList array_list = new ArrayList();
DBHelper mydb;
SendSPA OurSender;
Expand Down Expand Up @@ -100,6 +103,7 @@ public ConfigListFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mydb = new DBHelper(getActivity());
array_list = mydb.getAllConfigs();
OurSender = new SendSPA();
Expand All @@ -111,6 +115,12 @@ public void onCreate(Bundle savedInstanceState) {
setListAdapter(customAdapter);
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.list_menu, menu);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Expand All @@ -120,8 +130,31 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}

}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.detail_help) {
Intent detailIntent = new Intent(getActivity(), HelpActivity.class);
startActivity(detailIntent);
} else if (id == R.id.new_item) {
Activity activity = getActivity();
ConfigListActivity myactivity = (ConfigListActivity) activity;
if (myactivity.mTwoPane){
mCallbacks.onItemSelected("");
getListView().setItemChecked(mActivatedPosition, false);
} else {
Intent detailIntent = new Intent(getActivity(), ConfigDetailActivity.class);
detailIntent.putExtra(ConfigDetailFragment.ARG_ITEM_ID, "");
startActivity(detailIntent);
}


} else {
return false;
}
return true;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand All @@ -140,23 +173,23 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMen
super.onCreateContextMenu(menu, v, menuInfo);

MenuInflater inflater = this.getActivity().getMenuInflater();
inflater.inflate(R.menu.listmenu, menu);
inflater.inflate(R.menu.list_longtap_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
customAdapter = (ArrayAdapter) getListAdapter();
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String nick = ((TextView) info.targetView).getText().toString();
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete: // Deleting the selected option
mydb.deleteConfig(nick);
array_list.remove(info.position);
customAdapter.notifyDataSetChanged();
mydb.close();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();

return true;

case R.id.knock:
String nick = ((TextView) info.targetView).getText().toString();
OurSender.send(nick, getActivity());

default:
Expand All @@ -170,8 +203,27 @@ public void onUpdate() {
array_list.addAll(mydb.getAllConfigs());
customAdapter.notifyDataSetChanged();
mydb.close();
Log.v("fwknop2", "onUpdate runs");
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked

String nick = ((TextView) info.targetView).getText().toString();
mydb.deleteConfig(nick);
array_list.remove(info.position);
customAdapter.notifyDataSetChanged();

break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};

@Override
public void onAttach(Activity activity) {
Expand All @@ -198,9 +250,6 @@ public void onDetach() {
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);

// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(this.getListAdapter().getItem(position).toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.support.v4.content.Loader;

import com.sonelli.juicessh.pluginlibrary.PluginContract;
import biz.incomsystems.fwknop2.ConnectionSpinnerAdapter;

public class ConnectionListLoader implements LoaderManager.LoaderCallbacks<Cursor> {

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/biz/incomsystems/fwknop2/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public ArrayList<String> getAllConfigs() // This returns an array of Nick Name
}
res.close();
db.close();
array_list.add("New Config");
//array_list.add("New Config");
return array_list;
}

Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/biz/incomsystems/fwknop2/HelpActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package biz.incomsystems.fwknop2;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class HelpActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
TextView textView = (TextView) findViewById (R.id.help_page);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setTextColor(getResources().getColor(android.R.color.black));
textView.setText(Html.fromHtml(getString(R.string.help_page)));
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean("HaveShownPrefs", true);
ed.apply();
}
}
Loading

0 comments on commit 07c47f8

Please sign in to comment.