<commit_before>/***
 Copyright (c) 2015 CommonsWare, LLC

 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.commonsware.cwac.cam2;

import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Build;
import android.util.Log;
import com.commonsware.cwac.cam2.util.Size;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import de.greenrobot.event.EventBus;

/**
 * Implementation of a CameraEngine that supports the
 * original android.hardware.Camera API.
 */
@SuppressWarnings("deprecation")
public class ClassicCameraEngine extends CameraEngine
    implements MediaRecorder.OnInfoListener,
    Camera.PreviewCallback, Camera.OnZoomChangeListener {
  private final Context ctxt;
  private List<Descriptor> descriptors=null;
  private MediaRecorder recorder;
  private VideoTransaction xact;
  private int previewWidth, previewHeight;
  private int previewFormat;

  public ClassicCameraEngine(Context ctxt) {
    this.ctxt=ctxt.getApplicationContext();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public CameraSession.Builder buildSession(Context ctxt, CameraDescriptor descriptor) {
    return(new SessionBuilder(ctxt, descriptor));
  }

  /**
   * {@inheritDoc}
   */
  public void loadCameraDescriptors(final CameraSelectionCriteria criteria) {
    getThreadPool().execute(new Runnable() {
      @Override
      public void run() {
        if (descriptors == null) {
          int count=Camera.getNumberOfCameras();
          List<Descriptor> result=new ArrayList<Descriptor>();
          Camera.CameraInfo info=new Camera.CameraInfo();

          for (int cameraId=0; cameraId < count; cameraId++) {
            Camera.getCameraInfo(cameraId, info);
            Descriptor descriptor=new Descriptor(cameraId, info);

            Camera camera=Camera.open(descriptor.getCameraId());
            Camera.Parameters params=camera.getParameters();
            ArrayList<Size> sizes=new ArrayList<Size>();

            for (Camera.Size size : params.getSupportedPreviewSizes()) {
              if (size.height<2160 && size.width<2160) {
                sizes.add(new Size(size.width, size.height));
              }
            }

            descriptor.setPreviewSizes(sizes);

            sizes=new ArrayList<Size>();

            for (Camera.Size size : params.getSupportedPictureSizes()) {
              if (!"samsung".equals(Build.MANUFACTURER) ||
                  !"jflteuc".equals(Build.PRODUCT) ||
                  size.width<2048) {
                sizes.add(new Size(size.width, size.height));
              }
            }

            descriptor.setPictureSizes(sizes);
            camera.release();
            result.add(descriptor);
          }

          descriptors=result;
        }

        List<CameraDescriptor> result=new ArrayList<CameraDescriptor>();

        for (Descriptor descriptor : descriptors) {
          if (!criteria.getFacingExactMatch() ||
            descriptor.getScore(criteria)>0) {
            result.add(descriptor);
          }
        }

        Collections.sort(result, new Comparator<CameraDescriptor>() {<commit_msg>check for null params per issue #246<commit_after>/***
 Copyright (c) 2015 CommonsWare, LLC

 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.commonsware.cwac.cam2;

import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Build;
import android.util.Log;
import com.commonsware.cwac.cam2.util.Size;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import de.greenrobot.event.EventBus;

/**
 * Implementation of a CameraEngine that supports the
 * original android.hardware.Camera API.
 */
@SuppressWarnings("deprecation")
public class ClassicCameraEngine extends CameraEngine
    implements MediaRecorder.OnInfoListener,
    Camera.PreviewCallback, Camera.OnZoomChangeListener {
  private final Context ctxt;
  private List<Descriptor> descriptors=null;
  private MediaRecorder recorder;
  private VideoTransaction xact;
  private int previewWidth, previewHeight;
  private int previewFormat;

  public ClassicCameraEngine(Context ctxt) {
    this.ctxt=ctxt.getApplicationContext();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public CameraSession.Builder buildSession(Context ctxt, CameraDescriptor descriptor) {
    return(new SessionBuilder(ctxt, descriptor));
  }

  /**
   * {@inheritDoc}
   */
  public void loadCameraDescriptors(final CameraSelectionCriteria criteria) {
    getThreadPool().execute(new Runnable() {
      @Override
      public void run() {
        if (descriptors == null) {
          int count=Camera.getNumberOfCameras();
          List<Descriptor> result=new ArrayList<Descriptor>();
          Camera.CameraInfo info=new Camera.CameraInfo();

          for (int cameraId=0; cameraId < count; cameraId++) {
            Camera.getCameraInfo(cameraId, info);
            Descriptor descriptor=new Descriptor(cameraId, info);

            Camera camera=Camera.open(descriptor.getCameraId());
            Camera.Parameters params=camera.getParameters();

            if (params!=null) {
              ArrayList<Size> sizes=new ArrayList<Size>();

              for (Camera.Size size : params.getSupportedPreviewSizes()) {
                if (size.height<2160 && size.width<2160) {
                  sizes.add(new Size(size.width, size.height));
                }
              }

              descriptor.setPreviewSizes(sizes);

              sizes=new ArrayList<Size>();

              for (Camera.Size size : params.getSupportedPictureSizes()) {
                if (!"samsung".equals(Build.MANUFACTURER) ||
                  !"jflteuc".equals(Build.PRODUCT) ||
                  size.width<2048) {
                  sizes.add(new Size(size.width, size.height));
                }
              }

              descriptor.setPictureSizes(sizes);
              result.add(descriptor);
            }

            camera.release();
          }

          descriptors=result;
        }

        List<CameraDescriptor> result=new ArrayList<CameraDescriptor>();

        for (Descriptor descriptor : descriptors) {
          if (!criteria.getFacingExactMatch() ||
            descriptor.getScore(criteria)>0) {
            result.add(descriptor);
          }
        }

        Collections.sort(result, new Comparator<CameraDescriptor>() {
{'fraction_non_alphanumeric': 0.09775009533494343, 'fraction_numerical': 0.006482776153552815, 'mean_word_length': 2.979767324228629, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013090', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2027, 'n_words': 526}