• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.android.car.messenger.impl.datamodels.util;
18 
19 import static android.provider.BaseColumns._ID;
20 
21 import android.database.Cursor;
22 import android.provider.Telephony.Sms;
23 
24 import androidx.annotation.NonNull;
25 
26 import java.time.Instant;
27 
28 /** SMS Utils for parsing SMS Telephony Content */
29 class SmsUtils {
30 
SmsUtils()31     SmsUtils() {}
32 
33     /**
34      * Returns the parsed sms result as a {@link MmsSmsMessage}
35      *
36      * @throws IllegalArgumentException if desired columns are missing.
37      * @see CursorUtils#CONTENT_CONVERSATION_PROJECTION
38      */
39     @NonNull
parseSms(@onNull Cursor cursor)40     static MmsSmsMessage parseSms(@NonNull Cursor cursor) {
41         int threadIdIndex = cursor.getColumnIndex(Sms.THREAD_ID);
42         int recipientsIndex = cursor.getColumnIndex(Sms.ADDRESS);
43         int bodyIndex = cursor.getColumnIndex(Sms.BODY);
44         int subscriptionIdIndex = cursor.getColumnIndex(Sms.SUBSCRIPTION_ID);
45         int dateIndex = cursor.getColumnIndex(Sms.DATE);
46         int typeIndex = cursor.getColumnIndex(Sms.TYPE);
47         int readIndex = cursor.getColumnIndex(Sms.READ);
48 
49         MmsSmsMessage message = new MmsSmsMessage();
50         message.mThreadId = cursor.getInt(threadIdIndex);
51         message.mPhoneNumber = cursor.getString(recipientsIndex);
52         message.mBody = cursor.getString(bodyIndex);
53         message.mSubscriptionId = cursor.getInt(subscriptionIdIndex);
54         message.mType = cursor.getInt(typeIndex);
55         message.mDate = Instant.ofEpochMilli(cursor.getLong(dateIndex));
56         message.mRead = cursor.getInt(readIndex) == 1;
57         message.mId = cursor.getString(cursor.getColumnIndex(_ID));
58         return message;
59     }
60 }
61