Some improvements

This commit is contained in:
stom79 2017-11-30 07:32:14 +01:00
parent 257c799442
commit 64bb95e51a
2 changed files with 53 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
@ -58,7 +59,7 @@ public class BackGroundTask extends AsyncTask<Void, Void, Void> {
private Account account;
private WeakReference<Context> contextReference;
public BackGroundTask(Context context, Account account){
BackGroundTask(Context context, Account account){
this.contextReference = new WeakReference<>(context);
this.account = account;
}
@ -156,10 +157,18 @@ public class BackGroundTask extends AsyncTask<Void, Void, Void> {
httpsURLConnection.disconnect();
}
}
SystemClock.sleep(5000);
return null;
}
@Override
protected void onPostExecute(Void result) {
Intent streamingServiceIntent = new Intent(contextReference.get(), LiveNotificationService.class);
streamingServiceIntent.putExtra("userId", account.getId());
try {
contextReference.get().startService(streamingServiceIntent);
}catch (Exception ignored){}
}
private void onRetrieveStreaming(Helper.EventStreaming event, final Account account, JSONObject response) {

View File

@ -51,17 +51,6 @@ public class LiveNotificationService extends IntentService {
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
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);
@ -82,4 +71,46 @@ public class LiveNotificationService extends IntentService {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
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.getExtras() == null) {
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount();
if (accountStreams != null){
for (final Account accountStream : accountStreams) {
if (backGroundTaskHashMap.containsKey(accountStream.getAcct() + accountStream.getInstance()))
if (!backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).isCancelled())
backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).cancel(true);
BackGroundTask task = new BackGroundTask(getApplicationContext(), accountStream);
backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), task);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}else if((userId = intent.getStringExtra("userId")) != null){
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()).isCancelled())
backGroundTaskHashMap.get(accountStream.getAcct() + accountStream.getInstance()).cancel(true);
BackGroundTask task = new BackGroundTask(getApplicationContext(), accountStream);
backGroundTaskHashMap.put(accountStream.getAcct() + accountStream.getInstance(), task);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
}
}