Search in sources :

Example 1 with RecordOperationAppBroadcastReceiver

use of org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver in project weiciyuan by qii.

the class JBMentionsCommentNotificationServiceHelper method buildNotification.

private void buildNotification() {
    // int count = (data.getSize() >= Integer.valueOf(SettingUtility.getMsgCount()) ? unreadBean
    // .getMention_cmt() : data.getSize());
    int count = Math.min(unreadBean.getMention_cmt(), data.getSize());
    if (count == 0) {
        return;
    }
    Notification.Builder builder = new Notification.Builder(getBaseContext()).setTicker(ticker).setContentText(accountBean.getUsernick()).setSmallIcon(R.drawable.ic_notification).setAutoCancel(true).setContentIntent(getPendingIntent()).setOnlyAlertOnce(true);
    builder.setContentTitle(String.format(GlobalContext.getInstance().getString(R.string.new_mentions_comment), String.valueOf(count)));
    if (count > 1) {
        builder.setNumber(count);
    }
    if (clearNotificationEventReceiver.get(accountBean.getUid()) != null) {
        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver.get(accountBean.getUid()));
        JBMentionsCommentNotificationServiceHelper.clearNotificationEventReceiver.put(accountBean.getUid(), null);
    }
    RecordOperationAppBroadcastReceiver receiver = new RecordOperationAppBroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // new ClearUnreadDao(accountBean.getAccess_token())
                        // .clearMentionCommentUnread(unreadBean, accountBean.getUid());
                        // } catch (WeiboException ignored) {
                        ArrayList<String> ids = new ArrayList<String>();
                        for (CommentBean msg : data.getItemList()) {
                            ids.add(msg.getId());
                        }
                        NotificationDBTask.addUnreadNotification(accountBean.getUid(), ids, NotificationDBTask.UnreadDBType.mentionsComment);
                    } finally {
                        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver.get(accountBean.getUid()));
                        JBMentionsCommentNotificationServiceHelper.clearNotificationEventReceiver.put(accountBean.getUid(), null);
                    }
                }
            }).start();
        }
    };
    clearNotificationEventReceiver.put(accountBean.getUid(), receiver);
    IntentFilter intentFilter = new IntentFilter(RESET_UNREAD_MENTIONS_COMMENT_ACTION);
    GlobalContext.getInstance().registerReceiver(receiver, intentFilter);
    Intent broadcastIntent = new Intent(RESET_UNREAD_MENTIONS_COMMENT_ACTION);
    PendingIntent deletedPendingIntent = PendingIntent.getBroadcast(GlobalContext.getInstance(), 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setDeleteIntent(deletedPendingIntent);
    Intent intent = WriteReplyToCommentActivity.newIntentFromNotification(getApplicationContext(), accountBean, data.getItem(currentIndex));
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.reply_to_comment_light, getApplicationContext().getString(R.string.reply_to_comment), pendingIntent);
    if (count > 1) {
        Intent nextIntent = new Intent(JBMentionsCommentNotificationServiceHelper.this, JBMentionsCommentNotificationServiceHelper.class);
        nextIntent.putExtra(NotificationServiceHelper.ACCOUNT_ARG, accountBean);
        nextIntent.putExtra(NotificationServiceHelper.MENTIONS_COMMENT_ARG, data);
        nextIntent.putExtra(NotificationServiceHelper.UNREAD_ARG, unreadBean);
        nextIntent.putExtra(NotificationServiceHelper.PENDING_INTENT_INNER_ARG, clickToOpenAppPendingIntentInner);
        nextIntent.putExtra(NotificationServiceHelper.TICKER, ticker);
        String actionName;
        int nextIndex;
        int actionDrawable;
        if (currentIndex < count - 1) {
            nextIndex = currentIndex + 1;
            actionName = getString(R.string.next_message);
            actionDrawable = R.drawable.notification_action_next;
        } else {
            nextIndex = 0;
            actionName = getString(R.string.first_message);
            actionDrawable = R.drawable.notification_action_previous;
        }
        nextIntent.putExtra(NotificationServiceHelper.CURRENT_INDEX_ARG, nextIndex);
        PendingIntent retrySendIntent = PendingIntent.getService(JBMentionsCommentNotificationServiceHelper.this, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(actionDrawable, actionName, retrySendIntent);
    }
    Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle(builder);
    bigTextStyle.setBigContentTitle("@" + data.getItem(currentIndex).getUser().getScreen_name() + getString(R.string.comment_at_to_you));
    bigTextStyle.bigText(data.getItem(currentIndex).getText());
    String summaryText;
    if (count > 1) {
        summaryText = accountBean.getUsernick() + "(" + (currentIndex + 1) + "/" + count + ")";
    } else {
        summaryText = accountBean.getUsernick();
    }
    bigTextStyle.setSummaryText(summaryText);
    builder.setStyle(bigTextStyle);
    Utility.configVibrateLedRingTone(builder);
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(getMentionsCommentNotificationId(accountBean), builder.build());
}
Also used : Context(android.content.Context) GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) IntentFilter(android.content.IntentFilter) NotificationManager(android.app.NotificationManager) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RecordOperationAppBroadcastReceiver(org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver) CommentBean(org.qii.weiciyuan.bean.CommentBean) Notification(android.app.Notification) PendingIntent(android.app.PendingIntent)

Example 2 with RecordOperationAppBroadcastReceiver

use of org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver in project weiciyuan by qii.

the class JBMentionsWeiboNotificationServiceHelper method buildNotification.

private void buildNotification() {
    // int count = (data.getSize() >= Integer.valueOf(SettingUtility.getMsgCount()) ? unreadBean
    // .getMention_status() : data.getSize());
    int count = Math.min(unreadBean.getMention_status(), data.getSize());
    if (count == 0) {
        return;
    }
    Notification.Builder builder = new Notification.Builder(getBaseContext()).setTicker(ticker).setContentText(accountBean.getUsernick()).setSmallIcon(R.drawable.ic_notification).setAutoCancel(true).setContentIntent(getPendingIntent()).setOnlyAlertOnce(true);
    builder.setContentTitle(String.format(GlobalContext.getInstance().getString(R.string.new_mentions_weibo), String.valueOf(count)));
    if (count > 1) {
        builder.setNumber(count);
    }
    if (clearNotificationEventReceiver.get(accountBean.getUid()) != null) {
        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver.get(accountBean.getUid()));
        JBMentionsWeiboNotificationServiceHelper.clearNotificationEventReceiver.put(accountBean.getUid(), null);
    }
    RecordOperationAppBroadcastReceiver receiver = new RecordOperationAppBroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // new ClearUnreadDao(accountBean.getAccess_token())
                        // .clearMentionStatusUnread(unreadBean, accountBean.getUid());
                        ArrayList<String> ids = new ArrayList<String>();
                        for (MessageBean msg : data.getItemList()) {
                            ids.add(msg.getId());
                        }
                        NotificationDBTask.addUnreadNotification(accountBean.getUid(), ids, NotificationDBTask.UnreadDBType.mentionsWeibo);
                    // } catch (WeiboException ignored) {
                    } finally {
                        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver.get(accountBean.getUid()));
                        JBMentionsWeiboNotificationServiceHelper.clearNotificationEventReceiver.put(accountBean.getUid(), null);
                    }
                }
            }).start();
        }
    };
    clearNotificationEventReceiver.put(accountBean.getUid(), receiver);
    IntentFilter intentFilter = new IntentFilter(RESET_UNREAD_MENTIONS_WEIBO_ACTION);
    GlobalContext.getInstance().registerReceiver(clearNotificationEventReceiver.get(accountBean.getUid()), intentFilter);
    Intent broadcastIntent = new Intent(RESET_UNREAD_MENTIONS_WEIBO_ACTION);
    PendingIntent deletedPendingIntent = PendingIntent.getBroadcast(GlobalContext.getInstance(), 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setDeleteIntent(deletedPendingIntent);
    Intent intent = WriteCommentActivity.newIntentFromNotification(getApplicationContext(), accountBean, data.getItem(currentIndex));
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.comment_light, getApplicationContext().getString(R.string.comments), pendingIntent);
    if (count > 1) {
        Intent nextIntent = new Intent(JBMentionsWeiboNotificationServiceHelper.this, JBMentionsWeiboNotificationServiceHelper.class);
        nextIntent.putExtra(NotificationServiceHelper.ACCOUNT_ARG, accountBean);
        nextIntent.putExtra(NotificationServiceHelper.MENTIONS_WEIBO_ARG, data);
        nextIntent.putExtra(NotificationServiceHelper.UNREAD_ARG, unreadBean);
        nextIntent.putExtra(NotificationServiceHelper.PENDING_INTENT_INNER_ARG, clickToOpenAppPendingIntentInner);
        nextIntent.putExtra(NotificationServiceHelper.TICKER, ticker);
        String actionName;
        int nextIndex;
        int actionDrawable;
        if (currentIndex < count - 1) {
            nextIndex = currentIndex + 1;
            actionName = getString(R.string.next_message);
            actionDrawable = R.drawable.notification_action_next;
        } else {
            nextIndex = 0;
            actionName = getString(R.string.first_message);
            actionDrawable = R.drawable.notification_action_previous;
        }
        nextIntent.putExtra(NotificationServiceHelper.CURRENT_INDEX_ARG, nextIndex);
        PendingIntent retrySendIntent = PendingIntent.getService(JBMentionsWeiboNotificationServiceHelper.this, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(actionDrawable, actionName, retrySendIntent);
    }
    Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle(builder);
    if (data.getItem(currentIndex).getText().contains(accountBean.getUsernick())) {
        // mentioned you
        bigTextStyle.setBigContentTitle("@" + data.getItem(currentIndex).getUser().getScreen_name() + getString(R.string.weibo_at_to_you));
    } else {
        // retweeted your weibo
        bigTextStyle.setBigContentTitle("@" + data.getItem(currentIndex).getUser().getScreen_name() + getString(R.string.retweeted_your_weibo));
    }
    bigTextStyle.bigText(data.getItem(currentIndex).getText());
    String summaryText;
    if (count > 1) {
        summaryText = accountBean.getUsernick() + "(" + (currentIndex + 1) + "/" + count + ")";
    } else {
        summaryText = accountBean.getUsernick();
    }
    bigTextStyle.setSummaryText(summaryText);
    builder.setStyle(bigTextStyle);
    Utility.configVibrateLedRingTone(builder);
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(getMentionsWeiboNotificationId(accountBean), builder.build());
}
Also used : Context(android.content.Context) GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) MessageBean(org.qii.weiciyuan.bean.MessageBean) IntentFilter(android.content.IntentFilter) NotificationManager(android.app.NotificationManager) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RecordOperationAppBroadcastReceiver(org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver) Notification(android.app.Notification) PendingIntent(android.app.PendingIntent)

Example 3 with RecordOperationAppBroadcastReceiver

use of org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver in project weiciyuan by qii.

the class SimpleTextNotificationService method buildNotification.

private void buildNotification(String uid) {
    ValueWrapper valueWrapper = valueBagHashMap.get(uid);
    if (valueWrapper == null) {
        return;
    }
    final AccountBean accountBean = valueWrapper.accountBean;
    final UnreadBean unreadBean = valueWrapper.unreadBean;
    Intent clickToOpenAppPendingIntentInner = valueWrapper.clickToOpenAppPendingIntentInner;
    String ticker = valueWrapper.ticker;
    final RecordOperationAppBroadcastReceiver clearNotificationEventReceiver = valueWrapper.clearNotificationEventReceiver;
    Notification.Builder builder = new Notification.Builder(getBaseContext()).setTicker(ticker).setContentText(accountBean.getUsernick()).setSmallIcon(R.drawable.ic_notification).setAutoCancel(true).setContentIntent(getPendingIntent(clickToOpenAppPendingIntentInner)).setOnlyAlertOnce(true);
    builder.setContentTitle(ticker);
    Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver);
    valueWrapper.clearNotificationEventReceiver = new RecordOperationAppBroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        new ClearUnreadDao(accountBean.getAccess_token()).clearMentionStatusUnread(unreadBean, accountBean.getUid());
                        new ClearUnreadDao(accountBean.getAccess_token()).clearMentionCommentUnread(unreadBean, accountBean.getUid());
                        new ClearUnreadDao(accountBean.getAccess_token()).clearCommentUnread(unreadBean, accountBean.getUid());
                    } catch (WeiboException ignored) {
                    } finally {
                        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver);
                        if (Utility.isDebugMode()) {
                            new Handler(Looper.getMainLooper()).post(new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), "weiciyuan:remove notification items", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }
            }).start();
        }
    };
    IntentFilter intentFilter = new IntentFilter(RESET_UNREAD_MENTIONS_WEIBO_ACTION);
    GlobalContext.getInstance().registerReceiver(valueWrapper.clearNotificationEventReceiver, intentFilter);
    Intent broadcastIntent = new Intent(RESET_UNREAD_MENTIONS_WEIBO_ACTION);
    PendingIntent deletedPendingIntent = PendingIntent.getBroadcast(GlobalContext.getInstance(), accountBean.getUid().hashCode(), broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setDeleteIntent(deletedPendingIntent);
    Utility.configVibrateLedRingTone(builder);
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(getMentionsWeiboNotificationId(accountBean), builder.build());
}
Also used : Context(android.content.Context) GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) IntentFilter(android.content.IntentFilter) NotificationManager(android.app.NotificationManager) Handler(android.os.Handler) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RecordOperationAppBroadcastReceiver(org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver) Notification(android.app.Notification) ClearUnreadDao(org.qii.weiciyuan.dao.unread.ClearUnreadDao) UnreadBean(org.qii.weiciyuan.bean.UnreadBean) WeiboException(org.qii.weiciyuan.support.error.WeiboException) PendingIntent(android.app.PendingIntent) AccountBean(org.qii.weiciyuan.bean.AccountBean)

Example 4 with RecordOperationAppBroadcastReceiver

use of org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver in project weiciyuan by qii.

the class BigTextNotificationService method buildNotification.

private void buildNotification(String uid) {
    final ValueWrapper valueWrapper = valueBagHashMap.get(uid);
    if (valueWrapper == null) {
        return;
    }
    final AccountBean accountBean = valueWrapper.accountBean;
    final MessageListBean mentionsWeibo = valueWrapper.mentionsWeibo;
    final CommentListBean mentionsComment = valueWrapper.mentionsComment;
    final CommentListBean commentsToMe = valueWrapper.commentsToMe;
    final UnreadBean unreadBean = valueWrapper.unreadBean;
    int currentIndex = valueWrapper.currentIndex;
    Intent clickToOpenAppPendingIntentInner = valueWrapper.clickToOpenAppPendingIntentInner;
    String ticker = valueWrapper.ticker;
    ArrayList<Parcelable> notificationItems = valueWrapper.notificationItems;
    // int count = Math.min(unreadBean.getMention_status(), data.getSize());
    int count = notificationItems.size();
    if (count == 0) {
        return;
    }
    Parcelable itemBean = notificationItems.get(currentIndex);
    Notification.Builder builder = new Notification.Builder(getBaseContext()).setTicker(ticker).setContentText(ticker).setSubText(accountBean.getUsernick()).setSmallIcon(R.drawable.ic_notification).setAutoCancel(true).setContentIntent(getPendingIntent(clickToOpenAppPendingIntentInner, itemBean, accountBean)).setOnlyAlertOnce(true);
    builder.setContentTitle(getString(R.string.app_name));
    if (count > 1) {
        builder.setNumber(count);
    }
    Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), valueWrapper.clearNotificationEventReceiver);
    valueWrapper.clearNotificationEventReceiver = new RecordOperationAppBroadcastReceiver() {

        // mark these messages as read, write to database
        @Override
        public void onReceive(Context context, Intent intent) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        ArrayList<String> ids = new ArrayList<String>();
                        if (mentionsWeibo != null) {
                            for (MessageBean msg : mentionsWeibo.getItemList()) {
                                ids.add(msg.getId());
                            }
                            NotificationDBTask.addUnreadNotification(accountBean.getUid(), ids, NotificationDBTask.UnreadDBType.mentionsWeibo);
                        }
                        ids.clear();
                        if (commentsToMe != null) {
                            for (CommentBean msg : commentsToMe.getItemList()) {
                                ids.add(msg.getId());
                            }
                            NotificationDBTask.addUnreadNotification(accountBean.getUid(), ids, NotificationDBTask.UnreadDBType.commentsToMe);
                        }
                        ids.clear();
                        if (mentionsComment != null) {
                            for (CommentBean msg : mentionsComment.getItemList()) {
                                ids.add(msg.getId());
                            }
                            NotificationDBTask.addUnreadNotification(accountBean.getUid(), ids, NotificationDBTask.UnreadDBType.mentionsComment);
                        }
                    } finally {
                        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), valueWrapper.clearNotificationEventReceiver);
                        if (Utility.isDebugMode()) {
                            new Handler(Looper.getMainLooper()).post(new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), "weiciyuan:remove notification items" + System.currentTimeMillis(), Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }
            }).start();
        }
    };
    IntentFilter intentFilter = new IntentFilter(RESET_UNREAD_MENTIONS_WEIBO_ACTION);
    Utility.registerReceiverIgnoredReceiverHasRegisteredHereException(GlobalContext.getInstance(), valueWrapper.clearNotificationEventReceiver, intentFilter);
    Intent broadcastIntent = new Intent(RESET_UNREAD_MENTIONS_WEIBO_ACTION);
    PendingIntent deletedPendingIntent = PendingIntent.getBroadcast(GlobalContext.getInstance(), accountBean.getUid().hashCode(), broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setDeleteIntent(deletedPendingIntent);
    if (itemBean instanceof MessageBean) {
        MessageBean msg = (MessageBean) itemBean;
        Intent intent = WriteCommentActivity.newIntentFromNotification(getApplicationContext(), accountBean, msg);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(R.drawable.comment_light, getApplicationContext().getString(R.string.comments), pendingIntent);
    } else if (itemBean instanceof CommentBean) {
        CommentBean commentBean = (CommentBean) itemBean;
        Intent intent = WriteReplyToCommentActivity.newIntentFromNotification(getApplicationContext(), accountBean, commentBean);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(R.drawable.reply_to_comment_light, getApplicationContext().getString(R.string.reply_to_comment), pendingIntent);
    }
    String avatar = ((ItemBean) itemBean).getUser().getAvatar_large();
    String avatarPath = FileManager.getFilePathFromUrl(avatar, FileLocationMethod.avatar_large);
    if (ImageUtility.isThisBitmapCanRead(avatarPath) && TaskCache.isThisUrlTaskFinished(avatar)) {
        Bitmap bitmap = BitmapFactory.decodeFile(avatarPath, new BitmapFactory.Options());
        if (bitmap != null) {
            builder.setLargeIcon(bitmap);
        }
    }
    if (count > 1) {
        String actionName;
        int nextIndex;
        int actionDrawable;
        if (currentIndex < count - 1) {
            nextIndex = currentIndex + 1;
            actionName = getString(R.string.next_message);
            actionDrawable = R.drawable.notification_action_next;
        } else {
            nextIndex = 0;
            actionName = getString(R.string.first_message);
            actionDrawable = R.drawable.notification_action_previous;
        }
        Intent nextIntent = BigTextNotificationService.newIntent(accountBean, mentionsWeibo, commentsToMe, mentionsComment, unreadBean, clickToOpenAppPendingIntentInner, ticker, nextIndex);
        PendingIntent retrySendIntent = PendingIntent.getService(BigTextNotificationService.this, accountBean.getUid().hashCode(), nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(actionDrawable, actionName, retrySendIntent);
    }
    Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle(builder);
    bigTextStyle.setBigContentTitle(getItemBigContentTitle(accountBean, notificationItems, currentIndex));
    bigTextStyle.bigText(getItemBigText(notificationItems, currentIndex));
    String summaryText;
    if (count > 1) {
        summaryText = accountBean.getUsernick() + "(" + (currentIndex + 1) + "/" + count + ")";
    } else {
        summaryText = accountBean.getUsernick();
    }
    bigTextStyle.setSummaryText(summaryText);
    builder.setStyle(bigTextStyle);
    Utility.configVibrateLedRingTone(builder);
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(getMentionsWeiboNotificationId(accountBean), builder.build());
}
Also used : ArrayList(java.util.ArrayList) CommentBean(org.qii.weiciyuan.bean.CommentBean) Notification(android.app.Notification) Bitmap(android.graphics.Bitmap) UnreadBean(org.qii.weiciyuan.bean.UnreadBean) MessageListBean(org.qii.weiciyuan.bean.MessageListBean) CommentListBean(org.qii.weiciyuan.bean.CommentListBean) BitmapFactory(android.graphics.BitmapFactory) AccountBean(org.qii.weiciyuan.bean.AccountBean) Context(android.content.Context) GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) MessageBean(org.qii.weiciyuan.bean.MessageBean) IntentFilter(android.content.IntentFilter) NotificationManager(android.app.NotificationManager) Handler(android.os.Handler) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Parcelable(android.os.Parcelable) RecordOperationAppBroadcastReceiver(org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver) PendingIntent(android.app.PendingIntent)

Example 5 with RecordOperationAppBroadcastReceiver

use of org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver in project weiciyuan by qii.

the class JBCommentsToMeNotificationServiceHelper method buildNotification.

private void buildNotification() {
    // int count = (data.getSize() >= Integer.valueOf(SettingUtility.getMsgCount()) ? unreadBean
    // .getCmt() : data.getSize());
    int count = Math.min(unreadBean.getCmt(), data.getSize());
    if (count == 0) {
        return;
    }
    Notification.Builder builder = new Notification.Builder(getBaseContext()).setTicker(ticker).setContentText(accountBean.getUsernick()).setSmallIcon(R.drawable.ic_notification).setAutoCancel(true).setContentIntent(getPendingIntent()).setOnlyAlertOnce(true);
    builder.setContentTitle(String.format(GlobalContext.getInstance().getString(R.string.new_comments), String.valueOf(count)));
    if (count > 1) {
        builder.setNumber(count);
    }
    if (clearNotificationEventReceiver.get(accountBean.getUid()) != null) {
        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver.get(accountBean.getUid()));
        JBCommentsToMeNotificationServiceHelper.clearNotificationEventReceiver.put(accountBean.getUid(), null);
    }
    RecordOperationAppBroadcastReceiver receiver = new RecordOperationAppBroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // new ClearUnreadDao(accountBean.getAccess_token())
                        // .clearCommentUnread(unreadBean, accountBean.getUid());
                        // } catch (WeiboException ignored) {
                        ArrayList<String> ids = new ArrayList<String>();
                        for (CommentBean msg : data.getItemList()) {
                            ids.add(msg.getId());
                        }
                        NotificationDBTask.addUnreadNotification(accountBean.getUid(), ids, NotificationDBTask.UnreadDBType.commentsToMe);
                    } finally {
                        Utility.unregisterReceiverIgnoredReceiverNotRegisteredException(GlobalContext.getInstance(), clearNotificationEventReceiver.get(accountBean.getUid()));
                        JBCommentsToMeNotificationServiceHelper.clearNotificationEventReceiver.put(accountBean.getUid(), null);
                    }
                }
            }).start();
        }
    };
    clearNotificationEventReceiver.put(accountBean.getUid(), receiver);
    IntentFilter intentFilter = new IntentFilter(RESET_UNREAD_COMMENTS_TO_ME_ACTION);
    GlobalContext.getInstance().registerReceiver(receiver, intentFilter);
    Intent broadcastIntent = new Intent(RESET_UNREAD_COMMENTS_TO_ME_ACTION);
    PendingIntent deletedPendingIntent = PendingIntent.getBroadcast(GlobalContext.getInstance(), 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setDeleteIntent(deletedPendingIntent);
    Intent intent = WriteReplyToCommentActivity.newIntentFromNotification(getApplicationContext(), accountBean, data.getItem(currentIndex));
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.reply_to_comment_light, getApplicationContext().getString(R.string.reply_to_comment), pendingIntent);
    if (count > 1) {
        Intent nextIntent = new Intent(JBCommentsToMeNotificationServiceHelper.this, JBCommentsToMeNotificationServiceHelper.class);
        nextIntent.putExtra(NotificationServiceHelper.ACCOUNT_ARG, accountBean);
        nextIntent.putExtra(NotificationServiceHelper.COMMENTS_TO_ME_ARG, data);
        nextIntent.putExtra(NotificationServiceHelper.UNREAD_ARG, unreadBean);
        nextIntent.putExtra(NotificationServiceHelper.PENDING_INTENT_INNER_ARG, clickToOpenAppPendingIntentInner);
        nextIntent.putExtra(NotificationServiceHelper.TICKER, ticker);
        String actionName;
        int nextIndex;
        int actionDrawable;
        if (currentIndex < count - 1) {
            nextIndex = currentIndex + 1;
            actionName = getString(R.string.next_message);
            actionDrawable = R.drawable.notification_action_next;
        } else {
            nextIndex = 0;
            actionName = getString(R.string.first_message);
            actionDrawable = R.drawable.notification_action_previous;
        }
        nextIntent.putExtra(NotificationServiceHelper.CURRENT_INDEX_ARG, nextIndex);
        PendingIntent retrySendIntent = PendingIntent.getService(JBCommentsToMeNotificationServiceHelper.this, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(actionDrawable, actionName, retrySendIntent);
    }
    Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle(builder);
    CommentBean commentBean = data.getItem(currentIndex);
    if (commentBean.getReply_comment() != null) {
        bigTextStyle.setBigContentTitle("@" + data.getItem(currentIndex).getUser().getScreen_name() + getString(R.string.reply_to_you));
    } else {
        bigTextStyle.setBigContentTitle("@" + data.getItem(currentIndex).getUser().getScreen_name() + getString(R.string.comment_sent_to_you));
    }
    bigTextStyle.bigText(data.getItem(currentIndex).getText());
    String summaryText;
    if (count > 1) {
        summaryText = accountBean.getUsernick() + "(" + (currentIndex + 1) + "/" + count + ")";
    } else {
        summaryText = accountBean.getUsernick();
    }
    bigTextStyle.setSummaryText(summaryText);
    builder.setStyle(bigTextStyle);
    Utility.configVibrateLedRingTone(builder);
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(getCommentsToMeNotificationId(accountBean), builder.build());
}
Also used : Context(android.content.Context) GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) IntentFilter(android.content.IntentFilter) NotificationManager(android.app.NotificationManager) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RecordOperationAppBroadcastReceiver(org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver) CommentBean(org.qii.weiciyuan.bean.CommentBean) Notification(android.app.Notification) PendingIntent(android.app.PendingIntent)

Aggregations

Notification (android.app.Notification)5 NotificationManager (android.app.NotificationManager)5 PendingIntent (android.app.PendingIntent)5 Context (android.content.Context)5 Intent (android.content.Intent)5 IntentFilter (android.content.IntentFilter)5 RecordOperationAppBroadcastReceiver (org.qii.weiciyuan.support.lib.RecordOperationAppBroadcastReceiver)5 GlobalContext (org.qii.weiciyuan.support.utils.GlobalContext)5 ArrayList (java.util.ArrayList)4 CommentBean (org.qii.weiciyuan.bean.CommentBean)3 Handler (android.os.Handler)2 AccountBean (org.qii.weiciyuan.bean.AccountBean)2 MessageBean (org.qii.weiciyuan.bean.MessageBean)2 UnreadBean (org.qii.weiciyuan.bean.UnreadBean)2 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Parcelable (android.os.Parcelable)1 CommentListBean (org.qii.weiciyuan.bean.CommentListBean)1 MessageListBean (org.qii.weiciyuan.bean.MessageListBean)1 ClearUnreadDao (org.qii.weiciyuan.dao.unread.ClearUnreadDao)1