improve code check score

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2021-08-21 18:19:20 +02:00
parent 4041d3f09b
commit 56d57c0b37
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
4 changed files with 43 additions and 20 deletions

View File

@ -33,6 +33,7 @@ import android.view.Surface;
import android.widget.Toast; import android.widget.Toast;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.nextcloud.talk.R;
import com.nextcloud.talk.databinding.ActivityTakePictureBinding; import com.nextcloud.talk.databinding.ActivityTakePictureBinding;
import com.nextcloud.talk.models.TakePictureViewModel; import com.nextcloud.talk.models.TakePictureViewModel;
import com.nextcloud.talk.utils.FileUtils; import com.nextcloud.talk.utils.FileUtils;
@ -64,7 +65,7 @@ public class TakePhotoActivity extends AppCompatActivity {
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture; private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
private OrientationEventListener orientationEventListener; private OrientationEventListener orientationEventListener;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.ROOT); private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.ROOT);
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -81,17 +82,34 @@ public class TakePhotoActivity extends AppCompatActivity {
final ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); final ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
final Preview preview = getPreview(); final Preview preview = getPreview();
final ImageCapture imageCapture = getImageCapture(); final ImageCapture imageCapture = getImageCapture();
final Camera camera = cameraProvider.bindToLifecycle(this, viewModel.getCameraSelector(), imageCapture, preview); final Camera camera = cameraProvider.bindToLifecycle(
this,
viewModel.getCameraSelector(),
imageCapture,
preview);
viewModel.getCameraSelectorToggleButtonImageResource().observe(this, res -> binding.switchCamera.setImageDrawable(ContextCompat.getDrawable(this, res))); viewModel.getCameraSelectorToggleButtonImageResource()
viewModel.getTorchToggleButtonImageResource().observe(this, res -> binding.toggleTorch.setImageDrawable(ContextCompat.getDrawable(this, res))); .observe(
viewModel.isTorchEnabled().observe(this, enabled -> camera.getCameraControl().enableTorch(enabled)); this,
res -> binding.switchCamera.setImageDrawable(ContextCompat.getDrawable(this, res)));
viewModel.getTorchToggleButtonImageResource()
.observe(
this,
res -> binding.toggleTorch.setImageDrawable(ContextCompat.getDrawable(this, res)));
viewModel.isTorchEnabled()
.observe(
this,
enabled -> camera.getCameraControl().enableTorch(enabled));
binding.toggleTorch.setOnClickListener((v) -> viewModel.toggleTorchEnabled()); binding.toggleTorch.setOnClickListener((v) -> viewModel.toggleTorchEnabled());
binding.switchCamera.setOnClickListener((v) -> { binding.switchCamera.setOnClickListener((v) -> {
viewModel.toggleCameraSelector(); viewModel.toggleCameraSelector();
cameraProvider.unbindAll(); cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(this, viewModel.getCameraSelector(), imageCapture, preview); cameraProvider.bindToLifecycle(
this,
viewModel.getCameraSelector(),
imageCapture,
preview);
}); });
} catch (IllegalArgumentException | ExecutionException | InterruptedException e) { } catch (IllegalArgumentException | ExecutionException | InterruptedException e) {
Log.e(TAG, "Error taking picture", e); Log.e(TAG, "Error taking picture", e);
@ -130,12 +148,17 @@ public class TakePhotoActivity extends AppCompatActivity {
final String photoFileName = dateFormat.format(new Date())+ ".jpg"; final String photoFileName = dateFormat.format(new Date())+ ".jpg";
try { try {
final File photoFile = FileUtils.getTempCacheFile(this, "photos/" + photoFileName); final File photoFile = FileUtils.getTempCacheFile(this, "photos/" + photoFileName);
final ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions.Builder(photoFile).build(); final ImageCapture.OutputFileOptions options =
imageCapture.takePicture(options, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() { new ImageCapture.OutputFileOptions.Builder(photoFile).build();
imageCapture.takePicture(
options,
ContextCompat.getMainExecutor(this),
new ImageCapture.OnImageSavedCallback() {
@Override @Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
final Uri savedUri = Uri.fromFile(photoFile); final Uri savedUri = Uri.fromFile(photoFile);
Log.i(TAG, "onImageSaved - savedUri:" + savedUri.toString()); Log.i(TAG, "onImageSaved - savedUri:" + savedUri);
setResult(RESULT_OK, new Intent().setDataAndType(savedUri, "image/jpeg")); setResult(RESULT_OK, new Intent().setDataAndType(savedUri, "image/jpeg"));
finish(); finish();
} }
@ -143,14 +166,15 @@ public class TakePhotoActivity extends AppCompatActivity {
@Override @Override
public void onError(@NonNull ImageCaptureException e) { public void onError(@NonNull ImageCaptureException e) {
Log.e(TAG, "Error", e); Log.e(TAG, "Error", e);
//noinspection ResultOfMethodCallIgnored
photoFile.delete(); if(!photoFile.delete()) {
Log.w(TAG, "Deleting picture failed");
}
binding.takePhoto.setEnabled(true); binding.takePhoto.setEnabled(true);
} }
}); });
} catch (Exception e) { } catch (Exception e) {
// TODO replace string with placeholder Toast.makeText(this, R.string.take_photo_error_deleting_picture, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Error taking picture", Toast.LENGTH_SHORT).show();
} }
}); });

View File

@ -39,7 +39,8 @@ public class TakePictureViewModel extends ViewModel {
@NonNull @NonNull
private CameraSelector cameraSelector = DEFAULT_BACK_CAMERA; private CameraSelector cameraSelector = DEFAULT_BACK_CAMERA;
@NonNull @NonNull
private final MutableLiveData<Integer> cameraSelectorToggleButtonImageResource = new MutableLiveData<>(R.drawable.ic_baseline_camera_front_24); private final MutableLiveData<Integer> cameraSelectorToggleButtonImageResource =
new MutableLiveData<>(R.drawable.ic_baseline_camera_front_24);
@NonNull @NonNull
private final MutableLiveData<Boolean> torchEnabled = new MutableLiveData<>(false); private final MutableLiveData<Boolean> torchEnabled = new MutableLiveData<>(false);
@ -77,4 +78,3 @@ public class TakePictureViewModel extends ViewModel {
: R.drawable.ic_baseline_flash_on_24); : R.drawable.ic_baseline_flash_on_24);
} }
} }

View File

@ -60,7 +60,7 @@ import autodagger.AutoInjector;
@AutoInjector(NextcloudTalkApplication.class) @AutoInjector(NextcloudTalkApplication.class)
public class MagicPeerConnectionWrapper { public class MagicPeerConnectionWrapper {
private static String TAG = "MagicPeerConnectionWrapper"; private static final String TAG = "MagicPeerConWrapper";
private List<IceCandidate> iceCandidates = new ArrayList<>(); private List<IceCandidate> iceCandidates = new ArrayList<>();
private PeerConnection peerConnection; private PeerConnection peerConnection;
@ -129,7 +129,6 @@ public class MagicPeerConnectionWrapper {
EventBus.getDefault().post(new WebSocketCommunicationEvent("peerReadyForRequestingOffer", hashMap)); EventBus.getDefault().post(new WebSocketCommunicationEvent("peerReadyForRequestingOffer", hashMap));
} else if (!hasMCU && hasInitiated) { } else if (!hasMCU && hasInitiated) {
peerConnection.createOffer(magicSdpObserver, sdpConstraints); peerConnection.createOffer(magicSdpObserver, sdpConstraints);
} }
} }
} }
@ -178,7 +177,6 @@ public class MagicPeerConnectionWrapper {
} }
} }
public void sendNickChannelData(DataChannelMessageNick dataChannelMessage) { public void sendNickChannelData(DataChannelMessageNick dataChannelMessage) {
ByteBuffer buffer; ByteBuffer buffer;
if (magicDataChannel != null) { if (magicDataChannel != null) {
@ -186,7 +184,7 @@ public class MagicPeerConnectionWrapper {
buffer = ByteBuffer.wrap(LoganSquare.serialize(dataChannelMessage).getBytes()); buffer = ByteBuffer.wrap(LoganSquare.serialize(dataChannelMessage).getBytes());
magicDataChannel.send(new DataChannel.Buffer(buffer, false)); magicDataChannel.send(new DataChannel.Buffer(buffer, false));
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, "Failed to send channel data, attempting regular " + dataChannelMessage.toString()); Log.d(TAG, "Failed to send channel data, attempting regular " + dataChannelMessage);
} }
} }
} }
@ -198,7 +196,7 @@ public class MagicPeerConnectionWrapper {
buffer = ByteBuffer.wrap(LoganSquare.serialize(dataChannelMessage).getBytes()); buffer = ByteBuffer.wrap(LoganSquare.serialize(dataChannelMessage).getBytes());
magicDataChannel.send(new DataChannel.Buffer(buffer, false)); magicDataChannel.send(new DataChannel.Buffer(buffer, false));
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, "Failed to send channel data, attempting regular " + dataChannelMessage.toString()); Log.d(TAG, "Failed to send channel data, attempting regular " + dataChannelMessage);
} }
} }
} }

View File

@ -477,4 +477,5 @@
<string name="take_photo">Take a photo</string> <string name="take_photo">Take a photo</string>
<string name="take_photo_switch_camera">Switch camera</string> <string name="take_photo_switch_camera">Switch camera</string>
<string name="take_photo_toggle_torch">Toggle torch</string> <string name="take_photo_toggle_torch">Toggle torch</string>
<string name="take_photo_error_deleting_picture">Error taking picture</string>
</resources> </resources>