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