converted views into fragments
This commit is contained in:
parent
500c40a125
commit
b881af0bcf
|
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
android:theme="@style/Theme.Dermy"
|
||||
tools:targetApi="31" >
|
||||
|
||||
<activity android:name=".CameraActivity"
|
||||
<activity android:name=".MainActivity"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
package xyz.r0r5chach.dermy;
|
||||
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import xyz.r0r5chach.dermy.fragments.CameraFragment;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
public static final int REQUEST_CAMERA_PERMISSION = 200;
|
||||
|
||||
public MainActivity() {
|
||||
super(R.layout.activity_main);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
startCamera();
|
||||
}
|
||||
}
|
||||
|
||||
private void startCamera() {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.setReorderingAllowed(true)
|
||||
.add(R.id.fragment_container, CameraFragment.class, null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { //FIXME: deprecated listener fir request permissions
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == REQUEST_CAMERA_PERMISSION) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
startCamera();
|
||||
} else {
|
||||
Toast.makeText(this, "Camera permission is required", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
package xyz.r0r5chach.dermy;
|
||||
|
||||
public class MapActivity {
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
package xyz.r0r5chach.dermy;
|
||||
|
||||
public class NearbyActivity {
|
||||
}
|
||||
|
|
@ -1,12 +1,17 @@
|
|||
package xyz.r0r5chach.dermy;
|
||||
package xyz.r0r5chach.dermy.fragments;
|
||||
|
||||
import static xyz.r0r5chach.dermy.MainActivity.REQUEST_CAMERA_PERMISSION;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.core.CameraSelector;
|
||||
import androidx.camera.core.ImageCapture;
|
||||
import androidx.camera.core.ImageCaptureException;
|
||||
|
|
@ -15,38 +20,41 @@ import androidx.camera.lifecycle.ProcessCameraProvider;
|
|||
import androidx.camera.view.PreviewView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import java.io.File;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class CameraActivity extends AppCompatActivity {
|
||||
private static final int REQUEST_CAMERA_PERMISSION = 200;
|
||||
import xyz.r0r5chach.dermy.R;
|
||||
|
||||
public class CameraFragment extends Fragment {
|
||||
private PreviewView previewView;
|
||||
private ImageCapture imageCapture;
|
||||
private ExecutorService cameraExecutor;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_camera, container, false);
|
||||
previewView = view.findViewById(R.id.camera_preview);
|
||||
|
||||
previewView = findViewById(R.id.previewView);
|
||||
Button captureButton = findViewById(R.id.captureButton);
|
||||
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
startCamera();
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
|
||||
ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
|
||||
}
|
||||
|
||||
captureButton.setOnClickListener(v -> takePhoto());
|
||||
previewView.setOnClickListener(v -> takePhoto());
|
||||
|
||||
cameraExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void startCamera() {
|
||||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
|
||||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext());
|
||||
|
||||
cameraProviderFuture.addListener(() -> {
|
||||
try {
|
||||
|
|
@ -55,7 +63,7 @@ public class CameraActivity extends AppCompatActivity {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace(); //TODO: Replace with better logging
|
||||
}
|
||||
}, ContextCompat.getMainExecutor(this));
|
||||
}, ContextCompat.getMainExecutor(requireContext()));
|
||||
}
|
||||
|
||||
private void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
|
||||
|
|
@ -71,9 +79,9 @@ public class CameraActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
private void takePhoto() {
|
||||
File photoFile = new File(getExternalFilesDir(null), System.currentTimeMillis() + ".jpg");
|
||||
File photoFile = new File(requireContext().getExternalFilesDir(null), System.currentTimeMillis() + ".jpg");
|
||||
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(photoFile).build();
|
||||
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
|
||||
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(requireContext()), new ImageCapture.OnImageSavedCallback() {
|
||||
@Override
|
||||
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
|
||||
//TODO: Add image path to bundle to pass onto next activity
|
||||
|
|
@ -83,26 +91,14 @@ public class CameraActivity extends AppCompatActivity {
|
|||
@Override
|
||||
public void onError(@NonNull ImageCaptureException exception) {
|
||||
exception.printStackTrace(); //TODO: Replace with better logging
|
||||
Toast.makeText(CameraActivity.this, "Error saving photo: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(requireContext(), "Error saving photo: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
cameraExecutor.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == REQUEST_CAMERA_PERMISSION) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
startCamera();
|
||||
} else {
|
||||
Toast.makeText(this, "Camera permission is required", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package xyz.r0r5chach.dermy.fragments;
|
||||
|
||||
public class MapFragment {
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package xyz.r0r5chach.dermy.fragments;
|
||||
|
||||
public class NearbyFragment {
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package xyz.r0r5chach.dermy;
|
||||
package xyz.r0r5chach.dermy.fragments;
|
||||
|
||||
public class PreferencesActivity {
|
||||
public class PreferencesFragment {
|
||||
//TODO: Disclaimers
|
||||
//TODO: Useful Links
|
||||
//TODO: Export File
|
||||
|
|
@ -1,18 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<androidx.camera.view.PreviewView
|
||||
android:id="@+id/previewView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<Button
|
||||
android:id="@+id/captureButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal|bottom"
|
||||
android:text="@string/image_capture_button_text" />
|
||||
</androidx.camera.view.PreviewView>
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/fragment_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:name="xyz.r0r5chach.dermy.fragments.CameraFragment" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<androidx.camera.view.PreviewView
|
||||
android:id="@+id/camera_preview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
</androidx.camera.view.PreviewView>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Loading…
Reference in New Issue