Add notification to control state of the bridge.
This commit is contained in:
parent
ca0fd6a529
commit
ac26c0d721
7 changed files with 184 additions and 7 deletions
|
@ -9,6 +9,7 @@
|
|||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- For Programaker bridge -->
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
@ -20,7 +21,7 @@
|
|||
<service
|
||||
android:name=".bridge.ProgramakerBridgeService"
|
||||
android:enabled="true"
|
||||
android:exported="true"></service>
|
||||
android:exported="true" />
|
||||
|
||||
<activity android:name=".CardActivity">
|
||||
<intent-filter>
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
package com.codigoparallevar.minicards.bridge;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.IBinder;
|
||||
|
@ -8,7 +13,10 @@ import android.os.Process;
|
|||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.codigoparallevar.minicards.ConfigManager;
|
||||
import com.codigoparallevar.minicards.R;
|
||||
import com.codigoparallevar.minicards.types.functional.Tuple2;
|
||||
import com.codigoparallevar.minicards.ui_helpers.DoAsync;
|
||||
import com.programaker.api.ProgramakerApi;
|
||||
|
@ -16,18 +24,82 @@ import com.programaker.api.ProgramakerApi;
|
|||
public class ProgramakerBridgeService extends Service {
|
||||
public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION";
|
||||
public static final CharSequence BridgeUserNotificationChannelName = "User notifications";
|
||||
|
||||
private static String BridgeStatusNotificationChannel = "PROGRAMAKER_BRIDGE_STATUS_NOTIFICATION";
|
||||
private static CharSequence BridgeStatusNotificationChannelName = "Programaker bridge status";
|
||||
|
||||
private static final int BridgeStatusNotificationId = 9999;
|
||||
public static final String COMMAND_PROGRAMAKER_BRIDGE_STOP = "com.programaker.bridge.commands.stop";
|
||||
public static final String COMMAND_PROGRAMAKER_BRIDGE_START = "com.programaker.bridge.commands.start";
|
||||
|
||||
private static final long WAIT_TIME_BEFORE_RESTART_MILLIS = 10000; // 10s
|
||||
private ProgramakerAndroidBridge bridge = null;
|
||||
private static final String LogTag = "PM BridgeService";
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
setBridgeStatusNotification(getString(R.string.bridge_service_not_started), true);
|
||||
}
|
||||
|
||||
private void setBridgeStatusNotification(String title, boolean stopped) {
|
||||
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
assert notificationManager != null;
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel(
|
||||
ProgramakerBridgeService.BridgeStatusNotificationChannel,
|
||||
ProgramakerBridgeService.BridgeStatusNotificationChannelName,
|
||||
NotificationManager.IMPORTANCE_DEFAULT);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
}
|
||||
|
||||
Intent stopIntent = new Intent(this, ProgramakerBridgeService.class);
|
||||
stopIntent.setAction(COMMAND_PROGRAMAKER_BRIDGE_STOP);
|
||||
PendingIntent stopPendingIntent =
|
||||
PendingIntent.getService(this, 0, stopIntent, 0);
|
||||
|
||||
Intent startIntent = new Intent(this, ProgramakerBridgeService.class);
|
||||
startIntent.setAction(COMMAND_PROGRAMAKER_BRIDGE_START);
|
||||
PendingIntent startPendingIntent =
|
||||
PendingIntent.getService(this, 0, startIntent, 0);
|
||||
|
||||
NotificationCompat.Builder builder = new NotificationCompat
|
||||
.Builder(this, ProgramakerBridgeService.BridgeStatusNotificationChannel)
|
||||
.setContentTitle(title)
|
||||
.setSmallIcon(R.drawable.ic_vector_icon)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
|
||||
|
||||
|
||||
if (stopped) {
|
||||
builder.addAction(R.drawable.ic_start, getString(R.string.start_bridge), startPendingIntent);
|
||||
}
|
||||
else {
|
||||
builder.addAction(R.drawable.ic_cancel, getString(R.string.stop_bridge), stopPendingIntent);
|
||||
}
|
||||
|
||||
Notification notification = builder.build();
|
||||
|
||||
if (stopped) {
|
||||
notificationManager.notify(BridgeStatusNotificationId, notification);
|
||||
}
|
||||
else {
|
||||
this.startForeground(BridgeStatusNotificationId, notification);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Toast.makeText(this, "Starting bridge", Toast.LENGTH_SHORT).show();
|
||||
connectBridge();
|
||||
String action = intent.getAction();
|
||||
if (action == null) {
|
||||
action = "";
|
||||
}
|
||||
|
||||
if (action.equals(COMMAND_PROGRAMAKER_BRIDGE_STOP)) {
|
||||
this.stopSelf();
|
||||
}
|
||||
else {
|
||||
connectBridge();
|
||||
}
|
||||
|
||||
// If we get killed, after returning from here, restart
|
||||
return START_STICKY;
|
||||
|
@ -39,9 +111,8 @@ public class ProgramakerBridgeService extends Service {
|
|||
String token = config.getToken();
|
||||
if (token == null) {
|
||||
Toast.makeText(this, "Cannot start bridge", Toast.LENGTH_SHORT).show();
|
||||
if (token == null) {
|
||||
Log.e(LogTag, "Cannot start bridge: Token is null");
|
||||
}
|
||||
Log.e(LogTag, "Cannot start bridge: Token is null");
|
||||
this.stopSelf();
|
||||
}
|
||||
|
||||
if (ProgramakerBridgeService.this.bridge != null) {
|
||||
|
@ -72,6 +143,7 @@ public class ProgramakerBridgeService extends Service {
|
|||
bridgeId);
|
||||
ProgramakerBridgeService.this.bridge.start(
|
||||
() -> { // On ready
|
||||
setBridgeStatusNotification(getString(R.string.bridge_service_online), false);
|
||||
if (config.getBridgeConnectionId() == null) {
|
||||
new DoAsync().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
|
||||
new Tuple2<>(
|
||||
|
@ -100,11 +172,14 @@ public class ProgramakerBridgeService extends Service {
|
|||
}
|
||||
}, "ServiceStartArguments");
|
||||
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
||||
|
||||
setBridgeStatusNotification(getString(R.string.bridge_service_starting), false);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void onBridgeFailedAfterConnected() {
|
||||
Log.e(LogTag, "Bridge stopped after connected. Waiting 10s then restarting");
|
||||
setBridgeStatusNotification(getString(R.string.bridge_service_failed_restarting), false);
|
||||
try {
|
||||
Thread.sleep(WAIT_TIME_BEFORE_RESTART_MILLIS);
|
||||
} catch (InterruptedException e) {
|
||||
|
@ -121,6 +196,6 @@ public class ProgramakerBridgeService extends Service {
|
|||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Toast.makeText(this, "Bridge stopped", Toast.LENGTH_SHORT).show();
|
||||
setBridgeStatusNotification(getString(R.string.bridge_service_failed_stopping), true);
|
||||
}
|
||||
}
|
||||
|
|
5
app/src/main/res/drawable/ic_cancel.xml
Normal file
5
app/src/main/res/drawable/ic_cancel.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<vector android:alpha="0.99" android:height="24dp"
|
||||
android:viewportHeight="24.0" android:viewportWidth="24.0"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#FF000000" android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM17,15.59L15.59,17 12,13.41 8.41,17 7,15.59 10.59,12 7,8.41 8.41,7 12,10.59 15.59,7 17,8.41 13.41,12 17,15.59z"/>
|
||||
</vector>
|
5
app/src/main/res/drawable/ic_start.xml
Normal file
5
app/src/main/res/drawable/ic_start.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<vector android:alpha="0.99" android:autoMirrored="true"
|
||||
android:height="24dp" android:viewportHeight="24.0"
|
||||
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#FF000000" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM10,16.5v-9l6,4.5 -6,4.5z"/>
|
||||
</vector>
|
12
app/src/main/res/drawable/ic_vector_icon.xml
Normal file
12
app/src/main/res/drawable/ic_vector_icon.xml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<vector android:alpha="0.99" android:autoMirrored="true"
|
||||
android:height="328.6dp" android:viewportHeight="328.6"
|
||||
android:viewportWidth="322" android:width="322dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#00000000"
|
||||
android:pathData="M90.79,152.763 L287.19,286.563 173.29,25.493c0,0 -12.3,-23.91 -26,0.37 -13.6,24.3 -56.5,126.9 -56.5,126.9z"
|
||||
android:strokeAlpha="1" android:strokeColor="#000000"
|
||||
android:strokeLineCap="butt" android:strokeLineJoin="miter" android:strokeWidth="30.2362"/>
|
||||
<path android:fillColor="#00000000"
|
||||
android:pathData="M32.25,290.863 L132.09,230.763 74.8,191.163Z"
|
||||
android:strokeAlpha="1" android:strokeColor="#000000"
|
||||
android:strokeLineCap="butt" android:strokeLineJoin="miter" android:strokeWidth="30.2362"/>
|
||||
</vector>
|
|
@ -18,4 +18,11 @@
|
|||
<string name="placeholder_text">Placeholder text</string>
|
||||
<string name="back_to_deck">Back to card Deck</string>
|
||||
<string name="go_back">Go back</string>
|
||||
<string name="bridge_service_not_started">Programaker bridge not started</string>
|
||||
<string name="bridge_service_starting">Programaker bridge starting</string>
|
||||
<string name="bridge_service_online">Programaker bridge online</string>
|
||||
<string name="bridge_service_failed_restarting">Programaker bridge failed, restarting...</string>
|
||||
<string name="bridge_service_failed_stopping">Programaker bridge stopped</string>
|
||||
<string name="stop_bridge">Stop bridge</string>
|
||||
<string name="start_bridge">Start bridge</string>
|
||||
</resources>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue