14dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt/*
24dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * Copyright (C) 2016 The Android Open Source Project
34dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt *
44dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
54dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * you may not use this file except in compliance with the License.
64dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * You may obtain a copy of the License at
74dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt *
84dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
94dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt *
104dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
114dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
124dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * See the License for the specific language governing permissions and
144dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * limitations under the License.
154dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt */
164dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
174dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltpackage android.telephony.mbms;
184dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
191a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liuimport android.annotation.NonNull;
201a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liuimport android.annotation.Nullable;
214dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport android.os.Parcel;
224dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport android.os.Parcelable;
234dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport android.text.TextUtils;
244dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
2547c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwaltimport java.util.ArrayList;
26a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebingerimport java.util.Collections;
274dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport java.util.Date;
284dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport java.util.HashMap;
2947c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwaltimport java.util.List;
304dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport java.util.Locale;
314dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport java.util.Map;
32bfc5f1c9a8b4587fff77129f96d6525d2e7e49d9Hall Liuimport java.util.NoSuchElementException;
339f116ef480fce78c8a8211ba96e74607e33aec56Hall Liuimport java.util.Objects;
344dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwaltimport java.util.Set;
354dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
364dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt/**
371169bc596cece10452a078facdc6962550f01ac9Hall Liu * Describes a cell-broadcast service. This class should not be instantiated directly -- use
381a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu * {@link StreamingServiceInfo} or {@link FileServiceInfo}
394dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt * @hide
404dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt */
414f306dedc58c24b4e16c2418f26961d06d9a2d1aHall Liupublic class ServiceInfo {
424dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt    // arbitrary limit on the number of locale -> name pairs we support
43f725f98f170e43aacbfa7cb6a3935f277552efc8Robert Greenwalt    final static int MAP_LIMIT = 1000;
444dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
451169bc596cece10452a078facdc6962550f01ac9Hall Liu    private final Map<Locale, String> names;
461169bc596cece10452a078facdc6962550f01ac9Hall Liu    private final String className;
471169bc596cece10452a078facdc6962550f01ac9Hall Liu    private final List<Locale> locales;
481169bc596cece10452a078facdc6962550f01ac9Hall Liu    private final String serviceId;
491169bc596cece10452a078facdc6962550f01ac9Hall Liu    private final Date sessionStartTime;
501169bc596cece10452a078facdc6962550f01ac9Hall Liu    private final Date sessionEndTime;
514dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
521169bc596cece10452a078facdc6962550f01ac9Hall Liu    /** @hide */
5347c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt    public ServiceInfo(Map<Locale, String> newNames, String newClassName, List<Locale> newLocales,
544dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt            String newServiceId, Date start, Date end) {
554dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        if (newNames == null || newNames.isEmpty() || TextUtils.isEmpty(newClassName)
5647c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt                || newLocales == null || newLocales.isEmpty() || TextUtils.isEmpty(newServiceId)
574dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt                || start == null || end == null) {
584dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt            throw new IllegalArgumentException("Bad ServiceInfo construction");
594dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        }
604dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        if (newNames.size() > MAP_LIMIT) {
6147c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt            throw new RuntimeException("bad map length " + newNames.size());
6247c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        }
6347c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        if (newLocales.size() > MAP_LIMIT) {
6447c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt            throw new RuntimeException("bad locales length " + newLocales.size());
654dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        }
661a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu
674dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        names = new HashMap(newNames.size());
684dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        names.putAll(newNames);
694dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        className = newClassName;
7047c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        locales = new ArrayList(newLocales);
714dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        serviceId = newServiceId;
724dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        sessionStartTime = (Date)start.clone();
734dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        sessionEndTime = (Date)end.clone();
744dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt    }
754dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
761169bc596cece10452a078facdc6962550f01ac9Hall Liu    /** @hide */
771169bc596cece10452a078facdc6962550f01ac9Hall Liu    protected ServiceInfo(Parcel in) {
784dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        int mapCount = in.readInt();
794dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        if (mapCount > MAP_LIMIT || mapCount < 0) {
8047c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt            throw new RuntimeException("bad map length" + mapCount);
814dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        }
824dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        names = new HashMap(mapCount);
834dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        while (mapCount-- > 0) {
844dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt            Locale locale = (java.util.Locale) in.readSerializable();
854dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt            String name = in.readString();
864dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt            names.put(locale, name);
874dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        }
884dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        className = in.readString();
8947c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        int localesCount = in.readInt();
9047c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        if (localesCount > MAP_LIMIT || localesCount < 0) {
9147c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt            throw new RuntimeException("bad locale length " + localesCount);
9247c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        }
9347c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        locales = new ArrayList<Locale>(localesCount);
9447c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        while (localesCount-- > 0) {
9547c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt            Locale l = (java.util.Locale) in.readSerializable();
9647c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt            locales.add(l);
9747c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        }
984dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        serviceId = in.readString();
994dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        sessionStartTime = (java.util.Date) in.readSerializable();
1004dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        sessionEndTime = (java.util.Date) in.readSerializable();
1014dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt    }
1024dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
1034f306dedc58c24b4e16c2418f26961d06d9a2d1aHall Liu    /** @hide */
1044dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt    public void writeToParcel(Parcel dest, int flags) {
1054dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        Set<Locale> keySet = names.keySet();
1064dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        dest.writeInt(keySet.size());
1074dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        for (Locale l : keySet) {
1084dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt            dest.writeSerializable(l);
1094dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt            dest.writeString(names.get(l));
1104dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        }
1114dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        dest.writeString(className);
11247c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        int localesCount = locales.size();
11347c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        dest.writeInt(localesCount);
11447c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        for (Locale l : locales) {
11547c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt            dest.writeSerializable(l);
11647c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        }
1174dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        dest.writeString(serviceId);
1184dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        dest.writeSerializable(sessionStartTime);
1194dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt        dest.writeSerializable(sessionEndTime);
1204dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt    }
1214dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt
1221169bc596cece10452a078facdc6962550f01ac9Hall Liu    /**
1231a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu     * Get the user-displayable name for this cell-broadcast service corresponding to the
1241a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu     * provided {@link Locale}.
1251a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu     * @param locale The {@link Locale} in which you want the name of the service. This must be a
126a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger     *               value from the set returned by {@link #getNamedContentLocales()} -- an
127bfc5f1c9a8b4587fff77129f96d6525d2e7e49d9Hall Liu     *               {@link java.util.NoSuchElementException} may be thrown otherwise.
1281a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu     * @return The {@link CharSequence} providing the name of the service in the given
1291a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu     *         {@link Locale}
1301169bc596cece10452a078facdc6962550f01ac9Hall Liu     */
1311a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu    public @NonNull CharSequence getNameForLocale(@NonNull Locale locale) {
1321a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu        if (!names.containsKey(locale)) {
133bfc5f1c9a8b4587fff77129f96d6525d2e7e49d9Hall Liu            throw new NoSuchElementException("Locale not supported");
1341a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu        }
1351a5b1304687d591cd5f913e50e8636d139ec1a25Hall Liu        return names.get(locale);
1368023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    }
1378023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu
1381169bc596cece10452a078facdc6962550f01ac9Hall Liu    /**
139a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger     * Return an unmodifiable set of the current {@link Locale}s that have a user-displayable name
140a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger     * associated with them. The user-displayable name associated with any {@link Locale} in this
141a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger     * set can be retrieved with {@link #getNameForLocale(Locale)}.
142a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger     * @return An unmodifiable set of {@link Locale} objects corresponding to a user-displayable
143a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger     * content name in that locale.
144a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger     */
145a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger    public @NonNull Set<Locale> getNamedContentLocales() {
146a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger        return Collections.unmodifiableSet(names.keySet());
147a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger    }
148a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger
149a722139f9cdf3f3368589cf5667b29e26bd29459Brad Ebinger    /**
1501169bc596cece10452a078facdc6962550f01ac9Hall Liu     * The class name for this service - used to categorize and filter
1511169bc596cece10452a078facdc6962550f01ac9Hall Liu     */
152a96478a9b7d7be9c646a5679b817ea2fae27d704Hall Liu    public String getServiceClassName() {
1538023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu        return className;
1548023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    }
1558023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu
1561169bc596cece10452a078facdc6962550f01ac9Hall Liu    /**
1571169bc596cece10452a078facdc6962550f01ac9Hall Liu     * The languages available for this service content
1581169bc596cece10452a078facdc6962550f01ac9Hall Liu     */
15947c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt    public List<Locale> getLocales() {
16047c2a3e93eff59fe2309917662093e44c5ab70cfRobert Greenwalt        return locales;
1618023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    }
1628023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu
1631169bc596cece10452a078facdc6962550f01ac9Hall Liu    /**
1641169bc596cece10452a078facdc6962550f01ac9Hall Liu     * The carrier's identifier for the service.
1651169bc596cece10452a078facdc6962550f01ac9Hall Liu     */
1668023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    public String getServiceId() {
1678023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu        return serviceId;
1688023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    }
1698023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu
1701169bc596cece10452a078facdc6962550f01ac9Hall Liu    /**
1711169bc596cece10452a078facdc6962550f01ac9Hall Liu     * The start time indicating when this service will be available.
1721169bc596cece10452a078facdc6962550f01ac9Hall Liu     */
1738023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    public Date getSessionStartTime() {
1748023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu        return sessionStartTime;
1758023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    }
1768023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu
1771169bc596cece10452a078facdc6962550f01ac9Hall Liu    /**
1781169bc596cece10452a078facdc6962550f01ac9Hall Liu     * The end time indicating when this session stops being available.
1791169bc596cece10452a078facdc6962550f01ac9Hall Liu     */
1808023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    public Date getSessionEndTime() {
1818023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu        return sessionEndTime;
1828023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu    }
1838023332b01dda4f4f780fc5d765089ba090fb8d4Hall Liu
1849f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu    @Override
1859f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu    public boolean equals(Object o) {
1869f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        if (this == o) return true;
1879f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        if (o == null) {
1889f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu            return false;
1899f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        }
1909f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        if (!(o instanceof ServiceInfo)) {
1919f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu            return false;
1929f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        }
1939f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        ServiceInfo that = (ServiceInfo) o;
1949f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        return Objects.equals(names, that.names) &&
1959f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu                Objects.equals(className, that.className) &&
1969f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu                Objects.equals(locales, that.locales) &&
1979f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu                Objects.equals(serviceId, that.serviceId) &&
1989f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu                Objects.equals(sessionStartTime, that.sessionStartTime) &&
1999f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu                Objects.equals(sessionEndTime, that.sessionEndTime);
2009f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu    }
2019f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu
2029f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu    @Override
2039f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu    public int hashCode() {
2049f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu        return Objects.hash(names, className, locales, serviceId, sessionStartTime, sessionEndTime);
2059f116ef480fce78c8a8211ba96e74607e33aec56Hall Liu    }
2064dded7a1b5a7ff6ed7402a9c2c512e4751421d7fRobert Greenwalt}
207