Skip to content

Commit 213eae0

Browse files
authored
CameraX support (#24)
1 parent 9418e66 commit 213eae0

File tree

66 files changed

+3902
-231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3902
-231
lines changed

TMessagesProj/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ dependencies {
4444
implementation 'com.android.billingclient:billing:5.0.0'
4545
implementation files('libs/libgsaverification-client.aar')
4646

47+
// CameraX Implementation
48+
def camerax_version = "1.3.0-alpha02"
49+
implementation "androidx.camera:camera-camera2:${camerax_version}"
50+
implementation "androidx.camera:camera-core:${camerax_version}"
51+
implementation "androidx.camera:camera-extensions:${camerax_version}"
52+
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
53+
implementation "androidx.camera:camera-view:${camerax_version}"
54+
implementation "androidx.camera:camera-video:${camerax_version}"
55+
implementation 'androidx.interpolator:interpolator:1.0.0'
56+
4757
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
4858

4959
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10"

TMessagesProj/proguard-rules.pro

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
-keep class com.google.android.exoplayer2.metadata.flac.PictureFrame { *; }
2828
-keep class com.google.android.exoplayer2.decoder.SimpleOutputBuffer { *; }
2929

30+
# Keep all class member names of CameraX
31+
-keep class androidx.camera.extensions.** { *; }
32+
-keep class androidx.camera.camera2.internal.** { *; }
33+
-keep class androidx.camera.camera2.interop.** { *; }
34+
-keep class androidx.camera.core.** { *; }
35+
-keep class androidx.camera.core.impl.** { *; }
36+
-keep class androidx.camera.video.** { *; }
37+
3038
# https://developers.google.com/ml-kit/known-issues#android_issues
3139
-keep class com.google.mlkit.nl.languageid.internal.LanguageIdentificationJni { *; }
3240

@@ -96,6 +104,7 @@
96104
# Don't warn about checkerframework and Kotlin annotations
97105
-dontwarn org.checkerframework.**
98106
-dontwarn javax.annotation.**
107+
-dontwarn androidx.camera.extensions.**
99108

100109
# Use -keep to explicitly keep any other classes shrinking would remove
101110
-dontoptimize

TMessagesProj/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
<package android:name="com.google.android.apps.maps"/>
8181
</queries>
8282

83+
<uses-sdk tools:overrideLibrary="androidx.camera.view, androidx.camera.extensions, androidx.camera.lifecycle, androidx.camera.camera2, androidx.camera.core" />
84+
8385
<application
8486
android:name=".ApplicationLoader"
8587
android:allowBackup="false"

TMessagesProj/src/main/java/com/exteragram/messenger/ExteraConfig.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
import android.app.Activity;
1515
import android.content.SharedPreferences;
1616

17+
import com.exteragram.messenger.camera.CameraXUtils;
18+
1719
import org.telegram.messenger.AndroidUtilities;
1820
import org.telegram.messenger.ApplicationLoader;
1921
import org.telegram.messenger.BuildVars;
2022
import org.telegram.messenger.FileLog;
23+
import org.telegram.messenger.SharedConfig;
2124
import org.telegram.tgnet.TLRPC;
2225

2326
import java.util.Arrays;
@@ -79,6 +82,11 @@ public class ExteraConfig {
7982
public static boolean disablePlayback;
8083
public static boolean disableProximityEvents;
8184

85+
// camera
86+
public static int cameraType;
87+
public static boolean useCameraXOptimizedMode;
88+
public static int cameraResolution;
89+
8290
// updates
8391
public static long lastUpdateCheckTime;
8492
public static long updateScheduleTimestamp;
@@ -154,6 +162,10 @@ public static void loadConfig() {
154162
pauseOnMinimize = preferences.getBoolean("pauseOnMinimize", true);
155163
disablePlayback = preferences.getBoolean("disablePlayback", true);
156164

165+
cameraType = preferences.getInt("cameraType", CameraXUtils.getDefault());
166+
useCameraXOptimizedMode = preferences.getBoolean("useCameraXOptimizedMode", SharedConfig.getDevicePerformanceClass() != SharedConfig.PERFORMANCE_CLASS_HIGH);
167+
cameraResolution = preferences.getInt("cameraResolution", CameraXUtils.getCameraResolution());
168+
157169
changeStatus = preferences.getBoolean("changeStatus", true);
158170
newGroup = preferences.getBoolean("newGroup", true);
159171
newSecretChat = preferences.getBoolean("newSecretChat", false);
@@ -178,6 +190,18 @@ public static void loadConfig() {
178190
}
179191
}
180192

193+
public static void saveCameraType(int type) {
194+
editor.putInt("cameraType", cameraType = type);
195+
}
196+
197+
public static void toggleCameraXOptimizedMode() {
198+
editor.putBoolean("useCameraXOptimizedMode", useCameraXOptimizedMode ^= true);
199+
}
200+
201+
public static void saveCameraResolution(int resolution) {
202+
editor.putInt("cameraResolution", cameraResolution = resolution);
203+
}
204+
181205
public static boolean isExtera(TLRPC.Chat chat) {
182206
return Arrays.stream(OFFICIAL_CHANNELS).anyMatch(id -> id == chat.id);
183207
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This is the source code of OwlGram for Android v. 1.4.x.
3+
* It is licensed under GNU GPL v. 2 or later.
4+
* You should have received a copy of the license in this archive (see LICENSE).
5+
*
6+
* Copyright Laky64, 2021-2022.
7+
*/
8+
package com.exteragram.messenger.camera;
9+
10+
import android.content.Context;
11+
import android.util.AttributeSet;
12+
import android.view.TextureView;
13+
import android.widget.FrameLayout;
14+
15+
import androidx.annotation.NonNull;
16+
import androidx.annotation.Nullable;
17+
18+
import org.telegram.messenger.camera.CameraView;
19+
20+
import java.io.File;
21+
22+
public abstract class BaseCameraView extends FrameLayout {
23+
public BaseCameraView(@NonNull Context context) {
24+
super(context);
25+
}
26+
27+
public BaseCameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
28+
super(context, attrs);
29+
}
30+
31+
public BaseCameraView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
32+
super(context, attrs, defStyleAttr);
33+
}
34+
35+
public interface CameraViewDelegate {
36+
void onCameraCreated();
37+
38+
void onCameraInit();
39+
}
40+
41+
public abstract boolean isInited();
42+
43+
public abstract boolean isFrontface();
44+
45+
public abstract void switchCamera();
46+
47+
public abstract void setZoom(float value);
48+
49+
public float resetZoom() {
50+
setZoom(0.0f);
51+
return 0.0f;
52+
}
53+
54+
public abstract void focusToPoint(int x, int y);
55+
56+
public abstract void runHaptic();
57+
58+
public abstract void setRecordFile(File generateVideoPath);
59+
60+
public abstract void setFpsLimit(int fpsLimit);
61+
62+
public abstract void setDelegate(CameraView.CameraViewDelegate cameraViewDelegate);
63+
64+
public abstract boolean hasFrontFaceCamera();
65+
66+
public abstract TextureView getTextureView();
67+
68+
public abstract float getTextureHeight(float width, float height);
69+
70+
public abstract void startSwitchingAnimation();
71+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* This is the source code of OwlGram for Android v. 1.4.x.
3+
* It is licensed under GNU GPL v. 2 or later.
4+
* You should have received a copy of the license in this archive (see LICENSE).
5+
*
6+
* Copyright Laky64, 2021-2022.
7+
*/
8+
package com.exteragram.messenger.camera;
9+
10+
import android.animation.Animator;
11+
import android.animation.AnimatorListenerAdapter;
12+
import android.animation.ValueAnimator;
13+
import android.annotation.SuppressLint;
14+
import android.content.Context;
15+
import android.graphics.Bitmap;
16+
import android.graphics.Canvas;
17+
import android.graphics.Color;
18+
import android.graphics.Paint;
19+
import android.graphics.PorterDuff;
20+
import android.graphics.PorterDuffXfermode;
21+
import android.graphics.drawable.Drawable;
22+
import android.view.MotionEvent;
23+
import android.view.View;
24+
import android.widget.ImageView;
25+
import android.widget.RelativeLayout;
26+
27+
import org.telegram.messenger.AndroidUtilities;
28+
import org.telegram.messenger.R;
29+
import org.telegram.ui.Components.CubicBezierInterpolator;
30+
31+
@SuppressLint("ViewConstructor")
32+
public class ButtonEffect extends RelativeLayout {
33+
final private ImageView imageView;
34+
private ValueAnimator toggleAnimation;
35+
private boolean isSelected = false;
36+
private boolean reachedHalf = false;
37+
private float currAn = 0f;
38+
final public int cameraType;
39+
40+
@SuppressLint("ClickableViewAccessibility")
41+
public ButtonEffect(Context context, int camera_type) {
42+
super(context);
43+
cameraType = camera_type;
44+
imageView = new ImageView(context);
45+
imageView.setClickable(true);
46+
imageView.setOnTouchListener((View view, MotionEvent motionEvent) -> {
47+
if (motionEvent.getAction() == MotionEvent.ACTION_UP && !isSelected) {
48+
onItemClick(this, cameraType);
49+
}
50+
return false;
51+
});
52+
imageView.setImageBitmap(getIcon());
53+
54+
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
55+
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
56+
imageView.setLayoutParams(layoutParams);
57+
addView(imageView);
58+
}
59+
60+
private Bitmap getIcon() {
61+
int w = AndroidUtilities.dp(50);
62+
Bitmap bmp = Bitmap.createBitmap(w, w, Bitmap.Config.ARGB_8888);
63+
Canvas canvas = new Canvas(bmp);
64+
Drawable d = getResources().getDrawable(getIconRes(cameraType));
65+
d.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
66+
int s = (w * 60) / 100;
67+
int x = (w >> 1) - (s >> 1);
68+
int y = (w >> 1) - (s >> 1);
69+
d.setBounds(x, y, x + s, y + s);
70+
d.draw(canvas);
71+
if (isSelected) {
72+
Paint level_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
73+
level_paint.setColor(Color.WHITE);
74+
level_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
75+
int s2 = ((w * 80) / 100) >> 1;
76+
int x2 = (w >> 1);
77+
int y2 = (w >> 1);
78+
canvas.drawCircle(x2, y2, s2, level_paint);
79+
}
80+
return bmp;
81+
}
82+
83+
private int getIconRes(int icon) {
84+
switch (icon) {
85+
case CameraXController.CAMERA_HDR:
86+
return R.drawable.round_hdr_on_black;
87+
case CameraXController.CAMERA_NIGHT:
88+
return R.drawable.round_bedtime_black;
89+
case CameraXController.CAMERA_AUTO:
90+
return R.drawable.round_auto_fix_high_black;
91+
case CameraXController.CAMERA_WIDE:
92+
return R.drawable.round_landscape_black;
93+
case CameraXController.CAMERA_NONE:
94+
default:
95+
return R.drawable.round_photo_camera_black;
96+
}
97+
}
98+
99+
public void toggleButton(boolean enabled, boolean animated) {
100+
isSelected = enabled;
101+
if (!animated) {
102+
imageView.setImageBitmap(getIcon());
103+
} else {
104+
currAn = toggleAnimation != null ? 2f - currAn : 0;
105+
reachedHalf = false;
106+
if (toggleAnimation != null) {
107+
toggleAnimation.cancel();
108+
}
109+
imageView.animate().setListener(null).cancel();
110+
float timeAnimation = (2f - currAn) / 2f;
111+
toggleAnimation = ValueAnimator.ofFloat(currAn, 2f);
112+
toggleAnimation.addUpdateListener(valueAnimator -> {
113+
float v = (float) valueAnimator.getAnimatedValue();
114+
currAn = v;
115+
float rAn;
116+
if (v > 1f) {
117+
if (!reachedHalf) {
118+
reachedHalf = true;
119+
imageView.setImageBitmap(getIcon());
120+
}
121+
rAn = v - 1f;
122+
} else {
123+
rAn = 1f - v;
124+
}
125+
imageView.setScaleX(rAn);
126+
imageView.setScaleY(rAn);
127+
});
128+
toggleAnimation.addListener(new AnimatorListenerAdapter() {
129+
@Override
130+
public void onAnimationEnd(Animator animation) {
131+
super.onAnimationEnd(animation);
132+
toggleAnimation = null;
133+
imageView.setScaleX(1f);
134+
imageView.setScaleY(1f);
135+
}
136+
});
137+
toggleAnimation.setDuration(Math.round(300 * timeAnimation));
138+
toggleAnimation.setInterpolator(CubicBezierInterpolator.DEFAULT);
139+
toggleAnimation.start();
140+
}
141+
}
142+
143+
protected void onItemClick(ButtonEffect buttonEffect, int camera_type) {
144+
}
145+
}

0 commit comments

Comments
 (0)