Add notification to control state of the bridge.
This commit is contained in:
parent
ca0fd6a529
commit
ac26c0d721
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- For Programaker bridge -->
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
@ -20,7 +21,7 @@
|
|||||||
<service
|
<service
|
||||||
android:name=".bridge.ProgramakerBridgeService"
|
android:name=".bridge.ProgramakerBridgeService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:exported="true"></service>
|
android:exported="true" />
|
||||||
|
|
||||||
<activity android:name=".CardActivity">
|
<activity android:name=".CardActivity">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package com.codigoparallevar.minicards.bridge;
|
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.app.Service;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
@ -8,7 +13,10 @@ import android.os.Process;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
|
|
||||||
import com.codigoparallevar.minicards.ConfigManager;
|
import com.codigoparallevar.minicards.ConfigManager;
|
||||||
|
import com.codigoparallevar.minicards.R;
|
||||||
import com.codigoparallevar.minicards.types.functional.Tuple2;
|
import com.codigoparallevar.minicards.types.functional.Tuple2;
|
||||||
import com.codigoparallevar.minicards.ui_helpers.DoAsync;
|
import com.codigoparallevar.minicards.ui_helpers.DoAsync;
|
||||||
import com.programaker.api.ProgramakerApi;
|
import com.programaker.api.ProgramakerApi;
|
||||||
@ -16,18 +24,82 @@ import com.programaker.api.ProgramakerApi;
|
|||||||
public class ProgramakerBridgeService extends Service {
|
public class ProgramakerBridgeService extends Service {
|
||||||
public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION";
|
public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION";
|
||||||
public static final CharSequence BridgeUserNotificationChannelName = "User notifications";
|
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 static final long WAIT_TIME_BEFORE_RESTART_MILLIS = 10000; // 10s
|
||||||
private ProgramakerAndroidBridge bridge = null;
|
private ProgramakerAndroidBridge bridge = null;
|
||||||
private static final String LogTag = "PM BridgeService";
|
private static final String LogTag = "PM BridgeService";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
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
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
Toast.makeText(this, "Starting bridge", Toast.LENGTH_SHORT).show();
|
String action = intent.getAction();
|
||||||
connectBridge();
|
if (action == null) {
|
||||||
|
action = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action.equals(COMMAND_PROGRAMAKER_BRIDGE_STOP)) {
|
||||||
|
this.stopSelf();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
connectBridge();
|
||||||
|
}
|
||||||
|
|
||||||
// If we get killed, after returning from here, restart
|
// If we get killed, after returning from here, restart
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
@ -39,9 +111,8 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
String token = config.getToken();
|
String token = config.getToken();
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
Toast.makeText(this, "Cannot start bridge", Toast.LENGTH_SHORT).show();
|
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) {
|
if (ProgramakerBridgeService.this.bridge != null) {
|
||||||
@ -72,6 +143,7 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
bridgeId);
|
bridgeId);
|
||||||
ProgramakerBridgeService.this.bridge.start(
|
ProgramakerBridgeService.this.bridge.start(
|
||||||
() -> { // On ready
|
() -> { // On ready
|
||||||
|
setBridgeStatusNotification(getString(R.string.bridge_service_online), false);
|
||||||
if (config.getBridgeConnectionId() == null) {
|
if (config.getBridgeConnectionId() == null) {
|
||||||
new DoAsync().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
|
new DoAsync().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
|
||||||
new Tuple2<>(
|
new Tuple2<>(
|
||||||
@ -100,11 +172,14 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
}
|
}
|
||||||
}, "ServiceStartArguments");
|
}, "ServiceStartArguments");
|
||||||
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
|
|
||||||
|
setBridgeStatusNotification(getString(R.string.bridge_service_starting), false);
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onBridgeFailedAfterConnected() {
|
private void onBridgeFailedAfterConnected() {
|
||||||
Log.e(LogTag, "Bridge stopped after connected. Waiting 10s then restarting");
|
Log.e(LogTag, "Bridge stopped after connected. Waiting 10s then restarting");
|
||||||
|
setBridgeStatusNotification(getString(R.string.bridge_service_failed_restarting), false);
|
||||||
try {
|
try {
|
||||||
Thread.sleep(WAIT_TIME_BEFORE_RESTART_MILLIS);
|
Thread.sleep(WAIT_TIME_BEFORE_RESTART_MILLIS);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@ -121,6 +196,6 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
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="placeholder_text">Placeholder text</string>
|
||||||
<string name="back_to_deck">Back to card Deck</string>
|
<string name="back_to_deck">Back to card Deck</string>
|
||||||
<string name="go_back">Go back</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>
|
</resources>
|
||||||
|
72
asset-src/ic_vector_icon.svg
Normal file
72
asset-src/ic_vector_icon.svg
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<svg
|
||||||
|
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||||
|
xmlns:cc="http://creativecommons.org/ns#"
|
||||||
|
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||||
|
xmlns:svg="http://www.w3.org/2000/svg"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||||
|
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||||
|
version="1.1"
|
||||||
|
id="svg2693"
|
||||||
|
width="322"
|
||||||
|
height="328.6"
|
||||||
|
viewBox="0 0 322 328.6"
|
||||||
|
sodipodi:docname="ic_vector_icon.svg"
|
||||||
|
inkscape:version="1.0 (4035a4fb49, 2020-05-01)">
|
||||||
|
<metadata
|
||||||
|
id="metadata2699">
|
||||||
|
<rdf:RDF>
|
||||||
|
<cc:Work
|
||||||
|
rdf:about="">
|
||||||
|
<dc:format>image/svg+xml</dc:format>
|
||||||
|
<dc:type
|
||||||
|
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
||||||
|
<dc:title></dc:title>
|
||||||
|
</cc:Work>
|
||||||
|
</rdf:RDF>
|
||||||
|
</metadata>
|
||||||
|
<defs
|
||||||
|
id="defs2697" />
|
||||||
|
<sodipodi:namedview
|
||||||
|
inkscape:document-rotation="0"
|
||||||
|
pagecolor="#ffffff"
|
||||||
|
bordercolor="#666666"
|
||||||
|
borderopacity="1"
|
||||||
|
objecttolerance="10"
|
||||||
|
gridtolerance="10"
|
||||||
|
guidetolerance="10"
|
||||||
|
inkscape:pageopacity="0"
|
||||||
|
inkscape:pageshadow="2"
|
||||||
|
inkscape:window-width="1364"
|
||||||
|
inkscape:window-height="746"
|
||||||
|
id="namedview2695"
|
||||||
|
showgrid="false"
|
||||||
|
inkscape:zoom="0.7865"
|
||||||
|
inkscape:cx="222.3"
|
||||||
|
inkscape:cy="190.6"
|
||||||
|
inkscape:window-x="0"
|
||||||
|
inkscape:window-y="20"
|
||||||
|
inkscape:window-maximized="1"
|
||||||
|
inkscape:current-layer="g2701"
|
||||||
|
fit-margin-top="0"
|
||||||
|
fit-margin-left="0"
|
||||||
|
fit-margin-right="0"
|
||||||
|
fit-margin-bottom="0" />
|
||||||
|
<g
|
||||||
|
inkscape:groupmode="layer"
|
||||||
|
inkscape:label="Image"
|
||||||
|
id="g2701"
|
||||||
|
transform="translate(-14.41,-8.837)">
|
||||||
|
<path
|
||||||
|
style="fill:none;stroke:#000000;stroke-width:30.2362;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
d="M 105.2,161.6 301.6,295.4 187.7,34.33 c 0,0 -12.3,-23.91 -26,0.37 -13.6,24.3 -56.5,126.9 -56.5,126.9 z"
|
||||||
|
id="path3270"
|
||||||
|
sodipodi:nodetypes="cccsc" />
|
||||||
|
<path
|
||||||
|
style="fill:none;stroke:#000000;stroke-width:30.2362;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
d="M 46.66,299.7 146.5,239.6 89.21,200 Z"
|
||||||
|
id="path3312"
|
||||||
|
sodipodi:nodetypes="cccc" />
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 2.3 KiB |
Loading…
Reference in New Issue
Block a user