1/*
2 * Copyright (c) 2013 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
17package com.android.ims;
18
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.Map.Entry;
22import java.util.Set;
23
24import android.os.Bundle;
25import android.os.Parcel;
26import android.os.Parcelable;
27import android.telecom.Call;
28import android.telecom.Connection;
29
30/**
31 * Provides the conference information (defined in RFC 4575) for IMS conference call.
32 *
33 * @hide
34 */
35public class ImsConferenceState implements Parcelable {
36    /**
37     * conference-info : user
38     */
39    // user (String) : Tel or SIP URI
40    public static final String USER = "user";
41    // user > display text (String)
42    public static final String DISPLAY_TEXT = "display-text";
43    // user > endpoint (String) : URI or GRUU or Phone number
44    public static final String ENDPOINT = "endpoint";
45    // user > endpoint > status
46    public static final String STATUS = "status";
47
48    /**
49     * status-type (String) :
50     * "pending" : Endpoint is not yet in the session, but it is anticipated that he/she will
51     *      join in the near future.
52     * "dialing-out" : Focus has dialed out to connect the endpoint to the conference,
53     *      but the endpoint is not yet in the roster (probably being authenticated).
54     * "dialing-in" : Endpoint is dialing into the conference, not yet in the roster
55     *      (probably being authenticated).
56     * "alerting" : PSTN alerting or SIP 180 Ringing was returned for the outbound call;
57     *      endpoint is being alerted.
58     * "on-hold" : Active signaling dialog exists between an endpoint and a focus,
59     *      but endpoint is "on-hold" for this conference, i.e., he/she is neither "hearing"
60     *      the conference mix nor is his/her media being mixed in the conference.
61     * "connected" : Endpoint is a participant in the conference. Depending on the media policies,
62     *      he/she can send and receive media to and from other participants.
63     * "disconnecting" : Focus is in the process of disconnecting the endpoint
64     *      (e.g. in SIP a DISCONNECT or BYE was sent to the endpoint).
65     * "disconnected" : Endpoint is not a participant in the conference, and no active dialog
66     *      exists between the endpoint and the focus.
67     * "muted-via-focus" : Active signaling dialog exists beween an endpoint and a focus and
68     *      the endpoint can "listen" to the conference, but the endpoint's media is not being
69     *      mixed into the conference.
70     * "connect-fail" : Endpoint fails to join the conference by rejecting the conference call.
71     */
72    public static final String STATUS_PENDING = "pending";
73    public static final String STATUS_DIALING_OUT = "dialing-out";
74    public static final String STATUS_DIALING_IN = "dialing-in";
75    public static final String STATUS_ALERTING = "alerting";
76    public static final String STATUS_ON_HOLD = "on-hold";
77    public static final String STATUS_CONNECTED = "connected";
78    public static final String STATUS_DISCONNECTING = "disconnecting";
79    public static final String STATUS_DISCONNECTED = "disconnected";
80    public static final String STATUS_MUTED_VIA_FOCUS = "muted-via-focus";
81    public static final String STATUS_CONNECT_FAIL = "connect-fail";
82
83    /**
84     * conference-info : SIP status code (integer)
85     */
86    public static final String SIP_STATUS_CODE = "sipstatuscode";
87
88    public HashMap<String, Bundle> mParticipants = new HashMap<String, Bundle>();
89
90    public ImsConferenceState() {
91    }
92
93    public ImsConferenceState(Parcel in) {
94        readFromParcel(in);
95    }
96
97    @Override
98    public int describeContents() {
99        return 0;
100    }
101
102    @Override
103    public void writeToParcel(Parcel out, int flags) {
104        out.writeInt(mParticipants.size());
105
106        if (mParticipants.size() > 0) {
107            Set<Entry<String, Bundle>> entries = mParticipants.entrySet();
108
109            if (entries != null) {
110                Iterator<Entry<String, Bundle>> iterator = entries.iterator();
111
112                while (iterator.hasNext()) {
113                    Entry<String, Bundle> entry = iterator.next();
114
115                    out.writeString(entry.getKey());
116                    out.writeParcelable(entry.getValue(), 0);
117                }
118            }
119        }
120    }
121
122    private void readFromParcel(Parcel in) {
123        int size = in.readInt();
124
125        for (int i = 0; i < size; ++i) {
126            String user = in.readString();
127            Bundle state = in.readParcelable(null);
128            mParticipants.put(user, state);
129        }
130    }
131
132    public static final Creator<ImsConferenceState> CREATOR =
133            new Creator<ImsConferenceState>() {
134        @Override
135        public ImsConferenceState createFromParcel(Parcel in) {
136            return new ImsConferenceState(in);
137        }
138
139        @Override
140        public ImsConferenceState[] newArray(int size) {
141            return new ImsConferenceState[size];
142        }
143    };
144
145    /**
146     * Translates an {@code ImsConferenceState} status type to a telecom connection state.
147     *
148     * @param status The status type.
149     * @return The corresponding {@link android.telecom.Connection} state.
150     */
151    public static int getConnectionStateForStatus(String status) {
152        if (status.equals(STATUS_PENDING)) {
153            return Connection.STATE_INITIALIZING;
154        } else if (status.equals(STATUS_DIALING_IN)) {
155            return Connection.STATE_RINGING;
156        } else if (status.equals(STATUS_ALERTING) ||
157                status.equals(STATUS_DIALING_OUT)) {
158            return Connection.STATE_DIALING;
159        } else if (status.equals(STATUS_ON_HOLD)) {
160            return Connection.STATE_HOLDING;
161        } else if (status.equals(STATUS_CONNECTED) ||
162                status.equals(STATUS_MUTED_VIA_FOCUS) ||
163                status.equals(STATUS_DISCONNECTING)) {
164            return Connection.STATE_ACTIVE;
165        } else if (status.equals(STATUS_DISCONNECTED)) {
166            return Connection.STATE_DISCONNECTED;
167        }
168        return Call.STATE_ACTIVE;
169    }
170}
171