mirror of
https://codeberg.org/tom79/Fedilab.git
synced 2025-06-21 20:50:10 +03:00
Restore FOREGROUND_SERVICE
This commit is contained in:
parent
6d7faa996d
commit
0ffc7c0440
3 changed files with 46 additions and 3 deletions
|
@ -11,6 +11,8 @@
|
||||||
tools:ignore="ScopedStorage" />
|
tools:ignore="ScopedStorage" />
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
<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" />
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
||||||
|
|
||||||
<uses-feature
|
<uses-feature
|
||||||
android:name="android.hardware.camera"
|
android:name="android.hardware.camera"
|
||||||
|
|
|
@ -512,9 +512,9 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startInForeground() {
|
private void startInForeground() {
|
||||||
Intent retrieveInfoServiceIntent = new Intent(this, RetrieveInfoService.class);
|
Intent notificationIntent = new Intent(this, RetrieveInfoService.class);
|
||||||
try {
|
try {
|
||||||
startService(retrieveInfoServiceIntent);
|
startService(notificationIntent);
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,19 +16,28 @@ package app.fedilab.android.peertube.services;
|
||||||
|
|
||||||
import static app.fedilab.android.peertube.helper.Helper.peertubeInformation;
|
import static app.fedilab.android.peertube.helper.Helper.peertubeInformation;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.app.NotificationChannel;
|
||||||
|
import android.app.NotificationManager;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.content.pm.ServiceInfo;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import app.fedilab.android.R;
|
||||||
import app.fedilab.android.peertube.client.RetrofitPeertubeAPI;
|
import app.fedilab.android.peertube.client.RetrofitPeertubeAPI;
|
||||||
import app.fedilab.android.peertube.client.entities.PeertubeInformation;
|
import app.fedilab.android.peertube.client.entities.PeertubeInformation;
|
||||||
import app.fedilab.android.peertube.helper.EmojiHelper;
|
import app.fedilab.android.peertube.helper.EmojiHelper;
|
||||||
|
@ -37,6 +46,7 @@ import app.fedilab.android.peertube.helper.NetworkStateReceiver;
|
||||||
|
|
||||||
public class RetrieveInfoService extends Service implements NetworkStateReceiver.NetworkStateReceiverListener {
|
public class RetrieveInfoService extends Service implements NetworkStateReceiver.NetworkStateReceiverListener {
|
||||||
|
|
||||||
|
static String NOTIFICATION_CHANNEL_ID = "update_info_peertube";
|
||||||
private NetworkStateReceiver networkStateReceiver;
|
private NetworkStateReceiver networkStateReceiver;
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,6 +55,36 @@ public class RetrieveInfoService extends Service implements NetworkStateReceiver
|
||||||
networkStateReceiver = new NetworkStateReceiver();
|
networkStateReceiver = new NetworkStateReceiver();
|
||||||
networkStateReceiver.addListener(this);
|
networkStateReceiver.addListener(this);
|
||||||
ContextCompat.registerReceiver(RetrieveInfoService.this, networkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED);
|
ContextCompat.registerReceiver(RetrieveInfoService.this, networkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
|
||||||
|
getString(R.string.notification_channel_name),
|
||||||
|
NotificationManager.IMPORTANCE_DEFAULT);
|
||||||
|
channel.setSound(null, null);
|
||||||
|
|
||||||
|
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
||||||
|
Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
|
||||||
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
|
.setContentTitle(getString(R.string.app_name))
|
||||||
|
.setContentText(getString(R.string.notification_channel_name))
|
||||||
|
.setAutoCancel(true).build();
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
|
||||||
|
} else {
|
||||||
|
startForeground(1, notification);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
|
||||||
|
.setContentTitle(getString(R.string.app_name))
|
||||||
|
.setDefaults(Notification.DEFAULT_ALL)
|
||||||
|
.setContentText(getString(R.string.notification_channel_name))
|
||||||
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||||
|
.setAutoCancel(true);
|
||||||
|
|
||||||
|
Notification notification = builder.build();
|
||||||
|
startForeground(1, notification);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +140,7 @@ public class RetrieveInfoService extends Service implements NetworkStateReceiver
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
EmojiHelper.fillMapEmoji(getApplicationContext());
|
EmojiHelper.fillMapEmoji(getApplicationContext());
|
||||||
if (peertubeInformation == null || peertubeInformation.getCategories() == null || peertubeInformation.getCategories().isEmpty()) {
|
if (peertubeInformation == null || peertubeInformation.getCategories() == null || peertubeInformation.getCategories().size() == 0) {
|
||||||
peertubeInformation = new PeertubeInformation();
|
peertubeInformation = new PeertubeInformation();
|
||||||
peertubeInformation.setCategories(new LinkedHashMap<>());
|
peertubeInformation.setCategories(new LinkedHashMap<>());
|
||||||
peertubeInformation.setLanguages(new LinkedHashMap<>());
|
peertubeInformation.setLanguages(new LinkedHashMap<>());
|
||||||
|
@ -110,6 +150,7 @@ public class RetrieveInfoService extends Service implements NetworkStateReceiver
|
||||||
peertubeInformation.setTranslations(new LinkedHashMap<>());
|
peertubeInformation.setTranslations(new LinkedHashMap<>());
|
||||||
peertubeInformation = new RetrofitPeertubeAPI(RetrieveInfoService.this).getPeertubeInformation();
|
peertubeInformation = new RetrofitPeertubeAPI(RetrieveInfoService.this).getPeertubeInformation();
|
||||||
}
|
}
|
||||||
|
stopForeground(true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
thread.start();
|
thread.start();
|
||||||
|
|
Loading…
Reference in a new issue