<commit_before>/*
 * Copyright 2014 OpenMarket Ltd
 * Copyright 2018 New Vector Ltd
 *
 * 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 im.vector.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaActionSound;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.opengl.GLES20;
import android.os.Build;
import android.os.Bundle;
import android.os.HandlerThread;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.VideoView;

import org.jetbrains.annotations.NotNull;
import org.matrix.androidsdk.util.ImageUtils;
import org.matrix.androidsdk.util.Log;
import org.matrix.androidsdk.util.ResourceUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;

import im.vector.R;
import im.vector.VectorApp;
import im.vector.listeners.ImageViewOnTouchListener;
import im.vector.ui.themes.ActivityOtherThemes;
import im.vector.util.PermissionsToolsKt;
import im.vector.util.ViewUtilKt;
import im.vector.view.RecentMediaLayout;
import im.vector.view.VideoRecordView;
import kotlin.Triple;

/**
 * VectorMediasPickerActivity is used to take a photo or to send an old one.
 */
public class VectorMediasPickerActivity extends MXCActionBarActivity implements TextureView.SurfaceTextureListener {
    private static final String LOG_TAG = VectorMediasPickerActivity.class.getSimpleName();

    // public keys
    // boolean, display a mask to show the avatar rendering
    public static final String EXTRA_AVATAR_MODE = "EXTRA_AVATAR_MODE";

    // boolean, tell if the video recording is supported
    public static final String EXTRA_VIDEO_RECORDING_MODE = "EXTRA_VIDEO_RECORDING_MODE";

    // internal keys
    private static final String KEY_EXTRA_IS_TAKEN_IMAGE_DISPLAYED = "IS_TAKEN_IMAGE_DISPLAYED";
    private static final String KEY_EXTRA_TAKEN_IMAGE_ORIGIN = "TAKEN_IMAGE_ORIGIN";
    private static final String KEY_EXTRA_TAKEN_IMAGE_GALLERY_URI = "TAKEN_IMAGE_GALLERY_URI";
    private static final String KEY_EXTRA_TAKEN_IMAGE_CAMERA_URL = "TAKEN_IMAGE_CAMERA_URL";
    private static final String KEY_EXTRA_CAMERA_SIDE = "TAKEN_IMAGE_CAMERA_SIDE";
    private static final String KEY_PREFERENCE_CAMERA_IMAGE_NAME = "KEY_PREFERENCE_CAMERA_IMAGE_NAME";
    private static final String KEY_IS_AVATAR_MODE = "KEY_IS_AVATAR_MODE";
    private static final String KEY_EXTRA_TAKEN_VIDEO_URI = "KEY_EXTRA_TAKEN_VIDEO_URI";

    // activity request
    private static final int REQUEST_MEDIAS = 54;

    // common definitions
    private static final int JPEG_QUALITY_MAX = 100;
    private static final String MIME_TYPE_IMAGE_GIF = "image/gif";
    private static final int AVATAR_COMPRESSION_LEVEL = 50;

    private static final int GALLERY_COLUMN_COUNT = 4;
    private static final int GALLERY_RAW_COUNT = 3;
    private static final double SURFACE_VIEW_HEIGHT_RATIO = 0.95;
    private static final int GALLERY_TABLE_ITEM_SIZE = (GALLERY_COLUMN_COUNT * GALLERY_RAW_COUNT);

    private static final int IMAGE_ORIGIN_CAMERA = 1;
    private static final int IMAGE_ORIGIN_GALLERY = 2;

    private static final boolean UI_SHOW_TAKEN_IMAGE = true;
    private static final boolean UI_SHOW_CAMERA_PREVIEW = false;

    /**
     * define a recent media
     */
    private class MediaStoreMedia {
        // the media file URI
        public Uri mFileUri;

        // the media creation time
        public long mCreationTime;

        // the media thumbnail
        public Bitmap mThumbnail;

        // tell if the media is a video
        public boolean mIsVideo;

        // mime type
        public String mMimeType = "";
    }

    // recents medias list
    private final List<MediaStoreMedia> mMediaStoreMediasList = new ArrayList<>();

    //
    private MediaStoreMedia mSelectedGalleryImage;

    // camera object
    private Camera mCamera;
    private int mCameraId;
    private int mCameraOrientation = 0;

    // graphical items
    private View mSwitchCameraImageView;

    // camera preview and gallery selection layout
    private View mPreviewScrollView;
    private ImageView mTakeImageView;
    private TableLayout mGalleryTableLayout;
    private RelativeLayout mCameraPreviewLayout;
    private TextureView mCameraTextureView;
    private ImageView mCameraTextureMaskView;
    private SurfaceTexture mSurfaceTexture;

    // preview UI items
    private View mPreviewLayout;

    // image preview
    private View mImagePreviewLayout;
    private ImageView mImagePreviewImageView;
    private ImageView mImagePreviewAvatarModeMaskView;
    private ImageViewOnTouchListener imageViewOnTouchListener = new ImageViewOnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mIsAvatarMode) {
                return (super.onTouch(v, event));
            }
            return true;
        }
    };

    // video preview
    private View mVideoPreviewLayout;
    private VideoView mVideoView;
    private ImageView mVideoButtonView;

    // gallery management
    private RelativeLayout mPreviewAndGalleryLayout;<commit_msg>used ternary operator to get rid of brackets<commit_after>/*
 * Copyright 2014 OpenMarket Ltd
 * Copyright 2018 New Vector Ltd
 *
 * 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 im.vector.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaActionSound;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.opengl.GLES20;
import android.os.Build;
import android.os.Bundle;
import android.os.HandlerThread;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.VideoView;

import org.jetbrains.annotations.NotNull;
import org.matrix.androidsdk.util.ImageUtils;
import org.matrix.androidsdk.util.Log;
import org.matrix.androidsdk.util.ResourceUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;

import im.vector.R;
import im.vector.VectorApp;
import im.vector.listeners.ImageViewOnTouchListener;
import im.vector.ui.themes.ActivityOtherThemes;
import im.vector.util.PermissionsToolsKt;
import im.vector.util.ViewUtilKt;
import im.vector.view.RecentMediaLayout;
import im.vector.view.VideoRecordView;
import kotlin.Triple;

/**
 * VectorMediasPickerActivity is used to take a photo or to send an old one.
 */
public class VectorMediasPickerActivity extends MXCActionBarActivity implements TextureView.SurfaceTextureListener {
    private static final String LOG_TAG = VectorMediasPickerActivity.class.getSimpleName();

    // public keys
    // boolean, display a mask to show the avatar rendering
    public static final String EXTRA_AVATAR_MODE = "EXTRA_AVATAR_MODE";

    // boolean, tell if the video recording is supported
    public static final String EXTRA_VIDEO_RECORDING_MODE = "EXTRA_VIDEO_RECORDING_MODE";

    // internal keys
    private static final String KEY_EXTRA_IS_TAKEN_IMAGE_DISPLAYED = "IS_TAKEN_IMAGE_DISPLAYED";
    private static final String KEY_EXTRA_TAKEN_IMAGE_ORIGIN = "TAKEN_IMAGE_ORIGIN";
    private static final String KEY_EXTRA_TAKEN_IMAGE_GALLERY_URI = "TAKEN_IMAGE_GALLERY_URI";
    private static final String KEY_EXTRA_TAKEN_IMAGE_CAMERA_URL = "TAKEN_IMAGE_CAMERA_URL";
    private static final String KEY_EXTRA_CAMERA_SIDE = "TAKEN_IMAGE_CAMERA_SIDE";
    private static final String KEY_PREFERENCE_CAMERA_IMAGE_NAME = "KEY_PREFERENCE_CAMERA_IMAGE_NAME";
    private static final String KEY_IS_AVATAR_MODE = "KEY_IS_AVATAR_MODE";
    private static final String KEY_EXTRA_TAKEN_VIDEO_URI = "KEY_EXTRA_TAKEN_VIDEO_URI";

    // activity request
    private static final int REQUEST_MEDIAS = 54;

    // common definitions
    private static final int JPEG_QUALITY_MAX = 100;
    private static final String MIME_TYPE_IMAGE_GIF = "image/gif";
    private static final int AVATAR_COMPRESSION_LEVEL = 50;

    private static final int GALLERY_COLUMN_COUNT = 4;
    private static final int GALLERY_RAW_COUNT = 3;
    private static final double SURFACE_VIEW_HEIGHT_RATIO = 0.95;
    private static final int GALLERY_TABLE_ITEM_SIZE = (GALLERY_COLUMN_COUNT * GALLERY_RAW_COUNT);

    private static final int IMAGE_ORIGIN_CAMERA = 1;
    private static final int IMAGE_ORIGIN_GALLERY = 2;

    private static final boolean UI_SHOW_TAKEN_IMAGE = true;
    private static final boolean UI_SHOW_CAMERA_PREVIEW = false;

    /**
     * define a recent media
     */
    private class MediaStoreMedia {
        // the media file URI
        public Uri mFileUri;

        // the media creation time
        public long mCreationTime;

        // the media thumbnail
        public Bitmap mThumbnail;

        // tell if the media is a video
        public boolean mIsVideo;

        // mime type
        public String mMimeType = "";
    }

    // recents medias list
    private final List<MediaStoreMedia> mMediaStoreMediasList = new ArrayList<>();

    //
    private MediaStoreMedia mSelectedGalleryImage;

    // camera object
    private Camera mCamera;
    private int mCameraId;
    private int mCameraOrientation = 0;

    // graphical items
    private View mSwitchCameraImageView;

    // camera preview and gallery selection layout
    private View mPreviewScrollView;
    private ImageView mTakeImageView;
    private TableLayout mGalleryTableLayout;
    private RelativeLayout mCameraPreviewLayout;
    private TextureView mCameraTextureView;
    private ImageView mCameraTextureMaskView;
    private SurfaceTexture mSurfaceTexture;

    // preview UI items
    private View mPreviewLayout;

    // image preview
    private View mImagePreviewLayout;
    private ImageView mImagePreviewImageView;
    private ImageView mImagePreviewAvatarModeMaskView;
    private ImageViewOnTouchListener imageViewOnTouchListener = new ImageViewOnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return (mIsAvatarMode)?  super.onTouch(v, event) : true;
        }
    };

    // video preview
    private View mVideoPreviewLayout;
    private VideoView mVideoView;
    private ImageView mVideoButtonView;

    // gallery management
    private RelativeLayout mPreviewAndGalleryLayout;
{'fraction_non_alphanumeric': 0.06745328058927841, 'fraction_numerical': 0.00422861819669895, 'mean_word_length': 4.430740740740741, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1920075', 'n_tokens_mistral': 4266, 'n_tokens_neox': 4128, 'n_words': 1152}