Fix delayed

This commit is contained in:
tom79 2019-10-21 15:07:16 +02:00
parent a7598120c8
commit 3e2a6f057e
12 changed files with 139 additions and 112 deletions

View File

@ -17,8 +17,6 @@ package app.fedilab.android.activities;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@ -59,9 +57,9 @@ import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.SwitchCompat;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.Menu;
@ -69,14 +67,12 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@ -124,10 +120,8 @@ import app.fedilab.android.helper.MenuFloating;
import app.fedilab.android.services.BackupStatusService;
import app.fedilab.android.services.LiveNotificationDelayedService;
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.StatusCacheDAO;
import app.fedilab.android.sqlite.TempMuteDAO;
import app.fedilab.android.sqlite.TimelineCacheDAO;
import app.fedilab.android.sqlite.TimelinesDAO;
@ -154,9 +148,6 @@ import me.leolin.shortcutbadger.ShortcutBadger;
import static app.fedilab.android.asynctasks.ManageFiltersAsyncTask.action.GET_ALL_FILTER;
import static app.fedilab.android.helper.Helper.changeDrawableColor;
import static app.fedilab.android.sqlite.StatusCacheDAO.ARCHIVE_CACHE;
import static app.fedilab.android.sqlite.StatusCacheDAO.BOOKMARK_CACHE;
import static app.fedilab.android.sqlite.StatusCacheDAO.NOTIFICATION_CACHE;
public abstract class BaseMainActivity extends BaseActivity
@ -1357,7 +1348,6 @@ public abstract class BaseMainActivity extends BaseActivity
set_live_type.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
@Override
public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
sendBroadcast(new Intent(getApplicationContext(), StopLiveNotificationReceiver.class));
SharedPreferences.Editor editor = sharedpreferences.edit();
switch (position) {
case Helper.NOTIF_LIVE:

View File

@ -15,7 +15,6 @@
package app.fedilab.android.asynctasks;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
@ -29,7 +28,6 @@ import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.GNUAPI;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.services.StopLiveNotificationReceiver;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TagsCacheDAO;
import app.fedilab.android.R;

View File

@ -3539,13 +3539,7 @@ public class API {
notifications = parseNotificationResponse(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();
}
if( apiResponse != null) {

View File

@ -32,8 +32,6 @@ import app.fedilab.android.R;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.services.LiveNotificationDelayedService;
import app.fedilab.android.services.LiveNotificationService;
import app.fedilab.android.services.StopLiveNotificationReceiver;
import static android.content.Context.MODE_PRIVATE;

View File

@ -211,7 +211,7 @@ public class ArtListAdapter extends RecyclerView.Adapter implements OnPostAction
else if (status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0)
attachments.add(status.getMedia_attachments().get(0));
intent.putParcelableArrayListExtra("mediaArray", attachments);
b.putInt("position", 0);
b.putInt("position", 1);
intent.putExtras(b);
context.startActivity(intent);
}

View File

@ -97,7 +97,7 @@ import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveRelationshipInterface;
import app.fedilab.android.interfaces.OnRetrieveRemoteAccountInterface;
import app.fedilab.android.services.LiveNotificationDelayedService;
import app.fedilab.android.services.LiveNotificationService;
import app.fedilab.android.services.StopDelayedNotificationReceiver;
import app.fedilab.android.services.StopLiveNotificationReceiver;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.MainMenuDAO;
@ -1278,7 +1278,6 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (count2 > 0) {
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
SharedPreferences.Editor editor = sharedpreferences.edit();
switch (position) {
case Helper.NOTIF_LIVE:
@ -1286,17 +1285,22 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, false);
live_notif_per_account.setVisibility(View.VISIBLE);
editor.apply();
context.sendBroadcast(new Intent(context, StopDelayedNotificationReceiver.class));
break;
case Helper.NOTIF_DELAYED:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false);
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, true);
live_notif_per_account.setVisibility(View.VISIBLE);
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
editor.apply();
break;
case Helper.NOTIF_NONE:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false);
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, false);
live_notif_per_account.setVisibility(View.GONE);
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
context.sendBroadcast(new Intent(context, StopDelayedNotificationReceiver.class));
editor.apply();
break;
}

View File

@ -214,12 +214,9 @@ import app.fedilab.android.client.Entities.RemoteInstance;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.client.Entities.Tag;
import app.fedilab.android.client.Entities.TagTimeline;
import app.fedilab.android.client.Entities.Version;
import app.fedilab.android.client.Tls12SocketFactory;
import app.fedilab.android.fragments.ContentSettingsFragment;
import app.fedilab.android.services.LiveNotificationDelayedService;
import app.fedilab.android.services.LiveNotificationService;
import app.fedilab.android.services.StopLiveNotificationReceiver;
import app.fedilab.android.sqlite.MainMenuDAO;
import app.fedilab.android.sqlite.StatusCacheDAO;
import app.fedilab.android.sqlite.TimelineCacheDAO;
@ -4961,8 +4958,8 @@ public class Helper {
}
public static HashMap<String, Integer> sleeps = new HashMap<>();
public static void startSreaming(Context context) {
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
int liveNotifications = Helper.liveNotifType(context);
switch (liveNotifications){
case Helper.NOTIF_LIVE:

View File

@ -89,58 +89,60 @@ public class LiveNotificationDelayedService extends Service {
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null || intent.getBooleanExtra("stop", false)) {
totalAccount = 0;
if (Build.VERSION.SDK_INT >= 26) {
stopForeground(true);
}
stopSelf();
}
if (Build.VERSION.SDK_INT >= 26) {
channel = new NotificationChannel(CHANNEL_ID,
"Live notifications",
NotificationManager.IMPORTANCE_DEFAULT);
}else{
if (Build.VERSION.SDK_INT >= 26) {
channel = new NotificationChannel(CHANNEL_ID,
"Live notifications",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
totalAccount = 0;
for (Account account : accountStreams) {
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA") || account.getSocial().equals("PIXELFED")) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (allowStream) {
totalAccount++;
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
totalAccount = 0;
for (Account account : accountStreams) {
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA") || account.getSocial().equals("PIXELFED")) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (allowStream) {
totalAccount++;
}
}
}
}
if( totalAccount > 0) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setShowWhen(false)
.setContentIntent(pendingIntent)
.setContentTitle(getString(R.string.top_notification))
.setSmallIcon(R.drawable.fedilab_notification_icon)
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
startForeground(1, notification);
}else{
stopSelf();
}
startStream();
}
if( totalAccount > 0) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setShowWhen(false)
.setContentIntent(pendingIntent)
.setContentTitle(getString(R.string.top_notification))
.setSmallIcon(R.drawable.fedilab_notification_icon)
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
startForeground(1, notification);
}else{
stopSelf();
}
startStream();
if( totalAccount > 0) {
return START_STICKY;
}
@ -236,8 +238,10 @@ public class LiveNotificationDelayedService extends Service {
if( since_ids.containsKey(key) ){
last_notifid = since_ids.get(key);
}
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, last_notifid, false);
apiResponse = null;
try {
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, last_notifid, false);
}catch (Exception ignored){}
if( apiResponse != null && apiResponse.getNotifications() != null && apiResponse.getNotifications().size() > 0){
since_ids.put(key, apiResponse.getNotifications().get(0).getId());

View File

@ -114,43 +114,7 @@ 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);
if (Build.VERSION.SDK_INT >= 26) {
channel = new NotificationChannel(CHANNEL_ID,
"Live notifications",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
totalAccount = 0;
for (Account account : accountStreams) {
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) {
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (allowStream) {
totalAccount++;
}
}
}
}
if( totalAccount > 0) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.top_notification))
.setContentIntent(pendingIntent)
.setSmallIcon(getNotificationIcon(getApplicationContext()))
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
startForeground(1, notification);
}else{
stopSelf();
}
}
private void startStream() {
@ -175,7 +139,47 @@ public class LiveNotificationService extends Service implements NetworkStateRece
if (intent == null || intent.getBooleanExtra("stop", false)) {
totalAccount = 0;
stopSelf();
if (Build.VERSION.SDK_INT >= 26) {
stopForeground(true);
}
}else{
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if (Build.VERSION.SDK_INT >= 26) {
channel = new NotificationChannel(CHANNEL_ID,
"Live notifications",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
totalAccount = 0;
for (Account account : accountStreams) {
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) {
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (allowStream) {
totalAccount++;
}
}
}
}
if( totalAccount > 0) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.top_notification))
.setContentIntent(pendingIntent)
.setSmallIcon(getNotificationIcon(getApplicationContext()))
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
startForeground(1, notification);
}else{
stopSelf();
}
}
if( totalAccount > 0) {
return START_STICKY;

View File

@ -33,16 +33,14 @@ public class RestartLiveNotificationReceiver extends BroadcastReceiver {
@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
boolean delayedNotifications = sharedpreferences.getBoolean(Helper.SET_DELAYED_NOTIFICATIONS, true);
if (delayedNotifications) {
int type = Helper.liveNotifType(context);
if ( type == Helper.NOTIF_DELAYED) {
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationDelayedService.class);
try {
context.startService(streamingServiceIntent);
} catch (Exception ignored) {
}
}else if (liveNotifications) {
}else if (type == Helper.NOTIF_LIVE) {
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationService.class);
try {
context.startService(streamingServiceIntent);

View File

@ -0,0 +1,41 @@
package app.fedilab.android.services;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* 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.
*
* Fedilab 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 Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by Thomas on 18/10/2019.
* BroadcastReceiver for restarting delayed notification service
*/
public class StopDelayedNotificationReceiver extends BroadcastReceiver {
@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationDelayedService.class);
streamingServiceIntent.putExtra("stop", true);
try {
context.startService(streamingServiceIntent);
} catch (Exception ignored) {
}
}
}

View File

@ -34,8 +34,7 @@ public class StopLiveNotificationReceiver extends BroadcastReceiver {
streamingServiceIntent.putExtra("stop", true);
try {
context.startService(streamingServiceIntent);
} catch (Exception ignored) {
}
} catch (Exception ignored) { ignored.printStackTrace();}
}
}