Fixes issue with counters

This commit is contained in:
stom79 2018-01-02 14:47:13 +01:00
parent 357ba143dc
commit fd3f04b686
2 changed files with 75 additions and 22 deletions

View File

@ -207,6 +207,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
if( notifications != null && notifications.size() > 0) { if( notifications != null && notifications.size() > 0) {
for(Notification tmpNotification: notifications){ for(Notification tmpNotification: notifications){
if( lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) { if( lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) {
MainActivity.countNewNotifications++; MainActivity.countNewNotifications++;
} }

View File

@ -81,6 +81,8 @@ public class LiveNotificationService extends BaseService {
protected Account account; protected Account account;
private static HashMap<String, Thread> backGroundTaskHashMap = new HashMap<>(); private static HashMap<String, Thread> backGroundTaskHashMap = new HashMap<>();
private static HashMap<String, HttpsURLConnection> httpsURLConnectionHashMap = new HashMap<>();
private static HashMap<String, Integer> queuedIntentHashMap = new HashMap<>();
@SuppressWarnings("unused") @SuppressWarnings("unused")
public LiveNotificationService(String name) { public LiveNotificationService(String name) {
super(name); super(name);
@ -92,9 +94,48 @@ public class LiveNotificationService extends BaseService {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount();
if (accountStreams != null) {
for (final Account accountStream : accountStreams) {
queuedIntentHashMap.put(accountStream.getAcct() + accountStream.getInstance(), 0);
}
}
} }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
String userId;
if( liveNotifications && notify) {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
if (intent == null || intent.getStringExtra("userId") == null) {
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount();
if (accountStreams != null) {
for (final Account accountStream : accountStreams) {
int numberOfQueuedIntent = queuedIntentHashMap.get(accountStream.getAcct() + accountStream.getInstance()) + 1;
queuedIntentHashMap.put(accountStream.getAcct() + accountStream.getInstance(), numberOfQueuedIntent);
}
}
}else{
userId = intent.getStringExtra("userId");
final Account accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
if (accountStream != null) {
int numberOfQueuedIntent = queuedIntentHashMap.get(accountStream.getAcct() + accountStream.getInstance()) + 1;
queuedIntentHashMap.put(accountStream.getAcct() + accountStream.getInstance(), numberOfQueuedIntent);
}
}
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable @Nullable
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
@ -116,6 +157,31 @@ public class LiveNotificationService extends BaseService {
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount(); List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount();
if (accountStreams != null){ if (accountStreams != null){
for (final Account accountStream : accountStreams) { for (final Account accountStream : accountStreams) {
int numberOfQueuedIntent = queuedIntentHashMap.get(accountStream.getAcct() + accountStream.getInstance()) - 1;
queuedIntentHashMap.put(accountStream.getAcct() + accountStream.getInstance(), numberOfQueuedIntent);
if( queuedIntentHashMap.get(accountStream.getAcct() + accountStream.getInstance()) == 0) {
if (backGroundTaskHashMap.containsKey(accountStream.getAcct() + accountStream.getInstance())) {
if (!backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).isAlive())
backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).interrupt();
}
Thread thread = new Thread() {
@Override
public void run() {
taks(accountStream);
}
};
thread.start();
backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), thread);
}
}
}
}else {
userId = intent.getStringExtra("userId");
final Account accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
if (accountStream != null) {
int numberOfQueuedIntent = queuedIntentHashMap.get(accountStream.getAcct() + accountStream.getInstance()) - 1;
queuedIntentHashMap.put(accountStream.getAcct() + accountStream.getInstance(), numberOfQueuedIntent);
if( queuedIntentHashMap.get(accountStream.getAcct() + accountStream.getInstance()) == 0) {
if (backGroundTaskHashMap.containsKey(accountStream.getAcct() + accountStream.getInstance())) { if (backGroundTaskHashMap.containsKey(accountStream.getAcct() + accountStream.getInstance())) {
if (!backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).isAlive()) if (!backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).isAlive())
backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).interrupt(); backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).interrupt();
@ -129,24 +195,6 @@ public class LiveNotificationService extends BaseService {
thread.start(); thread.start();
backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), thread); backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), thread);
} }
}
}else {
userId = intent.getStringExtra("userId");
final Account accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
if (accountStream != null) {
if (backGroundTaskHashMap.containsKey(accountStream.getAcct() + accountStream.getInstance())) {
if (!backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).isAlive())
backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).interrupt();
}
Thread thread = new Thread() {
@Override
public void run() {
taks(accountStream);
}
};
thread.start();
backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), thread);
} }
} }
} }
@ -154,12 +202,15 @@ public class LiveNotificationService extends BaseService {
private void taks(Account account){ private void taks(Account account){
InputStream inputStream = null; InputStream inputStream = null;
HttpsURLConnection httpsURLConnection = null;
BufferedReader reader = null; BufferedReader reader = null;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
Helper.EventStreaming lastEvent = null; Helper.EventStreaming lastEvent = null;
if( account != null){ if( account != null){
try { try {
HttpsURLConnection httpsURLConnection = httpsURLConnectionHashMap.get(account.getAcct() + account.getInstance());
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
URL url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user"); URL url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user");
httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json"); httpsURLConnection.setRequestProperty("Content-Type", "application/json");
@ -171,7 +222,7 @@ public class LiveNotificationService extends BaseService {
httpsURLConnection.setRequestMethod("GET"); httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setConnectTimeout(70000); httpsURLConnection.setConnectTimeout(70000);
httpsURLConnection.setReadTimeout(70000); httpsURLConnection.setReadTimeout(70000);
httpsURLConnectionHashMap.put(account.getAcct() + account.getInstance(), httpsURLConnection);
if( httpsURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){ if( httpsURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream()); inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(inputStream)); reader = new BufferedReader(new InputStreamReader(inputStream));
@ -181,6 +232,7 @@ public class LiveNotificationService extends BaseService {
if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING, true) ) { if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING, true) ) {
return; return;
} }
if ((lastEvent == Helper.EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) { if ((lastEvent == Helper.EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) { switch (event.trim()) {
case "event: update": case "event: update":
@ -234,8 +286,8 @@ public class LiveNotificationService extends BaseService {
inputStream.close(); inputStream.close();
} catch (IOException ignored) {} } catch (IOException ignored) {}
} }
if (httpsURLConnection != null) if (httpsURLConnectionHashMap.get(account.getAcct() + account.getInstance()) != null)
httpsURLConnection.disconnect(); httpsURLConnectionHashMap.get(account.getAcct() + account.getInstance()).disconnect();
SystemClock.sleep(5000); SystemClock.sleep(5000);
Intent streamingIntent = new Intent(this, LiveNotificationService.class); Intent streamingIntent = new Intent(this, LiveNotificationService.class);
streamingIntent.putExtra("userId", account.getId()); streamingIntent.putExtra("userId", account.getId());