1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.messagingservice; 18 19 import java.util.ArrayList; 20 import java.util.Collections; 21 import java.util.List; 22 import java.util.concurrent.ThreadLocalRandom; 23 24 /** 25 * A simple class that denotes unread conversations and messages. In a real world application, 26 * this would be replaced by a content provider that actually gets the unread messages to be 27 * shown to the user. 28 */ 29 public class Conversations { 30 31 /** 32 * Set of strings used as messages by the sample. 33 */ 34 private static final String[] MESSAGES = new String[]{ 35 "Are you at home?", 36 "Can you give me a call?", 37 "Hey yt?", 38 "Don't forget to get some milk on your way back home", 39 "Is that project done?", 40 "Did you finish the Messaging app yet?" 41 }; 42 43 /** 44 * Senders of the said messages. 45 */ 46 private static final String[] PARTICIPANTS = new String[]{ 47 "John Smith", 48 "Robert Lawrence", 49 "James Smith", 50 "Jane Doe" 51 }; 52 53 static class Conversation { 54 55 private final int conversationId; 56 57 private final String participantName; 58 59 /** 60 * A given conversation can have a single or multiple messages. 61 * Note that the messages are sorted from *newest* to *oldest* 62 */ 63 private final List<String> messages; 64 65 private final long timestamp; 66 Conversation(int conversationId, String participantName, List<String> messages)67 public Conversation(int conversationId, String participantName, 68 List<String> messages) { 69 this.conversationId = conversationId; 70 this.participantName = participantName; 71 this.messages = messages == null ? Collections.<String>emptyList() : messages; 72 this.timestamp = System.currentTimeMillis(); 73 } 74 getConversationId()75 public int getConversationId() { 76 return conversationId; 77 } 78 getParticipantName()79 public String getParticipantName() { 80 return participantName; 81 } 82 getMessages()83 public List<String> getMessages() { 84 return messages; 85 } 86 getTimestamp()87 public long getTimestamp() { 88 return timestamp; 89 } 90 toString()91 public String toString() { 92 return "[Conversation: conversationId=" + conversationId + 93 ", participantName=" + participantName + 94 ", messages=" + messages + 95 ", timestamp=" + timestamp + "]"; 96 } 97 } 98 Conversations()99 private Conversations() { 100 } 101 getUnreadConversations(int howManyConversations, int messagesPerConversation)102 public static Conversation[] getUnreadConversations(int howManyConversations, 103 int messagesPerConversation) { 104 Conversation[] conversations = new Conversation[howManyConversations]; 105 for (int i = 0; i < howManyConversations; i++) { 106 conversations[i] = new Conversation( 107 ThreadLocalRandom.current().nextInt(), 108 name(), makeMessages(messagesPerConversation)); 109 } 110 return conversations; 111 } 112 makeMessages(int messagesPerConversation)113 private static List<String> makeMessages(int messagesPerConversation) { 114 int maxLen = MESSAGES.length; 115 List<String> messages = new ArrayList<>(messagesPerConversation); 116 for (int i = 0; i < messagesPerConversation; i++) { 117 messages.add(MESSAGES[ThreadLocalRandom.current().nextInt(0, maxLen)]); 118 } 119 return messages; 120 } 121 name()122 private static String name() { 123 return PARTICIPANTS[ThreadLocalRandom.current().nextInt(0, PARTICIPANTS.length)]; 124 } 125 } 126