1/*
2 * Copyright (C) 2016 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 */
16package android.telephony;
17
18import android.content.Context;
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import android.telecom.PhoneAccountHandle;
23import android.telephony.VisualVoicemailService.VisualVoicemailTask;
24import java.util.Collections;
25import java.util.List;
26
27/**
28 * Class to represent various settings for the visual voicemail SMS filter. When the filter is
29 * enabled, incoming SMS matching the generalized OMTP format:
30 *
31 * <p>[clientPrefix]:[prefix]:([key]=[value];)*
32 *
33 * <p>will be regarded as a visual voicemail SMS, and removed before reaching the SMS provider. The
34 * {@link VisualVoicemailService} in the current default dialer will be bound and
35 * {@link VisualVoicemailService#onSmsReceived(VisualVoicemailTask, VisualVoicemailSms)}
36 * will called with the information extracted from the SMS.
37 *
38 * <p>Use {@link android.telephony.VisualVoicemailSmsFilterSettings.Builder} to construct this
39 * class.
40 *
41 * @see TelephonyManager#setVisualVoicemailSmsFilterSettings(VisualVoicemailSmsFilterSettings)
42 */
43public final class VisualVoicemailSmsFilterSettings implements Parcelable {
44
45
46    /**
47     * The visual voicemail SMS message does not have to be a data SMS, and can be directed to any
48     * port.
49     */
50    public static final int DESTINATION_PORT_ANY = -1;
51
52    /**
53     * The visual voicemail SMS message can be directed to any port, but must be a data SMS.
54     */
55    public static final int DESTINATION_PORT_DATA_SMS = -2;
56
57    /**
58     * @hide
59     */
60    public static final String DEFAULT_CLIENT_PREFIX = "//VVM";
61    /**
62     * @hide
63     */
64    public static final List<String> DEFAULT_ORIGINATING_NUMBERS = Collections.emptyList();
65    /**
66     * @hide
67     */
68    public static final int DEFAULT_DESTINATION_PORT = DESTINATION_PORT_ANY;
69
70    /**
71     * Builder class for {@link VisualVoicemailSmsFilterSettings} objects.
72     */
73    public static class Builder {
74
75        private String mClientPrefix = DEFAULT_CLIENT_PREFIX;
76        private List<String> mOriginatingNumbers = DEFAULT_ORIGINATING_NUMBERS;
77        private int mDestinationPort = DEFAULT_DESTINATION_PORT;
78
79        public VisualVoicemailSmsFilterSettings build() {
80            return new VisualVoicemailSmsFilterSettings(this);
81        }
82
83        /**
84         * Sets the client prefix for the visual voicemail SMS filter. The client prefix will appear
85         * at the start of a visual voicemail SMS message, followed by a colon(:).
86         */
87        public Builder setClientPrefix(String clientPrefix) {
88            if (clientPrefix == null) {
89                throw new IllegalArgumentException("Client prefix cannot be null");
90            }
91            mClientPrefix = clientPrefix;
92            return this;
93        }
94
95        /**
96         * Sets the originating number whitelist for the visual voicemail SMS filter. If the list is
97         * not null only the SMS messages from a number in the list can be considered as a visual
98         * voicemail SMS. Otherwise, messages from any address will be considered.
99         */
100        public Builder setOriginatingNumbers(List<String> originatingNumbers) {
101            if (originatingNumbers == null) {
102                throw new IllegalArgumentException("Originating numbers cannot be null");
103            }
104            mOriginatingNumbers = originatingNumbers;
105            return this;
106        }
107
108        /**
109         * Sets the destination port for the visual voicemail SMS filter.
110         *
111         * @param destinationPort The destination port, or {@link #DESTINATION_PORT_ANY}, or {@link
112         * #DESTINATION_PORT_DATA_SMS}
113         */
114        public Builder setDestinationPort(int destinationPort) {
115            mDestinationPort = destinationPort;
116            return this;
117        }
118
119    }
120
121    /**
122     * The client prefix for the visual voicemail SMS filter. The client prefix will appear at the
123     * start of a visual voicemail SMS message, followed by a colon(:).
124     */
125    public final String clientPrefix;
126
127    /**
128     * The originating number whitelist for the visual voicemail SMS filter of a phone account. If
129     * the list is not null only the SMS messages from a number in the list can be considered as a
130     * visual voicemail SMS. Otherwise, messages from any address will be considered.
131     */
132    public final List<String> originatingNumbers;
133
134    /**
135     * The destination port for the visual voicemail SMS filter, or {@link #DESTINATION_PORT_ANY},
136     * or {@link #DESTINATION_PORT_DATA_SMS}
137     */
138    public final int destinationPort;
139
140    /**
141     * Use {@link Builder} to construct
142     */
143    private VisualVoicemailSmsFilterSettings(Builder builder) {
144        clientPrefix = builder.mClientPrefix;
145        originatingNumbers = builder.mOriginatingNumbers;
146        destinationPort = builder.mDestinationPort;
147    }
148
149    public static final Creator<VisualVoicemailSmsFilterSettings> CREATOR =
150            new Creator<VisualVoicemailSmsFilterSettings>() {
151                @Override
152                public VisualVoicemailSmsFilterSettings createFromParcel(Parcel in) {
153                    Builder builder = new Builder();
154                    builder.setClientPrefix(in.readString());
155                    builder.setOriginatingNumbers(in.createStringArrayList());
156                    builder.setDestinationPort(in.readInt());
157
158                    return builder.build();
159                }
160
161                @Override
162                public VisualVoicemailSmsFilterSettings[] newArray(int size) {
163                    return new VisualVoicemailSmsFilterSettings[size];
164                }
165            };
166
167    @Override
168    public int describeContents() {
169        return 0;
170    }
171
172    @Override
173    public void writeToParcel(Parcel dest, int flags) {
174        dest.writeString(clientPrefix);
175        dest.writeStringList(originatingNumbers);
176        dest.writeInt(destinationPort);
177    }
178
179    @Override
180    public String toString(){
181        return "[VisualVoicemailSmsFilterSettings "
182                + "clientPrefix=" + clientPrefix
183                + ", originatingNumbers=" + originatingNumbers
184                + ", destinationPort=" + destinationPort
185                + "]";
186    }
187
188}
189