3 import android.content.Context;
\r
4 import android.os.Bundle;
\r
5 import android.text.Editable;
\r
6 import android.text.TextWatcher;
\r
7 import android.view.View;
\r
8 import android.view.View.OnClickListener;
\r
9 import android.view.WindowManager.LayoutParams;
\r
10 import android.widget.Button;
\r
11 import android.widget.CheckBox;
\r
12 import android.widget.EditText;
\r
14 import com.td.db.Train;
\r
15 import com.td.db.TrainDao;
\r
16 import com.td.service.TrainDelayedServiceImpl;
\r
17 import com.td.utility.BaseActivity;
\r
18 import com.td.utility.EditPrefUtil;
\r
19 import com.td.utility.Util;
\r
20 import com.td.webapi.RouteUpdater;
\r
22 public class TrainEditActivity extends BaseActivity {
\r
23 public static final String PREF_TRE_ID = "TRE_ID";
\r
24 public static final String PREF_TRE_NAME = "TRE_NAME";
\r
25 public static final String PREF_TRE_SNAME = "TRE_SNAME";
\r
27 public static final String JR = "JR";
\r
28 public static final String LINE = "
\90ü";
\r
29 private EditText editText1;
\r
30 private EditText editText2;
\r
31 private CheckBox checkBox1;
\r
32 private CheckBox checkBox2;
\r
33 private Button button1;
\r
34 private Button button2;
\r
35 private Button button3;
\r
37 private EditPrefUtil pref = new EditPrefUtil(this);
\r
38 private Train train = null;
\r
41 public void onCreate(Bundle savedInstanceState) {
\r
42 super.onCreate(savedInstanceState);
\r
43 getWindow().setSoftInputMode(
\r
44 LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
\r
45 setContentView(R.layout.train_edit);
\r
47 editText1 = (EditText) findViewById(R.id.editText31);
\r
48 editText2 = (EditText) findViewById(R.id.editText32);
\r
49 checkBox1 = (CheckBox) findViewById(R.id.checkBox31);
\r
50 checkBox2 = (CheckBox) findViewById(R.id.checkBox32);
\r
51 button1 = (Button) findViewById(R.id.button31);
\r
52 button2 = (Button) findViewById(R.id.button32);
\r
53 button3 = (Button) findViewById(R.id.button33);
\r
55 editText1.setEnabled(false);
\r
57 editText2.addTextChangedListener(new TextWatcher() {
\r
58 public void afterTextChanged(Editable editable) {
\r
63 public void beforeTextChanged(CharSequence s, int start, int count,
\r
68 public void onTextChanged(CharSequence s, int start, int before,
\r
74 checkBox1.setOnClickListener(new OnClickListener() {
\r
75 public void onClick(View view) {
\r
80 checkBox2.setOnClickListener(new OnClickListener() {
\r
81 public void onClick(View view) {
\r
86 button1.setOnClickListener(new OnClickListener() {
\r
87 public void onClick(View view) {
\r
88 long id = train.getId();
\r
89 String sname = train.getSearchText();
\r
90 TrainDao.getInstance().set(id, sname, true);
\r
92 TrainDelayedServiceImpl.clearSchedule(pref);
\r
94 setResult(RESULT_OK);
\r
98 button2.setOnClickListener(new OnClickListener() {
\r
99 public void onClick(View view) {
\r
100 setResult(RESULT_CANCELED);
\r
104 button3.setOnClickListener(new OnClickListener() {
\r
105 public void onClick(View view) {
\r
106 String name = train.getName();
\r
107 String sname = RouteUpdater.filter(name);
\r
108 editText2.setText(sname);
\r
109 train.setSearchText(sname);
\r
117 protected void onResume() {
\r
124 String idStr = pref.get(PREF_TRE_ID);
\r
125 if (idStr == null) {
\r
126 Bundle extras = getIntent().getExtras();
\r
127 long id = extras.getLong("ID");
\r
128 train = TrainDao.getInstance().searchById(id);
\r
129 if (train != null) {
\r
130 name = train.getName();
\r
131 sname = train.getSearchText();
\r
134 long id = Long.valueOf(idStr);
\r
135 name = pref.get(PREF_TRE_NAME);
\r
136 sname = pref.get(PREF_TRE_SNAME);
\r
137 train = new Train(id, name, sname, Util.toInt(false));
\r
139 editText1.setText(name);
\r
140 editText2.setText(sname);
\r
142 } catch (Exception e) {
\r
144 e.printStackTrace();
\r
149 protected void onPause() {
\r
153 if (train != null) {
\r
154 pref.put(PREF_TRE_ID, String.valueOf(train.getId()));
\r
155 pref.put(PREF_TRE_NAME, train.getName());
\r
156 pref.put(PREF_TRE_SNAME, train.getSearchText());
\r
159 } catch (Exception e) {
\r
161 e.printStackTrace();
\r
165 private void editText() {
\r
166 if (train == null){
\r
169 String sname = editText2.getText().toString();
\r
170 train.setSearchText(sname);
\r
173 private void editLine() {
\r
175 if (train == null) {
\r
178 String sname = train.getSearchText();
\r
179 if (checkBox1.isChecked()) {
\r
180 int len = sname.length();
\r
181 int slen = LINE.length();
\r
183 && sname.substring(len - slen, len).equals(LINE)) {
\r
184 sname = sname.substring(0, len - slen);
\r
189 editText2.setText(sname);
\r
190 train.setSearchText(sname);
\r
191 } catch (Exception e) {
\r
193 e.printStackTrace();
\r
197 private void editJR() {
\r
199 if (train == null) {
\r
202 String sname = train.getSearchText();
\r
203 if (checkBox2.isChecked()) {
\r
204 int len = sname.length();
\r
205 int slen = JR.length();
\r
206 if (len >= slen && sname.substring(0, slen).equals(JR)) {
\r
207 sname = sname.substring(slen, len);
\r
210 sname = JR + sname;
\r
212 editText2.setText(sname);
\r
213 train.setSearchText(sname);
\r
214 } catch (Exception e) {
\r
216 e.printStackTrace();
\r
220 private void initStatus() {
\r
222 String name = train.getName();
\r
223 String sname = train.getSearchText();
\r
225 int len = name.length();
\r
226 String ch = name.substring(len - 1, len);
\r
227 if (!ch.equals(LINE)) {
\r
228 checkBox1.setEnabled(false);
\r
230 len = sname.length();
\r
232 if (sname.length() > 1) {
\r
233 ch2 = sname.substring(len - 1, len);
\r
235 checkBox1.setChecked(!ch2.equals(LINE));
\r
238 ch = name.substring(0, 2);
\r
239 if (!ch.equals(JR)) {
\r
240 checkBox2.setEnabled(false);
\r
243 if (sname.length() > 2) {
\r
244 ch2 = sname.substring(0, 2);
\r
246 checkBox2.setChecked(!ch2.equals(JR));
\r
248 } catch (Exception e) {
\r
250 e.printStackTrace();
\r
254 private void setStatus() {
\r
255 if (button1 == null) {
\r
258 if (train != null && train.getSearchText().length() > 0) {
\r
259 button1.setEnabled(true);
\r
261 button1.setEnabled(false);
\r
265 public static void init(Context context) {
\r
266 EditPrefUtil pref = new EditPrefUtil(context);
\r
267 pref.remove(PREF_TRE_ID);
\r