<commit_before>/*
 * FreeOTP
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * Copyright (C) 2013  <NAME>, Red Hat
 *
 * 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 org.fedorahosted.freeotp.add;

import java.util.List;

import org.fedorahosted.freeotp.R;
import org.fedorahosted.freeotp.Token;
import org.fedorahosted.freeotp.TokenPersistence;

import android.annotation.TargetApi;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class ScanActivity extends Activity implements SurfaceHolder.Callback {
    private final CameraInfo    mCameraInfo  = new CameraInfo();
    private final ScanAsyncTask mScanAsyncTask;
    private final int           mCameraId;
    private Handler             mHandler;
    private Camera              mCamera;

    private static class AutoFocusHandler extends Handler implements Camera.AutoFocusCallback {
        private final Camera mCamera;

        public AutoFocusHandler(Camera camera) {
            mCamera = camera;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mCamera.autoFocus(this);
        }

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            sendEmptyMessageDelayed(0, 1000);
        }
    }

    public static boolean haveCamera() {
        return findCamera(new CameraInfo()) >= 0;
    }

    private static int findCamera(CameraInfo cameraInfo) {
        int cameraId = Camera.getNumberOfCameras() - 1;

        // Find a back-facing camera, otherwise use the first camera.
        for (; cameraId > 0; cameraId--) {
            Camera.getCameraInfo(cameraId, cameraInfo);
            if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK)
                break;
        }

        return cameraId;
    }

    public ScanActivity() {
        super();

        mCameraId = findCamera(mCameraInfo);
        assert mCameraId >= 0;

        // Create the decoder thread
        mScanAsyncTask = new ScanAsyncTask() {
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Token token = TokenPersistence.addWithToast(ScanActivity.this, result);
                if (token == null || token.getImage() == null) {
                    finish();
                    return;
                }

                final ImageView image = (ImageView) findViewById(R.id.image);<commit_msg>Fix barcode scanner preview orientation on Nexus 5x<commit_after>/*
 * FreeOTP
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * Copyright (C) 2013  <NAME>, Red Hat
 *
 * 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 org.fedorahosted.freeotp.add;

import java.util.List;

import org.fedorahosted.freeotp.R;
import org.fedorahosted.freeotp.Token;
import org.fedorahosted.freeotp.TokenPersistence;

import android.annotation.TargetApi;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class ScanActivity extends Activity implements SurfaceHolder.Callback {
    private final CameraInfo    mCameraInfo  = new CameraInfo();
    private final ScanAsyncTask mScanAsyncTask;
    private final int           mCameraId;
    private Handler             mHandler;
    private Camera              mCamera;

    private static class AutoFocusHandler extends Handler implements Camera.AutoFocusCallback {
        private final Camera mCamera;

        public AutoFocusHandler(Camera camera) {
            mCamera = camera;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mCamera.autoFocus(this);
        }

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            sendEmptyMessageDelayed(0, 1000);
        }
    }

    public static boolean haveCamera() {
        return findCamera(new CameraInfo()) >= 0;
    }

    private static int findCamera(CameraInfo cameraInfo) {
        int cameraId = Camera.getNumberOfCameras();

        // Find a back-facing camera. Otherwise, use the first camera.
        while (cameraId-- > 0) {
            Camera.getCameraInfo(cameraId, cameraInfo);
            if (cameraId == 0 || cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK)
                break;
        }

        return cameraId;
    }

    public ScanActivity() {
        super();

        mCameraId = findCamera(mCameraInfo);
        assert mCameraId >= 0;

        // Create the decoder thread
        mScanAsyncTask = new ScanAsyncTask() {
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Token token = TokenPersistence.addWithToast(ScanActivity.this, result);
                if (token == null || token.getImage() == null) {
                    finish();
                    return;
                }

                final ImageView image = (ImageView) findViewById(R.id.image);
{'fraction_non_alphanumeric': 0.08248025738520035, 'fraction_numerical': 0.005118455688797894, 'mean_word_length': 2.6689914163090127, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '3749920', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1759, 'n_words': 538}