Bump java lib versions in preparation for 1.9.2
[fw/altos] / altosdroid / app / src / main / java / org / altusmetrum / AltosDroid / Tracker.java
1 /*
2  * Copyright © 2020 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 java.lang.ref.WeakReference;
22 import java.util.*;
23
24 import android.Manifest;
25 import android.app.Activity;
26 import android.app.PendingIntent;
27 import android.bluetooth.BluetoothAdapter;
28 import android.content.Intent;
29 import android.content.Context;
30 import android.content.ComponentName;
31 import android.content.ServiceConnection;
32 import android.content.DialogInterface;
33 import android.os.IBinder;
34 import android.os.Bundle;
35 import android.os.Handler;
36 import android.os.Message;
37 import android.os.Messenger;
38 import android.os.RemoteException;
39 import android.os.Parcelable;
40 import android.os.Parcel;
41 import androidx.fragment.app.FragmentActivity;
42 import androidx.fragment.app.FragmentManager;
43 import android.view.*;
44 import android.widget.*;
45 import android.app.AlertDialog;
46 import android.location.Location;
47 import android.location.LocationManager;
48 import android.location.LocationListener;
49 import android.hardware.usb.*;
50 import android.content.pm.PackageManager;
51 import androidx.core.app.ActivityCompat;
52 import org.altusmetrum.altoslib_14.*;
53
54 public class Tracker implements CharSequence, Comparable, Parcelable {
55         int     serial;
56         String  call;
57         double  frequency;
58         long    received_time;
59         String  display;
60
61         private void make_display() {
62                 if (frequency == 0.0)
63                         display = "Auto";
64                 else if (frequency == AltosLib.MISSING) {
65                         display = String.format("%-8.8s  %6d", call, serial);
66                 } else {
67                         display = String.format("%-8.8s %7.3f %6d", call, frequency, serial);
68                 }
69         }
70
71         public Tracker(int serial, String call, double frequency, long received_time) {
72                 if (call == null)
73                         call = "none";
74
75                 this.serial = serial;
76                 this.call = call;
77                 this.frequency = frequency;
78                 this.received_time = received_time;
79                 make_display();
80         }
81
82         public Tracker(int serial, String call, double frequency) {
83                 this(serial, call, frequency, 0);
84         }
85
86         public Tracker(AltosState s) {
87                 this(s == null ? 0 : s.cal_data().serial,
88                      s == null ? null : s.cal_data().callsign,
89                      s == null ? 0.0 : s.frequency,
90                      s == null ? 0 : s.received_time);
91         }
92
93         /* CharSequence */
94         public char charAt(int index) {
95                 return display.charAt(index);
96         }
97
98         public int length() {
99                 return display.length();
100         }
101
102         public CharSequence subSequence(int start, int end) throws IndexOutOfBoundsException {
103                 return display.subSequence(start, end);
104         }
105
106         public String toString() {
107                 return display.toString();
108         }
109
110         /* Comparable */
111         public int compareTo (Object other) {
112                 Tracker o = (Tracker) other;
113                 if (frequency == 0.0) {
114                         if (o.frequency == 0.0)
115                                 return 0;
116                         return -1;
117                 }
118                 if (o.frequency == 0.0)
119                         return 1;
120
121                 int     a = serial - o.serial;
122                 int     b = call.compareTo(o.call);
123                 int     c = (int) Math.signum(frequency - o.frequency);
124
125                 if (b != 0)
126                         return b;
127                 if (c != 0)
128                         return c;
129                 return a;
130         }
131
132         /* Parcelable */
133
134         public int describeContents() {
135                 AltosDebug.debug("describe contents %d", serial);
136                 return 0;
137         }
138
139         public void writeToParcel(Parcel out, int flags) {
140                 AltosDebug.debug("write to parcel %s", display);
141                 out.writeInt(serial);
142                 out.writeString(call);
143                 out.writeDouble(frequency);
144                 out.writeLong(received_time);
145         }
146
147         public static final Parcelable.Creator<Tracker> CREATOR
148         = new Parcelable.Creator<Tracker>() {
149                         public Tracker createFromParcel(Parcel in) {
150                                 AltosDebug.debug("createFromParcel");
151                                 return new Tracker(in);
152                         }
153
154                         public Tracker[] newArray(int size) {
155                                 AltosDebug.debug("newArray %d", size);
156                                 return new Tracker[size];
157                         }
158                 };
159
160         /* newer (-1), same (0), older(1) */
161         public int compareAge(Tracker o) {
162                 if (received_time == o.received_time)
163                         return 0;
164                 if (received_time == 0)
165                         return -1;
166                 if (o.received_time == 0)
167                         return 1;
168                 if (received_time > o.received_time)
169                         return -1;
170                 return 1;
171         }
172
173         public int compareCall(Tracker o) {
174                 int v = call.compareTo(o.call);
175                 if (v == 0)
176                         return v;
177                 if (call.equals("auto"))
178                         return -1;
179                 if (o.call.equals("auto"))
180                         return 1;
181                 return v;
182         }
183
184         public int compareSerial(Tracker o) {
185                 return serial - o.serial;
186         }
187
188         public int compareFrequency(Tracker o) {
189                 return (int) Math.signum(frequency - o.frequency);
190         }
191
192         private Tracker(Parcel in) {
193                 serial = in.readInt();
194                 call = in.readString();
195                 frequency = in.readDouble();
196                 received_time = in.readLong();
197                 make_display();
198                 AltosDebug.debug("Create from parcel %s", display);
199         }
200 }
201