1 /* 2 * Copyright (C) 2019 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 android.telephony.ims; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.WorkerThread; 22 import android.content.Context; 23 import android.net.Uri; 24 25 import java.util.List; 26 27 /** 28 * RcsMessageStore is the application interface to RcsProvider and provides access methods to 29 * RCS related database tables. 30 * 31 * @hide 32 */ 33 public class RcsMessageStore { 34 RcsControllerCall mRcsControllerCall; 35 RcsMessageStore(Context context)36 RcsMessageStore(Context context) { 37 mRcsControllerCall = new RcsControllerCall(context); 38 } 39 40 /** 41 * Returns the first chunk of existing {@link RcsThread}s in the common storage. 42 * 43 * @param queryParameters Parameters to specify to return a subset of all RcsThreads. 44 * Passing a value of null will return all threads. 45 * @throws RcsMessageStoreException if the query could not be completed on the storage 46 */ 47 @WorkerThread 48 @NonNull getRcsThreads(@ullable RcsThreadQueryParams queryParameters)49 public RcsThreadQueryResult getRcsThreads(@Nullable RcsThreadQueryParams queryParameters) 50 throws RcsMessageStoreException { 51 return new RcsThreadQueryResult(mRcsControllerCall, 52 mRcsControllerCall.call( 53 (iRcs, callingPackage) -> iRcs.getRcsThreads(queryParameters, 54 callingPackage))); 55 } 56 57 /** 58 * Returns the next chunk of {@link RcsThread}s in the common storage. 59 * 60 * @param continuationToken A token to continue the query to get the next chunk. This is 61 * obtained through {@link RcsThreadQueryResult#getContinuationToken}. 62 * @throws RcsMessageStoreException if the query could not be completed on the storage 63 */ 64 @WorkerThread 65 @NonNull getRcsThreads(@onNull RcsQueryContinuationToken continuationToken)66 public RcsThreadQueryResult getRcsThreads(@NonNull RcsQueryContinuationToken continuationToken) 67 throws RcsMessageStoreException { 68 return new RcsThreadQueryResult(mRcsControllerCall, 69 mRcsControllerCall.call( 70 (iRcs, callingPackage) -> iRcs.getRcsThreadsWithToken(continuationToken, 71 callingPackage))); 72 } 73 74 /** 75 * Returns the first chunk of existing {@link RcsParticipant}s in the common storage. 76 * 77 * @param queryParameters Parameters to specify to return a subset of all RcsParticipants. 78 * Passing a value of null will return all participants. 79 * @throws RcsMessageStoreException if the query could not be completed on the storage 80 */ 81 @WorkerThread 82 @NonNull getRcsParticipants( @ullable RcsParticipantQueryParams queryParameters)83 public RcsParticipantQueryResult getRcsParticipants( 84 @Nullable RcsParticipantQueryParams queryParameters) 85 throws RcsMessageStoreException { 86 return new RcsParticipantQueryResult(mRcsControllerCall, 87 mRcsControllerCall.call( 88 (iRcs, callingPackage) -> iRcs.getParticipants(queryParameters, 89 callingPackage))); 90 } 91 92 /** 93 * Returns the next chunk of {@link RcsParticipant}s in the common storage. 94 * 95 * @param continuationToken A token to continue the query to get the next chunk. This is 96 * obtained through 97 * {@link RcsParticipantQueryResult#getContinuationToken} 98 * @throws RcsMessageStoreException if the query could not be completed on the storage 99 */ 100 @WorkerThread 101 @NonNull getRcsParticipants( @onNull RcsQueryContinuationToken continuationToken)102 public RcsParticipantQueryResult getRcsParticipants( 103 @NonNull RcsQueryContinuationToken continuationToken) 104 throws RcsMessageStoreException { 105 return new RcsParticipantQueryResult(mRcsControllerCall, 106 mRcsControllerCall.call( 107 (iRcs, callingPackage) -> iRcs.getParticipantsWithToken(continuationToken, 108 callingPackage))); 109 } 110 111 /** 112 * Returns the first chunk of existing {@link RcsMessage}s in the common storage. 113 * 114 * @param queryParams Parameters to specify to return a subset of all RcsMessages. 115 * Passing a value of null will return all messages. 116 * @throws RcsMessageStoreException if the query could not be completed on the storage 117 */ 118 @WorkerThread 119 @NonNull getRcsMessages( @ullable RcsMessageQueryParams queryParams)120 public RcsMessageQueryResult getRcsMessages( 121 @Nullable RcsMessageQueryParams queryParams) throws RcsMessageStoreException { 122 return new RcsMessageQueryResult(mRcsControllerCall, 123 mRcsControllerCall.call( 124 (iRcs, callingPackage) -> iRcs.getMessages(queryParams, callingPackage))); 125 } 126 127 /** 128 * Returns the next chunk of {@link RcsMessage}s in the common storage. 129 * 130 * @param continuationToken A token to continue the query to get the next chunk. This is 131 * obtained through {@link RcsMessageQueryResult#getContinuationToken} 132 * @throws RcsMessageStoreException if the query could not be completed on the storage 133 */ 134 @WorkerThread 135 @NonNull getRcsMessages( @onNull RcsQueryContinuationToken continuationToken)136 public RcsMessageQueryResult getRcsMessages( 137 @NonNull RcsQueryContinuationToken continuationToken) throws RcsMessageStoreException { 138 return new RcsMessageQueryResult(mRcsControllerCall, 139 mRcsControllerCall.call( 140 (iRcs, callingPackage) -> iRcs.getMessagesWithToken(continuationToken, 141 callingPackage))); 142 } 143 144 /** 145 * Returns the first chunk of existing {@link RcsEvent}s in the common storage. 146 * 147 * @param queryParams Parameters to specify to return a subset of all RcsEvents. 148 * Passing a value of null will return all events. 149 * @throws RcsMessageStoreException if the query could not be completed on the storage 150 */ 151 @WorkerThread 152 @NonNull getRcsEvents( @ullable RcsEventQueryParams queryParams)153 public RcsEventQueryResult getRcsEvents( 154 @Nullable RcsEventQueryParams queryParams) throws RcsMessageStoreException { 155 return mRcsControllerCall.call( 156 (iRcs, callingPackage) -> iRcs.getEvents(queryParams, callingPackage)) 157 .getRcsEventQueryResult(mRcsControllerCall); 158 } 159 160 /** 161 * Returns the next chunk of {@link RcsEvent}s in the common storage. 162 * 163 * @param continuationToken A token to continue the query to get the next chunk. This is 164 * obtained through {@link RcsEventQueryResult#getContinuationToken}. 165 * @throws RcsMessageStoreException if the query could not be completed on the storage 166 */ 167 @WorkerThread 168 @NonNull getRcsEvents( @onNull RcsQueryContinuationToken continuationToken)169 public RcsEventQueryResult getRcsEvents( 170 @NonNull RcsQueryContinuationToken continuationToken) throws RcsMessageStoreException { 171 return mRcsControllerCall.call( 172 (iRcs, callingPackage) -> iRcs.getEventsWithToken(continuationToken, 173 callingPackage)) 174 .getRcsEventQueryResult(mRcsControllerCall); 175 } 176 177 /** 178 * Persists an {@link RcsEvent} to common storage. 179 * 180 * @param rcsEvent The {@link RcsEvent} to persist into storage. 181 * @throws RcsMessageStoreException if the query could not be completed on the storage 182 * @see RcsGroupThreadNameChangedEvent 183 * @see RcsGroupThreadIconChangedEvent 184 * @see RcsGroupThreadParticipantJoinedEvent 185 * @see RcsGroupThreadParticipantLeftEvent 186 * @see RcsParticipantAliasChangedEvent 187 */ 188 @WorkerThread 189 @NonNull persistRcsEvent(RcsEvent rcsEvent)190 public void persistRcsEvent(RcsEvent rcsEvent) throws RcsMessageStoreException { 191 rcsEvent.persist(mRcsControllerCall); 192 } 193 194 /** 195 * Creates a new 1 to 1 thread with the given participant and persists it in the storage. 196 * 197 * @param recipient The {@link RcsParticipant} that will receive the messages in this thread. 198 * @return The newly created {@link Rcs1To1Thread} 199 * @throws RcsMessageStoreException if the thread could not be persisted in the storage 200 */ 201 @WorkerThread 202 @NonNull createRcs1To1Thread(@onNull RcsParticipant recipient)203 public Rcs1To1Thread createRcs1To1Thread(@NonNull RcsParticipant recipient) 204 throws RcsMessageStoreException { 205 return new Rcs1To1Thread( 206 mRcsControllerCall, 207 mRcsControllerCall.call( 208 (iRcs, callingPackage) -> iRcs.createRcs1To1Thread(recipient.getId(), 209 callingPackage))); 210 } 211 212 /** 213 * Creates a new group thread with the given participants and persists it in the storage. 214 * 215 * @throws RcsMessageStoreException if the thread could not be persisted in the storage 216 */ 217 @WorkerThread 218 @NonNull createGroupThread(@ullable List<RcsParticipant> recipients, @Nullable String groupName, @Nullable Uri groupIcon)219 public RcsGroupThread createGroupThread(@Nullable List<RcsParticipant> recipients, 220 @Nullable String groupName, @Nullable Uri groupIcon) throws RcsMessageStoreException { 221 int[] recipientIds = null; 222 if (recipients != null) { 223 recipientIds = new int[recipients.size()]; 224 225 for (int i = 0; i < recipients.size(); i++) { 226 recipientIds[i] = recipients.get(i).getId(); 227 } 228 } 229 230 int[] finalRecipientIds = recipientIds; 231 232 int threadId = mRcsControllerCall.call( 233 (iRcs, callingPackage) -> iRcs.createGroupThread(finalRecipientIds, groupName, 234 groupIcon, callingPackage)); 235 236 return new RcsGroupThread(mRcsControllerCall, threadId); 237 } 238 239 /** 240 * Delete the given {@link RcsThread} from the storage. 241 * 242 * @param thread The thread to be deleted. 243 * @throws RcsMessageStoreException if the thread could not be deleted from the storage 244 */ 245 @WorkerThread deleteThread(@onNull RcsThread thread)246 public void deleteThread(@NonNull RcsThread thread) throws RcsMessageStoreException { 247 if (thread == null) { 248 return; 249 } 250 251 boolean isDeleteSucceeded = mRcsControllerCall.call( 252 (iRcs, callingPackage) -> iRcs.deleteThread(thread.getThreadId(), 253 thread.getThreadType(), callingPackage)); 254 255 if (!isDeleteSucceeded) { 256 throw new RcsMessageStoreException("Could not delete RcsThread"); 257 } 258 } 259 260 /** 261 * Creates a new participant and persists it in the storage. 262 * 263 * @param canonicalAddress The defining address (e.g. phone number) of the participant. 264 * @param alias The RCS alias for the participant. 265 * @throws RcsMessageStoreException if the participant could not be created on the storage 266 */ 267 @WorkerThread 268 @NonNull createRcsParticipant(String canonicalAddress, @Nullable String alias)269 public RcsParticipant createRcsParticipant(String canonicalAddress, @Nullable String alias) 270 throws RcsMessageStoreException { 271 return new RcsParticipant(mRcsControllerCall, mRcsControllerCall.call( 272 (iRcs, callingPackage) -> iRcs.createRcsParticipant(canonicalAddress, alias, 273 callingPackage))); 274 } 275 } 276