Search in sources :

Example 1 with FeedbackHelper

use of com.google.samples.apps.iosched.feedback.FeedbackHelper in project iosched by google.

the class SessionAlarmService method notifySessionFeedback.

/**
     *  A starred session is about to end. Notify the user to provide session feedback.
     *  Constructs and triggers a system notification. Does nothing if the session has already
     *  concluded.
     */
private void notifySessionFeedback(boolean debug) {
    LOGD(TAG, "Considering firing notification for session feedback.");
    if (debug) {
        LOGW(TAG, "Note: this is a debug notification.");
    }
    // Don't fire notification if this feature is disabled in settings
    if (!SettingsUtils.shouldShowSessionFeedbackReminders(this)) {
        LOGD(TAG, "Skipping session feedback notification. Disabled in settings.");
        return;
    }
    Cursor c = null;
    try {
        c = getContentResolver().query(ScheduleContract.Sessions.CONTENT_MY_SCHEDULE_URI, SessionsNeedingFeedbackQuery.PROJECTION, SessionsNeedingFeedbackQuery.WHERE_CLAUSE, null, null);
        if (c == null) {
            return;
        }
        FeedbackHelper feedbackHelper = new FeedbackHelper(this);
        List<String> needFeedbackIds = new ArrayList<String>();
        List<String> needFeedbackTitles = new ArrayList<String>();
        while (c.moveToNext()) {
            String sessionId = c.getString(SessionsNeedingFeedbackQuery.SESSION_ID);
            String sessionTitle = c.getString(SessionsNeedingFeedbackQuery.SESSION_TITLE);
            // Avoid repeated notifications.
            if (feedbackHelper.isFeedbackNotificationFiredForSession(sessionId)) {
                LOGD(TAG, "Skipping repeated session feedback notification for session '" + sessionTitle + "'");
                continue;
            }
            needFeedbackIds.add(sessionId);
            needFeedbackTitles.add(sessionTitle);
        }
        if (needFeedbackIds.size() == 0) {
            // the user has already been notified of all sessions needing feedback
            return;
        }
        LOGD(TAG, "Going forward with session feedback notification for " + needFeedbackIds.size() + " session(s).");
        final Resources res = getResources();
        Intent dismissalIntent = new Intent(ACTION_NOTIFICATION_DISMISSAL);
        PendingIntent dismissalPendingIntent = PendingIntent.getService(this, (int) new Date().getTime(), dismissalIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        String provideFeedbackTicker = res.getString(R.string.session_feedback_notification_ticker);
        NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
        //noinspection deprecation Ignore getColor deprecation until minSdk = 16
        notifBuilder.setColor(getResources().getColor(R.color.theme_primary));
        notifBuilder.setContentText(provideFeedbackTicker).setTicker(provideFeedbackTicker).setSmallIcon(R.drawable.ic_stat_notification).setPriority(Notification.PRIORITY_LOW).setLocalOnly(// make it local to the phone
        true).setDeleteIntent(dismissalPendingIntent).setAutoCancel(true);
        if (needFeedbackIds.size() == 1) {
            // Only 1 session needs feedback
            Uri sessionUri = ScheduleContract.Sessions.buildSessionUri(needFeedbackIds.get(0));
            PendingIntent pi = TaskStackBuilder.create(this).addNextIntent(new Intent(this, MyScheduleActivity.class)).addNextIntent(new Intent(Intent.ACTION_VIEW, sessionUri, this, SessionFeedbackActivity.class)).getPendingIntent(1, PendingIntent.FLAG_CANCEL_CURRENT);
            notifBuilder.setContentTitle(needFeedbackTitles.get(0)).setContentIntent(pi);
        } else {
            // Show information about several sessions that need feedback
            PendingIntent pi = TaskStackBuilder.create(this).addNextIntent(new Intent(this, MyScheduleActivity.class)).getPendingIntent(1, PendingIntent.FLAG_CANCEL_CURRENT);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(provideFeedbackTicker);
            for (String title : needFeedbackTitles) {
                inboxStyle.addLine(title);
            }
            notifBuilder.setContentTitle(getResources().getQuantityString(R.plurals.session_plurals, needFeedbackIds.size(), needFeedbackIds.size())).setStyle(inboxStyle).setContentIntent(pi);
        }
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        LOGD(TAG, "Now showing session feedback notification!");
        nm.notify(FEEDBACK_NOTIFICATION_ID, notifBuilder.build());
        for (int i = 0; i < needFeedbackIds.size(); i++) {
            feedbackHelper.setFeedbackNotificationAsFiredForSession(needFeedbackIds.get(i));
        }
    } finally {
        if (c != null) {
            try {
                c.close();
            } catch (Exception ignored) {
            }
        }
    }
}
Also used : NotificationManager(android.app.NotificationManager) TaskStackBuilder(android.support.v4.app.TaskStackBuilder) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor) Uri(android.net.Uri) Date(java.util.Date) FeedbackHelper(com.google.samples.apps.iosched.feedback.FeedbackHelper) MyScheduleActivity(com.google.samples.apps.iosched.myschedule.MyScheduleActivity) NotificationCompat(android.support.v4.app.NotificationCompat) Resources(android.content.res.Resources) PendingIntent(android.app.PendingIntent)

Aggregations

NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 TaskStackBuilder (android.support.v4.app.TaskStackBuilder)1 FeedbackHelper (com.google.samples.apps.iosched.feedback.FeedbackHelper)1 MyScheduleActivity (com.google.samples.apps.iosched.myschedule.MyScheduleActivity)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1