14ea814e93148acc7f61567ef654b5d19b69baab
[fw/altos] / altosdroid / app / src / main / java / org / altusmetrum / AltosDroid / IdleModeActivity.java
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.AltosDroid;
20
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.Window;
26 import android.view.View.OnClickListener;
27 import android.widget.*;
28
29 import org.altusmetrum.altoslib_13.*;
30
31 public class IdleModeActivity extends Activity {
32         private EditText callsign;
33         private Button connect;
34         private Button disconnect;
35         private Button reboot;
36         private Button igniters;
37
38         public static final String EXTRA_IDLE_MODE = "idle_mode";
39         public static final String EXTRA_IDLE_RESULT = "idle_result";
40
41         public static final int IDLE_MODE_CONNECT = 1;
42         public static final int IDLE_MODE_REBOOT = 2;
43         public static final int IDLE_MODE_IGNITERS = 3;
44         public static final int IDLE_MODE_DISCONNECT = 4;
45
46         private void done(int type) {
47                 AltosPreferences.set_callsign(callsign());
48                 Intent intent = new Intent();
49                 intent.putExtra(EXTRA_IDLE_RESULT, type);
50                 setResult(Activity.RESULT_OK, intent);
51                 finish();
52         }
53
54         private String callsign() {
55                 return callsign.getEditableText().toString();
56         }
57
58         public void connect_idle() {
59                 done(IDLE_MODE_CONNECT);
60         }
61
62         public void disconnect_idle() {
63                 AltosDebug.debug("Disconnect idle button pressed");
64                 done(IDLE_MODE_DISCONNECT);
65         }
66
67         public void reboot_idle() {
68                 done(IDLE_MODE_REBOOT);
69         }
70
71         public void igniters_idle() {
72                 done(IDLE_MODE_IGNITERS);
73         }
74
75         @Override
76         protected void onCreate(Bundle savedInstanceState) {
77                 setTheme(AltosDroid.dialog_themes[AltosDroidPreferences.font_size()]);
78                 super.onCreate(savedInstanceState);
79
80                 // Setup the window
81                 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
82                 setContentView(R.layout.idle_mode);
83
84                 callsign = (EditText) findViewById(R.id.set_callsign);
85                 callsign.setText(new StringBuffer(AltosPreferences.callsign()));
86
87                 connect = (Button) findViewById(R.id.connect_idle);
88                 connect.setOnClickListener(new OnClickListener() {
89                                 public void onClick(View v) {
90                                         connect_idle();
91                                 }
92                         });
93                 disconnect = (Button) findViewById(R.id.disconnect_idle);
94                 disconnect.setOnClickListener(new OnClickListener() {
95                                 public void onClick(View v) {
96                                         disconnect_idle();
97                                 }
98                         });
99
100                 boolean idle_mode = getIntent().getBooleanExtra(AltosDroid.EXTRA_IDLE_MODE, false);
101
102                 if (idle_mode)
103                         connect.setVisibility(View.GONE);
104                 else
105                         disconnect.setVisibility(View.GONE);
106
107                 reboot = (Button) findViewById(R.id.reboot_idle);
108                 reboot.setOnClickListener(new OnClickListener() {
109                                 public void onClick(View v) {
110                                         reboot_idle();
111                                 }
112                         });
113                 igniters = (Button) findViewById(R.id.igniters_idle);
114                 igniters.setOnClickListener(new OnClickListener() {
115                                 public void onClick(View v) {
116                                         igniters_idle();
117                                 }
118                         });
119
120                 // Set result CANCELED incase the user backs out
121                 setResult(Activity.RESULT_CANCELED);
122         }
123 }