Java Code Examples for org.telegram.tgnet.TLRPC#TL_messageMediaPoll

The following examples show how to use org.telegram.tgnet.TLRPC#TL_messageMediaPoll . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChatAttachAlertPollLayout.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
void onMenuItemClick(int id) {
    if (id == done_button) {
        if (quizPoll && parentAlert.doneItem.getAlpha() != 1.0f) {
            int checksCount = 0;
            for (int a = 0; a < answersChecks.length; a++) {
                if (!TextUtils.isEmpty(getFixedString(answers[a])) && answersChecks[a]) {
                    checksCount++;
                }
            }
            if (checksCount <= 0) {
                showQuizHint();
            }
            return;
        }
        TLRPC.TL_messageMediaPoll poll = new TLRPC.TL_messageMediaPoll();
        poll.poll = new TLRPC.TL_poll();
        poll.poll.multiple_choice = multipleChoise;
        poll.poll.quiz = quizPoll;
        poll.poll.public_voters = !anonymousPoll;
        poll.poll.question = getFixedString(questionString).toString();
        SerializedData serializedData = new SerializedData(10);
        for (int a = 0; a < answers.length; a++) {
            if (TextUtils.isEmpty(getFixedString(answers[a]))) {
                continue;
            }
            TLRPC.TL_pollAnswer answer = new TLRPC.TL_pollAnswer();
            answer.text = getFixedString(answers[a]).toString();
            answer.option = new byte[1];
            answer.option[0] = (byte) (48 + poll.poll.answers.size());
            poll.poll.answers.add(answer);
            if ((multipleChoise || quizPoll) && answersChecks[a]) {
                serializedData.writeByte(answer.option[0]);
            }
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("answers", Utilities.bytesToHex(serializedData.toByteArray()));
        poll.results = new TLRPC.TL_pollResults();
        CharSequence solution = getFixedString(solutionString);
        if (solution != null) {
            poll.results.solution = solution.toString();
            CharSequence[] message = new CharSequence[]{solution};
            ArrayList<TLRPC.MessageEntity> entities = MediaDataController.getInstance(parentAlert.currentAccount).getEntities(message, true);
            if (entities != null && !entities.isEmpty()) {
                poll.results.solution_entities = entities;
            }
            if (!TextUtils.isEmpty(poll.results.solution)) {
                poll.results.flags |= 16;
            }
        }
        ChatActivity chatActivity = (ChatActivity) parentAlert.baseFragment;
        if (chatActivity.isInScheduleMode()) {
            AlertsCreator.createScheduleDatePickerDialog(chatActivity.getParentActivity(), chatActivity.getDialogId(), (notify, scheduleDate) -> {
                delegate.sendPoll(poll, params, notify, scheduleDate);
                parentAlert.dismiss();
            });
        } else {
            delegate.sendPoll(poll, params, true, 0);
            parentAlert.dismiss();
        }
    }
}
 
Example 2
Source File: PollVotesAlert.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private void updateButtons() {
    votesPercents.clear();
    int restPercent = 100;
    boolean hasDifferent = false;
    int previousPercent = 0;
    TLRPC.TL_messageMediaPoll media = (TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media;
    ArrayList<Button> sortedPollButtons = new ArrayList<>();
    int maxVote = 0;

    for (int a = 0, N = voters.size(); a < N; a++) {
        VotesList list = voters.get(a);
        Button button = new Button();
        sortedPollButtons.add(button);
        votesPercents.put(list, button);
        if (!media.results.results.isEmpty()) {
            for (int b = 0, N2 = media.results.results.size(); b < N2; b++) {
                TLRPC.TL_pollAnswerVoters answer = media.results.results.get(b);
                if (Arrays.equals(list.option, answer.option)) {
                    button.votesCount = answer.voters;
                    button.decimal = 100 * (answer.voters / (float) media.results.total_voters);
                    button.percent = (int) button.decimal;
                    button.decimal -= button.percent;

                    if (previousPercent == 0) {
                        previousPercent = button.percent;
                    } else if (button.percent != 0 && previousPercent != button.percent) {
                        hasDifferent = true;
                    }
                    restPercent -= button.percent;
                    maxVote = Math.max(button.percent, maxVote);
                    break;
                }
            }
        }
    }

    if (hasDifferent && restPercent != 0) {
        Collections.sort(sortedPollButtons, (o1, o2) -> {
            if (o1.decimal > o2.decimal) {
                return -1;
            } else if (o1.decimal < o2.decimal) {
                return 1;
            }
            return 0;
        });
        for (int a = 0, N = Math.min(restPercent, sortedPollButtons.size()); a < N; a++) {
            sortedPollButtons.get(a).percent += 1;
        }
    }
}
 
Example 3
Source File: ChatAttachAlertPollLayout.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
void onMenuItemClick(int id) {
    if (id == done_button) {
        if (quizPoll && parentAlert.doneItem.getAlpha() != 1.0f) {
            int checksCount = 0;
            for (int a = 0; a < answersChecks.length; a++) {
                if (!TextUtils.isEmpty(getFixedString(answers[a])) && answersChecks[a]) {
                    checksCount++;
                }
            }
            if (checksCount <= 0) {
                showQuizHint();
            }
            return;
        }
        TLRPC.TL_messageMediaPoll poll = new TLRPC.TL_messageMediaPoll();
        poll.poll = new TLRPC.TL_poll();
        poll.poll.multiple_choice = multipleChoise;
        poll.poll.quiz = quizPoll;
        poll.poll.public_voters = !anonymousPoll;
        poll.poll.question = getFixedString(questionString).toString();
        SerializedData serializedData = new SerializedData(10);
        for (int a = 0; a < answers.length; a++) {
            if (TextUtils.isEmpty(getFixedString(answers[a]))) {
                continue;
            }
            TLRPC.TL_pollAnswer answer = new TLRPC.TL_pollAnswer();
            answer.text = getFixedString(answers[a]).toString();
            answer.option = new byte[1];
            answer.option[0] = (byte) (48 + poll.poll.answers.size());
            poll.poll.answers.add(answer);
            if ((multipleChoise || quizPoll) && answersChecks[a]) {
                serializedData.writeByte(answer.option[0]);
            }
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("answers", Utilities.bytesToHex(serializedData.toByteArray()));
        poll.results = new TLRPC.TL_pollResults();
        CharSequence solution = getFixedString(solutionString);
        if (solution != null) {
            poll.results.solution = solution.toString();
            CharSequence[] message = new CharSequence[]{solution};
            ArrayList<TLRPC.MessageEntity> entities = MediaDataController.getInstance(parentAlert.currentAccount).getEntities(message, true);
            if (entities != null && !entities.isEmpty()) {
                poll.results.solution_entities = entities;
            }
            if (!TextUtils.isEmpty(poll.results.solution)) {
                poll.results.flags |= 16;
            }
        }
        ChatActivity chatActivity = (ChatActivity) parentAlert.baseFragment;
        if (chatActivity.isInScheduleMode()) {
            AlertsCreator.createScheduleDatePickerDialog(chatActivity.getParentActivity(), chatActivity.getDialogId(), (notify, scheduleDate) -> {
                delegate.sendPoll(poll, params, notify, scheduleDate);
                parentAlert.dismiss();
            });
        } else {
            delegate.sendPoll(poll, params, true, 0);
            parentAlert.dismiss();
        }
    }
}
 
Example 4
Source File: PollVotesAlert.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private void updateButtons() {
    votesPercents.clear();
    int restPercent = 100;
    boolean hasDifferent = false;
    int previousPercent = 0;
    TLRPC.TL_messageMediaPoll media = (TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media;
    ArrayList<Button> sortedPollButtons = new ArrayList<>();
    int maxVote = 0;

    for (int a = 0, N = voters.size(); a < N; a++) {
        VotesList list = voters.get(a);
        Button button = new Button();
        sortedPollButtons.add(button);
        votesPercents.put(list, button);
        if (!media.results.results.isEmpty()) {
            for (int b = 0, N2 = media.results.results.size(); b < N2; b++) {
                TLRPC.TL_pollAnswerVoters answer = media.results.results.get(b);
                if (Arrays.equals(list.option, answer.option)) {
                    button.votesCount = answer.voters;
                    button.decimal = 100 * (answer.voters / (float) media.results.total_voters);
                    button.percent = (int) button.decimal;
                    button.decimal -= button.percent;

                    if (previousPercent == 0) {
                        previousPercent = button.percent;
                    } else if (button.percent != 0 && previousPercent != button.percent) {
                        hasDifferent = true;
                    }
                    restPercent -= button.percent;
                    maxVote = Math.max(button.percent, maxVote);
                    break;
                }
            }
        }
    }

    if (hasDifferent && restPercent != 0) {
        Collections.sort(sortedPollButtons, (o1, o2) -> {
            if (o1.decimal > o2.decimal) {
                return -1;
            } else if (o1.decimal < o2.decimal) {
                return 1;
            }
            return 0;
        });
        for (int a = 0, N = Math.min(restPercent, sortedPollButtons.size()); a < N; a++) {
            sortedPollButtons.get(a).percent += 1;
        }
    }
}
 
Example 5
Source File: PollCreateActivity.java    From Telegram-FOSS with GNU General Public License v2.0 votes vote down vote up
void sendPoll(TLRPC.TL_messageMediaPoll poll, HashMap<String, String> params, boolean notify, int scheduleDate); 
Example 6
Source File: ChatAttachAlertPollLayout.java    From Telegram-FOSS with GNU General Public License v2.0 votes vote down vote up
void sendPoll(TLRPC.TL_messageMediaPoll poll, HashMap<String, String> params, boolean notify, int scheduleDate); 
Example 7
Source File: PollCreateActivity.java    From Telegram with GNU General Public License v2.0 votes vote down vote up
void sendPoll(TLRPC.TL_messageMediaPoll poll, HashMap<String, String> params, boolean notify, int scheduleDate); 
Example 8
Source File: ChatAttachAlertPollLayout.java    From Telegram with GNU General Public License v2.0 votes vote down vote up
void sendPoll(TLRPC.TL_messageMediaPoll poll, HashMap<String, String> params, boolean notify, int scheduleDate);