2 * Copyright © 2020 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.AltosDroid;
22 import android.app.Activity;
23 import android.content.*;
25 import android.util.*;
26 import android.view.*;
27 import android.view.View.*;
28 import android.widget.*;
29 import android.graphics.*;
30 import android.graphics.drawable.*;
31 import android.widget.CompoundButton.OnCheckedChangeListener;
33 import org.altusmetrum.altoslib_14.*;
35 class TrackerComparatorCall implements Comparator<Tracker> {
36 public int compare(Tracker a, Tracker b) {
45 v = a.compareSerial(b);
48 return a.compareFrequency(b);
50 public boolean equals(Object o) {
51 return o instanceof TrackerComparatorCall;
55 class TrackerComparatorSerial implements Comparator<Tracker> {
56 public int compare(Tracker a, Tracker b) {
59 v = a.compareSerial(b);
68 return a.compareFrequency(b);
70 public boolean equals(Object o) {
71 return o instanceof TrackerComparatorSerial;
75 class TrackerComparatorAge implements Comparator<Tracker> {
76 public int compare(Tracker a, Tracker b) {
85 v = a.compareSerial(b);
88 return a.compareFrequency(b);
90 public boolean equals(Object o) {
91 return o instanceof TrackerComparatorAge;
95 class TrackerComparatorFrequency implements Comparator<Tracker> {
96 public int compare(Tracker a, Tracker b) {
99 v = a.compareFrequency(b);
105 v = a.compareCall(b);
108 return a.compareSerial(b);
110 public boolean equals(Object o) {
111 return o instanceof TrackerComparatorFrequency;
115 public class SelectTrackerActivity extends Activity implements OnTouchListener {
116 // Return Intent extra
117 public static final String EXTRA_SERIAL_NUMBER = "serial_number";
118 public static final String EXTRA_FREQUENCY = "frequency";
120 private int button_ids[] = {
123 R.id.frequency_button,
127 private static final int call_button = 0;
128 private static final int serial_button = 1;
129 private static final int freq_button = 2;
130 private static final int age_button = 3;
131 private RadioButton radio_buttons[] = new RadioButton[4];
132 private TableLayout table;
134 private Tracker[] trackers;
136 private void set_sort(int id) {
137 AltosDroidPreferences.set_tracker_sort(id);
141 private void resort() {
142 Comparator<Tracker> compare;
143 int tracker_sort = AltosDroidPreferences.tracker_sort();
144 AltosDebug.debug("sort %d", tracker_sort);
145 switch (tracker_sort) {
148 compare = new TrackerComparatorCall();
151 compare = new TrackerComparatorSerial();
154 compare = new TrackerComparatorFrequency();
157 compare = new TrackerComparatorAge();
160 Arrays.sort(trackers, compare);
164 void init_button_state() {
165 int tracker_sort = AltosDroidPreferences.tracker_sort();
166 for (int i = 0; i < 4; i++)
167 radio_buttons[i].setChecked(i == tracker_sort);
170 OnCheckedChangeListener button_listener = new OnCheckedChangeListener() {
172 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
173 int id = buttonView.getId();
176 for (int i = 0; i < 4; i++) {
177 if (id == button_ids[i])
180 radio_buttons[i].setChecked(false);
191 insert_tracker(Tracker tracker) {
192 TableRow row = (TableRow) getLayoutInflater().inflate(R.layout.tracker_ent, null);
194 ((TextView) row.findViewById(R.id.call_view)).setText(tracker.call);
195 if (tracker.serial == 0)
196 ((TextView) row.findViewById(R.id.serial_view)).setText("");
198 ((TextView) row.findViewById(R.id.serial_view)).setText(String.format("%d", tracker.serial));
199 if (tracker.frequency == 0.0)
200 ((TextView) row.findViewById(R.id.frequency_view)).setText("");
201 else if (tracker.frequency == AltosLib.MISSING)
202 ((TextView) row.findViewById(R.id.frequency_view)).setText("");
204 ((TextView) row.findViewById(R.id.frequency_view)).setText(String.format("%7.3f", tracker.frequency));
205 if (tracker.received_time != 0) {
206 int age = (int) ((start_time - tracker.received_time + 500) / 1000);
207 ((TextView) row.findViewById(R.id.age_view)).setText(AltosDroid.age_string(age));
209 ((TextView) row.findViewById(R.id.age_view)).setText("");
211 row.setClickable(true);
212 row.setOnTouchListener(this);
216 private void set_trackers() {
217 for (int i = table.getChildCount() - 1; i >= 1; i--)
218 table.removeViewAt(i);
219 for (Tracker tracker : trackers)
220 insert_tracker(tracker);
223 private void done(View v) {
224 int result = Activity.RESULT_CANCELED;
225 Intent intent = new Intent();
226 for (int i = 1; i < table.getChildCount(); i++) {
227 View child = table.getChildAt(i);
229 Tracker tracker = trackers[i - 1];
230 intent.putExtra(EXTRA_SERIAL_NUMBER, tracker.serial);
231 intent.putExtra(EXTRA_FREQUENCY, tracker.frequency);
232 result = Activity.RESULT_OK;
236 setResult(Activity.RESULT_OK, intent);
241 protected void onCreate(Bundle savedInstanceState) {
242 int title_id = getIntent().getIntExtra(AltosDroid.EXTRA_TRACKERS_TITLE, R.id.select_tracker);
243 AltosDebug.debug("get title id 0x%x %s", title_id, getResources().getText(title_id));
244 setTitle(getResources().getText(title_id));
245 setTheme(AltosDroid.dialog_themes[AltosDroidPreferences.font_size()]);
246 super.onCreate(savedInstanceState);
248 setContentView(R.layout.tracker_list);
249 // Set result CANCELED incase the user backs out
250 setResult(Activity.RESULT_CANCELED);
252 for (int i = 0; i < 4; i++) {
253 radio_buttons[i] = (RadioButton) findViewById(button_ids[i]);
254 radio_buttons[i].setOnCheckedChangeListener(button_listener);
257 ArrayList<Parcelable> tracker_array = (ArrayList<Parcelable>) getIntent().getParcelableArrayListExtra(AltosDroid.EXTRA_TRACKERS);
258 if (tracker_array != null) {
259 Object[] array = tracker_array.toArray();
260 trackers = new Tracker[array.length];
261 for (int i = 0; i < array.length; i++)
262 trackers[i] = (Tracker) array[i];
265 start_time = System.currentTimeMillis();
267 table = (TableLayout) findViewById(R.id.tracker_list);
277 public boolean onTouch(View v, MotionEvent event) {
278 int action = event.getAction() & MotionEvent.ACTION_MASK;
280 case MotionEvent.ACTION_UP:
281 case MotionEvent.ACTION_CANCEL:
282 case MotionEvent.ACTION_OUTSIDE:
283 v.setBackgroundColor(0);
286 case MotionEvent.ACTION_DOWN:
287 v.setBackgroundColor(Color.RED);
291 if (action == MotionEvent.ACTION_UP) {