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