TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java

151 lines
5.9 KiB
Java
Raw Normal View History

2020-09-18 14:37:34 +02:00
package app.fedilab.fedilabtube.services;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2022-05-04 09:43:54 +02:00
import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation;
2020-09-18 14:37:34 +02:00
import android.app.Notification;
2020-09-18 18:10:04 +02:00
import android.app.NotificationChannel;
import android.app.NotificationManager;
2020-09-18 14:37:34 +02:00
import android.app.Service;
2020-09-18 18:10:04 +02:00
import android.content.Context;
2020-09-18 14:37:34 +02:00
import android.content.Intent;
2020-09-18 16:08:52 +02:00
import android.content.IntentFilter;
2020-09-18 14:37:34 +02:00
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import java.util.LinkedHashMap;
2020-09-18 18:10:04 +02:00
import java.util.Objects;
2020-09-18 14:37:34 +02:00
import app.fedilab.fedilabtube.R;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-09-18 14:37:34 +02:00
import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
2020-10-15 15:16:41 +02:00
import app.fedilab.fedilabtube.helper.EmojiHelper;
2020-09-18 16:08:52 +02:00
import app.fedilab.fedilabtube.helper.NetworkStateReceiver;
2020-09-18 14:37:34 +02:00
2020-09-18 16:08:52 +02:00
public class RetrieveInfoService extends Service implements NetworkStateReceiver.NetworkStateReceiverListener {
2020-09-18 14:37:34 +02:00
2020-09-18 18:10:04 +02:00
static String NOTIFICATION_CHANNEL_ID = "update_info_peertube";
2020-09-18 16:08:52 +02:00
private NetworkStateReceiver networkStateReceiver;
public void onCreate() {
super.onCreate();
networkStateReceiver = new NetworkStateReceiver();
networkStateReceiver.addListener(this);
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}
2020-09-18 14:37:34 +02:00
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2020-09-18 18:10:04 +02:00
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);
android.app.Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
2020-09-18 14:37:34 +02:00
.setSmallIcon(R.drawable.ic_notification_tubelab)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.notification_channel_name))
2020-09-18 18:10:04 +02:00
.setAutoCancel(true).build();
2020-09-18 14:37:34 +02:00
startForeground(1, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
2020-09-18 18:10:04 +02:00
.setDefaults(Notification.DEFAULT_ALL)
2020-09-18 14:37:34 +02:00
.setContentText(getString(R.string.notification_channel_name))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
}
2020-10-16 11:37:40 +02:00
Thread thread = new Thread() {
@Override
public void run() {
EmojiHelper.fillMapEmoji(getApplicationContext());
peertubeInformation = new PeertubeInformation();
peertubeInformation.setCategories(new LinkedHashMap<>());
peertubeInformation.setLanguages(new LinkedHashMap<>());
peertubeInformation.setLicences(new LinkedHashMap<>());
peertubeInformation.setPrivacies(new LinkedHashMap<>());
peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>());
peertubeInformation.setTranslations(new LinkedHashMap<>());
peertubeInformation = new RetrofitPeertubeAPI(RetrieveInfoService.this).getPeertubeInformation();
stopForeground(true);
}
};
thread.start();
2020-09-18 14:37:34 +02:00
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
2020-09-18 16:08:52 +02:00
@Override
public void onDestroy() {
super.onDestroy();
if (networkStateReceiver != null) {
networkStateReceiver.removeListener(this);
unregisterReceiver(networkStateReceiver);
}
}
@Override
public void networkAvailable() {
Thread thread = new Thread() {
@Override
public void run() {
2020-10-15 15:16:41 +02:00
EmojiHelper.fillMapEmoji(getApplicationContext());
2020-11-11 10:34:28 +01:00
if (peertubeInformation == null || peertubeInformation.getCategories().size() == 0) {
2020-10-24 16:36:49 +02:00
peertubeInformation = new PeertubeInformation();
peertubeInformation.setCategories(new LinkedHashMap<>());
peertubeInformation.setLanguages(new LinkedHashMap<>());
peertubeInformation.setLicences(new LinkedHashMap<>());
peertubeInformation.setPrivacies(new LinkedHashMap<>());
peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>());
peertubeInformation.setTranslations(new LinkedHashMap<>());
peertubeInformation = new RetrofitPeertubeAPI(RetrieveInfoService.this).getPeertubeInformation();
}
2020-09-18 16:08:52 +02:00
stopForeground(true);
}
};
thread.start();
}
@Override
public void networkUnavailable() {
}
2020-09-18 14:37:34 +02:00
}