1 /*
2  * Copyright (C) 2022 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 
17 package android.telecom;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.content.ComponentName;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 
29 import com.android.server.telecom.flags.Flags;
30 
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 
34 /**
35  * Represents a voip call requested to stream to another device that the general streaming sender
36  * app should present to the receiver.
37  *
38  * @hide
39  */
40 @SystemApi
41 public final class StreamingCall implements Parcelable {
42     /**
43      * The state of a {@code StreamingCall} when newly created. General streaming sender should
44      * continuously stream call audio to the sender device as long as the {@code StreamingCall} is
45      * in this state.
46      */
47     public static final int STATE_STREAMING = 1;
48 
49     /**
50      * The state of a {@code StreamingCall} when in a holding state.
51      */
52     public static final int STATE_HOLDING = 2;
53 
54     /**
55      * The state of a {@code StreamingCall} when it's either disconnected or pulled back to the
56      * original device.
57      */
58     public static final int STATE_DISCONNECTED = 3;
59 
60     /**
61      * The ID associated with this call.  This is the same value as {@link CallControl#getCallId()}.
62      */
63     @FlaggedApi(Flags.FLAG_CALL_DETAILS_ID_CHANGES)
64     public static final String EXTRA_CALL_ID = "android.telecom.extra.CALL_ID";
65 
66     /**
67      * @hide
68      */
StreamingCall(@onNull Parcel in)69     private StreamingCall(@NonNull Parcel in) {
70         mComponentName = in.readParcelable(ComponentName.class.getClassLoader());
71         mDisplayName = in.readCharSequence();
72         mAddress = in.readParcelable(Uri.class.getClassLoader());
73         mExtras = in.readBundle();
74         mState = in.readInt();
75     }
76 
77     @NonNull
78     public static final Creator<StreamingCall> CREATOR = new Creator<>() {
79         @Override
80         public StreamingCall createFromParcel(@NonNull Parcel in) {
81             return new StreamingCall(in);
82         }
83 
84         @Override
85         public StreamingCall[] newArray(int size) {
86             return new StreamingCall[size];
87         }
88     };
89 
90     @Override
describeContents()91     public int describeContents() {
92         return 0;
93     }
94 
95     @Override
writeToParcel(@ndroidx.annotation.NonNull Parcel dest, int flags)96     public void writeToParcel(@androidx.annotation.NonNull Parcel dest, int flags) {
97         dest.writeParcelable(mComponentName, flags);
98         dest.writeCharSequence(mDisplayName);
99         dest.writeParcelable(mAddress, flags);
100         dest.writeBundle(mExtras);
101         dest.writeInt(mState);
102     }
103 
104     /**
105      * @hide
106      */
107     @IntDef(prefix = { "STATE_" },
108             value = {
109                     STATE_STREAMING,
110                     STATE_HOLDING,
111                     STATE_DISCONNECTED
112             })
113     @Retention(RetentionPolicy.SOURCE)
114     public @interface StreamingCallState {}
115 
116     private final ComponentName mComponentName;
117     private final CharSequence mDisplayName;
118     private final Uri mAddress;
119     private final Bundle mExtras;
120     @StreamingCallState
121     private int mState;
122     private StreamingCallAdapter mAdapter = null;
123 
StreamingCall(@onNull ComponentName componentName, @NonNull CharSequence displayName, @NonNull Uri address, @NonNull Bundle extras)124     public StreamingCall(@NonNull ComponentName componentName, @NonNull CharSequence displayName,
125             @NonNull Uri address, @NonNull Bundle extras) {
126         mComponentName = componentName;
127         mDisplayName = displayName;
128         mAddress = address;
129         mExtras = extras;
130         mState = STATE_STREAMING;
131     }
132 
133     /**
134      * @hide
135      */
setAdapter(StreamingCallAdapter adapter)136     public void setAdapter(StreamingCallAdapter adapter) {
137         mAdapter = adapter;
138     }
139 
140     /**
141      * @return The {@link ComponentName} to identify the original voip app of this
142      * {@code StreamingCall}. General streaming sender app can use this to query necessary
143      * information (app icon etc.) in order to present notification of the streaming call on the
144      * receiver side.
145      */
146     @NonNull
getComponentName()147     public ComponentName getComponentName() {
148         return mComponentName;
149     }
150 
151     /**
152      * @return The display name that the general streaming sender app can use this to present the
153      * {@code StreamingCall} to the receiver side.
154      */
155     @NonNull
getDisplayName()156     public CharSequence getDisplayName() {
157         return mDisplayName;
158     }
159 
160     /**
161      * @return The address (e.g., phone number) to which the {@code StreamingCall} is currently
162      * connected.
163      */
164     @NonNull
getAddress()165     public Uri getAddress() {
166         return mAddress;
167     }
168 
169     /**
170      * @return The state of this {@code StreamingCall}.
171      */
172     @StreamingCallState
getState()173     public int getState() {
174         return mState;
175     }
176 
177     /**
178      * @return The extra info the general streaming app need to stream the call from voip app or
179      * D2DI sdk.
180      */
181     @NonNull
getExtras()182     public Bundle getExtras() {
183         return mExtras;
184     }
185 
186     /**
187      * Sets the state of this {@code StreamingCall}. The general streaming sender app can use this
188      * to request holding, unholding and disconnecting this {@code StreamingCall}.
189      * @param state The current streaming state of the call.
190      */
requestStreamingState(@treamingCallState int state)191     public void requestStreamingState(@StreamingCallState int state) {
192         mAdapter.setStreamingState(state);
193     }
194 }
195