Search in sources :

Example 1 with CameraView

use of org.telegram.messenger.camera.CameraView in project Telegram-FOSS by Telegram-FOSS-Team.

the class ChatAttachAlertPhotoLayout method onShown.

@Override
void onShown() {
    isHidden = false;
    if (cameraView != null) {
        cameraView.setVisibility(VISIBLE);
    }
    if (cameraIcon != null) {
        cameraIcon.setVisibility(VISIBLE);
    }
    if (cameraView != null) {
        int count = gridView.getChildCount();
        for (int a = 0; a < count; a++) {
            View child = gridView.getChildAt(a);
            if (child instanceof PhotoAttachCameraCell) {
                child.setVisibility(View.INVISIBLE);
                break;
            }
        }
    }
    if (checkCameraWhenShown) {
        checkCameraWhenShown = false;
        checkCamera(true);
    }
}
Also used : PhotoAttachCameraCell(org.telegram.ui.Cells.PhotoAttachCameraCell) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) TextureView(android.view.TextureView) CameraView(org.telegram.messenger.camera.CameraView) Paint(android.graphics.Paint)

Example 2 with CameraView

use of org.telegram.messenger.camera.CameraView in project Telegram-FOSS by Telegram-FOSS-Team.

the class ChatAttachAlertPhotoLayout method checkCameraViewPosition.

private void checkCameraViewPosition() {
    if (Build.VERSION.SDK_INT >= 21) {
        if (cameraView != null) {
            cameraView.invalidateOutline();
        }
        RecyclerView.ViewHolder holder = gridView.findViewHolderForAdapterPosition(itemsPerRow - 1);
        if (holder != null) {
            holder.itemView.invalidateOutline();
        }
        if (!adapter.needCamera || !deviceHasGoodCamera || selectedAlbumEntry != galleryAlbumEntry) {
            holder = gridView.findViewHolderForAdapterPosition(0);
            if (holder != null) {
                holder.itemView.invalidateOutline();
            }
        }
    }
    if (cameraView != null) {
        cameraView.invalidate();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && recordTime != null) {
        MarginLayoutParams params = (MarginLayoutParams) recordTime.getLayoutParams();
        params.topMargin = (getRootWindowInsets() == null ? AndroidUtilities.dp(16) : getRootWindowInsets().getSystemWindowInsetTop() + AndroidUtilities.dp(2));
    }
    if (!deviceHasGoodCamera) {
        return;
    }
    int count = gridView.getChildCount();
    for (int a = 0; a < count; a++) {
        View child = gridView.getChildAt(a);
        if (child instanceof PhotoAttachCameraCell) {
            if (Build.VERSION.SDK_INT >= 19) {
                if (!child.isAttachedToWindow()) {
                    break;
                }
            }
            float topLocal = child.getY() + gridView.getY() + getY();
            float top = topLocal + parentAlert.getSheetContainer().getY();
            float left = child.getX() + gridView.getX() + getX() + parentAlert.getSheetContainer().getX();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                left -= getRootWindowInsets().getSystemWindowInsetLeft();
            }
            float maxY = (Build.VERSION.SDK_INT >= 21 && !parentAlert.inBubbleMode ? AndroidUtilities.statusBarHeight : 0) + ActionBar.getCurrentActionBarHeight();
            float newCameraViewOffsetY;
            if (topLocal < maxY) {
                newCameraViewOffsetY = maxY - topLocal;
            } else {
                newCameraViewOffsetY = 0;
            }
            if (newCameraViewOffsetY != cameraViewOffsetY) {
                cameraViewOffsetY = newCameraViewOffsetY;
                if (cameraView != null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        cameraView.invalidateOutline();
                    } else {
                        cameraView.invalidate();
                    }
                }
                if (cameraIcon != null) {
                    cameraIcon.invalidate();
                }
            }
            int containerHeight = parentAlert.getSheetContainer().getMeasuredHeight();
            maxY = (int) (containerHeight - parentAlert.buttonsRecyclerView.getMeasuredHeight() + parentAlert.buttonsRecyclerView.getTranslationY());
            if (topLocal + child.getMeasuredHeight() > maxY) {
                cameraViewOffsetBottomY = topLocal + child.getMeasuredHeight() - maxY;
            } else {
                cameraViewOffsetBottomY = 0;
            }
            cameraViewLocation[0] = left;
            cameraViewLocation[1] = top;
            applyCameraViewPosition();
            return;
        }
    }
    if (cameraViewOffsetY != 0 || cameraViewOffsetX != 0) {
        cameraViewOffsetX = 0;
        cameraViewOffsetY = 0;
        if (cameraView != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                cameraView.invalidateOutline();
            } else {
                cameraView.invalidate();
            }
        }
        if (cameraIcon != null) {
            cameraIcon.invalidate();
        }
    }
    cameraViewLocation[0] = AndroidUtilities.dp(-400);
    cameraViewLocation[1] = 0;
    applyCameraViewPosition();
}
Also used : PhotoAttachCameraCell(org.telegram.ui.Cells.PhotoAttachCameraCell) RecyclerView(androidx.recyclerview.widget.RecyclerView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) TextureView(android.view.TextureView) CameraView(org.telegram.messenger.camera.CameraView) Paint(android.graphics.Paint)

Example 3 with CameraView

use of org.telegram.messenger.camera.CameraView in project Telegram-FOSS by Telegram-FOSS-Team.

the class CameraScanActivity method createView.

@Override
public View createView(Context context) {
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setItemsColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText2), false);
    actionBar.setItemsBackgroundColor(Theme.getColor(Theme.key_actionBarWhiteSelector), false);
    actionBar.setCastShadows(false);
    if (!AndroidUtilities.isTablet() && !isQr()) {
        actionBar.showActionModeTop();
    }
    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {

        @Override
        public void onItemClick(int id) {
            if (id == -1) {
                finishFragment();
            }
        }
    });
    paint.setColor(0x7f000000);
    cornerPaint.setColor(0xffffffff);
    cornerPaint.setStyle(Paint.Style.STROKE);
    cornerPaint.setStrokeWidth(AndroidUtilities.dp(4));
    cornerPaint.setStrokeJoin(Paint.Join.ROUND);
    ViewGroup viewGroup = new ViewGroup(context) {

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            actionBar.measure(widthMeasureSpec, heightMeasureSpec);
            if (currentType == TYPE_MRZ) {
                cameraView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) (width * 0.704f), MeasureSpec.EXACTLY));
            } else {
                cameraView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                recognizedMrzView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED));
                if (galleryButton != null) {
                    galleryButton.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(60), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(60), MeasureSpec.EXACTLY));
                }
                flashButton.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(60), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(60), MeasureSpec.EXACTLY));
            }
            titleTextView.measure(MeasureSpec.makeMeasureSpec(width - AndroidUtilities.dp(72), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED));
            descriptionText.measure(MeasureSpec.makeMeasureSpec((int) (width * 0.9f), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED));
            setMeasuredDimension(width, height);
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            int width = r - l;
            int height = b - t;
            int y = 0;
            if (currentType == TYPE_MRZ) {
                cameraView.layout(0, y, cameraView.getMeasuredWidth(), y + cameraView.getMeasuredHeight());
                y = (int) (height * 0.65f);
                titleTextView.layout(AndroidUtilities.dp(36), y, AndroidUtilities.dp(36) + titleTextView.getMeasuredWidth(), y + titleTextView.getMeasuredHeight());
                recognizedMrzView.setTextSize(TypedValue.COMPLEX_UNIT_PX, cameraView.getMeasuredHeight() / 22);
                recognizedMrzView.setPadding(0, 0, 0, cameraView.getMeasuredHeight() / 15);
            } else {
                actionBar.layout(0, 0, actionBar.getMeasuredWidth(), actionBar.getMeasuredHeight());
                cameraView.layout(0, 0, cameraView.getMeasuredWidth(), cameraView.getMeasuredHeight());
                int size = (int) (Math.min(cameraView.getWidth(), cameraView.getHeight()) / 1.5f);
                if (currentType == TYPE_QR) {
                    y = (cameraView.getMeasuredHeight() - size) / 2 - titleTextView.getMeasuredHeight() - AndroidUtilities.dp(30);
                } else {
                    y = (cameraView.getMeasuredHeight() - size) / 2 - titleTextView.getMeasuredHeight() - AndroidUtilities.dp(64);
                }
                titleTextView.layout(AndroidUtilities.dp(36), y, AndroidUtilities.dp(36) + titleTextView.getMeasuredWidth(), y + titleTextView.getMeasuredHeight());
                recognizedMrzView.layout(0, getMeasuredHeight() - recognizedMrzView.getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight());
                int x;
                if (needGalleryButton) {
                    x = cameraView.getMeasuredWidth() / 2 + AndroidUtilities.dp(35);
                } else {
                    x = cameraView.getMeasuredWidth() / 2 - flashButton.getMeasuredWidth() / 2;
                }
                y = (cameraView.getMeasuredHeight() - size) / 2 + size + AndroidUtilities.dp(30);
                flashButton.layout(x, y, x + flashButton.getMeasuredWidth(), y + flashButton.getMeasuredHeight());
                if (galleryButton != null) {
                    x = cameraView.getMeasuredWidth() / 2 - AndroidUtilities.dp(35) - galleryButton.getMeasuredWidth();
                    galleryButton.layout(x, y, x + galleryButton.getMeasuredWidth(), y + galleryButton.getMeasuredHeight());
                }
            }
            y = (int) (height * 0.74f);
            int x = (int) (width * 0.05f);
            descriptionText.layout(x, y, x + descriptionText.getMeasuredWidth(), y + descriptionText.getMeasuredHeight());
        }

        @Override
        protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
            boolean result = super.drawChild(canvas, child, drawingTime);
            if (isQr() && child == cameraView) {
                int size = (int) (Math.min(child.getWidth(), child.getHeight()) / 1.5f);
                int x = (child.getWidth() - size) / 2;
                int y = (child.getHeight() - size) / 2;
                canvas.drawRect(0, 0, child.getMeasuredWidth(), y, paint);
                canvas.drawRect(0, y + size, child.getMeasuredWidth(), child.getMeasuredHeight(), paint);
                canvas.drawRect(0, y, x, y + size, paint);
                canvas.drawRect(x + size, y, child.getMeasuredWidth(), y + size, paint);
                path.reset();
                path.moveTo(x, y + AndroidUtilities.dp(20));
                path.lineTo(x, y);
                path.lineTo(x + AndroidUtilities.dp(20), y);
                canvas.drawPath(path, cornerPaint);
                path.reset();
                path.moveTo(x + size, y + AndroidUtilities.dp(20));
                path.lineTo(x + size, y);
                path.lineTo(x + size - AndroidUtilities.dp(20), y);
                canvas.drawPath(path, cornerPaint);
                path.reset();
                path.moveTo(x, y + size - AndroidUtilities.dp(20));
                path.lineTo(x, y + size);
                path.lineTo(x + AndroidUtilities.dp(20), y + size);
                canvas.drawPath(path, cornerPaint);
                path.reset();
                path.moveTo(x + size, y + size - AndroidUtilities.dp(20));
                path.lineTo(x + size, y + size);
                path.lineTo(x + size - AndroidUtilities.dp(20), y + size);
                canvas.drawPath(path, cornerPaint);
            }
            return result;
        }
    };
    viewGroup.setOnTouchListener((v, event) -> true);
    fragmentView = viewGroup;
    cameraView = new CameraView(context, false);
    cameraView.setUseMaxPreview(true);
    cameraView.setOptimizeForBarcode(true);
    cameraView.setDelegate(new CameraView.CameraViewDelegate() {

        @Override
        public void onCameraCreated(Camera camera) {
        }

        @Override
        public void onCameraInit() {
            startRecognizing();
        }
    });
    viewGroup.addView(cameraView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    if (currentType == TYPE_MRZ) {
        actionBar.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
        fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
    } else {
        actionBar.setBackgroundDrawable(null);
        actionBar.setAddToContainer(false);
        actionBar.setItemsColor(0xffffffff, false);
        actionBar.setItemsBackgroundColor(0x22ffffff, false);
        viewGroup.setBackgroundColor(Theme.getColor(Theme.key_wallet_blackBackground));
        viewGroup.addView(actionBar);
    }
    if (currentType == TYPE_QR_LOGIN) {
        actionBar.setTitle(LocaleController.getString("AuthAnotherClientScan", R.string.AuthAnotherClientScan));
    }
    Paint selectionPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    selectionPaint.setColor(ColorUtils.setAlphaComponent(Color.WHITE, 100));
    titleTextView = new TextView(context) {

        LinkPath textPath;

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            if (getText() instanceof Spanned) {
                Spanned spanned = (Spanned) getText();
                URLSpanNoUnderline[] innerSpans = spanned.getSpans(0, spanned.length(), URLSpanNoUnderline.class);
                if (innerSpans != null && innerSpans.length > 0) {
                    textPath = new LinkPath(true);
                    textPath.setAllowReset(false);
                    for (int a = 0; a < innerSpans.length; a++) {
                        int start = spanned.getSpanStart(innerSpans[a]);
                        int end = spanned.getSpanEnd(innerSpans[a]);
                        textPath.setCurrentLayout(getLayout(), start, 0);
                        int shift = getText() != null ? getPaint().baselineShift : 0;
                        textPath.setBaselineShift(shift != 0 ? shift + AndroidUtilities.dp(shift > 0 ? 5 : -2) : 0);
                        getLayout().getSelectionPath(start, end, textPath);
                    }
                    textPath.setAllowReset(true);
                }
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            if (textPath != null) {
                canvas.drawPath(textPath, selectionPaint);
            }
            super.onDraw(canvas);
        }
    };
    titleTextView.setGravity(Gravity.CENTER_HORIZONTAL);
    titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
    viewGroup.addView(titleTextView);
    descriptionText = new TextView(context);
    descriptionText.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText6));
    descriptionText.setGravity(Gravity.CENTER_HORIZONTAL);
    descriptionText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    viewGroup.addView(descriptionText);
    recognizedMrzView = new TextView(context);
    recognizedMrzView.setTextColor(0xffffffff);
    recognizedMrzView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
    recognizedMrzView.setAlpha(0);
    if (currentType == TYPE_MRZ) {
        titleTextView.setText(LocaleController.getString("PassportScanPassport", R.string.PassportScanPassport));
        descriptionText.setText(LocaleController.getString("PassportScanPassportInfo", R.string.PassportScanPassportInfo));
        titleTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
        recognizedMrzView.setTypeface(Typeface.MONOSPACE);
        cameraView.addView(recognizedMrzView);
    } else {
        if (needGalleryButton) {
        // titleTextView.setText(LocaleController.getString("WalletScanCode", R.string.WalletScanCode));
        } else {
            if (currentType == TYPE_QR) {
                titleTextView.setText(LocaleController.getString("AuthAnotherClientScan", R.string.AuthAnotherClientScan));
            } else {
                String text = LocaleController.getString("AuthAnotherClientInfo5", R.string.AuthAnotherClientInfo5);
                SpannableStringBuilder spanned = new SpannableStringBuilder(text);
                int index1 = text.indexOf('*');
                int index2 = text.indexOf('*', index1 + 1);
                if (index1 != -1 && index2 != -1 && index1 != index2) {
                    titleTextView.setMovementMethod(new AndroidUtilities.LinkMovementMethodMy());
                    spanned.replace(index2, index2 + 1, " ");
                    spanned.replace(index1, index1 + 1, " ");
                    index1 += 1;
                    index2 += 1;
                    spanned.setSpan(new URLSpanNoUnderline(LocaleController.getString("AuthAnotherClientDownloadClientUrl", R.string.AuthAnotherClientDownloadClientUrl)), index1, index2 - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spanned.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), index1, index2 - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                text = spanned.toString();
                index1 = text.indexOf('*');
                index2 = text.indexOf('*', index1 + 1);
                if (index1 != -1 && index2 != -1 && index1 != index2) {
                    spanned.replace(index2, index2 + 1, " ");
                    spanned.replace(index1, index1 + 1, " ");
                    index1 += 1;
                    index2 += 1;
                    spanned.setSpan(new URLSpanNoUnderline(LocaleController.getString("AuthAnotherWebClientUrl", R.string.AuthAnotherWebClientUrl)), index1, index2 - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spanned.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), index1, index2 - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                titleTextView.setLinkTextColor(Color.WHITE);
                titleTextView.setHighlightColor(Theme.getColor(Theme.key_windowBackgroundWhiteLinkSelection));
                titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                titleTextView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
                titleTextView.setPadding(0, 0, 0, 0);
                titleTextView.setText(spanned);
            }
        }
        titleTextView.setTextColor(0xffffffff);
        recognizedMrzView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        recognizedMrzView.setPadding(AndroidUtilities.dp(10), 0, AndroidUtilities.dp(10), AndroidUtilities.dp(10));
        if (needGalleryButton) {
        // recognizedMrzView.setText(LocaleController.getString("WalletScanCodeNotFound", R.string.WalletScanCodeNotFound));
        } else {
            recognizedMrzView.setText(LocaleController.getString("AuthAnotherClientNotFound", R.string.AuthAnotherClientNotFound));
        }
        viewGroup.addView(recognizedMrzView);
        if (needGalleryButton) {
            galleryButton = new ImageView(context);
            galleryButton.setScaleType(ImageView.ScaleType.CENTER);
            galleryButton.setImageResource(R.drawable.qr_gallery);
            galleryButton.setBackgroundDrawable(Theme.createSelectorDrawableFromDrawables(Theme.createCircleDrawable(AndroidUtilities.dp(60), 0x22ffffff), Theme.createCircleDrawable(AndroidUtilities.dp(60), 0x44ffffff)));
            viewGroup.addView(galleryButton);
            galleryButton.setOnClickListener(currentImage -> {
                if (getParentActivity() == null) {
                    return;
                }
                if (Build.VERSION.SDK_INT >= 23) {
                    if (getParentActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        getParentActivity().requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 4);
                        return;
                    }
                }
                PhotoAlbumPickerActivity fragment = new PhotoAlbumPickerActivity(PhotoAlbumPickerActivity.SELECT_TYPE_QR, false, false, null);
                fragment.setMaxSelectedPhotos(1, false);
                fragment.setAllowSearchImages(false);
                fragment.setDelegate(new PhotoAlbumPickerActivity.PhotoAlbumPickerActivityDelegate() {

                    @Override
                    public void didSelectPhotos(ArrayList<SendMessagesHelper.SendingMediaInfo> photos, boolean notify, int scheduleDate) {
                        try {
                            if (!photos.isEmpty()) {
                                SendMessagesHelper.SendingMediaInfo info = photos.get(0);
                                if (info.path != null) {
                                    Point screenSize = AndroidUtilities.getRealScreenSize();
                                    Bitmap bitmap = ImageLoader.loadBitmap(info.path, null, screenSize.x, screenSize.y, true);
                                    String text = tryReadQr(null, null, 0, 0, 0, bitmap);
                                    if (text != null) {
                                        if (delegate != null) {
                                            delegate.didFindQr(text);
                                        }
                                        removeSelfFromStack();
                                    }
                                }
                            }
                        } catch (Throwable e) {
                            FileLog.e(e);
                        }
                    }

                    @Override
                    public void startPhotoSelectActivity() {
                        try {
                            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                            photoPickerIntent.setType("image/*");
                            getParentActivity().startActivityForResult(photoPickerIntent, 11);
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                    }
                });
                presentFragment(fragment);
            });
        }
        flashButton = new ImageView(context);
        flashButton.setScaleType(ImageView.ScaleType.CENTER);
        flashButton.setImageResource(R.drawable.qr_flashlight);
        flashButton.setBackgroundDrawable(Theme.createCircleDrawable(AndroidUtilities.dp(60), 0x22ffffff));
        viewGroup.addView(flashButton);
        flashButton.setOnClickListener(currentImage -> {
            CameraSession session = cameraView.getCameraSession();
            if (session != null) {
                ShapeDrawable shapeDrawable = (ShapeDrawable) flashButton.getBackground();
                if (flashAnimator != null) {
                    flashAnimator.cancel();
                    flashAnimator = null;
                }
                flashAnimator = new AnimatorSet();
                ObjectAnimator animator = ObjectAnimator.ofInt(shapeDrawable, AnimationProperties.SHAPE_DRAWABLE_ALPHA, flashButton.getTag() == null ? 0x44 : 0x22);
                animator.addUpdateListener(animation -> flashButton.invalidate());
                flashAnimator.playTogether(animator);
                flashAnimator.setDuration(200);
                flashAnimator.setInterpolator(CubicBezierInterpolator.DEFAULT);
                flashAnimator.addListener(new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        flashAnimator = null;
                    }
                });
                flashAnimator.start();
                if (flashButton.getTag() == null) {
                    flashButton.setTag(1);
                    session.setTorchEnabled(true);
                } else {
                    flashButton.setTag(null);
                    session.setTorchEnabled(false);
                }
            }
        });
    }
    if (getParentActivity() != null) {
        getParentActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    fragmentView.setKeepScreenOn(true);
    return fragmentView;
}
Also used : AnimatorSet(android.animation.AnimatorSet) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) LinkPath(org.telegram.ui.Components.LinkPath) CameraSession(org.telegram.messenger.camera.CameraSession) TextView(android.widget.TextView) Camera(android.hardware.Camera) ImageView(android.widget.ImageView) URLSpanNoUnderline(org.telegram.ui.Components.URLSpanNoUnderline) ActionBar(org.telegram.ui.ActionBar.ActionBar) ViewGroup(android.view.ViewGroup) ObjectAnimator(android.animation.ObjectAnimator) Canvas(android.graphics.Canvas) Intent(android.content.Intent) Paint(android.graphics.Paint) Point(android.graphics.Point) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) CameraView(org.telegram.messenger.camera.CameraView) Spanned(android.text.Spanned) Paint(android.graphics.Paint) Point(android.graphics.Point) CameraView(org.telegram.messenger.camera.CameraView) NotFoundException(com.google.zxing.NotFoundException) AndroidUtilities(org.telegram.messenger.AndroidUtilities) Animator(android.animation.Animator) ObjectAnimator(android.animation.ObjectAnimator) ShapeDrawable(android.graphics.drawable.ShapeDrawable) SpannableStringBuilder(android.text.SpannableStringBuilder) TypefaceSpan(org.telegram.ui.Components.TypefaceSpan)

Example 4 with CameraView

use of org.telegram.messenger.camera.CameraView in project Telegram-FOSS by Telegram-FOSS-Team.

the class ChatAttachAlertPhotoLayout method hideCamera.

public void hideCamera(boolean async) {
    if (!deviceHasGoodCamera || cameraView == null) {
        return;
    }
    saveLastCameraBitmap();
    int count = gridView.getChildCount();
    for (int a = 0; a < count; a++) {
        View child = gridView.getChildAt(a);
        if (child instanceof PhotoAttachCameraCell) {
            child.setVisibility(View.VISIBLE);
            ((PhotoAttachCameraCell) child).updateBitmap();
            break;
        }
    }
    cameraView.destroy(async, null);
    if (cameraInitAnimation != null) {
        cameraInitAnimation.cancel();
        cameraInitAnimation = null;
    }
    AndroidUtilities.runOnUIThread(() -> {
        parentAlert.getContainer().removeView(cameraView);
        parentAlert.getContainer().removeView(cameraIcon);
        cameraView = null;
        cameraIcon = null;
    }, 300);
    canSaveCameraPreview = false;
}
Also used : PhotoAttachCameraCell(org.telegram.ui.Cells.PhotoAttachCameraCell) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) TextureView(android.view.TextureView) CameraView(org.telegram.messenger.camera.CameraView) Paint(android.graphics.Paint)

Example 5 with CameraView

use of org.telegram.messenger.camera.CameraView in project Telegram-FOSS by Telegram-FOSS-Team.

the class ChatAttachAlertPhotoLayout method showCamera.

public void showCamera() {
    if (parentAlert.paused || !mediaEnabled) {
        return;
    }
    if (cameraView == null) {
        cameraView = new CameraView(parentAlert.baseFragment.getParentActivity(), parentAlert.openWithFrontFaceCamera) {

            @Override
            protected void dispatchDraw(Canvas canvas) {
                if (Build.VERSION.SDK_INT >= 21) {
                    super.dispatchDraw(canvas);
                } else {
                    if (cameraAnimationInProgress) {
                        AndroidUtilities.rectTmp.set(animationClipLeft + cameraViewOffsetX * (1f - cameraOpenProgress), animationClipTop + cameraViewOffsetY * (1f - cameraOpenProgress), animationClipRight, animationClipBottom);
                    } else if (!cameraAnimationInProgress && !cameraOpened) {
                        AndroidUtilities.rectTmp.set(cameraViewOffsetX, cameraViewOffsetY, getMeasuredWidth(), getMeasuredHeight());
                    } else {
                        AndroidUtilities.rectTmp.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
                    }
                    canvas.save();
                    canvas.clipRect(AndroidUtilities.rectTmp);
                    super.dispatchDraw(canvas);
                    canvas.restore();
                }
            }
        };
        cameraView.setRecordFile(AndroidUtilities.generateVideoPath(parentAlert.baseFragment instanceof ChatActivity && ((ChatActivity) parentAlert.baseFragment).isSecretChat()));
        cameraView.setFocusable(true);
        cameraView.setFpsLimit(30);
        if (Build.VERSION.SDK_INT >= 21) {
            Path path = new Path();
            float[] radii = new float[8];
            cameraView.setOutlineProvider(new ViewOutlineProvider() {

                @Override
                public void getOutline(View view, Outline outline) {
                    if (cameraAnimationInProgress) {
                        AndroidUtilities.rectTmp.set(animationClipLeft + cameraViewOffsetX * (1f - cameraOpenProgress), animationClipTop + cameraViewOffsetY * (1f - cameraOpenProgress), animationClipRight, animationClipBottom);
                        outline.setRect((int) AndroidUtilities.rectTmp.left, (int) AndroidUtilities.rectTmp.top, (int) AndroidUtilities.rectTmp.right, (int) AndroidUtilities.rectTmp.bottom);
                    } else if (!cameraAnimationInProgress && !cameraOpened) {
                        int rad = AndroidUtilities.dp(8 * parentAlert.cornerRadius);
                        outline.setRoundRect((int) cameraViewOffsetX, (int) cameraViewOffsetY, view.getMeasuredWidth() + rad, view.getMeasuredHeight() + rad, rad);
                    } else {
                        outline.setRect(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
                    }
                }
            });
            cameraView.setClipToOutline(true);
        }
        cameraView.setContentDescription(LocaleController.getString("AccDescrInstantCamera", R.string.AccDescrInstantCamera));
        parentAlert.getContainer().addView(cameraView, 1, new FrameLayout.LayoutParams(itemSize, itemSize));
        cameraView.setDelegate(new CameraView.CameraViewDelegate() {

            @Override
            public void onCameraCreated(Camera camera) {
            }

            @Override
            public void onCameraInit() {
                String current = cameraView.getCameraSession().getCurrentFlashMode();
                String next = cameraView.getCameraSession().getNextFlashMode();
                if (current.equals(next)) {
                    for (int a = 0; a < 2; a++) {
                        flashModeButton[a].setVisibility(View.INVISIBLE);
                        flashModeButton[a].setAlpha(0.0f);
                        flashModeButton[a].setTranslationY(0.0f);
                    }
                } else {
                    setCameraFlashModeIcon(flashModeButton[0], cameraView.getCameraSession().getCurrentFlashMode());
                    for (int a = 0; a < 2; a++) {
                        flashModeButton[a].setVisibility(a == 0 ? View.VISIBLE : View.INVISIBLE);
                        flashModeButton[a].setAlpha(a == 0 && cameraOpened ? 1.0f : 0.0f);
                        flashModeButton[a].setTranslationY(0.0f);
                    }
                }
                switchCameraButton.setImageResource(cameraView.isFrontface() ? R.drawable.camera_revert1 : R.drawable.camera_revert2);
                switchCameraButton.setVisibility(cameraView.hasFrontFaceCamera() ? View.VISIBLE : View.INVISIBLE);
                if (!cameraOpened) {
                    cameraInitAnimation = new AnimatorSet();
                    cameraInitAnimation.playTogether(ObjectAnimator.ofFloat(cameraView, View.ALPHA, 0.0f, 1.0f), ObjectAnimator.ofFloat(cameraIcon, View.ALPHA, 0.0f, 1.0f));
                    cameraInitAnimation.setDuration(180);
                    cameraInitAnimation.addListener(new AnimatorListenerAdapter() {

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if (animation.equals(cameraInitAnimation)) {
                                canSaveCameraPreview = true;
                                cameraInitAnimation = null;
                                if (!isHidden) {
                                    int count = gridView.getChildCount();
                                    for (int a = 0; a < count; a++) {
                                        View child = gridView.getChildAt(a);
                                        if (child instanceof PhotoAttachCameraCell) {
                                            child.setVisibility(View.INVISIBLE);
                                            break;
                                        }
                                    }
                                }
                            }
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                            cameraInitAnimation = null;
                        }
                    });
                    cameraInitAnimation.start();
                }
            }
        });
        if (cameraIcon == null) {
            cameraIcon = new FrameLayout(parentAlert.baseFragment.getParentActivity()) {

                @Override
                protected void onDraw(Canvas canvas) {
                    int w = cameraDrawable.getIntrinsicWidth();
                    int h = cameraDrawable.getIntrinsicHeight();
                    int x = (itemSize - w) / 2;
                    int y = (itemSize - h) / 2;
                    if (cameraViewOffsetY != 0) {
                        y -= cameraViewOffsetY;
                    }
                    cameraDrawable.setBounds(x, y, x + w, y + h);
                    cameraDrawable.draw(canvas);
                }
            };
            cameraIcon.setWillNotDraw(false);
            cameraIcon.setClipChildren(true);
        }
        parentAlert.getContainer().addView(cameraIcon, 2, new FrameLayout.LayoutParams(itemSize, itemSize));
        cameraView.setAlpha(mediaEnabled ? 1.0f : 0.2f);
        cameraView.setEnabled(mediaEnabled);
        cameraIcon.setAlpha(mediaEnabled ? 1.0f : 0.2f);
        cameraIcon.setEnabled(mediaEnabled);
        if (isHidden) {
            cameraView.setVisibility(GONE);
            cameraIcon.setVisibility(GONE);
        }
        checkCameraViewPosition();
        invalidate();
    }
    if (zoomControlView != null) {
        zoomControlView.setZoom(0.0f, false);
        cameraZoom = 0.0f;
    }
    cameraView.setTranslationX(cameraViewLocation[0]);
    cameraView.setTranslationY(cameraViewLocation[1] + currentPanTranslationY);
    cameraIcon.setTranslationX(cameraViewLocation[0]);
    cameraIcon.setTranslationY(cameraViewLocation[1] + cameraViewOffsetY + currentPanTranslationY);
}
Also used : Path(android.graphics.Path) PhotoAttachCameraCell(org.telegram.ui.Cells.PhotoAttachCameraCell) ChatActivity(org.telegram.ui.ChatActivity) Canvas(android.graphics.Canvas) Outline(android.graphics.Outline) AnimatorSet(android.animation.AnimatorSet) ViewOutlineProvider(android.view.ViewOutlineProvider) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) TextureView(android.view.TextureView) CameraView(org.telegram.messenger.camera.CameraView) CameraView(org.telegram.messenger.camera.CameraView) Paint(android.graphics.Paint) Animator(android.animation.Animator) ObjectAnimator(android.animation.ObjectAnimator) ValueAnimator(android.animation.ValueAnimator) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) FrameLayout(android.widget.FrameLayout) Camera(android.hardware.Camera)

Aggregations

Paint (android.graphics.Paint)6 View (android.view.View)6 ImageView (android.widget.ImageView)6 TextView (android.widget.TextView)6 CameraView (org.telegram.messenger.camera.CameraView)6 TextureView (android.view.TextureView)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)5 PhotoAttachCameraCell (org.telegram.ui.Cells.PhotoAttachCameraCell)4 Animator (android.animation.Animator)2 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)2 AnimatorSet (android.animation.AnimatorSet)2 ObjectAnimator (android.animation.ObjectAnimator)2 Canvas (android.graphics.Canvas)2 Camera (android.hardware.Camera)2 ChatActivity (org.telegram.ui.ChatActivity)2 ValueAnimator (android.animation.ValueAnimator)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 Outline (android.graphics.Outline)1 Path (android.graphics.Path)1