Example usage for android.app PendingIntent getBroadcast

List of usage examples for android.app PendingIntent getBroadcast

Introduction

In this page you can find the example usage for android.app PendingIntent getBroadcast.

Prototype

public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, @Flags int flags) 

Source Link

Document

Retrieve a PendingIntent that will perform a broadcast, like calling Context#sendBroadcast(Intent) Context.sendBroadcast() .

Usage

From source file:com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager.java

@SuppressLint("InlinedApi")
private void setUpRemoteControl(final MediaInfo info) {
    if (!isFeatureEnabled(BaseCastManager.FEATURE_LOCKSCREEN)) {
        return;/*from ww w. j a va  2  s. c  om*/
    }
    LOGD(TAG, "setUpRemoteControl() was called");
    mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

    mMediaEventReceiver = new ComponentName(mContext, VideoIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(mMediaEventReceiver);

    if (mRemoteControlClientCompat == null) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        intent.setComponent(mMediaButtonReceiverComponent);
        mRemoteControlClientCompat = new RemoteControlClientCompat(
                PendingIntent.getBroadcast(mContext, 0, intent, 0));
        RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
    }
    mRemoteControlClientCompat.addToMediaRouter(mMediaRouter);
    mRemoteControlClientCompat.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
    if (info == null) {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
        return;
    } else {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }

    // Update the remote control's image
    updateLockScreenImage(info);

    // update the remote control's metadata
    updateLockScreenMetadata();
}

From source file:com.ferdi2005.secondgram.NotificationsController.java

private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();/*from  w  w w .j a  v  a  2  s  .c o m*/
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);
        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int dismissDate = preferences.getInt("dismissDate", 0);
        if (lastMessageObject.messageOwner.date <= dismissDate) {
            dismissNotification();
            return;
        }

        long dialog_id = lastMessageObject.getDialogId();
        long override_dialog_id = dialog_id;
        if (lastMessageObject.messageOwner.mentioned) {
            override_dialog_id = lastMessageObject.messageOwner.from_id;
        }
        int mid = lastMessageObject.getId();
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id != 0
                ? lastMessageObject.messageOwner.to_id.chat_id
                : lastMessageObject.messageOwner.to_id.channel_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().getUser(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().getChat(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        int needVibrate = 0;
        String choosenSoundPath = null;
        int ledColor = 0xff0000ff;
        boolean inAppSounds;
        boolean inAppVibrate;
        boolean inAppPreview = false;
        boolean inAppPriority;
        int priority = 0;
        int priorityOverride;
        int vibrateOverride;

        int notifyOverride = getNotifyOverride(preferences, override_dialog_id);
        if (!notifyAboutLast || notifyOverride == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notifyOverride == 0) {
            notifyDisabled = true;
        }

        if (!notifyDisabled && dialog_id == override_dialog_id && chat != null) {
            int notifyMaxCount;
            int notifyDelay;
            if (preferences.getBoolean("custom_" + dialog_id, false)) {
                notifyMaxCount = preferences.getInt("smart_max_count_" + dialog_id, 2);
                notifyDelay = preferences.getInt("smart_delay_" + dialog_id, 3 * 60);
            } else {
                notifyMaxCount = 2;
                notifyDelay = 3 * 60;
            }
            if (notifyMaxCount != 0) {
                Point dialogInfo = smartNotificationsDialogs.get(dialog_id);
                if (dialogInfo == null) {
                    dialogInfo = new Point(1, (int) (System.currentTimeMillis() / 1000));
                    smartNotificationsDialogs.put(dialog_id, dialogInfo);
                } else {
                    int lastTime = dialogInfo.y;
                    if (lastTime + notifyDelay < System.currentTimeMillis() / 1000) {
                        dialogInfo.set(1, (int) (System.currentTimeMillis() / 1000));
                    } else {
                        int count = dialogInfo.x;
                        if (count < notifyMaxCount) {
                            dialogInfo.set(count + 1, (int) (System.currentTimeMillis() / 1000));
                        } else {
                            notifyDisabled = true;
                        }
                    }
                }
            }
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            inAppPriority = preferences.getBoolean("EnableInAppPriority", false);
            boolean custom;
            if (custom = preferences.getBoolean("custom_" + dialog_id, false)) {
                vibrateOverride = preferences.getInt("vibrate_" + dialog_id, 0);
                priorityOverride = preferences.getInt("priority_" + dialog_id, 3);
                choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            } else {
                vibrateOverride = 0;
                priorityOverride = 3;
                choosenSoundPath = null;
            }
            boolean vibrateOnlyIfSilent = false;

            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_group", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("GroupLed", 0xff0000ff);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_messages", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("MessagesLed", 0xff0000ff);
            }
            if (custom) {
                if (preferences.contains("color_" + dialog_id)) {
                    ledColor = preferences.getInt("color_" + dialog_id, 0);
                }
            }

            if (priorityOverride != 3) {
                priority = priorityOverride;
            }

            if (needVibrate == 4) {
                vibrateOnlyIfSilent = true;
                needVibrate = 0;
            }
            if (needVibrate == 2 && (vibrateOverride == 1 || vibrateOverride == 3)
                    || needVibrate != 2 && vibrateOverride == 2
                    || vibrateOverride != 0 && vibrateOverride != 4) {
                needVibrate = vibrateOverride;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = 2;
                }
                if (!inAppPriority) {
                    priority = 0;
                } else if (priority == 2) {
                    priority = 1;
                }
            }
            if (vibrateOnlyIfSilent && needVibrate != 2) {
                try {
                    int mode = audioManager.getRingerMode();
                    if (mode != AudioManager.RINGER_MODE_SILENT && mode != AudioManager.RINGER_MODE_VIBRATE) {
                        needVibrate = 2;
                    }
                } catch (Exception e) {
                    FileLog.e(e);
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
                photoPath = null;
            } else {
                if (pushDialogs.size() == 1) {
                    if (chat != null) {
                        if (chat.photo != null && chat.photo.photo_small != null
                                && chat.photo.photo_small.volume_id != 0
                                && chat.photo.photo_small.local_id != 0) {
                            photoPath = chat.photo.photo_small;
                        }
                    } else if (user != null) {
                        if (user.photo != null && user.photo.photo_small != null
                                && user.photo.photo_small.volume_id != 0
                                && user.photo.photo_small.local_id != 0) {
                            photoPath = user.photo.photo_small;
                        }
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1 || AndroidUtilities.needShowPasscode(false)
                || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
        }

        String detailText;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = LocaleController.formatString("NotificationMessagesPeopleDisplayOrder",
                    R.string.NotificationMessagesPeopleDisplayOrder,
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromChats", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true).setNumber(total_unread_count)
                        .setContentIntent(contentIntent).setGroup("messages").setGroupSummary(true)
                        .setColor(0xff2ca5e0);

        mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            mBuilder.addPerson("tel:+" + user.phone);
        }

        int silent = 2;
        String lastMessage = null;
        boolean hasNewMessages = false;
        if (pushMessages.size() == 1) {
            MessageObject messageObject = pushMessages.get(0);
            String message = lastMessage = getStringForMessage(messageObject, false);
            silent = messageObject.messageOwner.silent ? 1 : 0;
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                MessageObject messageObject = pushMessages.get(i);
                String message = getStringForMessage(messageObject, false);
                if (message == null || messageObject.messageOwner.date <= dismissDate) {
                    continue;
                }
                if (silent == 2) {
                    lastMessage = message;
                    silent = messageObject.messageOwner.silent ? 1 : 0;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        Intent dismissIntent = new Intent(ApplicationLoader.applicationContext,
                NotificationDismissReceiver.class);
        dismissIntent.putExtra("messageDate", lastMessageObject.messageOwner.date);
        mBuilder.setDeleteIntent(PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 1,
                dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                mBuilder.setLargeIcon(img.getBitmap());
            } else {
                try {
                    float scaleFactor = 160.0f / AndroidUtilities.dp(50);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor;
                    Bitmap bitmap = BitmapFactory
                            .decodeFile(FileLoader.getPathToAttach(photoPath, true).toString(), options);
                    if (bitmap != null) {
                        mBuilder.setLargeIcon(bitmap);
                    }
                } catch (Throwable e) {
                    //ignore
                }
            }
        }

        if (!notifyAboutLast || silent == 1) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
        } else {
            if (priority == 0) {
                mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
            } else if (priority == 1) {
                mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
            } else if (priority == 2) {
                mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
            }
        }

        if (silent != 1 && !notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                if (lastMessage.length() > 100) {
                    lastMessage = lastMessage.substring(0, 100).replace('\n', ' ').trim() + "...";
                }
                mBuilder.setTicker(lastMessage);
            }
            if (!MediaController.getInstance().isRecordingAudio()) {
                if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                    if (choosenSoundPath.equals(defaultPath)) {
                        mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                                AudioManager.STREAM_NOTIFICATION);
                    } else {
                        mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                    }
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate == 2 || MediaController.getInstance().isRecordingAudio()) {
                mBuilder.setVibrate(new long[] { 0, 0 });
            } else if (needVibrate == 1) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else if (needVibrate == 0 || needVibrate == 4) {
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            } else if (needVibrate == 3) {
                mBuilder.setVibrate(new long[] { 0, 1000 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        if (Build.VERSION.SDK_INT < 24 && UserConfig.passcodeHash.length() == 0 && hasMessagesToReply()) {
            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, PopupReplyReceiver.class);
            if (Build.VERSION.SDK_INT <= 19) {
                mBuilder.addAction(R.drawable.ic_ab_reply2, LocaleController.getString("Reply", R.string.Reply),
                        PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 2, replyIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT));
            } else {
                mBuilder.addAction(R.drawable.ic_ab_reply, LocaleController.getString("Reply", R.string.Reply),
                        PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 2, replyIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT));
            }
        }
        showExtraNotifications(mBuilder, notifyAboutLast);
        notificationManager.notify(1, mBuilder.build());

        scheduleNotificationRepeat();
    } catch (Exception e) {
        FileLog.e(e);
    }
}

From source file:com.ferdi2005.secondgram.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;/*  w w w . j  a v  a  2 s. c om*/
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationId = oldIdsWear.get(dialog_id);
        if (notificationId == null) {
            notificationId = (int) dialog_id;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("com.ferdi2005.secondgram.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationId, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if ((!ChatObject.isChannel(chat) || chat != null && chat.megagroup)
                && !AndroidUtilities.needShowPasscode(false) && !UserConfig.isWaitingForPasscodeEnter) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("com.ferdi2005.secondgram.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationId, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationId, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).setAllowGeneratedReplies(true).addRemoteInput(remoteInputWear).build();
        }

        Integer count = pushDialogs.get(dialog_id);
        if (count == null) {
            count = 0;
        }
        NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(null)
                .setConversationTitle(String.format("%1$s (%2$s)", name, LocaleController
                        .formatPluralString("NewMessages", Math.max(count, messageObjects.size()))));

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
            messagingStyle.addMessage(message, ((long) messageObject.messageOwner.date) * 1000, null);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        String dismissalID = null;
        if (chat != null)
            dismissalID = "tgchat" + chat.id + "_" + max_id;
        else if (user != null)
            dismissalID = "tguser" + user.id + "_" + max_id;

        wearableExtender.setDismissalId(dismissalID);

        NotificationCompat.WearableExtender summaryExtender = new NotificationCompat.WearableExtender();
        summaryExtender.setDismissalId("summary_" + dismissalID);
        notificationBuilder.extend(summaryExtender);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true).setNumber(messageObjects.size()).setColor(0xff2ca5e0)
                        .setGroupSummary(false).setWhen(((long) messageObjects.get(0).messageOwner.date) * 1000)
                        .setStyle(messagingStyle).setContentIntent(contentIntent).extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            } else {
                try {
                    float scaleFactor = 160.0f / AndroidUtilities.dp(50);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor;
                    Bitmap bitmap = BitmapFactory
                            .decodeFile(FileLoader.getPathToAttach(photoPath, true).toString(), options);
                    if (bitmap != null) {
                        builder.setLargeIcon(bitmap);
                    }
                } catch (Throwable e) {
                    //ignore
                }
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationId, builder.build());
        wearNotificationsIds.put(dialog_id, notificationId);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}

From source file:com.piusvelte.sonet.core.SonetService.java

private void buildWidgetButtons(Integer appWidgetId, boolean updatesReady, int page, boolean hasbuttons,
        int scrollable, int buttons_bg_color, int buttons_color, int buttons_textsize, boolean display_profile,
        int margin) {
    final String widget = Integer.toString(appWidgetId);
    // Push update for this widget to the home screen
    int layout;/*w w w.  j av a 2  s  . com*/
    if (hasbuttons) {
        if (sNativeScrollingSupported) {
            if (margin > 0)
                layout = R.layout.widget_margin_scrollable;
            else
                layout = R.layout.widget_scrollable;
        } else if (display_profile) {
            if (margin > 0)
                layout = R.layout.widget_margin;
            else
                layout = R.layout.widget;
        } else {
            if (margin > 0)
                layout = R.layout.widget_noprofile_margin;
            else
                layout = R.layout.widget_noprofile;
        }
    } else {
        if (sNativeScrollingSupported) {
            if (margin > 0)
                layout = R.layout.widget_nobuttons_margin_scrollable;
            else
                layout = R.layout.widget_nobuttons_scrollable;
        } else if (display_profile) {
            if (margin > 0)
                layout = R.layout.widget_nobuttons_margin;
            else
                layout = R.layout.widget_nobuttons;
        } else {
            if (margin > 0)
                layout = R.layout.widget_nobuttons_noprofile_margin;
            else
                layout = R.layout.widget_nobuttons_noprofile;
        }
    }
    // wrap RemoteViews for backward compatibility
    RemoteViews views = new RemoteViews(getPackageName(), layout);
    if (hasbuttons) {
        Bitmap buttons_bg = Bitmap.createBitmap(1, 1, Config.ARGB_8888);
        Canvas buttons_bg_canvas = new Canvas(buttons_bg);
        buttons_bg_canvas.drawColor(buttons_bg_color);
        views.setImageViewBitmap(R.id.buttons_bg, buttons_bg);
        views.setTextColor(R.id.buttons_bg_clear, buttons_bg_color);
        views.setFloat(R.id.buttons_bg_clear, "setTextSize", buttons_textsize);
        views.setOnClickPendingIntent(R.id.button_post, PendingIntent.getActivity(SonetService.this, 0,
                Sonet.getPackageIntent(SonetService.this, SonetCreatePost.class)
                        .setAction(LauncherIntent.Action.ACTION_VIEW_CLICK)
                        .setData(Uri.withAppendedPath(Widgets.getContentUri(SonetService.this), widget)),
                0));
        views.setTextColor(R.id.button_post, buttons_color);
        views.setFloat(R.id.button_post, "setTextSize", buttons_textsize);
        views.setOnClickPendingIntent(R.id.button_configure, PendingIntent.getActivity(SonetService.this, 0,
                Sonet.getPackageIntent(SonetService.this, ManageAccounts.class).setAction(widget), 0));
        views.setTextColor(R.id.button_configure, buttons_color);
        views.setFloat(R.id.button_configure, "setTextSize", buttons_textsize);
        views.setOnClickPendingIntent(R.id.button_refresh, PendingIntent.getService(SonetService.this, 0,
                Sonet.getPackageIntent(SonetService.this, SonetService.class).setAction(widget), 0));
        views.setTextColor(R.id.button_refresh, buttons_color);
        views.setFloat(R.id.button_refresh, "setTextSize", buttons_textsize);
        views.setTextColor(R.id.page_up, buttons_color);
        views.setFloat(R.id.page_up, "setTextSize", buttons_textsize);
        views.setTextColor(R.id.page_down, buttons_color);
        views.setFloat(R.id.page_down, "setTextSize", buttons_textsize);
    }
    // set margin
    if (scrollable == 0) {
        final AppWidgetManager mgr = AppWidgetManager.getInstance(SonetService.this);
        // check if native scrolling is supported
        if (sNativeScrollingSupported) {
            // native scrolling
            try {
                final Intent intent = SonetRemoteViewsServiceWrapper.getRemoteAdapterIntent(SonetService.this);
                if (intent != null) {
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    intent.putExtra(Widgets.DISPLAY_PROFILE, display_profile);
                    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
                    sSetRemoteAdapter.invoke(views, appWidgetId, R.id.messages, intent);
                    // empty
                    sSetEmptyView.invoke(views, R.id.messages, R.id.empty_messages);
                    // onclick
                    // Bind a click listener template for the contents of the message list
                    final Intent onClickIntent = Sonet.getPackageIntent(SonetService.this, SonetWidget.class);
                    onClickIntent.setAction(ACTION_ON_CLICK);
                    onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
                    final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(SonetService.this, 0,
                            onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    sSetPendingIntentTemplate.invoke(views, R.id.messages, onClickPendingIntent);
                } else {
                    // fallback on non-scrolling widget
                    sNativeScrollingSupported = false;
                }
            } catch (NumberFormatException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalArgumentException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalAccessException e) {
                Log.e(TAG, e.toString());
            } catch (InvocationTargetException e) {
                Log.e(TAG, e.toString());
            }

        }
        if (!sNativeScrollingSupported) {
            Cursor statuses_styles = getContentResolver().query(
                    Uri.withAppendedPath(Statuses_styles.getContentUri(SonetService.this), widget),
                    new String[] { Statuses_styles._ID, Statuses_styles.FRIEND, Statuses_styles.PROFILE,
                            Statuses_styles.MESSAGE, Statuses_styles.CREATEDTEXT,
                            Statuses_styles.MESSAGES_COLOR, Statuses_styles.FRIEND_COLOR,
                            Statuses_styles.CREATED_COLOR, Statuses_styles.MESSAGES_TEXTSIZE,
                            Statuses_styles.FRIEND_TEXTSIZE, Statuses_styles.CREATED_TEXTSIZE,
                            Statuses_styles.STATUS_BG, Statuses_styles.ICON, Statuses_styles.PROFILE_BG,
                            Statuses_styles.FRIEND_BG, Statuses_styles.IMAGE_BG, Statuses_styles.IMAGE },
                    null, null, Statuses_styles.CREATED + " DESC LIMIT " + page + ",-1");
            if (statuses_styles.moveToFirst()) {
                int count_status = 0;
                views.removeAllViews(R.id.messages);
                while (!statuses_styles.isAfterLast() && (count_status < 16)) {
                    int friend_color = statuses_styles.getInt(6), created_color = statuses_styles.getInt(7),
                            friend_textsize = statuses_styles.getInt(9),
                            created_textsize = statuses_styles.getInt(10),
                            messages_color = statuses_styles.getInt(5),
                            messages_textsize = statuses_styles.getInt(8);
                    // get the item wrapper
                    RemoteViews itemView;
                    if (display_profile) {
                        itemView = new RemoteViews(getPackageName(), R.layout.widget_item);
                        // set profiles background
                        byte[] profile_bg = statuses_styles.getBlob(13);
                        if (profile_bg != null) {
                            Bitmap profile_bgbmp = BitmapFactory.decodeByteArray(profile_bg, 0,
                                    profile_bg.length, sBFOptions);
                            if (profile_bgbmp != null)
                                itemView.setImageViewBitmap(R.id.profile_bg, profile_bgbmp);
                        }
                        byte[] profile = statuses_styles.getBlob(2);
                        if (profile != null) {
                            Bitmap profilebmp = BitmapFactory.decodeByteArray(profile, 0, profile.length,
                                    sBFOptions);
                            if (profilebmp != null)
                                itemView.setImageViewBitmap(R.id.profile, profilebmp);
                        }
                    } else
                        itemView = new RemoteViews(getPackageName(), R.layout.widget_item_noprofile);
                    itemView.setTextViewText(R.id.friend_bg_clear, statuses_styles.getString(1));
                    itemView.setFloat(R.id.friend_bg_clear, "setTextSize", friend_textsize);
                    itemView.setTextViewText(R.id.message_bg_clear, statuses_styles.getString(3));
                    itemView.setFloat(R.id.message_bg_clear, "setTextSize", messages_textsize);
                    // set friends background
                    byte[] friend_bg = statuses_styles.getBlob(14);
                    if (friend_bg != null) {
                        Bitmap friend_bgbmp = BitmapFactory.decodeByteArray(friend_bg, 0, friend_bg.length,
                                sBFOptions);
                        if (friend_bgbmp != null)
                            itemView.setImageViewBitmap(R.id.friend_bg, friend_bgbmp);
                    }
                    // set messages background
                    byte[] status_bg = statuses_styles.getBlob(11);
                    if (status_bg != null) {
                        Bitmap status_bgbmp = BitmapFactory.decodeByteArray(status_bg, 0, status_bg.length,
                                sBFOptions);
                        if (status_bgbmp != null)
                            itemView.setImageViewBitmap(R.id.status_bg, status_bgbmp);
                    }
                    // set an image
                    byte[] image_bg = statuses_styles.getBlob(15);
                    byte[] image = statuses_styles.getBlob(16);
                    if ((image_bg != null) && (image != null)) {
                        Bitmap image_bgBmp = BitmapFactory.decodeByteArray(image_bg, 0, image_bg.length,
                                sBFOptions);
                        if (image_bgBmp != null) {
                            Bitmap imageBmp = BitmapFactory.decodeByteArray(image, 0, image.length, sBFOptions);
                            itemView.setImageViewBitmap(R.id.image_clear, image_bgBmp);
                            itemView.setImageViewBitmap(R.id.image, imageBmp);
                        }
                    }
                    itemView.setTextViewText(R.id.message, statuses_styles.getString(3));
                    itemView.setTextColor(R.id.message, messages_color);
                    itemView.setFloat(R.id.message, "setTextSize", messages_textsize);
                    itemView.setOnClickPendingIntent(R.id.item,
                            PendingIntent.getActivity(SonetService.this, 0,
                                    Sonet.getPackageIntent(SonetService.this, StatusDialog.class)
                                            .setData(Uri.withAppendedPath(
                                                    Statuses_styles.getContentUri(SonetService.this),
                                                    Long.toString(statuses_styles.getLong(0)))),
                                    0));
                    itemView.setTextViewText(R.id.friend, statuses_styles.getString(1));
                    itemView.setTextColor(R.id.friend, friend_color);
                    itemView.setFloat(R.id.friend, "setTextSize", friend_textsize);
                    itemView.setTextViewText(R.id.created, statuses_styles.getString(4));
                    itemView.setTextColor(R.id.created, created_color);
                    itemView.setFloat(R.id.created, "setTextSize", created_textsize);
                    // set icons
                    byte[] icon = statuses_styles.getBlob(12);
                    if (icon != null) {
                        Bitmap iconbmp = BitmapFactory.decodeByteArray(icon, 0, icon.length, sBFOptions);
                        if (iconbmp != null)
                            itemView.setImageViewBitmap(R.id.icon, iconbmp);
                    }
                    views.addView(R.id.messages, itemView);
                    count_status++;
                    statuses_styles.moveToNext();
                }
                if (hasbuttons && (page < statuses_styles.getCount())) {
                    // there are more statuses to show, allow paging down
                    views.setOnClickPendingIntent(R.id.page_down, PendingIntent.getService(SonetService.this, 0,
                            Sonet.getPackageIntent(SonetService.this, SonetService.class)
                                    .setAction(ACTION_PAGE_DOWN)
                                    .setData(Uri.withAppendedPath(Widgets.getContentUri(SonetService.this),
                                            widget))
                                    .putExtra(ACTION_PAGE_DOWN, page + 1),
                            PendingIntent.FLAG_UPDATE_CURRENT));
                }
            }
            statuses_styles.close();
            if (hasbuttons && (page > 0))
                views.setOnClickPendingIntent(R.id.page_up, PendingIntent.getService(SonetService.this, 0,
                        Sonet.getPackageIntent(SonetService.this, SonetService.class).setAction(ACTION_PAGE_UP)
                                .setData(Uri.withAppendedPath(Widgets.getContentUri(SonetService.this), widget))
                                .putExtra(ACTION_PAGE_UP, page - 1),
                        PendingIntent.FLAG_UPDATE_CURRENT));
        }
        Log.d(TAG, "update native widget: " + appWidgetId);
        mgr.updateAppWidget(appWidgetId, views);
        if (sNativeScrollingSupported) {
            Log.d(TAG, "trigger widget query: " + appWidgetId);
            try {
                // trigger query
                sNotifyAppWidgetViewDataChanged.invoke(mgr, appWidgetId, R.id.messages);
            } catch (NumberFormatException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalArgumentException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalAccessException e) {
                Log.e(TAG, e.toString());
            } catch (InvocationTargetException e) {
                Log.e(TAG, e.toString());
            }
        }
    } else if (updatesReady) {
        //         Log.d(TAG, "notify updatesReady");
        getContentResolver().notifyChange(Statuses_styles.getContentUri(SonetService.this), null);
    } else {
        AppWidgetManager.getInstance(SonetService.this).updateAppWidget(Integer.parseInt(widget), views);
        buildScrollableWidget(appWidgetId, scrollable, display_profile);
    }
}

From source file:com.shafiq.myfeedle.core.MyfeedleService.java

private void buildWidgetButtons(Integer appWidgetId, boolean updatesReady, int page, boolean hasbuttons,
        int scrollable, int buttons_bg_color, int buttons_color, int buttons_textsize, boolean display_profile,
        int margin) {
    final String widget = Integer.toString(appWidgetId);
    // Push update for this widget to the home screen
    int layout;/*from w ww .j  a  v a 2  s.co  m*/
    if (hasbuttons) {
        if (sNativeScrollingSupported) {
            if (margin > 0)
                layout = R.layout.widget_margin_scrollable;
            else
                layout = R.layout.widget_scrollable;
        } else if (display_profile) {
            if (margin > 0)
                layout = R.layout.widget_margin;
            else
                layout = R.layout.widget;
        } else {
            if (margin > 0)
                layout = R.layout.widget_noprofile_margin;
            else
                layout = R.layout.widget_noprofile;
        }
    } else {
        if (sNativeScrollingSupported) {
            if (margin > 0)
                layout = R.layout.widget_nobuttons_margin_scrollable;
            else
                layout = R.layout.widget_nobuttons_scrollable;
        } else if (display_profile) {
            if (margin > 0)
                layout = R.layout.widget_nobuttons_margin;
            else
                layout = R.layout.widget_nobuttons;
        } else {
            if (margin > 0)
                layout = R.layout.widget_nobuttons_noprofile_margin;
            else
                layout = R.layout.widget_nobuttons_noprofile;
        }
    }
    // wrap RemoteViews for backward compatibility
    RemoteViews views = new RemoteViews(getPackageName(), layout);
    if (hasbuttons) {
        Bitmap buttons_bg = Bitmap.createBitmap(1, 1, Config.ARGB_8888);
        Canvas buttons_bg_canvas = new Canvas(buttons_bg);
        buttons_bg_canvas.drawColor(buttons_bg_color);
        views.setImageViewBitmap(R.id.buttons_bg, buttons_bg);
        views.setTextColor(R.id.buttons_bg_clear, buttons_bg_color);
        views.setFloat(R.id.buttons_bg_clear, "setTextSize", buttons_textsize);
        views.setOnClickPendingIntent(R.id.button_post,
                PendingIntent.getActivity(MyfeedleService.this, 0,
                        Myfeedle.getPackageIntent(MyfeedleService.this, MyfeedleCreatePost.class)
                                .setAction(LauncherIntent.Action.ACTION_VIEW_CLICK).setData(Uri
                                        .withAppendedPath(Widgets.getContentUri(MyfeedleService.this), widget)),
                        0));
        views.setTextColor(R.id.button_post, buttons_color);
        views.setFloat(R.id.button_post, "setTextSize", buttons_textsize);
        views.setOnClickPendingIntent(R.id.button_configure, PendingIntent.getActivity(MyfeedleService.this, 0,
                Myfeedle.getPackageIntent(MyfeedleService.this, ManageAccounts.class).setAction(widget), 0));
        views.setTextColor(R.id.button_configure, buttons_color);
        views.setFloat(R.id.button_configure, "setTextSize", buttons_textsize);
        views.setOnClickPendingIntent(R.id.button_refresh, PendingIntent.getService(MyfeedleService.this, 0,
                Myfeedle.getPackageIntent(MyfeedleService.this, MyfeedleService.class).setAction(widget), 0));
        views.setTextColor(R.id.button_refresh, buttons_color);
        views.setFloat(R.id.button_refresh, "setTextSize", buttons_textsize);
        views.setTextColor(R.id.page_up, buttons_color);
        views.setFloat(R.id.page_up, "setTextSize", buttons_textsize);
        views.setTextColor(R.id.page_down, buttons_color);
        views.setFloat(R.id.page_down, "setTextSize", buttons_textsize);
    }
    // set margin
    if (scrollable == 0) {
        final AppWidgetManager mgr = AppWidgetManager.getInstance(MyfeedleService.this);
        // check if native scrolling is supported
        if (sNativeScrollingSupported) {
            // native scrolling
            try {
                final Intent intent = MyfeedleRemoteViewsServiceWrapper
                        .getRemoteAdapterIntent(MyfeedleService.this);
                if (intent != null) {
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    intent.putExtra(Widgets.DISPLAY_PROFILE, display_profile);
                    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
                    sSetRemoteAdapter.invoke(views, appWidgetId, R.id.messages, intent);
                    // empty
                    sSetEmptyView.invoke(views, R.id.messages, R.id.empty_messages);
                    // onclick
                    // Bind a click listener template for the contents of the message list
                    final Intent onClickIntent = Myfeedle.getPackageIntent(MyfeedleService.this,
                            MyfeedleWidget.class);
                    onClickIntent.setAction(ACTION_ON_CLICK);
                    onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
                    final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(MyfeedleService.this,
                            0, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    sSetPendingIntentTemplate.invoke(views, R.id.messages, onClickPendingIntent);
                } else {
                    // fallback on non-scrolling widget
                    sNativeScrollingSupported = false;
                }
            } catch (NumberFormatException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalArgumentException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalAccessException e) {
                Log.e(TAG, e.toString());
            } catch (InvocationTargetException e) {
                Log.e(TAG, e.toString());
            }

        }
        if (!sNativeScrollingSupported) {
            Cursor statuses_styles = getContentResolver().query(
                    Uri.withAppendedPath(Statuses_styles.getContentUri(MyfeedleService.this), widget),
                    new String[] { Statuses_styles._ID, Statuses_styles.FRIEND, Statuses_styles.PROFILE,
                            Statuses_styles.MESSAGE, Statuses_styles.CREATEDTEXT,
                            Statuses_styles.MESSAGES_COLOR, Statuses_styles.FRIEND_COLOR,
                            Statuses_styles.CREATED_COLOR, Statuses_styles.MESSAGES_TEXTSIZE,
                            Statuses_styles.FRIEND_TEXTSIZE, Statuses_styles.CREATED_TEXTSIZE,
                            Statuses_styles.STATUS_BG, Statuses_styles.ICON, Statuses_styles.PROFILE_BG,
                            Statuses_styles.FRIEND_BG, Statuses_styles.IMAGE_BG, Statuses_styles.IMAGE },
                    null, null, Statuses_styles.CREATED + " DESC LIMIT " + page + ",-1");
            if (statuses_styles.moveToFirst()) {
                int count_status = 0;
                views.removeAllViews(R.id.messages);
                while (!statuses_styles.isAfterLast() && (count_status < 16)) {
                    int friend_color = statuses_styles.getInt(6), created_color = statuses_styles.getInt(7),
                            friend_textsize = statuses_styles.getInt(9),
                            created_textsize = statuses_styles.getInt(10),
                            messages_color = statuses_styles.getInt(5),
                            messages_textsize = statuses_styles.getInt(8);
                    // get the item wrapper
                    RemoteViews itemView;
                    if (display_profile) {
                        itemView = new RemoteViews(getPackageName(), R.layout.widget_item);
                        // set profiles background
                        byte[] profile_bg = statuses_styles.getBlob(13);
                        if (profile_bg != null) {
                            Bitmap profile_bgbmp = BitmapFactory.decodeByteArray(profile_bg, 0,
                                    profile_bg.length, sBFOptions);
                            if (profile_bgbmp != null)
                                itemView.setImageViewBitmap(R.id.profile_bg, profile_bgbmp);
                        }
                        byte[] profile = statuses_styles.getBlob(2);
                        if (profile != null) {
                            Bitmap profilebmp = BitmapFactory.decodeByteArray(profile, 0, profile.length,
                                    sBFOptions);
                            if (profilebmp != null)
                                itemView.setImageViewBitmap(R.id.profile, profilebmp);
                        }
                    } else
                        itemView = new RemoteViews(getPackageName(), R.layout.widget_item_noprofile);
                    itemView.setTextViewText(R.id.friend_bg_clear, statuses_styles.getString(1));
                    itemView.setFloat(R.id.friend_bg_clear, "setTextSize", friend_textsize);
                    itemView.setTextViewText(R.id.message_bg_clear, statuses_styles.getString(3));
                    itemView.setFloat(R.id.message_bg_clear, "setTextSize", messages_textsize);
                    // set friends background
                    byte[] friend_bg = statuses_styles.getBlob(14);
                    if (friend_bg != null) {
                        Bitmap friend_bgbmp = BitmapFactory.decodeByteArray(friend_bg, 0, friend_bg.length,
                                sBFOptions);
                        if (friend_bgbmp != null)
                            itemView.setImageViewBitmap(R.id.friend_bg, friend_bgbmp);
                    }
                    // set messages background
                    byte[] status_bg = statuses_styles.getBlob(11);
                    if (status_bg != null) {
                        Bitmap status_bgbmp = BitmapFactory.decodeByteArray(status_bg, 0, status_bg.length,
                                sBFOptions);
                        if (status_bgbmp != null)
                            itemView.setImageViewBitmap(R.id.status_bg, status_bgbmp);
                    }
                    // set an image
                    byte[] image_bg = statuses_styles.getBlob(15);
                    byte[] image = statuses_styles.getBlob(16);
                    if ((image_bg != null) && (image != null)) {
                        Bitmap image_bgBmp = BitmapFactory.decodeByteArray(image_bg, 0, image_bg.length,
                                sBFOptions);
                        if (image_bgBmp != null) {
                            Bitmap imageBmp = BitmapFactory.decodeByteArray(image, 0, image.length, sBFOptions);
                            itemView.setImageViewBitmap(R.id.image_clear, image_bgBmp);
                            itemView.setImageViewBitmap(R.id.image, imageBmp);
                        }
                    }
                    itemView.setTextViewText(R.id.message, statuses_styles.getString(3));
                    itemView.setTextColor(R.id.message, messages_color);
                    itemView.setFloat(R.id.message, "setTextSize", messages_textsize);
                    itemView.setOnClickPendingIntent(R.id.item,
                            PendingIntent.getActivity(MyfeedleService.this, 0,
                                    Myfeedle.getPackageIntent(MyfeedleService.this, StatusDialog.class)
                                            .setData(Uri.withAppendedPath(
                                                    Statuses_styles.getContentUri(MyfeedleService.this),
                                                    Long.toString(statuses_styles.getLong(0)))),
                                    0));
                    itemView.setTextViewText(R.id.friend, statuses_styles.getString(1));
                    itemView.setTextColor(R.id.friend, friend_color);
                    itemView.setFloat(R.id.friend, "setTextSize", friend_textsize);
                    itemView.setTextViewText(R.id.created, statuses_styles.getString(4));
                    itemView.setTextColor(R.id.created, created_color);
                    itemView.setFloat(R.id.created, "setTextSize", created_textsize);
                    // set icons
                    byte[] icon = statuses_styles.getBlob(12);
                    if (icon != null) {
                        Bitmap iconbmp = BitmapFactory.decodeByteArray(icon, 0, icon.length, sBFOptions);
                        if (iconbmp != null)
                            itemView.setImageViewBitmap(R.id.icon, iconbmp);
                    }
                    views.addView(R.id.messages, itemView);
                    count_status++;
                    statuses_styles.moveToNext();
                }
                if (hasbuttons && (page < statuses_styles.getCount())) {
                    // there are more statuses to show, allow paging down
                    views.setOnClickPendingIntent(R.id.page_down, PendingIntent.getService(MyfeedleService.this,
                            0,
                            Myfeedle.getPackageIntent(MyfeedleService.this, MyfeedleService.class)
                                    .setAction(ACTION_PAGE_DOWN)
                                    .setData(Uri.withAppendedPath(Widgets.getContentUri(MyfeedleService.this),
                                            widget))
                                    .putExtra(ACTION_PAGE_DOWN, page + 1),
                            PendingIntent.FLAG_UPDATE_CURRENT));
                }
            }
            statuses_styles.close();
            if (hasbuttons && (page > 0))
                views.setOnClickPendingIntent(R.id.page_up, PendingIntent.getService(MyfeedleService.this, 0,
                        Myfeedle.getPackageIntent(MyfeedleService.this, MyfeedleService.class)
                                .setAction(ACTION_PAGE_UP)
                                .setData(Uri.withAppendedPath(Widgets.getContentUri(MyfeedleService.this),
                                        widget))
                                .putExtra(ACTION_PAGE_UP, page - 1),
                        PendingIntent.FLAG_UPDATE_CURRENT));
        }
        Log.d(TAG, "update native widget: " + appWidgetId);
        mgr.updateAppWidget(appWidgetId, views);
        if (sNativeScrollingSupported) {
            Log.d(TAG, "trigger widget query: " + appWidgetId);
            try {
                // trigger query
                sNotifyAppWidgetViewDataChanged.invoke(mgr, appWidgetId, R.id.messages);
            } catch (NumberFormatException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalArgumentException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalAccessException e) {
                Log.e(TAG, e.toString());
            } catch (InvocationTargetException e) {
                Log.e(TAG, e.toString());
            }
        }
    } else if (updatesReady) {
        //         Log.d(TAG, "notify updatesReady");
        getContentResolver().notifyChange(Statuses_styles.getContentUri(MyfeedleService.this), null);
    } else {
        AppWidgetManager.getInstance(MyfeedleService.this).updateAppWidget(Integer.parseInt(widget), views);
        buildScrollableWidget(appWidgetId, scrollable, display_profile);
    }
}

From source file:com.piusvelte.sonet.core.SonetService.java

private void buildScrollableWidget(Integer appWidgetId, int scrollableVersion, boolean display_profile) {
    // set widget as scrollable
    Intent replaceDummy = new Intent(LauncherIntent.Action.ACTION_SCROLL_WIDGET_START);
    replaceDummy.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    replaceDummy.putExtra(LauncherIntent.Extra.EXTRA_VIEW_ID, R.id.messages);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_LISTVIEW_LAYOUT_ID, R.layout.widget_listview);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_DATA_PROVIDER_ALLOW_REQUERY, true);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_CHILDREN_CLICKABLE, true);

    //provider//from   w  ww . jav  a2s. c  o m
    Uri uri = Uri.withAppendedPath(Statuses_styles.getContentUri(SonetService.this),
            Integer.toString(appWidgetId));
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_DATA_URI, uri.toString());
    String[] projection;
    if (display_profile)
        projection = new String[] { Statuses_styles._ID, Statuses_styles.FRIEND, Statuses_styles.PROFILE,
                Statuses_styles.MESSAGE, Statuses_styles.CREATEDTEXT, Statuses_styles.MESSAGES_COLOR,
                Statuses_styles.FRIEND_COLOR, Statuses_styles.CREATED_COLOR, Statuses_styles.MESSAGES_TEXTSIZE,
                Statuses_styles.FRIEND_TEXTSIZE, Statuses_styles.CREATED_TEXTSIZE, Statuses_styles.STATUS_BG,
                Statuses_styles.ICON, Statuses_styles.PROFILE_BG, Statuses_styles.FRIEND_BG,
                Statuses_styles.IMAGE_BG, Statuses_styles.IMAGE };
    else
        projection = new String[] { Statuses_styles._ID, Statuses_styles.FRIEND, Statuses_styles.PROFILE,
                Statuses_styles.MESSAGE, Statuses_styles.CREATEDTEXT, Statuses_styles.MESSAGES_COLOR,
                Statuses_styles.FRIEND_COLOR, Statuses_styles.CREATED_COLOR, Statuses_styles.MESSAGES_TEXTSIZE,
                Statuses_styles.FRIEND_TEXTSIZE, Statuses_styles.CREATED_TEXTSIZE, Statuses_styles.STATUS_BG,
                Statuses_styles.ICON, Statuses_styles.FRIEND_BG, Statuses_styles.IMAGE_BG,
                Statuses_styles.IMAGE };
    String sortOrder = Statuses_styles.CREATED + " DESC";
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_PROJECTION, projection);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_SORT_ORDER, sortOrder);
    String whereClause = Statuses_styles.WIDGET + "=?";
    String[] selectionArgs = new String[] { Integer.toString(appWidgetId) };
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_SELECTION, whereClause);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_SELECTION_ARGUMENTS, selectionArgs);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_ACTION_VIEW_URI_INDEX,
            SonetProvider.StatusesStylesColumns._id.ordinal());

    switch (scrollableVersion) {
    case 1:
        if (display_profile) {
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_ID, R.layout.widget_item);
            int[] cursorIndices = new int[] { SonetProvider.StatusesStylesColumns.friend.ordinal(),
                    SonetProvider.StatusesStylesColumns.message.ordinal(),
                    SonetProvider.StatusesStylesColumns.status_bg.ordinal(),
                    SonetProvider.StatusesStylesColumns.profile.ordinal(),
                    SonetProvider.StatusesStylesColumns.friend.ordinal(),
                    SonetProvider.StatusesStylesColumns.createdtext.ordinal(),
                    SonetProvider.StatusesStylesColumns.message.ordinal(),
                    SonetProvider.StatusesStylesColumns.icon.ordinal(),
                    SonetProvider.StatusesStylesColumns.profile_bg.ordinal(),
                    SonetProvider.StatusesStylesColumns.friend_bg.ordinal(),
                    SonetProvider.StatusesStylesColumns.image_bg.ordinal(),
                    SonetProvider.StatusesStylesColumns.image.ordinal() };
            int[] viewTypes = new int[] { LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB };
            int[] layoutIds = new int[] { R.id.friend_bg_clear, R.id.message_bg_clear, R.id.status_bg,
                    R.id.profile, R.id.friend, R.id.created, R.id.message, R.id.icon, R.id.profile_bg,
                    R.id.friend_bg, R.id.image_clear, R.id.image };
            int[] defaultResource = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            boolean[] clickable = new boolean[] { false, false, true, false, false, false, false, false, false,
                    false, false, false };
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_CURSOR_INDICES, cursorIndices);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_TYPES, viewTypes);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_IDS, layoutIds);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_DEFAULT_RESOURCES, defaultResource);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_CLICKABLE, clickable);
        } else {
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_ID,
                    R.layout.widget_item_noprofile);
            int[] cursorIndices = new int[] { SonetProvider.StatusesStylesColumnsNoProfile.friend.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.message.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.status_bg.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.friend.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.createdtext.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.message.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.icon.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.friend_bg.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.image_bg.ordinal(),
                    SonetProvider.StatusesStylesColumnsNoProfile.image.ordinal() };
            int[] viewTypes = new int[] { LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB };
            int[] layoutIds = new int[] { R.id.friend_bg_clear, R.id.message_bg_clear, R.id.status_bg,
                    R.id.friend, R.id.created, R.id.message, R.id.icon, R.id.friend_bg, R.id.image_clear,
                    R.id.image };
            int[] defaultResource = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            boolean[] clickable = new boolean[] { false, false, true, false, false, false, false, false, false,
                    false };
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_CURSOR_INDICES, cursorIndices);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_TYPES, viewTypes);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_IDS, layoutIds);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_DEFAULT_RESOURCES, defaultResource);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_CLICKABLE, clickable);
        }
        break;
    case 2:
        if (display_profile) {
            BoundRemoteViews itemViews = new BoundRemoteViews(R.layout.widget_item);

            Intent i = Sonet.getPackageIntent(SonetService.this, SonetWidget.class)
                    .setAction(LauncherIntent.Action.ACTION_VIEW_CLICK).setData(uri)
                    .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pi = PendingIntent.getBroadcast(SonetService.this, 0, i, 0);

            itemViews.SetBoundOnClickIntent(R.id.item, pi, LauncherIntent.Extra.Scroll.EXTRA_ITEM_POS,
                    SonetProvider.StatusesStylesColumns._id.ordinal());

            itemViews.setBoundCharSequence(R.id.friend_bg_clear, "setText",
                    SonetProvider.StatusesStylesColumns.friend.ordinal(), 0);
            itemViews.setBoundFloat(R.id.friend_bg_clear, "setTextSize",
                    SonetProvider.StatusesStylesColumns.friend_textsize.ordinal());

            itemViews.setBoundCharSequence(R.id.message_bg_clear, "setText",
                    SonetProvider.StatusesStylesColumns.message.ordinal(), 0);
            itemViews.setBoundFloat(R.id.message_bg_clear, "setTextSize",
                    SonetProvider.StatusesStylesColumns.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.status_bg, "setImageBitmap",
                    SonetProvider.StatusesStylesColumns.status_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image_clear, "setImageBitmap",
                    SonetProvider.StatusesStylesColumns.image_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image, "setImageBitmap",
                    SonetProvider.StatusesStylesColumns.image.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.profile, "setImageBitmap",
                    SonetProvider.StatusesStylesColumns.profile.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.friend, "setText",
                    SonetProvider.StatusesStylesColumns.friend.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.created, "setText",
                    SonetProvider.StatusesStylesColumns.createdtext.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.message, "setText",
                    SonetProvider.StatusesStylesColumns.message.ordinal(), 0);

            itemViews.setBoundInt(R.id.friend, "setTextColor",
                    SonetProvider.StatusesStylesColumns.friend_color.ordinal());
            itemViews.setBoundInt(R.id.created, "setTextColor",
                    SonetProvider.StatusesStylesColumns.created_color.ordinal());
            itemViews.setBoundInt(R.id.message, "setTextColor",
                    SonetProvider.StatusesStylesColumns.messages_color.ordinal());

            itemViews.setBoundFloat(R.id.friend, "setTextSize",
                    SonetProvider.StatusesStylesColumns.friend_textsize.ordinal());
            itemViews.setBoundFloat(R.id.created, "setTextSize",
                    SonetProvider.StatusesStylesColumns.created_textsize.ordinal());
            itemViews.setBoundFloat(R.id.message, "setTextSize",
                    SonetProvider.StatusesStylesColumns.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.icon, "setImageBitmap",
                    SonetProvider.StatusesStylesColumns.icon.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.profile_bg, "setImageBitmap",
                    SonetProvider.StatusesStylesColumns.profile_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.friend_bg, "setImageBitmap",
                    SonetProvider.StatusesStylesColumns.friend_bg.ordinal(), 0);

            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_REMOTEVIEWS, itemViews);
        } else {
            BoundRemoteViews itemViews = new BoundRemoteViews(R.layout.widget_item_noprofile);

            Intent i = Sonet.getPackageIntent(SonetService.this, SonetWidget.class)
                    .setAction(LauncherIntent.Action.ACTION_VIEW_CLICK).setData(uri)
                    .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pi = PendingIntent.getBroadcast(SonetService.this, 0, i, 0);

            itemViews.SetBoundOnClickIntent(R.id.item, pi, LauncherIntent.Extra.Scroll.EXTRA_ITEM_POS,
                    SonetProvider.StatusesStylesColumnsNoProfile._id.ordinal());

            itemViews.setBoundCharSequence(R.id.friend_bg_clear, "setText",
                    SonetProvider.StatusesStylesColumnsNoProfile.friend.ordinal(), 0);
            itemViews.setBoundFloat(R.id.friend_bg_clear, "setTextSize",
                    SonetProvider.StatusesStylesColumnsNoProfile.friend_textsize.ordinal());

            itemViews.setBoundCharSequence(R.id.message_bg_clear, "setText",
                    SonetProvider.StatusesStylesColumnsNoProfile.message.ordinal(), 0);
            itemViews.setBoundFloat(R.id.message_bg_clear, "setTextSize",
                    SonetProvider.StatusesStylesColumnsNoProfile.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.status_bg, "setImageBitmap",
                    SonetProvider.StatusesStylesColumnsNoProfile.status_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image_clear, "setImageBitmap",
                    SonetProvider.StatusesStylesColumnsNoProfile.image_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image, "setImageBitmap",
                    SonetProvider.StatusesStylesColumnsNoProfile.image.ordinal(), 0);

            itemViews.setBoundCharSequence(R.id.friend, "setText",
                    SonetProvider.StatusesStylesColumnsNoProfile.friend.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.created, "setText",
                    SonetProvider.StatusesStylesColumnsNoProfile.createdtext.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.message, "setText",
                    SonetProvider.StatusesStylesColumnsNoProfile.message.ordinal(), 0);

            itemViews.setBoundInt(R.id.friend, "setTextColor",
                    SonetProvider.StatusesStylesColumnsNoProfile.friend_color.ordinal());
            itemViews.setBoundInt(R.id.created, "setTextColor",
                    SonetProvider.StatusesStylesColumnsNoProfile.created_color.ordinal());
            itemViews.setBoundInt(R.id.message, "setTextColor",
                    SonetProvider.StatusesStylesColumnsNoProfile.messages_color.ordinal());

            itemViews.setBoundFloat(R.id.friend, "setTextSize",
                    SonetProvider.StatusesStylesColumnsNoProfile.friend_textsize.ordinal());
            itemViews.setBoundFloat(R.id.created, "setTextSize",
                    SonetProvider.StatusesStylesColumnsNoProfile.created_textsize.ordinal());
            itemViews.setBoundFloat(R.id.message, "setTextSize",
                    SonetProvider.StatusesStylesColumnsNoProfile.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.icon, "setImageBitmap",
                    SonetProvider.StatusesStylesColumnsNoProfile.icon.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.friend_bg, "setImageBitmap",
                    SonetProvider.StatusesStylesColumnsNoProfile.friend_bg.ordinal(), 0);

            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_REMOTEVIEWS, itemViews);
        }
        break;
    }
    sendBroadcast(replaceDummy);
}

From source file:com.shafiq.myfeedle.core.MyfeedleService.java

private void buildScrollableWidget(Integer appWidgetId, int scrollableVersion, boolean display_profile) {
    // set widget as scrollable
    Intent replaceDummy = new Intent(LauncherIntent.Action.ACTION_SCROLL_WIDGET_START);
    replaceDummy.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    replaceDummy.putExtra(LauncherIntent.Extra.EXTRA_VIEW_ID, R.id.messages);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_LISTVIEW_LAYOUT_ID, R.layout.widget_listview);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_DATA_PROVIDER_ALLOW_REQUERY, true);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_CHILDREN_CLICKABLE, true);

    //provider//from  ww  w.j  ava2  s . c  o  m
    Uri uri = Uri.withAppendedPath(Statuses_styles.getContentUri(MyfeedleService.this),
            Integer.toString(appWidgetId));
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_DATA_URI, uri.toString());
    String[] projection;
    if (display_profile)
        projection = new String[] { Statuses_styles._ID, Statuses_styles.FRIEND, Statuses_styles.PROFILE,
                Statuses_styles.MESSAGE, Statuses_styles.CREATEDTEXT, Statuses_styles.MESSAGES_COLOR,
                Statuses_styles.FRIEND_COLOR, Statuses_styles.CREATED_COLOR, Statuses_styles.MESSAGES_TEXTSIZE,
                Statuses_styles.FRIEND_TEXTSIZE, Statuses_styles.CREATED_TEXTSIZE, Statuses_styles.STATUS_BG,
                Statuses_styles.ICON, Statuses_styles.PROFILE_BG, Statuses_styles.FRIEND_BG,
                Statuses_styles.IMAGE_BG, Statuses_styles.IMAGE };
    else
        projection = new String[] { Statuses_styles._ID, Statuses_styles.FRIEND, Statuses_styles.PROFILE,
                Statuses_styles.MESSAGE, Statuses_styles.CREATEDTEXT, Statuses_styles.MESSAGES_COLOR,
                Statuses_styles.FRIEND_COLOR, Statuses_styles.CREATED_COLOR, Statuses_styles.MESSAGES_TEXTSIZE,
                Statuses_styles.FRIEND_TEXTSIZE, Statuses_styles.CREATED_TEXTSIZE, Statuses_styles.STATUS_BG,
                Statuses_styles.ICON, Statuses_styles.FRIEND_BG, Statuses_styles.IMAGE_BG,
                Statuses_styles.IMAGE };
    String sortOrder = Statuses_styles.CREATED + " DESC";
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_PROJECTION, projection);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_SORT_ORDER, sortOrder);
    String whereClause = Statuses_styles.WIDGET + "=?";
    String[] selectionArgs = new String[] { Integer.toString(appWidgetId) };
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_SELECTION, whereClause);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_SELECTION_ARGUMENTS, selectionArgs);
    replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_ACTION_VIEW_URI_INDEX,
            MyfeedleProvider.StatusesStylesColumns._id.ordinal());

    switch (scrollableVersion) {
    case 1:
        if (display_profile) {
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_ID, R.layout.widget_item);
            int[] cursorIndices = new int[] { MyfeedleProvider.StatusesStylesColumns.friend.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.message.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.status_bg.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.profile.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.friend.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.createdtext.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.message.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.icon.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.profile_bg.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.friend_bg.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.image_bg.ordinal(),
                    MyfeedleProvider.StatusesStylesColumns.image.ordinal() };
            int[] viewTypes = new int[] { LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB };
            int[] layoutIds = new int[] { R.id.friend_bg_clear, R.id.message_bg_clear, R.id.status_bg,
                    R.id.profile, R.id.friend, R.id.created, R.id.message, R.id.icon, R.id.profile_bg,
                    R.id.friend_bg, R.id.image_clear, R.id.image };
            int[] defaultResource = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            boolean[] clickable = new boolean[] { false, false, true, false, false, false, false, false, false,
                    false, false, false };
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_CURSOR_INDICES, cursorIndices);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_TYPES, viewTypes);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_IDS, layoutIds);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_DEFAULT_RESOURCES, defaultResource);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_CLICKABLE, clickable);
        } else {
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_ID,
                    R.layout.widget_item_noprofile);
            int[] cursorIndices = new int[] { MyfeedleProvider.StatusesStylesColumnsNoProfile.friend.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.message.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.status_bg.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.createdtext.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.message.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.icon.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend_bg.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.image_bg.ordinal(),
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.image.ordinal() };
            int[] viewTypes = new int[] { LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.TEXTVIEW,
                    LauncherIntent.Extra.Scroll.Types.TEXTVIEW, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB, LauncherIntent.Extra.Scroll.Types.IMAGEBLOB,
                    LauncherIntent.Extra.Scroll.Types.IMAGEBLOB };
            int[] layoutIds = new int[] { R.id.friend_bg_clear, R.id.message_bg_clear, R.id.status_bg,
                    R.id.friend, R.id.created, R.id.message, R.id.icon, R.id.friend_bg, R.id.image_clear,
                    R.id.image };
            int[] defaultResource = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            boolean[] clickable = new boolean[] { false, false, true, false, false, false, false, false, false,
                    false };
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_CURSOR_INDICES, cursorIndices);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_TYPES, viewTypes);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_IDS, layoutIds);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_DEFAULT_RESOURCES, defaultResource);
            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.Mapping.EXTRA_VIEW_CLICKABLE, clickable);
        }
        break;
    case 2:
        if (display_profile) {
            BoundRemoteViews itemViews = new BoundRemoteViews(R.layout.widget_item);

            Intent i = Myfeedle.getPackageIntent(MyfeedleService.this, MyfeedleWidget.class)
                    .setAction(LauncherIntent.Action.ACTION_VIEW_CLICK).setData(uri)
                    .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pi = PendingIntent.getBroadcast(MyfeedleService.this, 0, i, 0);

            itemViews.SetBoundOnClickIntent(R.id.item, pi, LauncherIntent.Extra.Scroll.EXTRA_ITEM_POS,
                    MyfeedleProvider.StatusesStylesColumns._id.ordinal());

            itemViews.setBoundCharSequence(R.id.friend_bg_clear, "setText",
                    MyfeedleProvider.StatusesStylesColumns.friend.ordinal(), 0);
            itemViews.setBoundFloat(R.id.friend_bg_clear, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumns.friend_textsize.ordinal());

            itemViews.setBoundCharSequence(R.id.message_bg_clear, "setText",
                    MyfeedleProvider.StatusesStylesColumns.message.ordinal(), 0);
            itemViews.setBoundFloat(R.id.message_bg_clear, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumns.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.status_bg, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumns.status_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image_clear, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumns.image_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumns.image.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.profile, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumns.profile.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.friend, "setText",
                    MyfeedleProvider.StatusesStylesColumns.friend.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.created, "setText",
                    MyfeedleProvider.StatusesStylesColumns.createdtext.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.message, "setText",
                    MyfeedleProvider.StatusesStylesColumns.message.ordinal(), 0);

            itemViews.setBoundInt(R.id.friend, "setTextColor",
                    MyfeedleProvider.StatusesStylesColumns.friend_color.ordinal());
            itemViews.setBoundInt(R.id.created, "setTextColor",
                    MyfeedleProvider.StatusesStylesColumns.created_color.ordinal());
            itemViews.setBoundInt(R.id.message, "setTextColor",
                    MyfeedleProvider.StatusesStylesColumns.messages_color.ordinal());

            itemViews.setBoundFloat(R.id.friend, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumns.friend_textsize.ordinal());
            itemViews.setBoundFloat(R.id.created, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumns.created_textsize.ordinal());
            itemViews.setBoundFloat(R.id.message, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumns.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.icon, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumns.icon.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.profile_bg, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumns.profile_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.friend_bg, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumns.friend_bg.ordinal(), 0);

            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_REMOTEVIEWS, itemViews);
        } else {
            BoundRemoteViews itemViews = new BoundRemoteViews(R.layout.widget_item_noprofile);

            Intent i = Myfeedle.getPackageIntent(MyfeedleService.this, MyfeedleWidget.class)
                    .setAction(LauncherIntent.Action.ACTION_VIEW_CLICK).setData(uri)
                    .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pi = PendingIntent.getBroadcast(MyfeedleService.this, 0, i, 0);

            itemViews.SetBoundOnClickIntent(R.id.item, pi, LauncherIntent.Extra.Scroll.EXTRA_ITEM_POS,
                    MyfeedleProvider.StatusesStylesColumnsNoProfile._id.ordinal());

            itemViews.setBoundCharSequence(R.id.friend_bg_clear, "setText",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend.ordinal(), 0);
            itemViews.setBoundFloat(R.id.friend_bg_clear, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend_textsize.ordinal());

            itemViews.setBoundCharSequence(R.id.message_bg_clear, "setText",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.message.ordinal(), 0);
            itemViews.setBoundFloat(R.id.message_bg_clear, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.status_bg, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.status_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image_clear, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.image_bg.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.image, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.image.ordinal(), 0);

            itemViews.setBoundCharSequence(R.id.friend, "setText",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.created, "setText",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.createdtext.ordinal(), 0);
            itemViews.setBoundCharSequence(R.id.message, "setText",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.message.ordinal(), 0);

            itemViews.setBoundInt(R.id.friend, "setTextColor",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend_color.ordinal());
            itemViews.setBoundInt(R.id.created, "setTextColor",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.created_color.ordinal());
            itemViews.setBoundInt(R.id.message, "setTextColor",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.messages_color.ordinal());

            itemViews.setBoundFloat(R.id.friend, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend_textsize.ordinal());
            itemViews.setBoundFloat(R.id.created, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.created_textsize.ordinal());
            itemViews.setBoundFloat(R.id.message, "setTextSize",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.messages_textsize.ordinal());

            itemViews.setBoundBitmap(R.id.icon, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.icon.ordinal(), 0);

            itemViews.setBoundBitmap(R.id.friend_bg, "setImageBitmap",
                    MyfeedleProvider.StatusesStylesColumnsNoProfile.friend_bg.ordinal(), 0);

            replaceDummy.putExtra(LauncherIntent.Extra.Scroll.EXTRA_ITEM_LAYOUT_REMOTEVIEWS, itemViews);
        }
        break;
    }
    sendBroadcast(replaceDummy);
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public void scheduleLocalNotification(LocalNotification notif, long firstTime, int repeat) {

    final Intent notificationIntent = new Intent(getContext(), LocalNotificationPublisher.class);
    notificationIntent.setAction(getContext().getApplicationInfo().packageName + "." + notif.getId());
    notificationIntent.putExtra(LocalNotificationPublisher.NOTIFICATION, createBundleFromNotification(notif));

    Intent contentIntent = new Intent();
    if (getActivity() != null) {
        contentIntent.setComponent(getActivity().getComponentName());
    }//from  w  ww  .j  ava2s  .  co m
    contentIntent.putExtra("LocalNotificationID", notif.getId());

    if (BACKGROUND_FETCH_NOTIFICATION_ID.equals(notif.getId()) && getBackgroundFetchListener() != null) {
        Context context = AndroidNativeUtil.getContext();

        Intent intent = new Intent(context, BackgroundFetchHandler.class);
        //there is an bug that causes this to not to workhttps://code.google.com/p/android/issues/detail?id=81812
        //intent.putExtra("backgroundClass", getBackgroundLocationListener().getName());
        //an ugly workaround to the putExtra bug 
        intent.setData(
                Uri.parse("http://codenameone.com/a?" + getBackgroundFetchListener().getClass().getName()));
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notificationIntent.putExtra(LocalNotificationPublisher.BACKGROUND_FETCH_INTENT, pendingIntent);

    } else {
        contentIntent.setData(
                Uri.parse("http://codenameone.com/a?LocalNotificationID=" + Uri.encode(notif.getId())));
    }
    PendingIntent pendingContentIntent = PendingIntent.getActivity(getContext(), 0, contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notificationIntent.putExtra(LocalNotificationPublisher.NOTIFICATION_INTENT, pendingContentIntent);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    if (BACKGROUND_FETCH_NOTIFICATION_ID.equals(notif.getId())) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime,
                getPreferredBackgroundFetchInterval() * 1000, pendingIntent);
    } else {
        if (repeat == LocalNotification.REPEAT_NONE) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, firstTime, pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_MINUTE) {

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 60 * 1000, pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_HOUR) {

            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTime,
                    AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_DAY) {

            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTime, AlarmManager.INTERVAL_DAY,
                    pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_WEEK) {

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, AlarmManager.INTERVAL_DAY * 7,
                    pendingIntent);

        }
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public void cancelLocalNotification(String notificationId) {
    Intent notificationIntent = new Intent(getContext(), LocalNotificationPublisher.class);
    notificationIntent.setAction(getContext().getApplicationInfo().packageName + "." + notificationId);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);//from   www  .ja v a  2s  .  c  om
}