root / src / com / rackspacecloud / android / AddExternalNodeActivity.java @ 01340459
History | View | Annotate | Download (4.3 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.regex.Matcher; |
4 |
import java.util.regex.Pattern; |
5 |
|
6 |
import android.content.Intent; |
7 |
import android.os.Bundle; |
8 |
import android.view.View; |
9 |
import android.view.Window; |
10 |
import android.view.View.OnClickListener; |
11 |
import android.widget.AdapterView; |
12 |
import android.widget.ArrayAdapter; |
13 |
import android.widget.Button; |
14 |
import android.widget.EditText; |
15 |
import android.widget.Spinner; |
16 |
import android.widget.TextView; |
17 |
import android.widget.AdapterView.OnItemSelectedListener; |
18 |
|
19 |
public class AddExternalNodeActivity extends CloudActivity { |
20 |
|
21 |
private final String[] CONDITIONS = {"Enabled", "Disabled", "Draining"}; |
22 |
private String selectedPort; |
23 |
private String selectedIp; |
24 |
private String selectedWeight; |
25 |
private boolean weighted; |
26 |
private String selectedCondition; |
27 |
private Spinner conditionSpinner;
|
28 |
private EditText ipAddress;
|
29 |
private EditText weightText;
|
30 |
|
31 |
public void onCreate(Bundle savedInstanceState) { |
32 |
super.onCreate(savedInstanceState);
|
33 |
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
34 |
setContentView(R.layout.addexternalnode); |
35 |
weighted = (Boolean) this.getIntent().getExtras().get("weighted"); |
36 |
restoreState(savedInstanceState); |
37 |
} |
38 |
|
39 |
protected void restoreState(Bundle state) { |
40 |
super.restoreState(state);
|
41 |
setupInputs(); |
42 |
} |
43 |
|
44 |
private void setupInputs(){ |
45 |
|
46 |
ipAddress = (EditText) findViewById(R.id.ip_address); |
47 |
|
48 |
weightText = (EditText) findViewById(R.id.node_weight_text); |
49 |
|
50 |
//if algorithm is not weighted then then node's weight will be null
|
51 |
if(!weighted){
|
52 |
TextView weightLabel = (TextView) findViewById(R.id.node_weight_label); |
53 |
weightLabel.setVisibility(View.GONE);
|
54 |
weightText.setVisibility(View.GONE);
|
55 |
} |
56 |
|
57 |
loadConditionSpinner(); |
58 |
setUpButton(); |
59 |
|
60 |
} |
61 |
|
62 |
private void setUpButton(){ |
63 |
Button submit = (Button) findViewById(R.id.add_node_button); |
64 |
submit.setOnClickListener(new OnClickListener() {
|
65 |
|
66 |
@Override
|
67 |
public void onClick(View v) { |
68 |
selectedIp = ipAddress.getText().toString(); |
69 |
selectedPort = ((EditText)findViewById(R.id.node_port_text)).getText().toString(); |
70 |
selectedWeight = weightText.getText().toString(); |
71 |
if(!validPort(selectedPort)){
|
72 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
73 |
} else if(!(weightText.getVisibility() == View.GONE || (weightText.getVisibility() != View.GONE && validWeight(selectedWeight)))){ |
74 |
showAlert("Error", "Weight must be between 1 and 100."); |
75 |
} else if(ipAddress.getText().toString().equals("")){ |
76 |
//TODO use regex to validate the ip for IPV4 and IPV6
|
77 |
showAlert("Error", "Enter an IP Address"); |
78 |
} else if(validIp(ipAddress.getText().toString())) { |
79 |
showAlert("Error", "Enter a valid IP Address"); |
80 |
} else {
|
81 |
Intent data = new Intent();
|
82 |
data.putExtra("nodeIp", selectedIp);
|
83 |
data.putExtra("nodePort", selectedPort);
|
84 |
data.putExtra("nodeCondition", selectedCondition);
|
85 |
data.putExtra("nodeWeight", selectedWeight);
|
86 |
setResult(RESULT_OK, data); |
87 |
finish(); |
88 |
} |
89 |
|
90 |
} |
91 |
}); |
92 |
} |
93 |
|
94 |
public void onBackPressed(){ |
95 |
setResult(RESULT_CANCELED); |
96 |
finish(); |
97 |
} |
98 |
|
99 |
private void loadConditionSpinner(){ |
100 |
conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner); |
101 |
|
102 |
ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS); |
103 |
conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
104 |
conditionSpinner.setAdapter(conditionAdapter); |
105 |
|
106 |
conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
|
107 |
|
108 |
@Override
|
109 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
110 |
selectedCondition = CONDITIONS[pos]; |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void onNothingSelected(AdapterView<?> arg0) { |
115 |
|
116 |
} |
117 |
|
118 |
}); |
119 |
} |
120 |
|
121 |
//basic ip validation just checks that the string
|
122 |
//is only composed of letters, numbers, ., :
|
123 |
private static boolean validIp(String ip){ |
124 |
//Enter regex
|
125 |
Pattern pattern = Pattern.compile("[a-zA-Z0-9.:]+"); |
126 |
Matcher match = pattern.matcher(ip);
|
127 |
return match.matches();
|
128 |
} |
129 |
|
130 |
private boolean validPort(String port){ |
131 |
return !port.equals("") && Integer.valueOf(port) > 0 && Integer.valueOf(port) < 65536; |
132 |
} |
133 |
|
134 |
private Boolean validWeight(String weight){ |
135 |
if(weight.equals("")){ |
136 |
return false; |
137 |
} |
138 |
else{
|
139 |
int w = Integer.valueOf(weight); |
140 |
return w >= 1 && w <= 100 ; |
141 |
} |
142 |
} |
143 |
|
144 |
} |
145 |
|