Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 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 | |
Cheuksan Wang | b72eb97 | 2014-11-21 16:54:02 -0800 | [diff] [blame] | 17 | package android.service.carrier; |
Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 18 | |
Abhijith Shastry | d7a305b | 2014-11-14 10:29:21 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * A parcelable list of PDUs representing contents of a possibly multi-part SMS. |
| 28 | */ |
| 29 | public final class MessagePdu implements Parcelable { |
| 30 | private static final int NULL_LENGTH = -1; |
| 31 | |
| 32 | private final List<byte[]> mPduList; |
| 33 | |
| 34 | /** |
Abhijith Shastry | d7a305b | 2014-11-14 10:29:21 -0800 | [diff] [blame] | 35 | * Constructs a MessagePdu with the list of message PDUs. |
| 36 | * |
Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 37 | * @param pduList the list of message PDUs |
| 38 | */ |
Abhijith Shastry | d7a305b | 2014-11-14 10:29:21 -0800 | [diff] [blame] | 39 | public MessagePdu(@NonNull List<byte[]> pduList) { |
| 40 | if (pduList == null || pduList.contains(null)) { |
| 41 | throw new IllegalArgumentException("pduList must not be null or contain nulls"); |
| 42 | } |
Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 43 | mPduList = pduList; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the contents of a possibly multi-part SMS. |
| 48 | * |
| 49 | * @return the list of PDUs |
| 50 | */ |
Abhijith Shastry | d7a305b | 2014-11-14 10:29:21 -0800 | [diff] [blame] | 51 | public @NonNull List<byte[]> getPdus() { |
Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 52 | return mPduList; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public int describeContents() { |
| 57 | return 0; |
| 58 | } |
| 59 | |
Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 60 | @Override |
| 61 | public void writeToParcel(Parcel dest, int flags) { |
| 62 | if (mPduList == null) { |
| 63 | dest.writeInt(NULL_LENGTH); |
| 64 | } else { |
| 65 | dest.writeInt(mPduList.size()); |
| 66 | for (byte[] messagePdu : mPduList) { |
| 67 | dest.writeByteArray(messagePdu); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Constructs a {@link MessagePdu} from a {@link Parcel}. |
| 74 | */ |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 75 | public static final @android.annotation.NonNull Parcelable.Creator<MessagePdu> CREATOR |
Cheuksan Wang | f9c50c4 | 2014-10-21 15:58:23 -0700 | [diff] [blame] | 76 | = new Parcelable.Creator<MessagePdu>() { |
| 77 | @Override |
| 78 | public MessagePdu createFromParcel(Parcel source) { |
| 79 | int size = source.readInt(); |
| 80 | List<byte[]> pduList; |
| 81 | if (size == NULL_LENGTH) { |
| 82 | pduList = null; |
| 83 | } else { |
| 84 | pduList = new ArrayList<>(size); |
| 85 | for (int i = 0; i < size; i++) { |
| 86 | pduList.add(source.createByteArray()); |
| 87 | } |
| 88 | } |
| 89 | return new MessagePdu(pduList); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public MessagePdu[] newArray(int size) { |
| 94 | return new MessagePdu[size]; |
| 95 | } |
| 96 | }; |
| 97 | } |