Associates notifications in top bar to the right account & logs in it if the current authenticated account is different

This commit is contained in:
tom79 2017-05-28 14:58:04 +02:00
parent 5bc23d3a23
commit 0ecb808ecd
11 changed files with 71 additions and 48 deletions

View File

@ -64,6 +64,8 @@ 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;
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.changeUser;
import static fr.gouv.etalab.mastodon.helper.Helper.menuAccounts;
import static fr.gouv.etalab.mastodon.helper.Helper.updateHeaderAccountInfo;
@ -147,18 +149,7 @@ public class MainActivity extends AppCompatActivity
});
boolean menuWasSelected = false;
if( getIntent() != null && getIntent().getExtras() != null ){
Bundle extras = getIntent().getExtras();
if (extras.getInt(INTENT_ACTION) == NOTIFICATION_INTENT){
navigationView.setCheckedItem(R.id.nav_notification);
navigationView.getMenu().performIdentifierAction(R.id.nav_notification, 0);
menuWasSelected = true;
}else if( extras.getInt(INTENT_ACTION) == HOME_TIMELINE_INTENT){
navigationView.setCheckedItem(R.id.nav_home);
navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0);
menuWasSelected = true;
}
}
mamageNewIntent(getIntent());
if (savedInstanceState == null && !menuWasSelected) {
navigationView.setCheckedItem(R.id.nav_home);
navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0);
@ -201,15 +192,32 @@ public class MainActivity extends AppCompatActivity
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mamageNewIntent(intent);
}
/**
* Manages new intents
* @param intent Intent - intent related to a notification in top bar
*/
private void mamageNewIntent(Intent intent){
if( intent == null || intent.getExtras() == null )
return;
Bundle extras = intent.getExtras();
String userIdIntent;
if( extras.containsKey(INTENT_ACTION) ){
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); //Id of the authenticated account
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
userIdIntent = extras.getString(PREF_KEY_ID); //Id of the account in the intent
if (extras.getInt(INTENT_ACTION) == NOTIFICATION_INTENT){
if( userId!= null && !userId.equals(userIdIntent)) //Connected account is different from the id in the intent
changeUser(MainActivity.this, userIdIntent); //Connects the account which is related to the notification
navigationView.setCheckedItem(R.id.nav_notification);
navigationView.getMenu().performIdentifierAction(R.id.nav_notification, 0);
}else if( extras.getInt(INTENT_ACTION) == HOME_TIMELINE_INTENT){
if( userId!= null && !userId.equals(userIdIntent)) //Connected account is different from the id in the intent
changeUser(MainActivity.this, userIdIntent); //Connects the account which is related to the notification
navigationView.setCheckedItem(R.id.nav_home);
navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0);
}

View File

@ -32,16 +32,17 @@ public class RetrieveHomeTimelineServiceAsyncTask extends AsyncTask<Void, Void,
private Context context;
private List<fr.gouv.etalab.mastodon.client.Entities.Status> statuses;
private String since_id;
private String acct;
private String acct, userId;
private OnRetrieveHomeTimelineServiceInterface listener;
private String instance;
public RetrieveHomeTimelineServiceAsyncTask(Context context, String instance, String since_id, String acct, OnRetrieveHomeTimelineServiceInterface onRetrieveHomeTimelineServiceInterface){
public RetrieveHomeTimelineServiceAsyncTask(Context context, String instance, String since_id, String acct, String userId, OnRetrieveHomeTimelineServiceInterface onRetrieveHomeTimelineServiceInterface){
this.context = context;
this.since_id = since_id;
this.listener = onRetrieveHomeTimelineServiceInterface;
this.acct = acct;
this.instance = instance;
this.userId = userId;
}
@Override
@ -52,7 +53,7 @@ public class RetrieveHomeTimelineServiceAsyncTask extends AsyncTask<Void, Void,
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveHomeTimelineService(statuses, acct);
listener.onRetrieveHomeTimelineService(statuses, acct, userId);
}
}

View File

@ -34,24 +34,26 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
private Context context;
private List<Notification> notifications;
private String max_id;
private String acct;
private String acct, userId;
private OnRetrieveNotificationsInterface listener;
private String instance;
public RetrieveNotificationsAsyncTask(Context context, String max_id, String acct, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
public RetrieveNotificationsAsyncTask(Context context, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
this.context = context;
this.max_id = max_id;
this.listener = onRetrieveNotificationsInterface;
this.acct = acct;
this.userId = userId;
}
public RetrieveNotificationsAsyncTask(Context context, String instance, String max_id, String acct, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
public RetrieveNotificationsAsyncTask(Context context, String instance, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
this.context = context;
this.max_id = max_id;
this.listener = onRetrieveNotificationsInterface;
this.acct = acct;
this.instance = instance;
this.userId = userId;
}
@Override
@ -65,7 +67,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveNotifications(notifications, acct);
listener.onRetrieveNotifications(notifications, acct, userId);
}
}

View File

@ -92,7 +92,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
if(firstVisibleItem + visibleItemCount == totalItemCount ) {
if(!flag_loading ) {
flag_loading = true;
asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null,DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null, null,DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
@ -108,7 +108,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
notifications = new ArrayList<>();
firstLoad = true;
flag_loading = true;
asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null,DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,
@ -116,7 +116,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
R.color.colorPrimaryDark);
asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return rootView;
}
@ -145,7 +145,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
@Override
public void onRetrieveNotifications(List<Notification> notifications, String acct) {
public void onRetrieveNotifications(List<Notification> notifications, String acct, String userId) {
if( firstLoad && (notifications == null || notifications.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
@ -170,13 +170,13 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
//Store last notification id to avoid to notify for those that have been already seen
if( notifications != null && notifications.size() > 0) {
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
//acct is null when used in Fragment, data need to be retrieved via shared preferences and db
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
//acct is null as userId when used in Fragment, data need to be retrieved via shared preferences and db
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account currentAccount = new AccountDAO(context, db).getAccountByID(userId);
if( currentAccount != null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + currentAccount.getAcct(), notifications.get(0).getId());
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + currentAccount.getId(), notifications.get(0).getId());
editor.apply();
}
}

View File

@ -256,7 +256,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
Account currentAccount = new AccountDAO(context, db).getAccountByID(userId);
if( currentAccount != null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + currentAccount.getAcct(), statuses.get(0).getId());
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + currentAccount.getId(), statuses.get(0).getId());
editor.apply();
}
}

View File

@ -414,19 +414,16 @@ public class Helper {
/**
* Sends notification with intent
* @param context Context
* @param intentAction int intent action
* @param intent Intent associated to the notifcation
* @param notificationId int id of the notification
* @param icon Bitmap profile picture
* @param title String title of the notification
* @param message String message for the notification
*/
public static void notify_user(Context context, int intentAction, int notificationId, Bitmap icon, String title, String message ) {
public static void notify_user(Context context, Intent intent, int notificationId, Bitmap icon, String title, String message ) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
// prepare intent which is triggered if the user click on the notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
final Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra(INTENT_ACTION, intentAction);
PendingIntent pIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// build notification
@ -601,7 +598,7 @@ public class Helper {
* @param activity Activity
* @param userID String - the new user id
*/
private static void changeUser(Activity activity, String userID) {
public static void changeUser(Activity activity, String userID) {
SQLiteDatabase db = Sqlite.getInstance(activity, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(activity,db).getAccountByID(userID);

View File

@ -23,5 +23,5 @@ import fr.gouv.etalab.mastodon.client.Entities.Status;
* Interface when home timeline toots have been retrieved
*/
public interface OnRetrieveHomeTimelineServiceInterface {
void onRetrieveHomeTimelineService(List<Status> statuses, String acct);
void onRetrieveHomeTimelineService(List<Status> statuses, String acct, String userId);
}

View File

@ -24,5 +24,5 @@ import fr.gouv.etalab.mastodon.client.Entities.Notification;
* Interface when notifications have been retrieved
*/
public interface OnRetrieveNotificationsInterface {
void onRetrieveNotifications(List<Notification> notifications, String acct);
void onRetrieveNotifications(List<Notification> notifications, String acct, String userId);
}

View File

@ -15,6 +15,7 @@ package fr.gouv.etalab.mastodon.jobs;
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
@ -34,6 +35,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveHomeTimelineServiceAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Status;
@ -45,6 +47,8 @@ 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;
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
@ -102,9 +106,9 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
return;
//Retrieve users in db that owner has.
for (Account account: accounts) {
String since_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + account.getAcct(), null);
String since_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + account.getId(), null);
notificationId = (int) Math.round(Double.parseDouble(account.getId())/1000);
new RetrieveHomeTimelineServiceAsyncTask(getContext(), account.getInstance(), since_id, account.getAcct(), HomeTimelineSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveHomeTimelineServiceAsyncTask(getContext(), account.getInstance(), since_id, account.getAcct(), account.getId(), HomeTimelineSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
@ -112,13 +116,13 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
@Override
public void onRetrieveHomeTimelineService(List<Status> statuses, String acct) {
public void onRetrieveHomeTimelineService(List<Status> statuses, String acct, String userId) {
if( statuses == null || statuses.size() == 0)
return;
Bitmap icon_notification = null;
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String max_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + acct, null);
String max_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, null);
//No previous notifications in cache, so no notification will be sent
if( max_id != null ){
String message;
@ -153,10 +157,14 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
message = getContext().getResources().getQuantityString(R.plurals.other_notif_hometimeline, statuses.size(), statuses.size());
else
message = "";
notify_user(getContext(), HOME_TIMELINE_INTENT, notificationId, icon_notification,title,message);
final Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra(INTENT_ACTION, HOME_TIMELINE_INTENT);
intent.putExtra(PREF_KEY_ID, userId);
notify_user(getContext(), intent, notificationId, icon_notification,title,message);
}
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + acct, statuses.get(0).getId());
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, statuses.get(0).getId());
editor.apply();
}

View File

@ -15,6 +15,7 @@ package fr.gouv.etalab.mastodon.jobs;
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
@ -36,6 +37,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.helper.Helper;
import mastodon.etalab.gouv.fr.mastodon.R;
@ -46,7 +48,9 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
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;
@ -112,9 +116,9 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
return;
//Retrieve users in db that owner has.
for (Account account: accounts) {
String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getAcct(), null);
String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null);
notificationId = (int) Math.round(Double.parseDouble(account.getId())/1000);
new RetrieveNotificationsAsyncTask(getContext(), account.getInstance(), max_id, account.getAcct(), NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveNotificationsAsyncTask(getContext(), account.getInstance(), max_id, account.getAcct(), account.getId(), NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
@ -122,7 +126,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
@Override
public void onRetrieveNotifications(List<Notification> notifications, String acct) {
public void onRetrieveNotifications(List<Notification> notifications, String acct, String userId) {
if( notifications == null || notifications.size() == 0)
return;
Bitmap icon_notification = null;
@ -132,7 +136,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
//boolean notif_ask = sharedpreferences.getBoolean(Helper.SET_NOTIF_ASK, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + acct, null);
String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
//No previous notifications in cache, so no notification will be sent
if( max_id != null ){
int newFollows = 0;
@ -213,11 +217,15 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
message = getContext().getResources().getQuantityString(R.plurals.other_notifications, other, other);
else
message = "";
notify_user(getContext(), NOTIFICATION_INTENT, notificationId, icon_notification,title,message);
final Intent intent = new Intent(getContext(), 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, userId);
notify_user(getContext(), intent, notificationId, icon_notification,title,message);
}
}
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + acct, notifications.get(0).getId());
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notifications.get(0).getId());
editor.apply();
}

View File

@ -76,7 +76,6 @@ public class AccountDAO {
db.insert(Sqlite.TABLE_USER_ACCOUNT, null, values);
}catch (Exception e) {
e.printStackTrace();
return false;
}
return true;