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.Arrays; 21 import java.util.Collections; 22 import java.util.List; 23 import java.util.concurrent.ThreadLocalRandom; 24 25 /** 26 * A simple class that denotes unread conversations and messages. In a real world application, 27 * this would be replaced by a content provider that actually gets the unread messages to be 28 * shown to the user. 29 */ 30 public class Conversations { 31 32 /** 33 * Set of strings used as messages by the sample. 34 */ 35 private static final String[] MESSAGES = new String[]{ 36 "Are you at home?", 37 "Can you give me a call?", 38 "Hey yt?", 39 "Don't forget to get some milk on your way back home", 40 "Is that project done?", 41 "Did you finish the Messaging app yet?" 42 }; 43 44 /** 45 * Senders of the said messages. 46 */ 47 private static final String[] PARTICIPANTS = new String[]{ 48 "John Smith", 49 "Robert Lawrence", 50 "James Smith", 51 "Jane Doe" 52 }; 53 54 static class Conversation { 55 56 private final int conversationId; 57 58 private final String participantName; 59 60 /** 61 * A given conversation can have a single or multiple messages. 62 * Note that the messages are sorted from *newest* to *oldest* 63 */ 64 private final List<String> messages; 65 66 private final long timestamp; 67 68 public Conversation(int conversationId, String participantName, 69 List<String> messages) { 70 this.conversationId = conversationId; 71 this.participantName = participantName; 72 this.messages = messages == null ? Collections.<String>emptyList() : messages; 73 this.timestamp = System.currentTimeMillis(); 74 } 75 76 public int getConversationId() { 77 return conversationId; 78 } 79 80 public String getParticipantName() { 81 return participantName; 82 } 83 84 public List<String> getMessages() { 85 return messages; 86 } 87 88 public long getTimestamp() { 89 return timestamp; 90 } 91 92 public String toString() { 93 return "[Conversation: conversationId=" + conversationId + 94 ", participantName=" + participantName + 95 ", messages=" + messages + 96 ", timestamp=" + timestamp + "]"; 97 } 98 } 99 100 private Conversations() { 101 } 102 103 public static Conversation[] getUnreadConversations(int howManyConversations, 104 int messagesPerConversation) { 105 Conversation[] conversations = new Conversation[howManyConversations]; 106 for (int i = 0; i < howManyConversations; i++) { 107 conversations[i] = new Conversation( 108 ThreadLocalRandom.current().nextInt(), 109 name(), makeMessages(messagesPerConversation)); 110 } 111 return conversations; 112 } 113 114 private static List<String> makeMessages(int messagesPerConversation) { 115 int maxLen = MESSAGES.length; 116 List<String> messages = new ArrayList<>(messagesPerConversation); 117 for (int i = 0; i < messagesPerConversation; i++) { 118 messages.add(MESSAGES[ThreadLocalRandom.current().nextInt(0, maxLen)]); 119 } 120 return messages; 121 } 122 123 private static String name() { 124 return PARTICIPANTS[ThreadLocalRandom.current().nextInt(0, PARTICIPANTS.length)]; 125 } 126 } 127