summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/callcomposer/cameraui/CameraMediaChooserView.java
blob: df0e7ad3f5c5e062d7984816577879cb8516b407 (plain)
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
/*
 * Copyright (C) 2016 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.dialer.callcomposer.cameraui;

import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.android.dialer.callcomposer.camera.CameraManager;
import com.android.dialer.callcomposer.camera.HardwareCameraPreview;
import com.android.dialer.callcomposer.camera.SoftwareCameraPreview;
import com.android.dialer.common.LogUtil;

/** Used to display the view of the camera. */
public class CameraMediaChooserView extends FrameLayout {
  private static final String STATE_CAMERA_INDEX = "camera_index";
  private static final String STATE_SUPER = "super";

  // True if we have at least queued an update to the view tree to support software rendering
  // fallback
  private boolean isSoftwareFallbackActive;

  public CameraMediaChooserView(final Context context, final AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected Parcelable onSaveInstanceState() {
    final Bundle bundle = new Bundle();
    bundle.putParcelable(STATE_SUPER, super.onSaveInstanceState());
    final int cameraIndex = CameraManager.get().getCameraIndex();
    LogUtil.i("CameraMediaChooserView.onSaveInstanceState", "saving camera index:" + cameraIndex);
    bundle.putInt(STATE_CAMERA_INDEX, cameraIndex);
    return bundle;
  }

  @Override
  protected void onRestoreInstanceState(final Parcelable state) {
    if (!(state instanceof Bundle)) {
      return;
    }

    final Bundle bundle = (Bundle) state;
    final int cameraIndex = bundle.getInt(STATE_CAMERA_INDEX);
    super.onRestoreInstanceState(bundle.getParcelable(STATE_SUPER));

    LogUtil.i(
        "CameraMediaChooserView.onRestoreInstanceState", "restoring camera index:" + cameraIndex);
    if (cameraIndex != -1) {
      CameraManager.get().selectCameraByIndex(cameraIndex);
    } else {
      resetState();
    }
  }

  public void resetState() {
    CameraManager.get().selectCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
  }

  @Override
  protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    // If the canvas isn't hardware accelerated, we have to replace the HardwareCameraPreview
    // with a SoftwareCameraPreview which supports software rendering
    if (!canvas.isHardwareAccelerated() && !isSoftwareFallbackActive) {
      isSoftwareFallbackActive = true;
      // Post modifying the tree since we can't modify the view tree during a draw pass
      post(
          new Runnable() {
            @Override
            public void run() {
              final HardwareCameraPreview cameraPreview =
                  (HardwareCameraPreview) findViewById(R.id.camera_preview);
              if (cameraPreview == null) {
                return;
              }
              final ViewGroup parent = ((ViewGroup) cameraPreview.getParent());
              final int index = parent.indexOfChild(cameraPreview);
              final SoftwareCameraPreview softwareCameraPreview =
                  new SoftwareCameraPreview(getContext());
              // Be sure to remove the hardware view before adding the software view to
              // prevent having 2 camera previews active at the same time
              parent.removeView(cameraPreview);
              parent.addView(softwareCameraPreview, index);
            }
          });
    }
  }
}