Some cleaning and adds previous streaming service if user wants to stop the new one.

This commit is contained in:
stom79 2017-11-12 08:33:24 +01:00
parent bf8d5eb734
commit 5bf0472d18
54 changed files with 559 additions and 285 deletions

View File

@ -38,15 +38,19 @@
<service
android:name=".services.StreamingService"
android:exported="false"/>
<service
android:name=".services.LiveNotificationService"
android:exported="false"/>
<receiver android:name=".services.RestartServiceReceiver"
android:exported="false">
<intent-filter>
<action android:name="RestartStreamingService" />
</intent-filter>
</receiver>
<receiver android:name=".services.RestartServiceReceiver" >
<!-- This intent filter receives the boot completed event -->
<receiver android:name=".services.RestartLiveNotificationReceiver"
android:exported="false">
<intent-filter>
<action android:name="RestartStreamingService" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

View File

@ -32,6 +32,7 @@ import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRelationshipAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRemoteAccountsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
@ -43,7 +44,6 @@ import fr.gouv.etalab.mastodon.helper.ExpandableHeightListView;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRelationshipInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRemoteAccountInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -50,12 +50,13 @@ import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.client.KinrarClient;
import fr.gouv.etalab.mastodon.client.MastalabSSLSocketFactory;
import fr.gouv.etalab.mastodon.client.OauthClient;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.USER_AGENT;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;

View File

@ -74,6 +74,7 @@ import java.util.Locale;
import java.util.Stack;
import java.util.regex.Matcher;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveInstanceAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMetaDataAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask;
@ -92,6 +93,7 @@ import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveInstanceInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
import fr.gouv.etalab.mastodon.services.LiveNotificationService;
import fr.gouv.etalab.mastodon.services.StreamingService;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
@ -1057,22 +1059,22 @@ public abstract class BaseMainActivity extends AppCompatActivity
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
StreamingService.EventStreaming eventStreaming = (StreamingService.EventStreaming) intent.getSerializableExtra("eventStreaming");
LiveNotificationService.EventStreaming eventStreaming = (LiveNotificationService.EventStreaming) intent.getSerializableExtra("eventStreaming");
assert b != null;
userIdService = b.getString("userIdService", null);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if( userIdService != null && userIdService.equals(userId)) {
if (eventStreaming == StreamingService.EventStreaming.NOTIFICATION) {
if (eventStreaming == LiveNotificationService.EventStreaming.NOTIFICATION) {
Notification notification = b.getParcelable("data");
if (notificationsFragment != null) {
notificationsFragment.refresh(notification);
}
} else if (eventStreaming == StreamingService.EventStreaming.UPDATE) {
} else if (eventStreaming == LiveNotificationService.EventStreaming.UPDATE) {
Status status = b.getParcelable("data");
if (homeFragment != null) {
homeFragment.refresh(status);
}
} else if (eventStreaming == StreamingService.EventStreaming.DELETE) {
} else if (eventStreaming == LiveNotificationService.EventStreaming.DELETE) {
//noinspection unused
String id = b.getString("id");
if (notificationsFragment != null) {
@ -1460,9 +1462,17 @@ public abstract class BaseMainActivity extends AppCompatActivity
return activityPaused;
}
public void startSreaming(boolean restart){
streamingIntent = new Intent(this, StreamingService.class);
streamingIntent.putExtra("restart",restart);
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);
}else {
streamingIntent = new Intent(this, StreamingService.class);
}
startService(streamingIntent);
}
}

View File

@ -58,6 +58,8 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.UpdateCredentialAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
@ -69,7 +71,7 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountInterface;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateCredentialInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;

View File

@ -33,13 +33,14 @@ import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -31,12 +31,13 @@ import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveInstanceAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Instance;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveInstanceInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;

View File

@ -63,11 +63,12 @@ import java.security.UnrecoverableKeyException;
import java.util.ArrayList;
import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.MastalabSSLSocketFactory;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.EXTERNAL_STORAGE_REQUEST_CODE;
import static fr.gouv.etalab.mastodon.helper.Helper.USER_AGENT;

View File

@ -19,8 +19,10 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -63,6 +63,7 @@ import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRelationshipAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRemoteAccountsAsyncTask;
@ -81,7 +82,7 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRelationshipInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRemoteAccountInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -26,6 +26,7 @@ import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
@ -34,7 +35,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.drawers.SearchListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveSearchInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -70,6 +70,7 @@ import java.util.ArrayList;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
@ -90,7 +91,6 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsAccountInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRelationshipInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;

View File

@ -51,6 +51,7 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveContextAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
@ -65,7 +66,6 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveContextInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -143,7 +143,7 @@ import fr.gouv.etalab.mastodon.jobs.ScheduledTootsSyncJob;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;

View File

@ -37,7 +37,7 @@ import android.widget.FrameLayout;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.webview.MastalabWebChromeClient;
import fr.gouv.etalab.mastodon.webview.MastalabWebViewClient;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.EXTERNAL_STORAGE_REQUEST_CODE;
import static fr.gouv.etalab.mastodon.helper.Helper.manageDownloads;

View File

@ -43,7 +43,7 @@ import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.client.OauthClient;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**
* Created by Thomas on 24/04/2017.

View File

@ -45,11 +45,12 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.*;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.USER_AGENT;

View File

@ -23,7 +23,6 @@ import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -42,6 +41,7 @@ import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.io.File;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
@ -50,9 +50,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
/**
@ -111,11 +109,11 @@ public class AccountSearchDevAdapter extends BaseAdapter implements OnPostAction
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_account_search_dev, parent, false);
holder = new ViewHolder();
holder.account_pp = (ImageView) convertView.findViewById(R.id.account_pp);
holder.account_dn = (TextView) convertView.findViewById(R.id.account_dn);
holder.account_un = (TextView) convertView.findViewById(R.id.account_un);
holder.account_follow = (FloatingActionButton) convertView.findViewById(R.id.account_follow);
holder.acccount_container = (LinearLayout) convertView.findViewById(R.id.acccount_container);
holder.account_pp = convertView.findViewById(R.id.account_pp);
holder.account_dn = convertView.findViewById(R.id.account_dn);
holder.account_un = convertView.findViewById(R.id.account_un);
holder.account_follow = convertView.findViewById(R.id.account_follow);
holder.acccount_container = convertView.findViewById(R.id.acccount_container);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
@ -124,6 +122,7 @@ public class AccountSearchDevAdapter extends BaseAdapter implements OnPostAction
final float scale = context.getResources().getDisplayMetrics().density;
if( account != null && account.isLocked()){
Drawable img = ContextCompat.getDrawable(context, R.drawable.ic_lock_outline);
assert img != null;
img.setBounds(0,0,(int) (20 * scale + 0.5f),(int) (20 * scale + 0.5f));
holder.account_dn.setCompoundDrawables( null, null, img, null);
}else{
@ -132,13 +131,15 @@ public class AccountSearchDevAdapter extends BaseAdapter implements OnPostAction
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
assert account != null;
holder.account_dn.setText(Helper.shortnameToUnicode(account.getDisplay_name(), true));
holder.account_un.setText(String.format("@%s",account.getAcct()));
}else {
assert account != null;
holder.account_dn.setText(Helper.shortnameToUnicode(account.getDisplay_name(), true));
holder.account_un.setText(String.format("@%s",account.getAcct()));
}
changeDrawableColor(context, R.drawable.ic_lock_outline,R.color.mastodonC4);
Helper.changeDrawableColor(context, R.drawable.ic_lock_outline,R.color.mastodonC4);
//Profile picture
imageLoader.displayImage(account.getAvatar(), holder.account_pp, options);

View File

@ -45,7 +45,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**

View File

@ -39,6 +39,8 @@ import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
@ -46,7 +48,6 @@ import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;

View File

@ -30,9 +30,10 @@ import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.TootActivity;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import mastodon.etalab.gouv.fr.mastodon.R;

View File

@ -35,12 +35,14 @@ import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Mention;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -38,7 +38,7 @@ import java.util.List;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**
* Created by Thomas on 03/11/2017.

View File

@ -38,7 +38,7 @@ import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;

View File

@ -33,8 +33,9 @@ import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import mastodon.etalab.gouv.fr.mastodon.R;
/**

View File

@ -57,6 +57,7 @@ import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MediaActivity;
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
import fr.gouv.etalab.mastodon.activities.ShowConversationActivity;
@ -73,7 +74,6 @@ import fr.gouv.etalab.mastodon.helper.CrossActions;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import fr.gouv.etalab.mastodon.interfaces.OnPostNotificationsActionInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.helper.Helper;

View File

@ -51,7 +51,7 @@ import fr.gouv.etalab.mastodon.jobs.ApplicationJob;
import fr.gouv.etalab.mastodon.jobs.ScheduledTootsSyncJob;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;

View File

@ -48,7 +48,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**

View File

@ -71,6 +71,7 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MediaActivity;
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
import fr.gouv.etalab.mastodon.activities.ShowConversationActivity;
@ -93,7 +94,6 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRepliesInterface;
import fr.gouv.etalab.mastodon.interfaces.OnTranslatedInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.activities.MainActivity.currentLocale;
import static fr.gouv.etalab.mastodon.helper.Helper.THEME_DARK;

View File

@ -23,7 +23,7 @@ import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**

View File

@ -26,7 +26,8 @@ import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**

View File

@ -40,7 +40,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveManyRelationshipsInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.drawers.AccountsListAdapter;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsInterface;

View File

@ -37,7 +37,7 @@ import fr.gouv.etalab.mastodon.drawers.DraftsListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;

View File

@ -38,7 +38,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.drawers.AccountsFollowRequestAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**

View File

@ -33,13 +33,13 @@ import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingNotificationsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveNotificationsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;

View File

@ -42,7 +42,7 @@ import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveScheduledTootsInterface;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;

View File

@ -35,6 +35,7 @@ import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
@ -43,7 +44,6 @@ import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingFeedsInterface;
import fr.gouv.etalab.mastodon.services.StreamingFederatedTimelineService;
import fr.gouv.etalab.mastodon.services.StreamingLocalTimelineService;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;

View File

@ -52,7 +52,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static android.app.Activity.RESULT_OK;
import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT;

View File

@ -16,10 +16,8 @@ package fr.gouv.etalab.mastodon.fragments;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
@ -41,15 +39,8 @@ import android.widget.TimePicker;
import android.widget.Toast;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.BaseMainActivity;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.services.StreamingService;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.compareDate;

View File

@ -25,7 +25,7 @@ import android.widget.SeekBar;
import android.widget.TextView;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**

View File

@ -24,7 +24,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**

View File

@ -41,7 +41,7 @@ import fr.gouv.etalab.mastodon.drawers.AccountsSearchAdapter;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**
* Will handle cross actions between accounts boost/favourites/pin and replies

View File

@ -123,6 +123,7 @@ import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.HashTagActivity;
import fr.gouv.etalab.mastodon.activities.LoginActivity;
import fr.gouv.etalab.mastodon.activities.MainActivity;
@ -140,7 +141,6 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
import static android.content.Context.DOWNLOAD_SERVICE;

View File

@ -40,6 +40,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveHomeTimelineServiceAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
@ -50,7 +51,7 @@ import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveHomeTimelineServiceInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;

View File

@ -44,7 +44,7 @@ import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveNotificationsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Notification;

View File

@ -0,0 +1,369 @@
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.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.BaseMainActivity;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.client.TLSSocketFactory;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
/**
* Created by Thomas on 28/08/2017.
* Manage service for streaming api and new notifications
*/
public class LiveNotificationService extends Service {
public enum EventStreaming{
UPDATE,
NOTIFICATION,
DELETE,
NONE
}
protected Account account;
private boolean restartCalled;
public void onCreate() {
super.onCreate();
restartCalled = false;
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);
if( liveNotifications && notify){
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount();
for(final Account accountStream: accountStreams){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
streamOnUser(accountStream);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
if(extras != null) {
boolean restart = (boolean) extras.get("restart");
if( restart) {
SystemClock.sleep(1000);
sendBroadcast(new Intent("RestartStreamingService"));
stopSelf();
}
}
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void streamOnUser(Account accountStream){
InputStream inputStream = null;
HttpsURLConnection httpsURLConnection = null;
BufferedReader reader = null;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
EventStreaming lastEvent = null;
if( accountStream != null){
try {
URL url = new URL("https://" + accountStream.getInstance() + "/api/v1/streaming/user");
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + accountStream.getToken());
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Keep-Alive", "header");
httpsURLConnection.setRequestProperty("Connection", "close");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setConnectTimeout(70000);
httpsURLConnection.setReadTimeout(70000);
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(inputStream));
String event;
EventStreaming eventStreaming;
while((event = reader.readLine()) != null) {
if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING, true) ) {
stopSelf();
return;
}
if ((lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) {
case "event: update":
lastEvent = EventStreaming.UPDATE;
break;
case "event: notification":
lastEvent = EventStreaming.NOTIFICATION;
break;
case "event: delete":
lastEvent = EventStreaming.DELETE;
break;
default:
lastEvent = EventStreaming.NONE;
}
} else {
if (!event.startsWith("data: ")) {
lastEvent = EventStreaming.NONE;
continue;
}
event = event.substring(6);
if (lastEvent == EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE;
} else if (lastEvent == EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION;
} else if (lastEvent == EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE;
event = "{id:" + event + "}";
} else {
eventStreaming = EventStreaming.UPDATE;
}
lastEvent = EventStreaming.NONE;
try {
JSONObject eventJson = new JSONObject(event);
onRetrieveStreaming(eventStreaming, accountStream, eventJson);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (IOException ignored){}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignored) {}
}
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
if( !restartCalled ) {
restartCalled = true;
SystemClock.sleep(1000);
sendBroadcast(new Intent("RestartStreamingService"));
stopSelf();
}
}
}
}
public void onRetrieveStreaming(EventStreaming event, final Account account, JSONObject response) {
if( response == null )
return;
//No previous notifications in cache, so no notification will be sent
Status status ;
final Notification notification;
String dataId = null;
Bundle b = new Bundle();
if( event == EventStreaming.NOTIFICATION){
notification = API.parseNotificationResponse(getApplicationContext(), response);
b.putParcelable("data", notification);
boolean activityPaused;
try {
activityPaused = BaseMainActivity.activityState();
}catch (Exception e){
activityPaused = true;
}
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
boolean canNotify = Helper.canNotify(getApplicationContext());
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if((userId == null || !userId.equals(account.getId()) || activityPaused) && liveNotifications && canNotify && notify) {
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
boolean somethingToPush = (notif_follow || notif_add || notif_mention || notif_share);
String title = null;
if( somethingToPush && notification != null){
switch (notification.getType()){
case "mention":
if(notif_mention){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_mention));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_mention));
}
break;
case "reblog":
if(notif_share){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_reblog));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_reblog));
}
break;
case "favourite":
if(notif_add){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_favourite));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_favourite));
}
break;
case "follow":
if(notif_follow){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_follow));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_follow));
}
break;
default:
}
//Some others notification
final Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
intent.putExtra(PREF_KEY_ID, account.getId());
long notif_id = Long.parseLong(account.getId());
final int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
if( notification.getAccount().getAvatar() != null ) {
ImageLoader imageLoaderNoty = ImageLoader.getInstance();
File cacheDir = new File(getApplicationContext().getCacheDir(), getApplicationContext().getString(R.string.app_name));
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.imageDownloader(new PatchBaseImageDownloader(getApplicationContext()))
.threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
.diskCache(new UnlimitedDiskCache(cacheDir))
.build();
imageLoaderNoty.init(config);
DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false)
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
final String finalTitle = title;
if( title != null) {
imageLoaderNoty.loadImage(notification.getAccount().getAvatar(), options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
notify_user(getApplicationContext(), intent, notificationId, loadedImage, finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null);
if (lastNotif == null || Long.parseLong(notification.getId()) > Long.parseLong(lastNotif)) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), notification.getId());
editor.apply();
}
}
@Override
public void onLoadingFailed(java.lang.String imageUri, android.view.View view, FailReason failReason) {
notify_user(getApplicationContext(), intent, notificationId, BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.mastodonlogo), finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null);
if (lastNotif == null || Long.parseLong(notification.getId()) > Long.parseLong(lastNotif)) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), notification.getId());
editor.apply();
}
}
});
}
}
}
}
}else if ( event == EventStreaming.UPDATE){
status = API.parseStatuses(getApplicationContext(), response);
status.setReplies(new ArrayList<Status>());
status.setNew(true);
b.putParcelable("data", status);
}else if( event == EventStreaming.DELETE){
try {
dataId = response.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
}
if( account != null)
b.putString("userIdService",account.getId());
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
intentBC.putExtra("eventStreaming", event);
intentBC.putExtras(b);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
}
}

View File

@ -0,0 +1,43 @@
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.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
* Created by Thomas on 22/09/2017.
* BroadcastReceiver for restarting the service
*/
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 notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
if( notify && liveNotifications) {
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationService.class);
context.startService(streamingServiceIntent);
}
}
}

View File

@ -13,13 +13,9 @@ 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;
import android.content.SharedPreferences;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
* Created by Thomas on 22/09/2017.
@ -28,16 +24,10 @@ import fr.gouv.etalab.mastodon.helper.Helper;
public class RestartServiceReceiver 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 notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
if( notify && liveNotifications) {
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), StreamingService.class);
context.startService(streamingServiceIntent);
}
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), StreamingService.class);
context.startService(streamingServiceIntent);
}
}

View File

@ -40,7 +40,6 @@ import javax.net.ssl.HttpsURLConnection;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.TLSSocketFactory;
import fr.gouv.etalab.mastodon.helper.Helper;
@ -61,9 +60,11 @@ public class StreamingFederatedTimelineService extends IntentService {
*
* @param name Used to name the worker thread, important only for debugging.
*/
@SuppressWarnings("unused")
public StreamingFederatedTimelineService(String name) {
super(name);
}
@SuppressWarnings("unused")
public StreamingFederatedTimelineService() {
super("StreamingFederatedTimelineService");
}

View File

@ -59,9 +59,11 @@ public class StreamingLocalTimelineService extends IntentService {
*
* @param name Used to name the worker thread, important only for debugging.
*/
@SuppressWarnings("unused")
public StreamingLocalTimelineService(String name) {
super(name);
}
@SuppressWarnings("unused")
public StreamingLocalTimelineService() {
super("StreamingLocalTimelineService");
}

View File

@ -13,61 +13,37 @@ 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.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import fr.gouv.etalab.mastodon.activities.BaseMainActivity;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.client.TLSSocketFactory;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
/**
@ -75,9 +51,25 @@ import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
* Manage service for streaming api and new notifications
*/
public class StreamingService extends Service {
public class StreamingService extends IntentService {
private EventStreaming lastEvent;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
@SuppressWarnings("unused")
public StreamingService(String name) {
super(name);
}
@SuppressWarnings("unused")
public StreamingService() {
super("StreamingService");
}
private static HttpsURLConnection httpsURLConnection;
public enum EventStreaming{
UPDATE,
NOTIFICATION,
@ -86,62 +78,32 @@ public class StreamingService extends Service {
}
protected Account account;
private boolean restartCalled;
public void onCreate() {
super.onCreate();
restartCalled = false;
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);
if( liveNotifications && notify){
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount();
for(final Account accountStream: accountStreams){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
streamOnUser(accountStream);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING+userId, true);
editor.apply();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
if(extras != null) {
boolean restart = (boolean) extras.get("restart");
if( restart) {
SystemClock.sleep(1000);
sendBroadcast(new Intent("RestartStreamingService"));
stopSelf();
}
}
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void streamOnUser(Account accountStream){
InputStream inputStream = null;
HttpsURLConnection httpsURLConnection = null;
protected void onHandleIntent(@Nullable Intent intent) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
InputStream inputStream;
BufferedReader reader = null;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
EventStreaming lastEvent = null;
Account accountStream = null;
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
if( userId != null) {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
accountStream = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
}
if( accountStream != null){
try {
URL url = new URL("https://" + accountStream.getInstance() + "/api/v1/streaming/user");
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
@ -158,11 +120,8 @@ public class StreamingService extends Service {
String event;
EventStreaming eventStreaming;
while((event = reader.readLine()) != null) {
if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING, true) ) {
if( !sharedpreferences.getBoolean(Helper.SHOULD_CONTINUE_STREAMING + accountStream.getId(), true) )
stopSelf();
return;
}
if ((lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) {
case "event: update":
@ -208,144 +167,29 @@ public class StreamingService extends Service {
if(reader != null){
try{
reader.close();
}catch (IOException ignored){}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignored) {}
}
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
if( !restartCalled ) {
restartCalled = true;
SystemClock.sleep(1000);
sendBroadcast(new Intent("RestartStreamingService"));
stopSelf();
}catch (IOException e){
e.printStackTrace();
}
}
SystemClock.sleep(1000);
sendBroadcast(new Intent("RestartStreamingService"));
}
}
}
public void onRetrieveStreaming(EventStreaming event, final Account account, JSONObject response) {
public void onRetrieveStreaming(EventStreaming event, Account account, JSONObject response) {
if( response == null )
return;
//No previous notifications in cache, so no notification will be sent
Status status ;
final Notification notification;
Notification notification;
//noinspection unused
String dataId = null;
Bundle b = new Bundle();
if( event == EventStreaming.NOTIFICATION){
notification = API.parseNotificationResponse(getApplicationContext(), response);
b.putParcelable("data", notification);
boolean activityPaused;
try {
activityPaused = BaseMainActivity.activityState();
}catch (Exception e){
activityPaused = true;
}
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
boolean canNotify = Helper.canNotify(getApplicationContext());
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if((userId == null || !userId.equals(account.getId()) || activityPaused) && liveNotifications && canNotify && notify) {
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
boolean somethingToPush = (notif_follow || notif_add || notif_mention || notif_share);
String title = null;
if( somethingToPush && notification != null){
switch (notification.getType()){
case "mention":
if(notif_mention){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_mention));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_mention));
}
break;
case "reblog":
if(notif_share){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_reblog));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_reblog));
}
break;
case "favourite":
if(notif_add){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_favourite));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_favourite));
}
break;
case "follow":
if(notif_follow){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getApplicationContext().getString(R.string.notif_follow));
else
title = String.format("@%s %s", notification.getAccount().getUsername(),getApplicationContext().getString(R.string.notif_follow));
}
break;
default:
}
//Some others notification
final Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
intent.putExtra(PREF_KEY_ID, account.getId());
long notif_id = Long.parseLong(account.getId());
final int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
if( notification.getAccount().getAvatar() != null ) {
ImageLoader imageLoaderNoty = ImageLoader.getInstance();
File cacheDir = new File(getApplicationContext().getCacheDir(), getApplicationContext().getString(R.string.app_name));
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.imageDownloader(new PatchBaseImageDownloader(getApplicationContext()))
.threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
.diskCache(new UnlimitedDiskCache(cacheDir))
.build();
imageLoaderNoty.init(config);
DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false)
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
final String finalTitle = title;
if( title != null) {
imageLoaderNoty.loadImage(notification.getAccount().getAvatar(), options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
notify_user(getApplicationContext(), intent, notificationId, loadedImage, finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null);
if (lastNotif == null || Long.parseLong(notification.getId()) > Long.parseLong(lastNotif)) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), notification.getId());
editor.apply();
}
}
@Override
public void onLoadingFailed(java.lang.String imageUri, android.view.View view, FailReason failReason) {
notify_user(getApplicationContext(), intent, notificationId, BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.mastodonlogo), finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null);
if (lastNotif == null || Long.parseLong(notification.getId()) > Long.parseLong(lastNotif)) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), notification.getId());
editor.apply();
}
}
});
}
}
}
}
}else if ( event == EventStreaming.UPDATE){
status = API.parseStatuses(getApplicationContext(), response);
status.setReplies(new ArrayList<Status>());
@ -353,6 +197,7 @@ public class StreamingService extends Service {
b.putParcelable("data", status);
}else if( event == EventStreaming.DELETE){
try {
//noinspection UnusedAssignment
dataId = response.getString("id");
} catch (JSONException e) {
e.printStackTrace();
@ -366,4 +211,4 @@ public class StreamingService extends Service {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
}
}
}

View File

@ -32,10 +32,10 @@ import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnTranslatedInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.activities.BaseMainActivity.currentLocale;

View File

@ -30,7 +30,7 @@ import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**
* Created by Thomas on 25/06/2017.

View File

@ -25,7 +25,7 @@ import android.webkit.WebViewClient;
import android.widget.TextView;
import fr.gouv.etalab.mastodon.activities.WebviewActivity;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.R;
/**
* Created by Thomas on 25/06/2017.