package com.bottle.hp.album.capture; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.hardware.Camera; import android.net.Uri; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Environment; import android.os.Handler; import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; import android.view.OrientationEventListener; import android.view.View; import android.view.WindowManager; import com.bottle.hp.album.BaseActivity; import com.bottle.hp.album.R$id; import com.bottle.hp.album.R$layout; import com.bottle.hp.album.R$string; import com.bottle.hp.album.Utils; import com.bottle.hp.album.capture.camera.CameraWrapper; import com.bottle.hp.album.capture.camera.NativeCamera; import com.bottle.hp.album.capture.configuration.CaptureConfiguration; import com.bottle.hp.album.capture.configuration.PredefinedCaptureConfigurations$CaptureQuality; import com.bottle.hp.album.capture.configuration.PredefinedCaptureConfigurations$CaptureResolution; import com.bottle.hp.album.capture.preview.CapturePreviewInterface; import com.bottle.hp.album.capture.recorder.AlreadyUsedException; import com.bottle.hp.album.capture.recorder.VideoRecorder; import com.bottle.hp.album.capture.recorder.VideoRecorderInterface; import com.bottle.hp.album.capture.view.VideoCaptureView; import com.ubtech.permission.JimuPermissionRequest; import com.ubtrobot.log.ALog; import com.yanzhenjie.permission.Permission$Group; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; /* loaded from: classes.dex */ public class VideoCaptureActivity extends BaseActivity implements Camera.PictureCallback, CapturePreviewInterface, VideoCaptureView.IVideoCaptureUIListener, VideoRecorderInterface, View.OnClickListener, Camera.PreviewCallback { private static int y = 20000; private VideoCaptureView d; private CameraWrapper e; private String g; private Timer l; private CaptureConfiguration p; private VideoRecorder q; private AlbumOrientationEventListener s; private int t; private TimeCounter u; private final String c = VideoCaptureActivity.class.getSimpleName(); private int f = 5; private boolean h = false; private long i = 0; private boolean j = false; private long k = 0; private Handler m = new Handler(); private boolean n = false; private VideoFile o = null; private int r = 0; private int v = 100; private boolean x = false; private class AlbumOrientationEventListener extends OrientationEventListener { public AlbumOrientationEventListener(Context context, int i) { super(context, i); } @Override // android.view.OrientationEventListener public void onOrientationChanged(int i) { int i2; if (i == -1 || (i2 = (((i + 45) / 90) * 90) % 360) == VideoCaptureActivity.this.t) { return; } VideoCaptureActivity.this.t = i2; ALog.a(VideoCaptureActivity.this.c).d("角度" + VideoCaptureActivity.this.t); VideoCaptureActivity videoCaptureActivity = VideoCaptureActivity.this; videoCaptureActivity.l(videoCaptureActivity.t); } } private class StartRecordTask extends TimerTask { private StartRecordTask() { } @Override // java.util.TimerTask, java.lang.Runnable public void run() { try { VideoCaptureActivity.this.x = false; VideoCaptureActivity.this.q.f(); VideoCaptureActivity.this.m.postDelayed(new Runnable() { // from class: com.bottle.hp.album.capture.VideoCaptureActivity.StartRecordTask.1 @Override // java.lang.Runnable public void run() { if (VideoCaptureActivity.this.e.c() == null) { VideoCaptureActivity.this.b(R$string.tips_ask_camera_permission, 2); } else { VideoCaptureActivity.this.d.d(); } } }, 10L); } catch (AlreadyUsedException unused) { ALog.a(VideoCaptureActivity.this.c).d("Cannot toggle recording after cleaning up all resources"); } } } private class TimeCounter extends CountDownTimer { private long a; public TimeCounter(long j, long j2) { super(j, j2); this.a = 0L; } public long a() { return this.a; } @Override // android.os.CountDownTimer public void onFinish() { VideoCaptureActivity.this.d.a(100); } @Override // android.os.CountDownTimer public void onTick(long j) { int i = (int) (VideoCaptureActivity.y - j); this.a = i; VideoCaptureActivity.this.d.a((i * 100) / VideoCaptureActivity.y); } } private void G0() { TimeCounter timeCounter = this.u; if (timeCounter != null) { timeCounter.cancel(); } } private File H0() { File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + File.separator + "Jimu"); if (!file.exists()) { file.mkdirs(); } this.g = file.getAbsolutePath() + File.separator + Utils.a() + ".png"; return new File(this.g); } /* JADX INFO: Access modifiers changed from: private */ public void I0() { try { if (this.q != null) { this.d.getPreviewSurfaceHolder().removeCallback(this.q.b()); this.q.d(); } this.e = new CameraWrapper(new NativeCamera(), ((WindowManager) getSystemService("window")).getDefaultDisplay().getRotation(), this.r, this); this.o = a((Bundle) null); this.q = new VideoRecorder(this, this.p, this.o, this.e, this.d.getPreviewSurfaceHolder()); } catch (Exception e) { e.printStackTrace(); Log.e(this.c, e.getMessage()); } } private void J0() { if (System.currentTimeMillis() - this.k < 1000) { return; } if (this.e.c() != null) { try { this.e.a((Camera.ShutterCallback) null, (Camera.PictureCallback) null, this); } catch (RuntimeException e) { e.printStackTrace(); Log.e(this.c, "拍照失败:" + e.getMessage()); b(R$string.tips_ask_camera_permission, 2); } } else { Log.e(this.c, "camera 对象为空"); b(R$string.tips_ask_camera_permission, 2); } this.k = System.currentTimeMillis(); } private boolean K0() { TimeCounter timeCounter = this.u; if (timeCounter == null || timeCounter.a() >= 1500) { return false; } J0(); VideoFile videoFile = this.o; if (videoFile != null && videoFile.a().exists()) { this.o.a().delete(); sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(this.o.a()))); } return true; } private void initView() { this.d = (VideoCaptureView) findViewById(R$id.videoCaptureView); this.d.setUIListener(this); } @Override // com.bottle.hp.album.capture.view.VideoCaptureView.IVideoCaptureUIListener public void B0() { if (this.r == 0) { this.r = 1; } else { this.r = 0; } I0(); } protected CaptureConfiguration E0() { CaptureConfiguration captureConfiguration = (CaptureConfiguration) getIntent().getParcelableExtra("com.bottle.extracaptureconfiguration"); if (captureConfiguration != null) { return captureConfiguration; } CaptureConfiguration captureConfiguration2 = new CaptureConfiguration(); ALog.a(this.c).d("No captureconfiguration passed - using default configuration"); return captureConfiguration2; } @Override // com.bottle.hp.album.capture.recorder.VideoRecorderInterface public void f0() { if (!this.j) { this.m.post(new Runnable() { // from class: com.bottle.hp.album.capture.VideoCaptureActivity.3 @Override // java.lang.Runnable public void run() { VideoCaptureActivity.this.q.a("user cancel"); } }); return; } this.g = null; ALog.a(this.c).d("开始录制"); this.m.postDelayed(new Runnable() { // from class: com.bottle.hp.album.capture.VideoCaptureActivity.4 @Override // java.lang.Runnable public void run() { VideoCaptureActivity videoCaptureActivity = VideoCaptureActivity.this; videoCaptureActivity.u = videoCaptureActivity.new TimeCounter(VideoCaptureActivity.y, VideoCaptureActivity.this.v); VideoCaptureActivity.this.u.start(); } }, 300L); } @Override // com.bottle.hp.album.capture.view.VideoCaptureView.IVideoCaptureUIListener public void h0() { this.j = true; if (this.f == 1) { return; } this.i = System.currentTimeMillis(); this.l = new Timer(); this.l.schedule(new StartRecordTask(), 300L); } @Override // com.bottle.hp.album.capture.recorder.VideoRecorderInterface public boolean j0() { return JimuPermissionRequest.b(this, Permission$Group.c); } @Override // com.bottle.hp.album.capture.recorder.VideoRecorderInterface public void l(String str) { ALog.a(this.c).d("停止录制"); G0(); if (this.x) { this.g = ""; } else { if (K0()) { return; } onCancel(); } } @Override // com.bottle.hp.album.capture.recorder.VideoRecorderInterface public void m(String str) { ALog.a(this.c).d("录制失败"); G0(); setResult(0); finish(); } @Override // androidx.fragment.app.FragmentActivity, android.app.Activity public void onActivityResult(int i, int i2, Intent intent) { super.onActivityResult(i, i2, intent); if (101 == i) { if (i2 == -1) { x0(); } else { onCancel(); } } } @Override // com.bottle.hp.album.capture.view.VideoCaptureView.IVideoCaptureUIListener public void onCancel() { VideoFile videoFile = this.o; if (videoFile != null && videoFile.a().exists()) { this.o.a().delete(); sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(this.o.a()))); } if (!TextUtils.isEmpty(this.g)) { File file = new File(this.g); if (file.exists()) { file.delete(); sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(file))); } } this.m.postDelayed(new Runnable() { // from class: com.bottle.hp.album.capture.VideoCaptureActivity.1 @Override // java.lang.Runnable public void run() { VideoCaptureActivity.this.I0(); } }, 10L); this.m.postDelayed(new Runnable() { // from class: com.bottle.hp.album.capture.VideoCaptureActivity.2 @Override // java.lang.Runnable public void run() { VideoCaptureActivity.this.d.b(); } }, 500L); } @Override // android.view.View.OnClickListener public void onClick(View view) { } @Override // com.bottle.hp.album.BaseActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, androidx.core.app.ComponentActivity, android.app.Activity protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R$layout.activity_video_capture); c(bundle); initView(); this.s = new AlbumOrientationEventListener(this, 3); if (this.s.canDetectOrientation()) { this.s.enable(); } else { Log.e(this.c, "Can't Detect Orientation"); } this.f = getIntent().getIntExtra("recordType", 5); int i = this.f; if (i == 1) { this.d.c(); } else if (i == 2) { this.d.e(); } else { this.d.a(); } } @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, android.app.Activity protected void onDestroy() { Handler handler = this.m; if (handler != null) { handler.removeCallbacksAndMessages(null); } super.onDestroy(); VideoRecorder videoRecorder = this.q; if (videoRecorder != null) { videoRecorder.d(); } this.s.disable(); } @Override // androidx.appcompat.app.AppCompatActivity, android.app.Activity, android.view.KeyEvent.Callback public boolean onKeyDown(int i, KeyEvent keyEvent) { if (this.j) { return true; } if (i != 4) { return super.onKeyDown(i, keyEvent); } setResult(0); finish(); return true; } @Override // androidx.fragment.app.FragmentActivity, android.app.Activity public void onPause() { super.onPause(); } @Override // android.hardware.Camera.PictureCallback public void onPictureTaken(byte[] bArr, Camera camera) { a(bArr); } @Override // android.hardware.Camera.PreviewCallback public void onPreviewFrame(byte[] bArr, Camera camera) { if (bArr == null || bArr.length == 0) { } } @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, androidx.core.app.ComponentActivity, android.app.Activity public void onSaveInstanceState(Bundle bundle) { bundle.putBoolean("com.bottle.savedrecordedboolean", this.n); bundle.putString("com.bottle.savedoutputfilename", this.o.b()); super.onSaveInstanceState(bundle); } @Override // android.app.Activity, android.view.Window.Callback public void onWindowFocusChanged(boolean z) { super.onWindowFocusChanged(z); if (this.h) { return; } this.h = true; I0(); } @Override // com.bottle.hp.album.capture.view.VideoCaptureView.IVideoCaptureUIListener public void r0() { this.j = false; if (this.f == 1) { J0(); return; } this.l.cancel(); if (System.currentTimeMillis() - this.i >= 300) { this.q.a("stop recoding"); } else if (this.f == 5) { J0(); } } @Override // com.bottle.hp.album.capture.view.VideoCaptureView.IVideoCaptureUIListener public void t0() { Log.e("TestLog", "播放视频onPlayVideo"); VideoPreviewActivity.a(this, 1001, this.o.b()); } @Override // com.bottle.hp.album.capture.recorder.VideoRecorderInterface public void u0() { sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(this.o.a()))); ALog.a(this.c).d("录制成功"); this.x = true; if (this.u.a() < 1500) { this.x = false; } else { this.m.post(new Runnable() { // from class: com.bottle.hp.album.capture.VideoCaptureActivity.5 @Override // java.lang.Runnable public void run() { VideoCaptureActivity videoCaptureActivity = VideoCaptureActivity.this; VideoPreviewActivity.a(videoCaptureActivity, 101, videoCaptureActivity.o.b()); try { VideoCaptureActivity.this.e.j(); } catch (Exception e) { e.printStackTrace(); } } }); } } @Override // com.bottle.hp.album.capture.view.VideoCaptureView.IVideoCaptureUIListener public void x0() { Intent intent = new Intent(); if (TextUtils.isEmpty(this.g)) { intent.putExtra("data", this.o.b()); intent.putExtra("data_type", 1); } else { intent.putExtra("data", this.g); intent.putExtra("data_type", 0); } setResult(-1, intent); finish(); } @Override // com.bottle.hp.album.capture.view.VideoCaptureView.IVideoCaptureUIListener public void y0() { if (this.j) { return; } setResult(0); finish(); } public static CaptureConfiguration c(int i, int i2) { PredefinedCaptureConfigurations$CaptureResolution predefinedCaptureConfigurations$CaptureResolution = PredefinedCaptureConfigurations$CaptureResolution.RES_720P; PredefinedCaptureConfigurations$CaptureQuality predefinedCaptureConfigurations$CaptureQuality = PredefinedCaptureConfigurations$CaptureQuality.LOW; if (i2 != 1) { if (i2 == 2) { predefinedCaptureConfigurations$CaptureQuality = PredefinedCaptureConfigurations$CaptureQuality.MEDIUM; } else if (i2 == 3) { predefinedCaptureConfigurations$CaptureQuality = PredefinedCaptureConfigurations$CaptureQuality.HIGH; } } return new CaptureConfiguration(predefinedCaptureConfigurations$CaptureResolution, predefinedCaptureConfigurations$CaptureQuality, i, -1); } @Override // com.bottle.hp.album.capture.preview.CapturePreviewInterface public void i() { finish(); } private boolean b(Bundle bundle) { if (bundle == null) { return false; } return bundle.getBoolean("com.bottle.savedrecordedboolean", false); } public static void a(Activity activity, int i, int i2) { Intent intent = new Intent(activity, (Class) VideoCaptureActivity.class); intent.putExtra("com.bottle.extracaptureconfiguration", c(y / 1000, 1)); intent.putExtra("com.bottle.extraoutputfilename", ""); intent.putExtra("recordType", i2); activity.startActivityForResult(intent, i); } private void c(Bundle bundle) { this.p = E0(); this.n = b(bundle); this.o = a(bundle); } /* JADX INFO: Access modifiers changed from: private */ public void l(int i) { int i2; CameraWrapper cameraWrapper = this.e; if (cameraWrapper == null || cameraWrapper.c() == null) { return; } try { Camera.Parameters parameters = this.e.c().getParameters(); Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.getCameraInfo(this.r, cameraInfo); int i3 = ((i + 45) / 90) * 90; if (cameraInfo.facing == 1) { i2 = ((cameraInfo.orientation - i3) + 360) % 360; } else { i2 = (cameraInfo.orientation + i3) % 360; } parameters.setRotation(i2); this.e.c().setParameters(parameters); } catch (RuntimeException e) { e.printStackTrace(); Log.e(this.c, e.getMessage()); } } /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:45:0x002e -> B:9:0x0079). Please report as a decompilation issue!!! */ private void a(byte[] bArr) { FileOutputStream fileOutputStream = null; try { } catch (Exception e) { e.printStackTrace(); } try { try { File H0 = H0(); FileOutputStream fileOutputStream2 = new FileOutputStream(H0); try { fileOutputStream2.write(bArr); a(H0); fileOutputStream2.close(); } catch (FileNotFoundException e2) { e = e2; fileOutputStream = fileOutputStream2; e.printStackTrace(); Log.e(this.c, "File not found: " + e.getMessage()); if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e3) { e = e3; fileOutputStream = fileOutputStream2; e.printStackTrace(); Log.e(this.c, "Error accessing file: " + e.getMessage()); if (fileOutputStream != null) { fileOutputStream.close(); } } catch (Exception e4) { e = e4; fileOutputStream = fileOutputStream2; e.printStackTrace(); if (fileOutputStream != null) { fileOutputStream.close(); } } catch (Throwable th) { th = th; fileOutputStream = fileOutputStream2; if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (Exception e5) { e5.printStackTrace(); } } throw th; } } catch (FileNotFoundException e6) { e = e6; } catch (IOException e7) { e = e7; } catch (Exception e8) { e = e8; } } catch (Throwable th2) { th = th2; } } private void a(File file) { try { sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(file))); this.e.j(); this.d.a(file.getAbsolutePath(), 0); } catch (Exception e) { e.printStackTrace(); Log.e(this.c, e.getMessage()); } } protected VideoFile a(Bundle bundle) { if (bundle != null) { return new VideoFile(bundle.getString("com.bottle.savedoutputfilename")); } return new VideoFile(getIntent().getStringExtra("com.bottle.extraoutputfilename")); } }