Search in sources :

Example 1 with FollowsChecker

use of com.thebluealliance.androidclient.gcm.FollowsChecker in project the-blue-alliance-android by the-blue-alliance.

the class ScoreNotification method buildNotification.

@Override
public Notification buildNotification(Context context, FollowsChecker followsChecker) {
    Resources r = context.getResources();
    matchKey = match.getKey();
    String matchTitle = MatchHelper.getMatchTitleFromMatchKey(context, matchKey);
    String matchAbbrevTitle = MatchHelper.getAbbrevMatchTitleFromMatchKey(context, matchKey);
    IMatchAlliancesContainer alliances = match.getAlliances();
    int redScore = Match.getRedScore(alliances);
    int blueScore = Match.getBlueScore(alliances);
    // Boldify the team numbers that the user is following, but only if the system supports
    // java 8 language features
    CharSequence firstTeams;
    CharSequence secondTeams;
    ArrayList<String> redTeams = Match.teamNumbers(Match.getRedTeams(alliances));
    ArrayList<String> blueTeams = Match.teamNumbers(Match.getBlueTeams(alliances));
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Predicate<String> isFollowing = teamNumber -> followsChecker.followsTeam(context, teamNumber, matchKey, NotificationTypes.MATCH_SCORE);
        firstTeams = Utilities.boldNameList(redTeams, isFollowing);
        secondTeams = Utilities.boldNameList(blueTeams, isFollowing);
    } else {
        firstTeams = Utilities.stringifyListOfStrings(context, redTeams);
        secondTeams = Utilities.stringifyListOfStrings(context, blueTeams);
    }
    // Make sure the score string is formatted properly with the winning score first
    String scoreString;
    if (blueScore > redScore) {
        scoreString = blueScore + "-" + redScore;
        CharSequence temp = firstTeams;
        firstTeams = secondTeams;
        secondTeams = temp;
    } else {
        scoreString = redScore + "-" + blueScore;
    }
    MatchType matchType = MatchType.fromShortType(match.getCompLevel());
    boolean useSpecial2015Format = match.getYear() == 2015 && matchType != MatchType.FINAL;
    String eventShortName = EventHelper.shortName(eventName);
    String template;
    if (useSpecial2015Format) {
        // firstTeams played secondTeams (for 2015 non-finals matches)
        template = context.getString(R.string.notification_score_teams_played_teams);
    } else if (blueScore == redScore) {
        // firstTeams tied secondTeams
        template = context.getString(R.string.notification_score_teams_tied_teams);
    } else {
        // firstTeams beat secondTeams
        template = context.getString(R.string.notification_score_teams_beat_teams);
    }
    CharSequence notificationBody = TextUtils.expandTemplate(template, eventShortName, matchTitle, firstTeams, secondTeams, scoreString);
    // We can finally build the notification!
    Intent instance = getIntent(context);
    stored = new StoredNotification();
    stored.setType(getNotificationType());
    String eventCode = EventHelper.getEventCode(matchKey);
    String notificationTitle = r.getString(R.string.notification_score_title, eventCode, matchAbbrevTitle);
    stored.setTitle(notificationTitle);
    stored.setBody(notificationBody.toString());
    stored.setIntent(MyTBAHelper.serializeIntent(instance));
    stored.setTime(Calendar.getInstance().getTime());
    stored.setMessageData(messageData);
    stored.setSystemId(getNotificationId());
    NotificationCompat.Builder builder = getBaseBuilder(context, instance).setContentTitle(notificationTitle).setContentText(notificationBody);
    NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(notificationBody);
    builder.setStyle(style);
    return builder.build();
}
Also used : JsonParseException(com.google.gson.JsonParseException) Context(android.content.Context) JsonObject(com.google.gson.JsonObject) MatchWriter(com.thebluealliance.androidclient.database.writers.MatchWriter) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) NotificationCompat(androidx.core.app.NotificationCompat) Date(java.util.Date) Intent(android.content.Intent) MatchHelper(com.thebluealliance.androidclient.helpers.MatchHelper) ArrayList(java.util.ArrayList) Calendar(java.util.Calendar) Gson(com.google.gson.Gson) MatchType(com.thebluealliance.androidclient.types.MatchType) View(android.view.View) Build(android.os.Build) R(com.thebluealliance.androidclient.R) EventHelper(com.thebluealliance.androidclient.helpers.EventHelper) JSONHelper(com.thebluealliance.androidclient.helpers.JSONHelper) ViewMatchActivity(com.thebluealliance.androidclient.activities.ViewMatchActivity) LayoutInflater(android.view.LayoutInflater) Match(com.thebluealliance.androidclient.models.Match) TextUtils(android.text.TextUtils) MatchRenderer(com.thebluealliance.androidclient.renderers.MatchRenderer) ScoreNotificationViewModel(com.thebluealliance.androidclient.viewmodels.ScoreNotificationViewModel) IMatchAlliancesContainer(com.thebluealliance.api.model.IMatchAlliancesContainer) MyTBAHelper(com.thebluealliance.androidclient.helpers.MyTBAHelper) MatchListElement(com.thebluealliance.androidclient.listitems.MatchListElement) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) Predicate(com.google.common.base.Predicate) Utilities(com.thebluealliance.androidclient.Utilities) FollowsChecker(com.thebluealliance.androidclient.gcm.FollowsChecker) GamedayTickerClickListener(com.thebluealliance.androidclient.listeners.GamedayTickerClickListener) Notification(android.app.Notification) Resources(android.content.res.Resources) MatchView(com.thebluealliance.androidclient.views.MatchView) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) Intent(android.content.Intent) MatchType(com.thebluealliance.androidclient.types.MatchType) IMatchAlliancesContainer(com.thebluealliance.api.model.IMatchAlliancesContainer) NotificationCompat(androidx.core.app.NotificationCompat) Resources(android.content.res.Resources)

Example 2 with FollowsChecker

use of com.thebluealliance.androidclient.gcm.FollowsChecker in project the-blue-alliance-android by the-blue-alliance.

the class UpcomingMatchNotification method buildNotification.

/**
 * @param context a Context object for use by the notification builder
 * @param followsChecker for checking which teams the user follows
 * @return A constructed notification
 */
@Override
public Notification buildNotification(Context context, FollowsChecker followsChecker) {
    String scheduledStartTimeString;
    if (JSONHelper.isNull(matchTime)) {
        scheduledStartTimeString = "";
    } else {
        long scheduledStartTimeUNIX = matchTime.getAsLong();
        // We multiply by 1000 because the Date constructor expects ms
        Date scheduledStartTime = new Date(scheduledStartTimeUNIX * 1000);
        java.text.DateFormat format = android.text.format.DateFormat.getTimeFormat(context);
        scheduledStartTimeString = format.format(scheduledStartTime);
    }
    // Boldify the team numbers that the user is following, but only if the system supports
    // java 8 language features
    CharSequence redTeamNumbers;
    CharSequence blueTeamNumbers;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Predicate<String> isFollowing = teamNumber -> followsChecker.followsTeam(context, teamNumber, matchKey, NotificationTypes.UPCOMING_MATCH);
        redTeamNumbers = Utilities.boldNameList(Arrays.asList(redTeams), isFollowing);
        blueTeamNumbers = Utilities.boldNameList(Arrays.asList(blueTeams), isFollowing);
    } else {
        redTeamNumbers = Utilities.stringifyListOfStrings(context, Arrays.asList(redTeams));
        blueTeamNumbers = Utilities.stringifyListOfStrings(context, Arrays.asList(blueTeams));
    }
    String matchTitle = MatchHelper.getMatchTitleFromMatchKey(context, matchKey);
    String matchAbbrevTitle = MatchHelper.getAbbrevMatchTitleFromMatchKey(context, matchKey);
    String eventShortName = EventHelper.shortName(eventName);
    String template = scheduledStartTimeString.isEmpty() ? context.getString(R.string.notification_upcoming_match_no_time) : context.getString(R.string.notification_upcoming_match);
    CharSequence contentText = TextUtils.expandTemplate(template, eventShortName, matchTitle, redTeamNumbers, blueTeamNumbers, scheduledStartTimeString);
    Intent instance = getIntent(context);
    stored = new StoredNotification();
    stored.setType(getNotificationType());
    String eventCode = EventHelper.getEventCode(matchKey);
    String notificationTitle = context.getString(R.string.notification_upcoming_match_title, eventCode, matchAbbrevTitle);
    stored.setTitle(notificationTitle);
    stored.setBody(contentText.toString());
    stored.setMessageData(messageData);
    stored.setIntent(MyTBAHelper.serializeIntent(instance));
    stored.setTime(Calendar.getInstance().getTime());
    stored.setSystemId(getNotificationId());
    PendingIntent watchIntent = null;
    String watchTitle = null;
    if (webcast != null && webcast.isJsonObject()) {
        JsonObject webcastJson = webcast.getAsJsonObject();
        WebcastType webcastType = WebcastHelper.getType(webcastJson.get("type").getAsString());
        if (webcastType != WebcastType.NONE) {
            watchTitle = webcastType.render(context);
            Intent webcastIntent = WebcastHelper.getIntentForWebcast(context, matchKey, webcastType, webcastJson, 0);
            watchIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), webcastIntent, 0);
        }
    }
    NotificationCompat.Builder builder = getBaseBuilder(context, instance).setContentTitle(notificationTitle).setContentText(contentText);
    // Add Watch button
    if (watchIntent != null) {
        builder.addAction(R.drawable.ic_videocam_black_24dp, watchTitle, watchIntent);
    }
    NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(contentText);
    builder.setStyle(style);
    return builder.build();
}
Also used : JsonParseException(com.google.gson.JsonParseException) Context(android.content.Context) JsonObject(com.google.gson.JsonObject) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) WebcastHelper(com.thebluealliance.androidclient.helpers.WebcastHelper) Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) NotificationCompat(androidx.core.app.NotificationCompat) WebcastType(com.thebluealliance.androidclient.types.WebcastType) Date(java.util.Date) Intent(android.content.Intent) MatchHelper(com.thebluealliance.androidclient.helpers.MatchHelper) PendingIntent(android.app.PendingIntent) JsonParser(com.google.gson.JsonParser) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) Calendar(java.util.Calendar) Gson(com.google.gson.Gson) View(android.view.View) Build(android.os.Build) R(com.thebluealliance.androidclient.R) EventHelper(com.thebluealliance.androidclient.helpers.EventHelper) JSONHelper(com.thebluealliance.androidclient.helpers.JSONHelper) UpcomingMatchNotificationViewModel(com.thebluealliance.androidclient.viewmodels.UpcomingMatchNotificationViewModel) ViewMatchActivity(com.thebluealliance.androidclient.activities.ViewMatchActivity) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) MyTBAHelper(com.thebluealliance.androidclient.helpers.MyTBAHelper) MatchListElement(com.thebluealliance.androidclient.listitems.MatchListElement) JsonArray(com.google.gson.JsonArray) List(java.util.List) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) Predicate(com.google.common.base.Predicate) Utilities(com.thebluealliance.androidclient.Utilities) FollowsChecker(com.thebluealliance.androidclient.gcm.FollowsChecker) GamedayTickerClickListener(com.thebluealliance.androidclient.listeners.GamedayTickerClickListener) JsonNull(com.google.gson.JsonNull) Notification(android.app.Notification) MatchView(com.thebluealliance.androidclient.views.MatchView) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) JsonObject(com.google.gson.JsonObject) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Date(java.util.Date) WebcastType(com.thebluealliance.androidclient.types.WebcastType) NotificationCompat(androidx.core.app.NotificationCompat) Context(android.content.Context) PendingIntent(android.app.PendingIntent)

Example 3 with FollowsChecker

use of com.thebluealliance.androidclient.gcm.FollowsChecker in project the-blue-alliance-android by the-blue-alliance.

the class TeamMatchVideoNotification method buildNotification.

@Override
public Notification buildNotification(Context context, FollowsChecker followsChecker) {
    Resources r = context.getResources();
    ArrayList<String> teamNumbers = Match.teamNumbers(mMatchTeamKeys);
    CharSequence teamNumberString;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Predicate<String> isFollowing = teamNumber -> followsChecker.followsTeam(context, teamNumber, mMatchKey, NotificationTypes.MATCH_VIDEO);
        teamNumberString = Utilities.boldNameList(teamNumbers, isFollowing);
    } else {
        teamNumberString = Utilities.stringifyListOfStrings(context, teamNumbers);
    }
    String matchTitle = MatchHelper.getAbbrevMatchTitleFromMatchKey(context, mMatchKey);
    String eventCode = EventHelper.getEventCode(mMatchKey);
    String title = r.getString(R.string.notification_team_match_video, eventCode, matchTitle);
    String notificationBody = r.getString(R.string.notification_team_match_video_content, EventHelper.shortName(mEventName), teamNumberString);
    // We can finally build the notification!
    Intent instance = getIntent(context);
    stored = new StoredNotification();
    stored.setType(getNotificationType());
    stored.setTitle(title);
    stored.setBody(notificationBody);
    stored.setIntent(MyTBAHelper.serializeIntent(instance));
    stored.setTime(Calendar.getInstance().getTime());
    stored.setMessageData(messageData);
    stored.setSystemId(getNotificationId());
    NotificationCompat.Builder builder = getBaseBuilder(context, instance).setContentTitle(title).setContentText(notificationBody);
    NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(notificationBody);
    builder.setStyle(style);
    return builder.build();
}
Also used : JsonParseException(com.google.gson.JsonParseException) Context(android.content.Context) JsonObject(com.google.gson.JsonObject) MatchWriter(com.thebluealliance.androidclient.database.writers.MatchWriter) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) NotificationCompat(androidx.core.app.NotificationCompat) Date(java.util.Date) Intent(android.content.Intent) MatchHelper(com.thebluealliance.androidclient.helpers.MatchHelper) ArrayList(java.util.ArrayList) Calendar(java.util.Calendar) Gson(com.google.gson.Gson) Build(android.os.Build) R(com.thebluealliance.androidclient.R) EventHelper(com.thebluealliance.androidclient.helpers.EventHelper) ViewMatchActivity(com.thebluealliance.androidclient.activities.ViewMatchActivity) Match(com.thebluealliance.androidclient.models.Match) MyTBAHelper(com.thebluealliance.androidclient.helpers.MyTBAHelper) List(java.util.List) Nullable(androidx.annotation.Nullable) Predicate(com.google.common.base.Predicate) Utilities(com.thebluealliance.androidclient.Utilities) FollowsChecker(com.thebluealliance.androidclient.gcm.FollowsChecker) Notification(android.app.Notification) TeamMatchVideoNotificationViewModel(com.thebluealliance.androidclient.viewmodels.TeamMatchVideoNotificationViewModel) Resources(android.content.res.Resources) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) NotificationCompat(androidx.core.app.NotificationCompat) Intent(android.content.Intent) Resources(android.content.res.Resources)

Aggregations

Notification (android.app.Notification)3 Context (android.content.Context)3 Intent (android.content.Intent)3 Build (android.os.Build)3 Nullable (androidx.annotation.Nullable)3 NotificationCompat (androidx.core.app.NotificationCompat)3 Predicate (com.google.common.base.Predicate)3 Gson (com.google.gson.Gson)3 JsonObject (com.google.gson.JsonObject)3 JsonParseException (com.google.gson.JsonParseException)3 R (com.thebluealliance.androidclient.R)3 Utilities (com.thebluealliance.androidclient.Utilities)3 ViewMatchActivity (com.thebluealliance.androidclient.activities.ViewMatchActivity)3 FollowsChecker (com.thebluealliance.androidclient.gcm.FollowsChecker)3 EventHelper (com.thebluealliance.androidclient.helpers.EventHelper)3 MatchHelper (com.thebluealliance.androidclient.helpers.MatchHelper)3 MyTBAHelper (com.thebluealliance.androidclient.helpers.MyTBAHelper)3 StoredNotification (com.thebluealliance.androidclient.models.StoredNotification)3 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3