Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 8 | PointedEar | 1 | package de.pointedears.converter.app; |
| 5 | PointedEar | 2 | |
| 6 | PointedEar | 3 | import java.util.HashMap; |
| 4 | |||
| 5 | PointedEar | 5 | import android.app.Activity; |
| 6 | import android.os.Bundle; |
||
| 6 | PointedEar | 7 | import android.text.Editable; |
| 8 | import android.view.KeyEvent; |
||
| 9 | import android.view.View; |
||
| 13 | PointedEar | 10 | import android.view.View.OnClickListener; |
| 6 | PointedEar | 11 | import android.view.View.OnKeyListener; |
| 12 | import android.widget.AdapterView; |
||
| 13 | import android.widget.AdapterView.OnItemSelectedListener; |
||
| 13 | PointedEar | 14 | import android.widget.Button; |
| 6 | PointedEar | 15 | import android.widget.EditText; |
| 16 | import android.widget.Spinner; |
||
| 8 | PointedEar | 17 | import de.pointedears.converter.R; |
| 5 | PointedEar | 18 | |
| 6 | PointedEar | 19 | /** |
| 20 | * Activity that implements length conversion |
||
| 21 | * |
||
| 22 | * @author pelinux |
||
| 23 | */ |
||
| 5 | PointedEar | 24 | public class LengthsActivity extends Activity |
| 25 | { |
||
| 6 | PointedEar | 26 | /* |
| 27 | * Constants for mapping value strings to internal IDs |
||
| 28 | */ |
||
| 29 | private static final String VALUE_INCHES = "inch"; //$NON-NLS-1$ |
||
| 7 | PointedEar | 30 | private static final String VALUE_KILOMETERS = "km"; //$NON-NLS-1$ |
| 6 | PointedEar | 31 | private static final String VALUE_METERS = "m"; //$NON-NLS-1$ |
| 32 | private static final String VALUE_MILES = "mi"; //$NON-NLS-1$ |
||
| 33 | private static final int ITEM_INCHES = 0; |
||
| 7 | PointedEar | 34 | private static final int ITEM_KILOMETERS = 1; |
| 35 | private static final int ITEM_METERS = 2; |
||
| 36 | private static final int ITEM_MILES = 3; |
||
| 6 | PointedEar | 37 | |
| 38 | /** |
||
| 39 | * Maps value strings to internal IDs |
||
| 40 | */ |
||
| 41 | private final static HashMap<String, Integer> valueToId = |
||
| 42 | new HashMap<String, Integer>(); |
||
| 43 | static |
||
| 44 | { |
||
| 45 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
||
| 46 | LengthsActivity.ITEM_INCHES); |
||
| 7 | PointedEar | 47 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
| 48 | LengthsActivity.ITEM_KILOMETERS); |
||
| 6 | PointedEar | 49 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
| 50 | LengthsActivity.ITEM_METERS); |
||
| 51 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
||
| 52 | LengthsActivity.ITEM_MILES); |
||
| 53 | } |
||
| 54 | |||
| 7 | PointedEar | 55 | /* Unit spinners (dropdowns) */ |
| 56 | private Spinner spinnerUnit1; |
||
| 57 | private Spinner spinnerUnit2; |
||
| 58 | |||
| 5 | PointedEar | 59 | /** Called when the activity is first created. */ |
| 60 | |||
| 61 | @Override |
||
| 62 | public void onCreate(Bundle savedInstanceState) |
||
| 63 | { |
||
| 64 | super.onCreate(savedInstanceState); |
||
| 6 | PointedEar | 65 | this.setContentView(R.layout.activity_lengths); |
| 66 | |||
| 67 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
||
| 68 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
||
| 69 | |||
| 70 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
||
| 71 | @Override |
||
| 72 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
| 73 | { |
||
| 74 | Editable editable1 = ((EditText) v).getText(); |
||
| 75 | |||
| 7 | PointedEar | 76 | Double value1; |
| 6 | PointedEar | 77 | try |
| 78 | { |
||
| 79 | value1 = Double.parseDouble(editable1.toString()); |
||
| 80 | } |
||
| 81 | catch (NumberFormatException e) |
||
| 82 | { |
||
| 7 | PointedEar | 83 | value1 = null; |
| 6 | PointedEar | 84 | } |
| 85 | |||
| 86 | String string2 = ""; //$NON-NLS-1$ |
||
| 7 | PointedEar | 87 | if (value1 != null) |
| 6 | PointedEar | 88 | { |
| 89 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
||
| 90 | } |
||
| 91 | |||
| 92 | editValue2.setText(string2); |
||
| 93 | |||
| 94 | return false; |
||
| 95 | } |
||
| 96 | }; |
||
| 97 | editValue1.setOnKeyListener(editValue1OnKey); |
||
| 98 | |||
| 99 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
||
| 100 | @Override |
||
| 101 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
| 102 | { |
||
| 103 | Editable editable2 = ((EditText) v).getText(); |
||
| 104 | |||
| 7 | PointedEar | 105 | Double value2; |
| 6 | PointedEar | 106 | try |
| 107 | { |
||
| 108 | value2 = Double.parseDouble(editable2.toString()); |
||
| 109 | } |
||
| 110 | catch (NumberFormatException e) |
||
| 111 | { |
||
| 7 | PointedEar | 112 | value2 = null; |
| 6 | PointedEar | 113 | } |
| 114 | |||
| 115 | String string1 = ""; //$NON-NLS-1$ |
||
| 7 | PointedEar | 116 | if (value2 != null) |
| 6 | PointedEar | 117 | { |
| 118 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
||
| 119 | } |
||
| 120 | |||
| 121 | editValue1.setText(string1); |
||
| 122 | |||
| 123 | return false; |
||
| 124 | } |
||
| 125 | }; |
||
| 126 | editValue2.setOnKeyListener(editValue2OnKey); |
||
| 127 | |||
| 128 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
||
| 129 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
||
| 130 | |||
| 131 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
||
| 132 | @Override |
||
| 133 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
||
| 134 | long arg3) |
||
| 135 | { |
||
| 136 | /* Simulate input in second EditText so that first EditText is updated */ |
||
| 137 | editValue2OnKey.onKey(editValue2, 0, null); |
||
| 138 | } |
||
| 139 | |||
| 140 | @Override |
||
| 141 | public void onNothingSelected(AdapterView<?> arg0) |
||
| 142 | { |
||
| 143 | /* no-op */ |
||
| 144 | } |
||
| 145 | }); |
||
| 146 | |||
| 7 | PointedEar | 147 | this.spinnerUnit2.setSelection(1); |
| 6 | PointedEar | 148 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
| 149 | @Override |
||
| 150 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
||
| 151 | long arg3) |
||
| 152 | { |
||
| 153 | /* Simulate input in first EditText so that second EditText is updated */ |
||
| 154 | editValue1OnKey.onKey(editValue1, 0, null); |
||
| 155 | } |
||
| 156 | |||
| 157 | @Override |
||
| 158 | public void onNothingSelected(AdapterView<?> arg0) |
||
| 159 | { |
||
| 160 | /* no-op */ |
||
| 161 | } |
||
| 162 | }); |
||
| 13 | PointedEar | 163 | |
| 164 | Button buttonClear = |
||
| 165 | (Button) this.findViewById(R.id.lengths_button_clear); |
||
| 166 | buttonClear.setOnClickListener(new OnClickListener() { |
||
| 167 | |||
| 168 | @SuppressWarnings("nls") |
||
| 169 | @Override |
||
| 170 | public void onClick(View v) |
||
| 171 | { |
||
| 172 | editValue1.setText(""); |
||
| 173 | editValue2.setText(""); |
||
| 174 | } |
||
| 175 | }); |
||
| 5 | PointedEar | 176 | } |
| 177 | |||
| 6 | PointedEar | 178 | /** |
| 179 | * @param value |
||
| 180 | * @return |
||
| 181 | */ |
||
| 182 | private String getConvertedValue(double value, boolean reverse) |
||
| 183 | { |
||
| 184 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
||
| 185 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
||
| 186 | String[] itemArray = |
||
| 187 | this.getResources().getStringArray(R.array.length_units_values); |
||
| 188 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
||
| 189 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
||
| 190 | |||
| 191 | if (reverse) |
||
| 192 | { |
||
| 193 | String tmp = selectedItemValue1; |
||
| 194 | selectedItemValue1 = selectedItemValue2; |
||
| 195 | selectedItemValue2 = tmp; |
||
| 196 | } |
||
| 197 | |||
| 198 | int itemId1 = LengthsActivity.valueToId.get(selectedItemValue1); |
||
| 199 | int itemId2 = LengthsActivity.valueToId.get(selectedItemValue2); |
||
| 200 | |||
| 201 | Double newValue = value; |
||
| 202 | |||
| 203 | switch (itemId1) |
||
| 204 | { |
||
| 205 | case ITEM_INCHES: |
||
| 206 | switch (itemId2) |
||
| 207 | { |
||
| 7 | PointedEar | 208 | case ITEM_KILOMETERS: |
| 209 | /* see ITEM_METERS */ |
||
| 210 | newValue = new Double((value * 0.0254) / 1000); |
||
| 211 | break; |
||
| 212 | |||
| 6 | PointedEar | 213 | case ITEM_METERS: |
| 214 | newValue = new Double(value * 0.0254); |
||
| 215 | break; |
||
| 216 | |||
| 217 | case ITEM_MILES: |
||
| 218 | /* 12 in/ft and 5280 ft/mi */ |
||
| 219 | newValue = new Double(value / 12 / 5280); |
||
| 220 | break; |
||
| 7 | PointedEar | 221 | } |
| 222 | break; |
||
| 6 | PointedEar | 223 | |
| 7 | PointedEar | 224 | case ITEM_KILOMETERS: |
| 225 | switch (itemId2) |
||
| 226 | { |
||
| 227 | case ITEM_INCHES: |
||
| 228 | /* 1 m = 39.370 in */ |
||
| 229 | newValue = new Double(value * 1000 * 39.370); |
||
| 6 | PointedEar | 230 | break; |
| 7 | PointedEar | 231 | |
| 232 | case ITEM_METERS: |
||
| 233 | newValue = new Double(value * 1000); |
||
| 234 | break; |
||
| 235 | |||
| 236 | case ITEM_MILES: |
||
| 237 | newValue = new Double(value / 1.609344); |
||
| 238 | break; |
||
| 6 | PointedEar | 239 | } |
| 240 | break; |
||
| 241 | |||
| 242 | case ITEM_METERS: |
||
| 243 | switch (itemId2) |
||
| 244 | { |
||
| 245 | case ITEM_INCHES: |
||
| 246 | /* 1 m = 39.370 in */ |
||
| 247 | newValue = new Double(value * 39.370); |
||
| 248 | break; |
||
| 249 | |||
| 7 | PointedEar | 250 | case ITEM_KILOMETERS: |
| 251 | newValue = new Double(value / 1000); |
||
| 252 | break; |
||
| 253 | |||
| 6 | PointedEar | 254 | case ITEM_MILES: |
| 255 | /* 1 mi = 1609.344 m */ |
||
| 256 | newValue = new Double(value / 1609.344); |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | break; |
||
| 260 | |||
| 261 | case ITEM_MILES: |
||
| 262 | switch (itemId2) |
||
| 263 | { |
||
| 264 | case ITEM_INCHES: |
||
| 265 | /* 1 mi = 5280 ft, 1 ft = 12 in */ |
||
| 266 | newValue = new Double(value * 5280 * 12); |
||
| 267 | break; |
||
| 268 | |||
| 269 | case ITEM_KILOMETERS: |
||
| 270 | newValue = new Double(value * 1.609344); |
||
| 271 | break; |
||
| 272 | |||
| 273 | case ITEM_METERS: |
||
| 7 | PointedEar | 274 | newValue = new Double(value * 1609.344); |
| 6 | PointedEar | 275 | break; |
| 276 | } |
||
| 7 | PointedEar | 277 | break; |
| 6 | PointedEar | 278 | } |
| 279 | |||
| 280 | return newValue.toString(); |
||
| 281 | } |
||
| 5 | PointedEar | 282 | } |