From 103e19d0976a92be3284b2c15e20d9b367c59568 Mon Sep 17 00:00:00 2001 From: tom79 Date: Fri, 30 Aug 2019 17:41:15 +0200 Subject: [PATCH] Some improvements --- .../android/activities/BaseMainActivity.java | 3 +- .../fragments/ContentSettingsFragment.java | 20 ++++++++++-- .../services/LiveNotificationService.java | 31 ++++++++++++++----- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java index 844790fc6..0be48d722 100644 --- a/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java @@ -112,6 +112,7 @@ import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.MenuFloating; import app.fedilab.android.services.BackupStatusService; import app.fedilab.android.services.LiveNotificationService; +import app.fedilab.android.services.StopLiveNotificationReceiver; import app.fedilab.android.sqlite.AccountDAO; import app.fedilab.android.sqlite.Sqlite; import app.fedilab.android.sqlite.TempMuteDAO; @@ -1646,7 +1647,7 @@ public abstract class BaseMainActivity extends BaseActivity SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); boolean backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true); if(!backgroundProcess) - sendBroadcast(new Intent("StopLiveNotificationService")); + sendBroadcast(new Intent(getApplicationContext(), StopLiveNotificationReceiver.class)); if( hidde_menu != null) LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(hidde_menu); if( update_topbar != null) diff --git a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java index 1f9b3d66b..f8260200a 100644 --- a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java +++ b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java @@ -17,6 +17,10 @@ package app.fedilab.android.fragments; import android.annotation.SuppressLint; import android.annotation.TargetApi; +import android.app.Activity; +import android.app.ActivityManager; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.TimePickerDialog; import android.content.ContentUris; import android.content.Context; @@ -76,6 +80,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.Set; import app.fedilab.android.R; @@ -89,6 +94,8 @@ import app.fedilab.android.client.Entities.Status; import app.fedilab.android.filelister.FileListerDialog; import app.fedilab.android.filelister.OnFileSelectedListener; import app.fedilab.android.helper.Helper; +import app.fedilab.android.services.LiveNotificationService; +import app.fedilab.android.services.StopLiveNotificationReceiver; import app.fedilab.android.sqlite.AccountDAO; import app.fedilab.android.sqlite.Sqlite; import es.dmoral.toasty.Toasty; @@ -96,6 +103,7 @@ import mabbas007.tagsedittext.TagsEditText; import static android.app.Activity.RESULT_OK; +import static android.content.Context.ACTIVITY_SERVICE; import static android.content.Context.MODE_PRIVATE; import static app.fedilab.android.fragments.ContentSettingsFragment.type.ADMIN; import static app.fedilab.android.fragments.ContentSettingsFragment.type.BATTERY; @@ -1050,10 +1058,17 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable editor.apply(); if( set_live_notif.isChecked() ){ try { - ((MainActivity) context).startSreaming(); + Intent streamingIntent = new Intent(context, LiveNotificationService.class); + context.startService(streamingIntent); }catch (Exception ignored){ignored.printStackTrace();} }else{ - context.sendBroadcast(new Intent("StopLiveNotificationService")); + context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class)); + if (Build.VERSION.SDK_INT >= 26) { + NotificationManager notif = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); + if( notif != null) { + notif.deleteNotificationChannel(LiveNotificationService.CHANNEL_ID); + } + } } } }); @@ -2054,7 +2069,6 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable return null; } - /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. diff --git a/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java b/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java index d4f2bf08a..44f75310f 100644 --- a/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java +++ b/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java @@ -15,6 +15,8 @@ package app.fedilab.android.services; * see . */ import android.app.AlarmManager; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; @@ -35,7 +37,7 @@ import android.preference.PreferenceManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; +import androidx.core.app.NotificationCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import com.bumptech.glide.Glide; import com.bumptech.glide.load.DataSource; @@ -85,7 +87,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece static { Helper.installProvider(); } - + public static String CHANNEL_ID = "live_notifications"; protected Account account; boolean backgroundProcess; private static HashMap threads = new HashMap<>(); @@ -97,9 +99,12 @@ public class LiveNotificationService extends Service implements NetworkStateRece networkStateReceiver = new NetworkStateReceiver(); networkStateReceiver.addListener(this); registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true); } private void startStream(){ + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true); boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); @@ -119,10 +124,24 @@ public class LiveNotificationService extends Service implements NetworkStateRece if( intent == null || intent.getBooleanExtra("stop", false) ) { stopSelf(); } - if( backgroundProcess) + if( backgroundProcess) { + if (Build.VERSION.SDK_INT >= 26) { + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, + "Live notifications", + NotificationManager.IMPORTANCE_DEFAULT); + + ((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); + + android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("") + .setContentText("").build(); + + startForeground(1, notification); + } return START_STICKY; - else + }else { return START_NOT_STICKY; + } } @Override @@ -151,9 +170,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece } private void restart(){ - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - ContextCompat.startForegroundService(getApplicationContext(),new Intent(getApplicationContext(), LiveNotificationService.class)); - } else { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { Intent restartServiceIntent = new Intent(LiveNotificationService.this, LiveNotificationService.class); restartServiceIntent.setPackage(getPackageName()); PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);