Home | History | Annotate | Download | only in testingcamera2
      1 /*
      2  * Copyright (C) 2014 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 package com.android.testingcamera2;
     17 
     18 import java.lang.reflect.Array;
     19 import java.util.Locale;
     20 
     21 import android.app.AlertDialog;
     22 import android.app.Dialog;
     23 import android.app.DialogFragment;
     24 import android.content.Context;
     25 import android.content.DialogInterface;
     26 import android.hardware.camera2.CameraAccessException;
     27 import android.hardware.camera2.CameraCharacteristics;
     28 import android.hardware.camera2.CameraManager;
     29 import android.hardware.camera2.CameraMetadata;
     30 import android.os.Bundle;
     31 
     32 /**
     33  * A simple dialog that writes out a given camera's camera characteristics into its message.
     34  *
     35  * <p>Does not depend on the rest of TestingCamera for operation.</p>
     36  *
     37  */
     38 public class CameraInfoDialogFragment extends DialogFragment {
     39 
     40     private String mCameraId;
     41 
     42     public CameraInfoDialogFragment() {
     43         super();
     44         mCameraId = null;
     45     }
     46 
     47     /**
     48      * Set the camera ID for which to display the information.
     49      *
     50      * <p>Only effective if called before showing the dialog.</p>
     51      */
     52     public void setCameraId(String cameraId) {
     53         mCameraId = cameraId;
     54     }
     55 
     56     @Override
     57     public Dialog onCreateDialog(Bundle savedInstanceState) {
     58         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     59 
     60         String title = String.format("Info: Camera %s", mCameraId);
     61 
     62         CameraCharacteristics info = null;
     63         if (mCameraId != null) {
     64             try {
     65                 info = ((CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE)).
     66                         getCameraCharacteristics(mCameraId);
     67             } catch (CameraAccessException e) {
     68                 TLog.e(String.format("Can't get characteristics for camera %s: %s", mCameraId, e));
     69             }
     70         }
     71 
     72         String infoText = formatCameraCharacteristics(info);
     73 
     74         builder.setTitle(title)
     75                .setMessage(infoText)
     76                .setPositiveButton(R.string.camera_info_dialog_ok_button,
     77                        new DialogInterface.OnClickListener() {
     78                 @Override
     79                 public void onClick(DialogInterface dialog, int id) {
     80                        // do nothing, dialog fragment will hide itself
     81                    }
     82                });
     83 
     84         return builder.create();
     85     }
     86 
     87     /**
     88      * Convert camera characteristics into a key = values list for display
     89      * @param info camera characteristics to format
     90      * @return a multi-line string containing the list of key = value pairs
     91      */
     92     // Assumes every value type has a reasonable toString()
     93     private String formatCameraCharacteristics(CameraCharacteristics info) {
     94         String infoText;
     95         if (info != null) {
     96             StringBuilder infoBuilder = new StringBuilder("Camera characteristics:\n\n");
     97 
     98             for (CameraCharacteristics.Key<?> key : info.getKeys()) {
     99                 infoBuilder.append(String.format(Locale.US, "%s:  ", key.getName()));
    100 
    101                 Object val = info.get(key);
    102                 if (val.getClass().isArray()) {
    103                     // Iterate an array-type value
    104                     // Assumes camera characteristics won't have arrays of arrays as values
    105                     int len = Array.getLength(val);
    106                     infoBuilder.append("[ ");
    107                     for (int i = 0; i < len; i++) {
    108                         infoBuilder.append(String.format(Locale.US,
    109                                 "%s%s",
    110                                 Array.get(val, i),
    111                                 (i + 1 == len ) ? "" : ", "));
    112                     }
    113                     infoBuilder.append(" ]\n\n");
    114                 } else {
    115                     // Single value
    116                     infoBuilder.append(String.format(Locale.US, "%s\n\n", val.toString()));
    117                 }
    118             }
    119             infoText = infoBuilder.toString();
    120         } else {
    121             infoText = "No info";
    122         }
    123         return infoText;
    124     }
    125 
    126 }
    127