improve service
This commit is contained in:
parent
d5c6d83582
commit
ae4dd1245f
|
@ -16,6 +16,9 @@ android {
|
|||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
debug {
|
||||
pseudoLocalesEnabled true
|
||||
}
|
||||
}
|
||||
|
||||
productFlavors {
|
||||
|
|
|
@ -47,24 +47,19 @@
|
|||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver android:name=".services.StopLiveNotificationReceiver"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="StopLiveNotificationReceiver" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<service
|
||||
android:name=".services.StreamingFederatedTimelineService"
|
||||
android:exported="false"/>
|
||||
<receiver android:name=".services.RestartFederatedServiceReceiver"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="RestartStreamingFederatedService" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<service
|
||||
android:name=".services.StreamingLocalTimelineService"
|
||||
android:exported="false"/>
|
||||
<receiver android:name=".services.RestartLocalServiceReceiver"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="RestartStreamingLocalService" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<activity
|
||||
android:name=".activities.MainActivity"
|
||||
android:label="@string/app_name"
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package fr.gouv.etalab.mastodon.activities;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
|
@ -139,7 +140,6 @@ public abstract class BaseMainActivity extends BaseActivity
|
|||
public static int countNewStatus = 0;
|
||||
public static int countNewNotifications = 0;
|
||||
private String userIdService;
|
||||
private Intent streamingIntent;
|
||||
public static String lastHomeId = null, lastNotificationId = null;
|
||||
boolean notif_follow, notif_add, notif_mention, notif_share, show_boosts, show_replies;
|
||||
String show_filtered;
|
||||
|
@ -225,7 +225,7 @@ public abstract class BaseMainActivity extends BaseActivity
|
|||
changeDrawableColor(getApplicationContext(), R.drawable.ic_notifications,R.color.dark_text);
|
||||
changeDrawableColor(getApplicationContext(), R.drawable.ic_people,R.color.dark_text);
|
||||
changeDrawableColor(getApplicationContext(), R.drawable.ic_public,R.color.dark_text);
|
||||
startSreaming(false);
|
||||
startSreaming();
|
||||
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
|
||||
tabLayout.setTabMode(TabLayout.MODE_FIXED);
|
||||
|
||||
|
@ -1234,13 +1234,6 @@ public abstract class BaseMainActivity extends BaseActivity
|
|||
@Override
|
||||
public void onStop(){
|
||||
super.onStop();
|
||||
if( streamingIntent != null) {
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, false);
|
||||
stopService(streamingIntent);
|
||||
editor.apply();
|
||||
}
|
||||
if( receive_federated_data != null)
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(receive_federated_data);
|
||||
if( receive_local_data != null)
|
||||
|
@ -1638,13 +1631,19 @@ public abstract class BaseMainActivity extends BaseActivity
|
|||
}
|
||||
|
||||
|
||||
public void startSreaming(boolean restart){
|
||||
public void startSreaming(){
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
||||
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
|
||||
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
|
||||
if( notify && liveNotifications) {
|
||||
streamingIntent = new Intent(this, LiveNotificationService.class);
|
||||
streamingIntent.putExtra("restart", restart);
|
||||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
assert manager != null;
|
||||
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||||
if (LiveNotificationService.class.getName().equals(service.service.getClassName())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Intent streamingIntent = new Intent(this, LiveNotificationService.class);
|
||||
startService(streamingIntent);
|
||||
}
|
||||
|
||||
|
|
|
@ -257,8 +257,10 @@ public class SettingsFragment extends Fragment {
|
|||
editor.apply();
|
||||
if( set_live_notif.isChecked() ){
|
||||
try {
|
||||
((MainActivity) context).startSreaming(true);
|
||||
((MainActivity) context).startSreaming();
|
||||
}catch (Exception ignored){ignored.printStackTrace();}
|
||||
}else{
|
||||
context.sendBroadcast(new Intent("StopLiveNotificationService"));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -14,6 +14,7 @@ package fr.gouv.etalab.mastodon.services;
|
|||
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
|
@ -27,6 +28,7 @@ import android.os.Looper;
|
|||
import android.os.SystemClock;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
|
@ -75,67 +77,70 @@ import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
|||
* Manage service for streaming api and new notifications
|
||||
*/
|
||||
|
||||
public class LiveNotificationService extends BaseService {
|
||||
public class LiveNotificationService extends Service {
|
||||
|
||||
|
||||
|
||||
protected Account account;
|
||||
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")
|
||||
public LiveNotificationService(String name) {
|
||||
super(name);
|
||||
}
|
||||
@SuppressWarnings("unused")
|
||||
public LiveNotificationService() {
|
||||
super("LiveNotificationService");
|
||||
}
|
||||
private boolean stop = false;
|
||||
|
||||
public void 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);
|
||||
}
|
||||
}
|
||||
Log.v(Helper.TAG,"onCreate= ");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
Log.v(Helper.TAG,"onStartCommand= " + intent);
|
||||
if( intent == null || intent.getBooleanExtra("stop", false) ) {
|
||||
stop = true;
|
||||
stopSelf();
|
||||
}
|
||||
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) {
|
||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
if( liveNotifications && notify){
|
||||
|
||||
if( intent == null || intent.getStringExtra("userId") == null) {
|
||||
|
||||
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount();
|
||||
if (accountStreams != null) {
|
||||
if (accountStreams != null){
|
||||
for (final Account accountStream : accountStreams) {
|
||||
int numberOfQueuedIntent = queuedIntentHashMap.get(accountStream.getAcct() + accountStream.getInstance()) + 1;
|
||||
queuedIntentHashMap.put(accountStream.getAcct() + accountStream.getInstance(), numberOfQueuedIntent);
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
taks(accountStream);
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
|
||||
}
|
||||
}
|
||||
}else{
|
||||
}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);
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
taks(accountStream);
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
|
@ -143,69 +148,18 @@ public class LiveNotificationService extends BaseService {
|
|||
}
|
||||
|
||||
@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.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);
|
||||
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.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if( !stop)
|
||||
sendBroadcast(new Intent("RestartLiveNotificationService"));
|
||||
}
|
||||
|
||||
private void taks(Account account){
|
||||
InputStream inputStream = null;
|
||||
|
||||
BufferedReader reader = null;
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
Helper.EventStreaming lastEvent = null;
|
||||
|
||||
if( account != null){
|
||||
try {
|
||||
HttpsURLConnection httpsURLConnection = httpsURLConnectionHashMap.get(account.getAcct() + account.getInstance());
|
||||
|
@ -220,8 +174,6 @@ public class LiveNotificationService extends BaseService {
|
|||
httpsURLConnection.setRequestProperty("Connection", "close");
|
||||
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
|
||||
httpsURLConnection.setRequestMethod("GET");
|
||||
httpsURLConnection.setConnectTimeout(70000);
|
||||
httpsURLConnection.setReadTimeout(70000);
|
||||
httpsURLConnectionHashMap.put(account.getAcct() + account.getInstance(), httpsURLConnection);
|
||||
if( httpsURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
|
||||
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
|
||||
|
@ -229,10 +181,7 @@ public class LiveNotificationService extends BaseService {
|
|||
String event;
|
||||
Helper.EventStreaming eventStreaming;
|
||||
while((event = reader.readLine()) != null) {
|
||||
if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING, true) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log.v(Helper.TAG,account.getAcct()+"@" + account.getInstance() + " -> " + event);
|
||||
if ((lastEvent == Helper.EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
|
||||
switch (event.trim()) {
|
||||
case "event: update":
|
||||
|
@ -274,7 +223,7 @@ public class LiveNotificationService extends BaseService {
|
|||
httpsURLConnection.disconnect();
|
||||
}
|
||||
|
||||
} catch (Exception ignored) {}finally {
|
||||
} catch (Exception ignored) {Log.v(Helper.TAG,account.getAcct()+"@" + account.getInstance() + " -> " + ignored.getMessage());}finally {
|
||||
|
||||
if (reader != null) {
|
||||
try {
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
package fr.gouv.etalab.mastodon.services;
|
||||
/* Copyright 2017 Thomas Schneider
|
||||
*
|
||||
* This file is a part of Mastalab
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Mastalab 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 Mastalab; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
/**
|
||||
* Created by Thomas on 29/09/2017.
|
||||
* BroadcastReceiver for restarting the service for listening local timeline
|
||||
*/
|
||||
|
||||
public class RestartLocalServiceReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), StreamingLocalTimelineService.class);
|
||||
try {
|
||||
context.startService(streamingServiceIntent);
|
||||
}catch (Exception ignored){}
|
||||
}
|
||||
|
||||
}
|
|
@ -13,20 +13,24 @@ package fr.gouv.etalab.mastodon.services;
|
|||
*
|
||||
* You should have received a copy of the GNU General Public License along with Mastalab; 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 26/09/2017.
|
||||
* BroadcastReceiver for restarting the service for listening federated timeline
|
||||
* Created by Thomas on 22/09/2017.
|
||||
* BroadcastReceiver for restarting the service
|
||||
*/
|
||||
|
||||
public class RestartFederatedServiceReceiver extends BroadcastReceiver {
|
||||
public class StopLiveNotificationReceiver extends BroadcastReceiver {
|
||||
|
||||
@SuppressLint("UnsafeProtectedBroadcastReceiver")
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), StreamingFederatedTimelineService.class);
|
||||
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationService.class);
|
||||
streamingServiceIntent.putExtra("stop",true);
|
||||
try {
|
||||
context.startService(streamingServiceIntent);
|
||||
}catch (Exception ignored){}
|
|
@ -143,7 +143,10 @@ public class StreamingFederatedTimelineService extends IntentService {
|
|||
}
|
||||
if( sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + accountStream.getId() + accountStream.getInstance(), true)) {
|
||||
SystemClock.sleep(1000);
|
||||
sendBroadcast(new Intent("RestartStreamingFederatedService"));
|
||||
Intent streamingFederatedTimelineService = new Intent(this, StreamingFederatedTimelineService.class);
|
||||
try {
|
||||
startService(streamingFederatedTimelineService);
|
||||
}catch (Exception ignored){}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -142,7 +142,10 @@ public class StreamingLocalTimelineService extends IntentService {
|
|||
}
|
||||
if( sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + accountStream.getId() + accountStream.getInstance(), true)) {
|
||||
SystemClock.sleep(1000);
|
||||
sendBroadcast(new Intent("RestartStreamingLocalService"));
|
||||
Intent streamingLocalTimelineService = new Intent(this, StreamingLocalTimelineService.class);
|
||||
try {
|
||||
startService(streamingLocalTimelineService);
|
||||
}catch (Exception ignored){}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue