You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.3 KiB
106 lines
3.3 KiB
package dev.garrettmills.starship.hyperlink;
|
|
|
|
import android.app.Notification;
|
|
import android.app.PendingIntent;
|
|
import android.app.Service;
|
|
import android.content.Intent;
|
|
import android.os.Handler;
|
|
import android.os.IBinder;
|
|
import android.os.Looper;
|
|
import android.util.Log;
|
|
import android.widget.Toast;
|
|
|
|
import com.android.volley.Response;
|
|
import com.android.volley.VolleyError;
|
|
|
|
import java.sql.Time;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
import dev.garrettmills.starship.hyperlink.relay.ServerSentRequest;
|
|
import dev.garrettmills.starship.hyperlink.util.APIv1;
|
|
import dev.garrettmills.starship.hyperlink.util.GsonRequest;
|
|
import dev.garrettmills.starship.hyperlink.util.NotAuthenticatedException;
|
|
|
|
public class MessagingService extends Service {
|
|
private final Handler handler = new Handler();
|
|
private final Timer timer = new Timer();
|
|
|
|
public MessagingService() {
|
|
}
|
|
|
|
@Override
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
Intent notificationIntent = new Intent(this, MainActivity.class);
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
|
|
|
|
Notification notification = new Notification.Builder(this, Hyperlink.NOTIFICATION_CHANNEL)
|
|
.setContentTitle("Starship Hyperlink is Running")
|
|
.setContentText("Your text messages are being relayed to Hyperlink.")
|
|
.setSmallIcon(R.drawable.ic_launcher_foreground)
|
|
.setContentIntent(pendingIntent)
|
|
.setTicker("ticker")
|
|
.build();
|
|
|
|
startForeground(Hyperlink.SERVICE_NOTIFICATION_ID, notification);
|
|
startConnection();
|
|
return START_STICKY;
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
timer.cancel();
|
|
}
|
|
|
|
private void startConnection() {
|
|
TimerTask task = new TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
handler.post(() -> {
|
|
if ( !tick() ) {
|
|
stopSelf();
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
timer.schedule(task, 0, Hyperlink.MESSAGE_TICK_POLL_INTERVAL_MS);
|
|
}
|
|
|
|
private boolean tick() {
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
try {
|
|
headers.put("X-Hyperlink-Access-Token", APIv1.getToken().getToken());
|
|
} catch (NotAuthenticatedException e) {
|
|
return false;
|
|
}
|
|
|
|
GsonRequest<ServerSentRequest[]> request = new GsonRequest<>(
|
|
APIv1.resolveEndpoint("/request/queue"),
|
|
ServerSentRequest[].class,
|
|
headers,
|
|
(Response.Listener<ServerSentRequest[]>) response -> {
|
|
for (ServerSentRequest ssr : response) {
|
|
handleServerSentRequest(ssr);
|
|
}
|
|
},
|
|
error -> Log.e("MessagingService", error.getMessage())
|
|
);
|
|
|
|
Hyperlink.httpRequestQueue.add(request);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public IBinder onBind(Intent intent) {
|
|
return null;
|
|
}
|
|
|
|
protected void handleServerSentRequest(ServerSentRequest request) {
|
|
Log.d("MessagingService", "Received server-sent request " + request.getUUID() + " to endpoint " + request.getEndpoint());
|
|
}
|
|
}
|