1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.incallui;
import android.content.res.Configuration;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.ViewTreeObserver;
/**
* Fragment containing video calling surfaces.
*/
public class VideoCallFragment extends BaseFragment<VideoCallPresenter,
VideoCallPresenter.VideoCallUi> implements VideoCallPresenter.VideoCallUi {
/**
* Used to indicate that the surface dimensions are not set.
*/
private static final int DIMENSIONS_NOT_SET = -1;
/**
* Surface ID for the display surface.
*/
public static final int SURFACE_DISPLAY = 1;
/**
* Surface ID for the preview surface.
*/
public static final int SURFACE_PREVIEW = 2;
// Static storage used to retain the video surfaces across Activity restart.
// TextureViews are not parcelable, so it is not possible to store them in the saved state.
private static boolean sVideoSurfacesInUse = false;
private static VideoCallSurface sPreviewSurface = null;
private static VideoCallSurface sDisplaySurface = null;
/**
* {@link ViewStub} holding the video call surfaces. This is the parent for the
* {@link VideoCallFragment}. Used to ensure that the video surfaces are only inflated when
* required.
*/
private ViewStub mVideoViewsStub;
/**
* Inflated view containing the video call surfaces represented by the {@link ViewStub}.
*/
private View mVideoViews;
/**
* {@code True} when the entering the activity again after a restart due to orientation change.
*/
private boolean mIsActivityRestart;
/**
* {@code True} when the layout of the activity has been completed.
*/
private boolean mIsLayoutComplete = false;
/**
* {@code True} if in landscape mode.
*/
private boolean mIsLandscape;
/**
* The width of the surface.
*/
private int mWidth = DIMENSIONS_NOT_SET;
/**
* The height of the surface.
*/
private int mHeight = DIMENSIONS_NOT_SET;
/**
* Inner-class representing a {@link TextureView} and its associated {@link SurfaceTexture} and
* {@link Surface}. Used to manage the lifecycle of these objects across device orientation
* changes.
*/
private class VideoCallSurface implements TextureView.SurfaceTextureListener,
View.OnClickListener {
private int mSurfaceId;
private TextureView mTextureView;
private SurfaceTexture mSavedSurfaceTexture;
private Surface mSavedSurface;
/**
* Creates an instance of a {@link VideoCallSurface}.
*
* @param surfaceId The surface ID of the surface.
* @param textureView The {@link TextureView} for the surface.
*/
public VideoCallSurface(int surfaceId, TextureView textureView) {
this(surfaceId, textureView, DIMENSIONS_NOT_SET, DIMENSIONS_NOT_SET);
}
/**
* Creates an instance of a {@link VideoCallSurface}.
*
* @param surfaceId The surface ID of the surface.
* @param textureView The {@link TextureView} for the surface.
* @param width The width of the surface.
* @param height The height of the surface.
*/
public VideoCallSurface(int surfaceId, TextureView textureView, int width, int height) {
mWidth = width;
mHeight = height;
mSurfaceId = surfaceId;
recreateView(textureView);
}
/**
* Recreates a {@link VideoCallSurface} after a device orientation change. Re-applies the
* saved {@link SurfaceTexture} to the
*
* @param view The {@link TextureView}.
*/
public void recreateView(TextureView view) {
mTextureView = view;
mTextureView.setSurfaceTextureListener(this);
mTextureView.setOnClickListener(this);
if (mSavedSurfaceTexture != null) {
mTextureView.setSurfaceTexture(mSavedSurfaceTexture);
}
}
/**
* Handles {@link SurfaceTexture} callback to indicate that a {@link SurfaceTexture} has
* been successfully created.
*
* @param surfaceTexture The {@link SurfaceTexture} which has been created.
* @param width The width of the {@link SurfaceTexture}.
* @param height The height of the {@link SurfaceTexture}.
*/
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
int height) {
boolean surfaceCreated;
// Where there is no saved {@link SurfaceTexture} available, use the newly created one.
// If a saved {@link SurfaceTexture} is available, we are re-creating after an
// orientation change.
if (mSavedSurfaceTexture == null) {
mSavedSurfaceTexture = surfaceTexture;
surfaceCreated = createSurface();
} else {
// A saved SurfaceTexture was found.
surfaceCreated = true;
}
// Inform presenter that the surface is available.
if (surfaceCreated) {
getPresenter().onSurfaceCreated(mSurfaceId);
}
}
/**
* Handles a change in the {@link SurfaceTexture}'s size.
*
* @param surfaceTexture The {@link SurfaceTexture}.
* @param width The new width.
* @param height The new height.
*/
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width,
int height) {
// Not handled
}
/**
* Handles {@link SurfaceTexture} destruct callback, indicating that it has been destroyed.
*
* @param surfaceTexture The {@link SurfaceTexture}.
* @return {@code True} if the {@link TextureView} can release the {@link SurfaceTexture}.
*/
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
/**
* Destroying the surface texture; inform the presenter so it can null the surfaces.
*/
if (mSavedSurfaceTexture == null) {
getPresenter().onSurfaceDestroyed(mSurfaceId);
if (mSavedSurface != null) {
mSavedSurface.release();
mSavedSurface = null;
}
}
// The saved SurfaceTexture will be null if we're shutting down, so we want to
// return "true" in that case (indicating that TextureView can release the ST).
return (mSavedSurfaceTexture == null);
}
/**
* Handles {@link SurfaceTexture} update callback.
* @param surface
*/
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Not Handled
}
/**
* Retrieves the current {@link TextureView}.
*
* @return The {@link TextureView}.
*/
public TextureView getTextureView() {
return mTextureView;
}
/**
* Called by the user presenter to indicate that the surface is no longer required due to a
* change in video state. Releases and clears out the saved surface and surface textures.
*/
public void setDoneWithSurface() {
if (mSavedSurface != null) {
mSavedSurface.release();
mSavedSurface = null;
}
if (mSavedSurfaceTexture != null) {
mSavedSurfaceTexture.release();
mSavedSurfaceTexture = null;
}
}
/**
* Retrieves the saved surface instance.
*
* @return The surface.
*/
public Surface getSurface() {
return mSavedSurface;
}
/**
* Sets the dimensions of the surface.
*
* @param width The width of the surface, in pixels.
* @param height The height of the surface, in pixels.
*/
public void setSurfaceDimensions(int width, int height) {
mWidth = width;
mHeight = height;
if (mSavedSurfaceTexture != null) {
createSurface();
}
}
/**
* Creates the {@link Surface}, adjusting the {@link SurfaceTexture} buffer size.
*/
private boolean createSurface() {
if (mWidth != DIMENSIONS_NOT_SET && mHeight != DIMENSIONS_NOT_SET &&
mSavedSurfaceTexture != null) {
mSavedSurfaceTexture.setDefaultBufferSize(mWidth, mHeight);
mSavedSurface = new Surface(mSavedSurfaceTexture);
return true;
}
return false;
}
/**
* Handles a user clicking the surface, which is the trigger to toggle the full screen
* Video UI.
*
* @param view The view receiving the click.
*/
@Override
public void onClick(View view) {
getPresenter().onSurfaceClick(mSurfaceId);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIsActivityRestart = sVideoSurfacesInUse;
}
/**
* Handles creation of the activity and initialization of the presenter.
*
* @param savedInstanceState The saved instance state.
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mIsLandscape = getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE;
getPresenter().init(getActivity());
}
/**
* Handles creation of the fragment view.
*
* @param inflater The inflater.
* @param container The view group containing the fragment.
* @param savedInstanceState The saved instance state.
* @return
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View view = inflater.inflate(R.layout.video_call_fragment, container, false);
// Attempt to center the incoming video view, if it is in the layout.
final ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Check if the layout includes the incoming video surface -- this will only be the
// case for a video call.
View displayVideo = view.findViewById(R.id.incomingVideo);
if (displayVideo != null) {
centerDisplayView(displayVideo);
}
mIsLayoutComplete = true;
// Remove the listener so we don't continually re-layout.
ViewTreeObserver observer = view.getViewTreeObserver();
if (observer.isAlive()) {
observer.removeOnGlobalLayoutListener(this);
}
}
});
return view;
}
/**
* Centers the display view vertically for portrait orientation, and horizontally for
* lanscape orientations. The view is centered within the available space not occupied by
* the call card.
*
* @param displayVideo The video view to center.
*/
private void centerDisplayView(View displayVideo) {
// In a lansdcape layout we need to ensure we horizontally center the view based on whether
// the layout is left-to-right or right-to-left.
// In a left-to-right locale, the space for the video view is to the right of the call card
// so we need to translate it in the +X direction.
// In a right-to-left locale, the space for the video view is to the left of the call card
// so we need to translate it in the -X direction.
final boolean isLayoutRtl = InCallPresenter.isRtl();
float spaceBesideCallCard = InCallPresenter.getInstance().getSpaceBesideCallCard();
if (mIsLandscape) {
float videoViewTranslation = displayVideo.getWidth() / 2
- spaceBesideCallCard / 2;
if (isLayoutRtl) {
displayVideo.setTranslationX(-videoViewTranslation);
} else {
displayVideo.setTranslationX(videoViewTranslation);
}
} else {
float videoViewTranslation = displayVideo.getHeight() / 2
- spaceBesideCallCard / 2;
displayVideo.setTranslationY(videoViewTranslation);
}
}
/**
* After creation of the fragment view, retrieves the required views.
*
* @param view The fragment view.
* @param savedInstanceState The saved instance state.
*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mVideoViewsStub = (ViewStub) view.findViewById(R.id.videoCallViewsStub);
// If the surfaces are already in use, we have just changed orientation or otherwise
// re-created the fragment. In this case we need to inflate the video call views and
// restore the surfaces.
if (sVideoSurfacesInUse) {
inflateVideoCallViews();
}
}
/**
* Creates the presenter for the {@link VideoCallFragment}.
* @return The presenter instance.
*/
@Override
public VideoCallPresenter createPresenter() {
return new VideoCallPresenter();
}
/**
* @return The user interface for the presenter, which is this fragment.
*/
@Override
VideoCallPresenter.VideoCallUi getUi() {
return this;
}
/**
* Toggles visibility of the video UI.
*
* @param show {@code True} if the video surfaces should be shown.
*/
@Override
public void showVideoUi(boolean show) {
int visibility = show ? View.VISIBLE : View.GONE;
getView().setVisibility(visibility);
if (show) {
inflateVideoCallViews();
} else {
cleanupSurfaces();
}
if (mVideoViews != null ) {
mVideoViews.setVisibility(visibility);
}
}
/**
* Cleans up the video telephony surfaces. Used when the presenter indicates a change to an
* audio-only state. Since the surfaces are static, it is important to ensure they are cleaned
* up promptly.
*/
@Override
public void cleanupSurfaces() {
if (sDisplaySurface != null) {
sDisplaySurface.setDoneWithSurface();
sDisplaySurface = null;
}
if (sPreviewSurface != null) {
sPreviewSurface.setDoneWithSurface();
sPreviewSurface = null;
}
sVideoSurfacesInUse = false;
}
@Override
public boolean isActivityRestart() {
return mIsActivityRestart;
}
/**
* @return {@code True} if the display video surface has been created.
*/
@Override
public boolean isDisplayVideoSurfaceCreated() {
return sDisplaySurface != null && sDisplaySurface.getSurface() != null;
}
/**
* @return {@code True} if the preview video surface has been created.
*/
@Override
public boolean isPreviewVideoSurfaceCreated() {
return sPreviewSurface != null && sPreviewSurface.getSurface() != null;
}
/**
* {@link android.view.Surface} on which incoming video for a video call is displayed.
* {@code Null} until the video views {@link android.view.ViewStub} is inflated.
*/
@Override
public Surface getDisplayVideoSurface() {
return sDisplaySurface == null ? null : sDisplaySurface.getSurface();
}
/**
* {@link android.view.Surface} on which a preview of the outgoing video for a video call is
* displayed. {@code Null} until the video views {@link android.view.ViewStub} is inflated.
*/
@Override
public Surface getPreviewVideoSurface() {
return sPreviewSurface == null ? null : sPreviewSurface.getSurface();
}
/**
* Changes the dimensions of the preview surface. Called when the dimensions change due to a
* device orientation change.
*
* @param width The new width.
* @param height The new height.
*/
@Override
public void setPreviewSize(int width, int height) {
if (sPreviewSurface != null) {
TextureView preview = sPreviewSurface.getTextureView();
if (preview == null ) {
return;
}
ViewGroup.LayoutParams params = preview.getLayoutParams();
params.width = width;
params.height = height;
preview.setLayoutParams(params);
sPreviewSurface.setSurfaceDimensions(width, height);
}
}
/**
* Inflates the {@link ViewStub} containing the incoming and outgoing surfaces, if necessary,
* and creates {@link VideoCallSurface} instances to track the surfaces.
*/
private void inflateVideoCallViews() {
if (mVideoViews == null ) {
mVideoViews = mVideoViewsStub.inflate();
}
if (mVideoViews != null) {
TextureView displaySurface = (TextureView) mVideoViews.findViewById(R.id.incomingVideo);
Point screenSize = getScreenSize();
setSurfaceSizeAndTranslation(displaySurface, screenSize);
if (!sVideoSurfacesInUse) {
// Where the video surfaces are not already in use (first time creating them),
// setup new VideoCallSurface instances to track them.
sDisplaySurface = new VideoCallSurface(SURFACE_DISPLAY,
(TextureView) mVideoViews.findViewById(R.id.incomingVideo), screenSize.x,
screenSize.y);
sPreviewSurface = new VideoCallSurface(SURFACE_PREVIEW,
(TextureView) mVideoViews.findViewById(R.id.previewVideo));
sVideoSurfacesInUse = true;
} else {
// In this case, the video surfaces are already in use (we are recreating the
// Fragment after a destroy/create cycle resulting from a rotation.
sDisplaySurface.recreateView((TextureView) mVideoViews.findViewById(
R.id.incomingVideo));
sPreviewSurface.recreateView((TextureView) mVideoViews.findViewById(
R.id.previewVideo));
}
}
}
/**
* Resizes a surface so that it has the same size as the full screen and so that it is
* centered vertically below the call card.
*
* @param textureView The {@link TextureView} to resize and position.
* @param size The size of the screen.
*/
private void setSurfaceSizeAndTranslation(TextureView textureView, Point size) {
// Set the surface to have that size.
ViewGroup.LayoutParams params = textureView.getLayoutParams();
params.width = size.x;
params.height = size.y;
textureView.setLayoutParams(params);
// It is only possible to center the display view if layout of the views has completed.
// It is only after layout is complete that the dimensions of the Call Card has been
// established, which is a prerequisite to centering the view.
// Incoming video calls will center the view
if (mIsLayoutComplete && ((mIsLandscape && textureView.getTranslationX() == 0) || (
!mIsLandscape && textureView.getTranslationY() == 0))) {
centerDisplayView(textureView);
}
}
/**
* Determines the size of the device screen.
*
* @return {@link Point} specifying the width and height of the screen.
*/
private Point getScreenSize() {
// Get current screen size.
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
}
|