mirror of
https://github.com/TimeCrafters/TimeCraftersConfigurationTool.git
synced 2025-12-15 05:02:33 +00:00
Use a service for TACNET Connection instead of an app thread
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
<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.ACCESS_WIFI_STATE" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
|
|||||||
@@ -41,14 +41,21 @@ public class TACNETConnectionService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
|
disconnect();
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connect() {
|
private void connect() {
|
||||||
|
if (!Backend.instance().tacnet().isConnected()) {
|
||||||
|
final String hostname = Backend.instance().getSettings().hostname;
|
||||||
|
final int port = Backend.instance().getSettings().port;
|
||||||
|
|
||||||
|
Backend.instance().tacnet().connect(hostname, port);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disconnect() {
|
private void disconnect() {
|
||||||
Backend.instance().stopServer();
|
Backend.instance().tacnet().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void foregroundify() {
|
private void foregroundify() {
|
||||||
@@ -57,7 +64,8 @@ public class TACNETConnectionService extends Service {
|
|||||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
|
||||||
|
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
.setContentTitle("TACNET Connection is running")
|
.setContentTitle("TACNET Connection")
|
||||||
|
.setContentText("Connected to: " + Backend.instance().getSettings().hostname + ":" + Backend.instance().getSettings().port)
|
||||||
.setSmallIcon(R.drawable.tacnet)
|
.setSmallIcon(R.drawable.tacnet)
|
||||||
.setContentIntent(pendingIntent);
|
.setContentIntent(pendingIntent);
|
||||||
Notification notification = builder.build();
|
Notification notification = builder.build();
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import android.app.NotificationManager;
|
|||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.BitmapFactory;
|
import android.net.DhcpInfo;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -17,12 +18,19 @@ import org.timecrafters.TimeCraftersConfigurationTool.MainActivity;
|
|||||||
import org.timecrafters.TimeCraftersConfigurationTool.R;
|
import org.timecrafters.TimeCraftersConfigurationTool.R;
|
||||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
|
||||||
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
|
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
|
||||||
|
|
||||||
public class TACNETServerService extends Service {
|
public class TACNETServerService extends Service {
|
||||||
private static final String CHANNEL_ID = "TACNET_SERVER_SERVICE";
|
private static final String CHANNEL_ID = "TACNET_SERVER_SERVICE";
|
||||||
private static final String TAG = "TACNETServerService";
|
private static final String TAG = "TACNETServerService";
|
||||||
private static final int ID = 8962_0;
|
private static final int ID = 8962_0;
|
||||||
|
private Intent intent;
|
||||||
|
private String address;
|
||||||
|
|
||||||
public TACNETServerService() {
|
public TACNETServerService() {
|
||||||
}
|
}
|
||||||
@@ -34,10 +42,12 @@ public class TACNETServerService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
this.intent = intent;
|
||||||
|
|
||||||
createNotificationChannel();
|
createNotificationChannel();
|
||||||
|
|
||||||
foregroundify();
|
|
||||||
startServer();
|
startServer();
|
||||||
|
foregroundify();
|
||||||
|
|
||||||
return START_REDELIVER_INTENT;
|
return START_REDELIVER_INTENT;
|
||||||
}
|
}
|
||||||
@@ -53,6 +63,8 @@ public class TACNETServerService extends Service {
|
|||||||
if (Backend.instance().getServer() == null) {
|
if (Backend.instance().getServer() == null) {
|
||||||
Log.i(TAG, "Starting server...");
|
Log.i(TAG, "Starting server...");
|
||||||
Backend.instance().startServer();
|
Backend.instance().startServer();
|
||||||
|
|
||||||
|
findAddress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,13 +72,34 @@ public class TACNETServerService extends Service {
|
|||||||
Backend.instance().stopServer();
|
Backend.instance().stopServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void findAddress() {
|
||||||
|
final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
|
||||||
|
final DhcpInfo dhcp = manager.getDhcpInfo();
|
||||||
|
int ipAddress = dhcp.ipAddress;
|
||||||
|
|
||||||
|
/* https://stackoverflow.com/a/54825244 */
|
||||||
|
ipAddress = (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) ?
|
||||||
|
Integer.reverseBytes(ipAddress) : ipAddress;
|
||||||
|
byte[] ipAddressByte = BigInteger.valueOf(ipAddress).toByteArray();
|
||||||
|
|
||||||
|
try {
|
||||||
|
InetAddress myAddr = InetAddress.getByAddress(ipAddressByte);
|
||||||
|
address = myAddr.getHostAddress();
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.i(TAG, "" + address);
|
||||||
|
}
|
||||||
|
|
||||||
private void foregroundify() {
|
private void foregroundify() {
|
||||||
Intent notificationIntent = new Intent(this, MainActivity.class);
|
Intent notificationIntent = new Intent(this, MainActivity.class);
|
||||||
notificationIntent.putExtra("navigate_to_tacnet", true);
|
notificationIntent.putExtra("navigate_to_tacnet", true);
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, FLAG_UPDATE_CURRENT);
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
.setContentTitle("TACNET server is running")
|
.setContentTitle("TACNET Server")
|
||||||
|
.setContentText("Running at: " + address + ":" + Backend.instance().getSettings().port)
|
||||||
.setSmallIcon(R.drawable.tacnet)
|
.setSmallIcon(R.drawable.tacnet)
|
||||||
.setContentIntent(pendingIntent);
|
.setContentIntent(pendingIntent);
|
||||||
Notification notification = builder.build();
|
Notification notification = builder.build();
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import org.timecrafters.TimeCraftersConfigurationTool.R;
|
|||||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
||||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.TACNET;
|
import org.timecrafters.TimeCraftersConfigurationTool.backend.TACNET;
|
||||||
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersFragment;
|
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersFragment;
|
||||||
|
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.TACNETConnectionService;
|
||||||
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.TACNETServerService;
|
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.TACNETServerService;
|
||||||
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.support.ConnectionStatsSyncHandler;
|
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.support.ConnectionStatsSyncHandler;
|
||||||
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.support.ServerStatsSyncHandler;
|
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.support.ServerStatsSyncHandler;
|
||||||
@@ -95,7 +96,7 @@ public class TACNETHostFragment extends TimeCraftersFragment {
|
|||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Backend.instance().saveSettings();
|
Backend.instance().saveSettings();
|
||||||
|
|
||||||
Backend.instance().tacnet().connect(hostname.getText().toString(), Integer.parseInt(port.getText().toString()));
|
getActivity().startService(new Intent(getContext(), TACNETConnectionService.class));
|
||||||
|
|
||||||
root.removeAllViews();
|
root.removeAllViews();
|
||||||
inflateTACNETConnectionStatus(container);
|
inflateTACNETConnectionStatus(container);
|
||||||
@@ -125,7 +126,7 @@ public class TACNETHostFragment extends TimeCraftersFragment {
|
|||||||
disconnect.setOnClickListener(new View.OnClickListener() {
|
disconnect.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Backend.instance().tacnet().close();
|
getActivity().stopService(new Intent(getContext(), TACNETConnectionService.class));
|
||||||
Backend.instance().stopErrorSound();
|
Backend.instance().stopErrorSound();
|
||||||
connectionStatsSyncHandler.stop();
|
connectionStatsSyncHandler.stop();
|
||||||
|
|
||||||
|
|||||||
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
#Sun Oct 25 13:32:16 CDT 2020
|
#Tue Nov 10 11:51:46 CST 2020
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
|
||||||
|
|||||||
Reference in New Issue
Block a user