1 /* 2 * Copyright (C) 2015 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.messaging.ui.mediapicker; 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.messaging.R; 28 import com.android.messaging.ui.PersistentInstanceState; 29 import com.android.messaging.util.ThreadUtil; 30 31 public class CameraMediaChooserView extends FrameLayout implements PersistentInstanceState { 32 private static final String KEY_CAMERA_INDEX = "camera_index"; 33 34 // True if we have at least queued an update to the view tree to support software rendering 35 // fallback 36 private boolean mIsSoftwareFallbackActive; 37 CameraMediaChooserView(final Context context, final AttributeSet attrs)38 public CameraMediaChooserView(final Context context, final AttributeSet attrs) { 39 super(context, attrs); 40 } 41 42 @Override onSaveInstanceState()43 protected Parcelable onSaveInstanceState() { 44 final Bundle bundle = new Bundle(); 45 bundle.putInt(KEY_CAMERA_INDEX, CameraManager.get().getCameraIndex()); 46 return bundle; 47 } 48 49 @Override onRestoreInstanceState(final Parcelable state)50 protected void onRestoreInstanceState(final Parcelable state) { 51 if (!(state instanceof Bundle)) { 52 return; 53 } 54 55 final Bundle bundle = (Bundle) state; 56 CameraManager.get().selectCameraByIndex(bundle.getInt(KEY_CAMERA_INDEX)); 57 } 58 59 @Override saveState()60 public Parcelable saveState() { 61 return onSaveInstanceState(); 62 } 63 64 @Override restoreState(final Parcelable restoredState)65 public void restoreState(final Parcelable restoredState) { 66 onRestoreInstanceState(restoredState); 67 } 68 69 @Override resetState()70 public void resetState() { 71 CameraManager.get().selectCamera(Camera.CameraInfo.CAMERA_FACING_BACK); 72 } 73 74 @Override onDraw(final Canvas canvas)75 protected void onDraw(final Canvas canvas) { 76 super.onDraw(canvas); 77 // If the canvas isn't hardware accelerated, we have to replace the HardwareCameraPreview 78 // with a SoftwareCameraPreview which supports software rendering 79 if (!canvas.isHardwareAccelerated() && !mIsSoftwareFallbackActive) { 80 mIsSoftwareFallbackActive = true; 81 // Post modifying the tree since we can't modify the view tree during a draw pass 82 ThreadUtil.getMainThreadHandler().post(new Runnable() { 83 @Override 84 public void run() { 85 final HardwareCameraPreview cameraPreview = 86 (HardwareCameraPreview) findViewById(R.id.camera_preview); 87 if (cameraPreview == null) { 88 return; 89 } 90 final ViewGroup parent = ((ViewGroup) cameraPreview.getParent()); 91 final int index = parent.indexOfChild(cameraPreview); 92 final SoftwareCameraPreview softwareCameraPreview = 93 new SoftwareCameraPreview(getContext()); 94 // Be sure to remove the hardware view before adding the software view to 95 // prevent having 2 camera previews active at the same time 96 parent.removeView(cameraPreview); 97 parent.addView(softwareCameraPreview, index); 98 } 99 }); 100 } 101 } 102 } 103