Home | History | Annotate | Download | only in cameraui
      1 /*
      2  * Copyright (C) 2016 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 com.android.dialer.callcomposer.cameraui;
     18 
     19 import android.content.Context;
     20 import android.graphics.Canvas;
     21 import android.hardware.Camera;
     22 import android.os.Bundle;
     23 import android.os.Parcelable;
     24 import android.util.AttributeSet;
     25 import android.view.ViewGroup;
     26 import android.widget.FrameLayout;
     27 import com.android.dialer.callcomposer.camera.CameraManager;
     28 import com.android.dialer.callcomposer.camera.HardwareCameraPreview;
     29 import com.android.dialer.callcomposer.camera.SoftwareCameraPreview;
     30 import com.android.dialer.common.LogUtil;
     31 
     32 /** Used to display the view of the camera. */
     33 public class CameraMediaChooserView extends FrameLayout {
     34   private static final String STATE_CAMERA_INDEX = "camera_index";
     35   private static final String STATE_SUPER = "super";
     36 
     37   // True if we have at least queued an update to the view tree to support software rendering
     38   // fallback
     39   private boolean isSoftwareFallbackActive;
     40 
     41   public CameraMediaChooserView(final Context context, final AttributeSet attrs) {
     42     super(context, attrs);
     43   }
     44 
     45   @Override
     46   protected Parcelable onSaveInstanceState() {
     47     final Bundle bundle = new Bundle();
     48     bundle.putParcelable(STATE_SUPER, super.onSaveInstanceState());
     49     final int cameraIndex = CameraManager.get().getCameraIndex();
     50     LogUtil.i("CameraMediaChooserView.onSaveInstanceState", "saving camera index:" + cameraIndex);
     51     bundle.putInt(STATE_CAMERA_INDEX, cameraIndex);
     52     return bundle;
     53   }
     54 
     55   @Override
     56   protected void onRestoreInstanceState(final Parcelable state) {
     57     if (!(state instanceof Bundle)) {
     58       return;
     59     }
     60 
     61     final Bundle bundle = (Bundle) state;
     62     final int cameraIndex = bundle.getInt(STATE_CAMERA_INDEX);
     63     super.onRestoreInstanceState(bundle.getParcelable(STATE_SUPER));
     64 
     65     LogUtil.i(
     66         "CameraMediaChooserView.onRestoreInstanceState", "restoring camera index:" + cameraIndex);
     67     if (cameraIndex != -1) {
     68       CameraManager.get().selectCameraByIndex(cameraIndex);
     69     } else {
     70       resetState();
     71     }
     72   }
     73 
     74   public void resetState() {
     75     CameraManager.get().selectCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
     76   }
     77 
     78   @Override
     79   protected void onDraw(final Canvas canvas) {
     80     super.onDraw(canvas);
     81     // If the canvas isn't hardware accelerated, we have to replace the HardwareCameraPreview
     82     // with a SoftwareCameraPreview which supports software rendering
     83     if (!canvas.isHardwareAccelerated() && !isSoftwareFallbackActive) {
     84       isSoftwareFallbackActive = true;
     85       // Post modifying the tree since we can't modify the view tree during a draw pass
     86       post(
     87           new Runnable() {
     88             @Override
     89             public void run() {
     90               final HardwareCameraPreview cameraPreview =
     91                   (HardwareCameraPreview) findViewById(R.id.camera_preview);
     92               if (cameraPreview == null) {
     93                 return;
     94               }
     95               final ViewGroup parent = ((ViewGroup) cameraPreview.getParent());
     96               final int index = parent.indexOfChild(cameraPreview);
     97               final SoftwareCameraPreview softwareCameraPreview =
     98                   new SoftwareCameraPreview(getContext());
     99               // Be sure to remove the hardware view before adding the software view to
    100               // prevent having 2 camera previews active at the same time
    101               parent.removeView(cameraPreview);
    102               parent.addView(softwareCameraPreview, index);
    103             }
    104           });
    105     }
    106   }
    107 }
    108