Browse Source

Init board notifications

pull/61/merge
Ezerous 6 years ago
parent
commit
1889a2f0cd
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 8
      app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java
  2. 44
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BoardBookmarksFragment.java
  3. 12
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarkActivity.java
  4. 25
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/TopicBookmarksFragment.java
  5. 37
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  6. 37
      app/src/main/java/gr/thmmy/mthmmy/model/PostNotification.java
  7. 48
      app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java
  8. 12
      app/src/main/res/layout/fragment_bookmarks_board_row.xml
  9. 1
      app/src/main/res/values/strings.xml

8
app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java

@ -95,7 +95,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
}
thisPageBookmark = new Bookmark(boardTitle, ThmmyPage.getBoardId(boardUrl), false);
setBoardBookmark((ImageButton) findViewById(R.id.bookmark));
setBoardBookmark(findViewById(R.id.bookmark));
createDrawer();
progressBar = findViewById(R.id.progressBar);
@ -167,7 +167,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
@Override
public void onResume() {
super.onResume();
refreshBoardBookmark((ImageButton) findViewById(R.id.bookmark));
refreshBoardBookmark(findViewById(R.id.bookmark));
}
@Override
@ -318,11 +318,9 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
|| !Objects.equals(boardTitle, parsedTitle)) {
boardTitle = parsedTitle;
toolbar.setTitle(boardTitle);
thisPageBookmark = new Bookmark(boardTitle, ThmmyPage.getBoardId(boardUrl), false);
thisPageBookmark = new Bookmark(boardTitle, "b" + ThmmyPage.getBoardId(boardUrl), true);
}
Timber.d("topix " + tempTopics);
parsedTopics.clear();
parsedSubBoards.clear();
parsedTopics.addAll(tempTopics);

44
app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BoardBookmarksFragment.java

@ -2,13 +2,16 @@ package gr.thmmy.mthmmy.activities.bookmarks;
import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -27,10 +30,14 @@ public class BoardBookmarksFragment extends Fragment {
protected static final String ARG_BOARD_BOOKMARKS = "BOARD_BOOKMARKS";
public static final String INTERACTION_CLICK_BOARD_BOOKMARK = "CLICK_BOARD_BOOKMARK";
public static final String INTERACTION_TOGGLE_BOARD_NOTIFICATION = "TOGGLE_BOARD_NOTIFICATION";
public static final String INTERACTION_REMOVE_BOARD_BOOKMARK= "REMOVE_BOARD_BOOKMARK";
ArrayList<Bookmark> boardBookmarks = null;
private static Drawable notificationsEnabledButtonImage;
private static Drawable notificationsDisabledButtonImage;
// Required empty public constructor
public BoardBookmarksFragment() {
}
@ -59,6 +66,16 @@ public class BoardBookmarksFragment extends Fragment {
boardBookmarks = Bookmark.arrayFromString(bundledBoardBookmarks);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notificationsEnabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_on, null);
else
notificationsEnabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_on, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notificationsDisabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_off, null);
else
notificationsDisabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_off, null);
}
@Override
@ -74,19 +91,31 @@ public class BoardBookmarksFragment extends Fragment {
if (bookmarkedBoard != null && bookmarkedBoard.getTitle() != null) {
final LinearLayout row = (LinearLayout) layoutInflater.inflate(
R.layout.fragment_bookmarks_board_row, bookmarksLinearView, false);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
row.setOnClickListener(view -> {
Activity activity = getActivity();
if (activity instanceof BookmarkActivity){
((BookmarkActivity) activity).onBoardInteractionListener(INTERACTION_CLICK_BOARD_BOOKMARK, bookmarkedBoard);
}
}
});
((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedBoard.getTitle());
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ImageButton notificationsEnabledButton = row.findViewById(R.id.toggle_notification);
if (!bookmarkedBoard.isNotificationsEnabled()) {
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage);
}
notificationsEnabledButton.setOnClickListener(view -> {
Activity activity = getActivity();
if (activity instanceof BookmarkActivity) {
if (((BookmarkActivity) activity).onBoardInteractionListener(INTERACTION_TOGGLE_BOARD_NOTIFICATION, bookmarkedBoard)) {
notificationsEnabledButton.setImageDrawable(notificationsEnabledButtonImage);
} else {
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage);
}
}
});
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(view -> {
Activity activity = getActivity();
if (activity instanceof BookmarkActivity){
((BookmarkActivity) activity).onBoardInteractionListener(INTERACTION_REMOVE_BOARD_BOOKMARK, bookmarkedBoard);
@ -97,7 +126,6 @@ public class BoardBookmarksFragment extends Fragment {
if (boardBookmarks.isEmpty()){
bookmarksLinearView.addView(bookmarksListEmptyMessage());
}
}
});
bookmarksLinearView.addView(row);
}

12
app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarkActivity.java

@ -84,8 +84,9 @@ public class BookmarkActivity extends BaseActivity {
return true;
}
public void onBoardInteractionListener(String interactionType, Bookmark bookmarkedBoard) {
if (interactionType.equals(BoardBookmarksFragment.INTERACTION_CLICK_BOARD_BOOKMARK)) {
public boolean onBoardInteractionListener(String interactionType, Bookmark bookmarkedBoard) {
switch (interactionType) {
case BoardBookmarksFragment.INTERACTION_CLICK_BOARD_BOOKMARK:
Intent intent = new Intent(BookmarkActivity.this, BoardActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_BOARD_URL, "https://www.thmmy.gr/smf/index.php?board="
@ -94,10 +95,15 @@ public class BookmarkActivity extends BaseActivity {
intent.putExtras(extras);
startActivity(intent);
finish();
} else if (interactionType.equals(BoardBookmarksFragment.INTERACTION_REMOVE_BOARD_BOOKMARK)) {
break;
case BoardBookmarksFragment.INTERACTION_TOGGLE_BOARD_NOTIFICATION:
return toggleNotification(bookmarkedBoard);
case BoardBookmarksFragment.INTERACTION_REMOVE_BOARD_BOOKMARK:
removeBookmark(bookmarkedBoard);
Toast.makeText(BookmarkActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
/**

25
app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/TopicBookmarksFragment.java

@ -62,18 +62,16 @@ public class TopicBookmarksFragment extends Fragment {
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notificationsEnabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_on, null);
} else {
else
notificationsEnabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_on, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notificationsDisabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_off, null);
} else {
else
notificationsDisabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_off, null);
}
}
@Override
public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container,
@ -88,14 +86,11 @@ public class TopicBookmarksFragment extends Fragment {
if (bookmarkedTopic != null && bookmarkedTopic.getTitle() != null) {
final LinearLayout row = (LinearLayout) layoutInflater.inflate(
R.layout.fragment_bookmarks_topic_row, bookmarksLinearView, false);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
row.setOnClickListener(view -> {
Activity activity = getActivity();
if (activity instanceof BookmarkActivity) {
((BookmarkActivity) activity).onTopicInteractionListener(INTERACTION_CLICK_TOPIC_BOOKMARK, bookmarkedTopic);
}
}
});
((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedTopic.getTitle());
@ -104,9 +99,7 @@ public class TopicBookmarksFragment extends Fragment {
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage);
}
notificationsEnabledButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notificationsEnabledButton.setOnClickListener(view -> {
Activity activity = getActivity();
if (activity instanceof BookmarkActivity) {
if (((BookmarkActivity) activity).onTopicInteractionListener(INTERACTION_TOGGLE_TOPIC_NOTIFICATION, bookmarkedTopic)) {
@ -115,11 +108,8 @@ public class TopicBookmarksFragment extends Fragment {
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage);
}
}
}
});
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(view -> {
Activity activity = getActivity();
if (activity instanceof BookmarkActivity) {
((BookmarkActivity) activity).onTopicInteractionListener(INTERACTION_REMOVE_TOPIC_BOOKMARK, bookmarkedTopic);
@ -130,7 +120,6 @@ public class TopicBookmarksFragment extends Fragment {
if (topicBookmarks.isEmpty()){
bookmarksLinearView.addView(bookmarksListEmptyMessage());
}
}
});
bookmarksLinearView.addView(row);
}

37
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -78,7 +78,7 @@ import static gr.thmmy.mthmmy.utils.FileUtils.getMimeType;
import static gr.thmmy.mthmmy.utils.LaunchType.LAUNCH_TYPE.FIRST_LAUNCH_EVER;
import static gr.thmmy.mthmmy.utils.LaunchType.LAUNCH_TYPE.INDETERMINATE;
public abstract class BaseActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener{
public abstract class BaseActivity extends AppCompatActivity {
// Client & Cookies
protected static OkHttpClient client;
@ -117,9 +117,7 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
}
BaseViewModel baseViewModel = ViewModelProviders.of(this).get(BaseViewModel.class);
baseViewModel.getCurrentPageBookmark().observe(this, thisPageBookmark -> {
setTopicBookmark(thisPageBookmarkMenuButton);
});
baseViewModel.getCurrentPageBookmark().observe(this, thisPageBookmark -> setTopicBookmark(thisPageBookmarkMenuButton));
}
@Override
@ -577,7 +575,11 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
if (boardsBookmarked == null) return;
if (bookmark.matchExists(boardsBookmarked)) {
boardsBookmarked.remove(bookmark.findIndex(boardsBookmarked));
} else boardsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId(), false));
FirebaseMessaging.getInstance().unsubscribeFromTopic(bookmark.getId());
} else {
boardsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId(), false));
FirebaseMessaging.getInstance().subscribeToTopic(bookmark.getId());
}
updateBoardBookmarks();
}
@ -617,13 +619,22 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
topicsBookmarked.get(bookmark.findIndex(topicsBookmarked)).toggleNotificationsEnabled();
updateTopicBookmarks();
if (topicsBookmarked.get(bookmark.findIndex(topicsBookmarked)).isNotificationsEnabled()) {
if (topicsBookmarked.get(bookmark.findIndex(topicsBookmarked)).isNotificationsEnabled())
FirebaseMessaging.getInstance().subscribeToTopic(bookmark.getId());
} else {
else
FirebaseMessaging.getInstance().unsubscribeFromTopic(bookmark.getId());
}
return topicsBookmarked.get(bookmark.findIndex(topicsBookmarked)).isNotificationsEnabled();
} else if (bookmark.matchExists(boardsBookmarked)) {
boardsBookmarked.get(bookmark.findIndex(boardsBookmarked)).toggleNotificationsEnabled();
updateBoardBookmarks();
if (boardsBookmarked.get(bookmark.findIndex(boardsBookmarked)).isNotificationsEnabled())
FirebaseMessaging.getInstance().subscribeToTopic(bookmark.getId());
else
FirebaseMessaging.getInstance().unsubscribeFromTopic(bookmark.getId());
return boardsBookmarked.get(bookmark.findIndex(boardsBookmarked)).isNotificationsEnabled();
}
return false;
}
@ -727,14 +738,6 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
}
//----------------------------PRIVACY POLICY------------------
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getString(R.string.pref_pp_accepted_key)))
if(!sharedPreferences.getBoolean(key, false))
showUserConsentDialog();
}
private void showUserConsentDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("User Agreement");
@ -785,8 +788,6 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
}
}
//----------------------------------MISC----------------------
protected void setMainActivity(MainActivity mainActivity) {
this.mainActivity = mainActivity;

37
app/src/main/java/gr/thmmy/mthmmy/model/PostNotification.java

@ -8,10 +8,12 @@ package gr.thmmy.mthmmy.model;
* </p>.
*/
public class PostNotification {
final int postId;
final int topicId;
final String topicTitle;
final String poster;
private final int postId;
private final int topicId;
private final String topicTitle;
private final String poster;
private final int boardId;
private final String boardTitle;
// Suppresses default constructor
@SuppressWarnings("unused")
@ -20,6 +22,8 @@ public class PostNotification {
this.topicId = -1;
this.topicTitle = null;
this.poster = null;
this.boardId = -1;
this.boardTitle = null;
}
/**
@ -30,12 +34,17 @@ public class PostNotification {
* @param topicId this post's topicId
* @param topicTitle this post's topicTitle
* @param poster username of this post's author
* @param boardId one of this post's boardIds (-1 if it is a topic notification)
* @param boardTitle one of this post's boardTitles (null if it is a topic notification)
*/
public PostNotification(int postId, int topicId, String topicTitle, String poster) {
public PostNotification(int postId, int topicId, String topicTitle, String poster, int boardId, String boardTitle) {
this.postId = postId;
this.topicId = topicId;
this.topicTitle = topicTitle;
this.poster = poster;
this.boardId = boardId;
this.boardTitle = boardTitle;
}
/**
@ -73,6 +82,24 @@ public class PostNotification {
public String getPoster() {
return poster;
}
/**
* Gets this post's boardId.
*
* @return this post's boardId
*/
public int getBoardId() {
return boardId;
}
/**
* Gets this post's boardTitle.
*
* @return this post's boardTitle
*/
public String getBoardTitle() {
return boardTitle;
}
}

48
app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java

@ -54,9 +54,19 @@ public class NotificationService extends FirebaseMessagingService {
int postId = Integer.parseInt(json.getString("postId"));
String topicTitle = json.getString("topicTitle");
String poster = json.getString("poster");
sendNotification(new PostNotification(postId, topicId, topicTitle, poster));
int boardId = -1;
String boardTitle = null;
if(remoteMessage.getFrom().contains("b")){
Timber.i("FCM BOARD type message detected.");
boardId = Integer.parseInt(json.getString("boardId"));
boardTitle = json.getString("boardTitle");
}
else
Timber.i("FCM TOPIC type message detected.");
sendNotification(new PostNotification(postId, topicId, topicTitle, poster, boardId, boardTitle));
} else
Timber.v("Notification suppressed (own userID).");
Timber.i("Notification suppressed (own userID).");
} catch (JSONException e) {
Timber.e(e, "JSON Exception");
}
@ -78,6 +88,8 @@ public class NotificationService extends FirebaseMessagingService {
private void sendNotification(PostNotification postNotification) {
Timber.i("Creating a notification...");
boolean isTopicNotification = postNotification.getBoardId() == -1;
//Reads notifications preferences
SharedPreferences settingsFile = getSharedPreferences(SETTINGS_SHARED_PREFS, Context.MODE_PRIVATE);
String notificationsSound = settingsFile.getString(SELECTED_RINGTONE, null);
@ -118,15 +130,30 @@ public class NotificationService extends FirebaseMessagingService {
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode++, intent,
PendingIntent.FLAG_ONE_SHOT);
final int topicId = postNotification.getTopicId();
String contentText = "New post by " + postNotification.getPoster();
int notificationId;
String contentText;
if(isTopicNotification){
notificationId = postNotification.getTopicId();
contentText = "New post by " + postNotification.getPoster();
}
else{
// Using Cantor pairing function (plus the minus sign) for id uniqueness
int k1 = postNotification.getTopicId();
int k2 = postNotification.getBoardId();
notificationId = -(((k1+k2)*(k1+k2+1))/2+k2);
contentText = "New post in " + postNotification.getTopicTitle();
}
int newPostsCount = 1;
if (buildVersion >= Build.VERSION_CODES.M) {
Notification existingNotification = getActiveNotification(topicId);
Notification existingNotification = getActiveNotification(notificationId);
if (existingNotification != null) {
newPostsCount = existingNotification.extras.getInt(NEW_POSTS_COUNT) + 1;
if(isTopicNotification)
contentText = newPostsCount + " new posts";
else
contentText = newPostsCount + " new posts in " + postNotification.getTopicTitle();
}
}
@ -136,13 +163,18 @@ public class NotificationService extends FirebaseMessagingService {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(postNotification.getTopicTitle())
.setContentText(contentText)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setGroup(GROUP_KEY)
.addExtras(notificationExtras);
if(isTopicNotification)
notificationBuilder.setContentTitle(postNotification.getTopicTitle());
else
notificationBuilder.setContentTitle(postNotification.getBoardTitle());
//Applies user's notifications preferences
if (notificationDefaultValues != -1) {
notificationBuilder.setDefaults(notificationDefaultValues);
@ -162,7 +194,7 @@ public class NotificationService extends FirebaseMessagingService {
boolean createSummaryNotification = false;
if (buildVersion >= Build.VERSION_CODES.M)
createSummaryNotification = otherNotificationsExist(topicId);
createSummaryNotification = otherNotificationsExist(notificationId);
NotificationCompat.Builder summaryNotificationBuilder = null;
@ -185,7 +217,7 @@ public class NotificationService extends FirebaseMessagingService {
if (buildVersion >= Build.VERSION_CODES.O)
notificationManager.createNotificationChannel(new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH));
notificationManager.notify(NEW_POST_TAG, topicId, notificationBuilder.build());
notificationManager.notify(NEW_POST_TAG, notificationId, notificationBuilder.build());
if (createSummaryNotification)
notificationManager.notify(SUMMARY_TAG, 0, summaryNotificationBuilder.build());

12
app/src/main/res/layout/fragment_bookmarks_board_row.xml

@ -23,6 +23,18 @@
android:textColor="@color/primary_text"
android:textSize="18sp"/>
<ImageButton
android:id="@+id/toggle_notification"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:paddingStart="6dp"
android:paddingEnd="6dp"
android:background="@android:color/transparent"
android:contentDescription="@string/toggle_notification"
app:srcCompat="@drawable/ic_notification_on"/>
<ImageButton
android:id="@+id/remove_bookmark"
android:layout_width="wrap_content"

1
app/src/main/res/values/strings.xml

@ -14,7 +14,6 @@
<string name="info">Info</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="pref_pp_accepted_key">pref_pp_accepted</string>
<string name="user_agreement_dialog_text">"To use mTHMMY you have to agree to our Privacy Policy by choosing one of the buttons below. Choose \"Yes, I want to help\", if you consent to the collection of anonymized data that will help us improve the app. Otherwise, choose \"Nope, leave me alone\". You can change your preferences any time through the app's Settings.</string>
<!--Login Activity-->

Loading…
Cancel
Save