Some improvements

This commit is contained in:
tom79 2019-08-30 17:41:15 +02:00
parent ecc603110a
commit 103e19d097
3 changed files with 43 additions and 11 deletions

View File

@ -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)

View File

@ -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.

View File

@ -15,6 +15,8 @@ package app.fedilab.android.services;
* see <http://www.gnu.org/licenses>. */
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<String, Thread> 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);