Skip to content
Snippets Groups Projects
Commit 0104e97b authored by FurWaz's avatar FurWaz
Browse files

Added permission call and orientation photo layout

parent 8987339a
Branches
No related tags found
No related merge requests found
package com.furwaz.roomview; package com.furwaz.roomview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.animation.ValueAnimator;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.hardware.Camera; import android.hardware.Camera;
...@@ -11,7 +17,10 @@ import android.hardware.SensorEventListener; ...@@ -11,7 +17,10 @@ import android.hardware.SensorEventListener;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageButton; import android.widget.ImageButton;
...@@ -23,9 +32,11 @@ import android.widget.Toast; ...@@ -23,9 +32,11 @@ import android.widget.Toast;
import java.io.File; import java.io.File;
import Common.BuildingManager; import Common.BuildingManager;
import Common.Callback;
import Common.ImageManager; import Common.ImageManager;
import Popups.WalkPopup; import Popups.WalkPopup;
import Structures.BuildingInfo; import Structures.BuildingInfo;
import Structures.Orientation;
import Structures.PhotoInfo; import Structures.PhotoInfo;
import Structures.RoomInfo; import Structures.RoomInfo;
import Structures.WalkInfo; import Structures.WalkInfo;
...@@ -38,6 +49,9 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe ...@@ -38,6 +49,9 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe
protected static final int MODE_EDIT = 1; protected static final int MODE_EDIT = 1;
protected static final int MODE_PHOTO = 2; protected static final int MODE_PHOTO = 2;
Callback _on_resolve_callback;
Callback _on_reject_callback;
SensorManager sm = null; SensorManager sm = null;
Sensor accelerometer = null; Sensor accelerometer = null;
LevelCanvas levelView = null; LevelCanvas levelView = null;
...@@ -47,6 +61,9 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe ...@@ -47,6 +61,9 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe
ZoneInfo zone = null; ZoneInfo zone = null;
PhotoInfo photo = null; PhotoInfo photo = null;
Orientation displayed_orient = Orientation.NORTH;
LinearLayout orient_layout = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -94,23 +111,32 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe ...@@ -94,23 +111,32 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe
Toast.makeText(this, "Error : no camera detected on this device", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Error : no camera detected on this device", Toast.LENGTH_SHORT).show();
this.finish(); this.finish();
} }
Camera c = getCamera();
FrameLayout cam_prev = findViewById(R.id.camera_preview); FrameLayout cam_prev = findViewById(R.id.camera_preview);
CameraPreview camPrev = new CameraPreview(this, c); ImageButton btn_nope = findViewById(R.id.btn_nope);
cam_prev.addView(camPrev); ImageButton btn_foto = findViewById(R.id.btn_foto);
Button btn_nope = findViewById(R.id.btn_nope);
Button btn_foto = findViewById(R.id.btn_foto);
btn_nope.setOnClickListener(view -> { btn_nope.setOnClickListener(view -> {
this.finish(); this.finish();
}); });
btn_foto.setOnClickListener(view -> {
c.takePicture(null, null, (bytes, camera) -> { getCamera(c -> {
photo.setImage(bytes); Camera camera = (Camera) c;
setViewMode(MODE_EDIT); CameraPreview camPrev = new CameraPreview(this, camera);
cam_prev.addView(camPrev);
btn_foto.setOnClickListener(view -> {
camera.takePicture(null, null, (bytes, cam) -> {
photo.setImage(bytes);
setViewMode(MODE_EDIT);
});
}); });
return null;
}, error -> {
Toast.makeText(this, (String) error, Toast.LENGTH_SHORT).show();
finish();
return null;
}); });
levelView = findViewById(R.id.level_view); levelView = findViewById(R.id.level_view);
...@@ -147,6 +173,40 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe ...@@ -147,6 +173,40 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe
selector.setPhoto(photo); selector.setPhoto(photo);
} }
public int getOrientationLayoutPadding(Orientation orient) {
switch (orient) {
case NORTH: return 0;
case EST: return 80;
case SOUTH: return 160;
case WEST: return 240;
}
return 0;
}
public void displayOrientation(Orientation orient) {
if (this.displayed_orient == orient) return;
float old_padding = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
getOrientationLayoutPadding(this.displayed_orient),
getResources().getDisplayMetrics()
);
float new_padding = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
getOrientationLayoutPadding(orient),
getResources().getDisplayMetrics()
);
this.displayed_orient = orient;
if (orient_layout == null) {
orient_layout = findViewById(R.id.orient_layout);
}
ValueAnimator animator = ValueAnimator.ofInt(-(int) old_padding, -(int) new_padding);
animator.addUpdateListener(valueAnimator -> orient_layout.setPadding((int) valueAnimator.getAnimatedValue(), 0, 0, 0));
animator.setDuration(400);
animator.start();
}
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
...@@ -177,21 +237,41 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe ...@@ -177,21 +237,41 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe
if (sensorEvent.sensor == accelerometer) { if (sensorEvent.sensor == accelerometer) {
float x_val = sensorEvent.values[0]; float x_val = sensorEvent.values[0];
float y_val = sensorEvent.values[1]; float y_val = sensorEvent.values[1];
float z_val = sensorEvent.values[2];
float orient = (float) Math.atan2(y_val, x_val); float orient = (float) Math.atan2(y_val, x_val);
if (levelView != null) if (levelView != null)
levelView.askForDraw(orient); levelView.askForDraw(orient);
if (orient < 1)
displayOrientation(Orientation.NORTH);
else displayOrientation(Orientation.EST);
} }
} }
protected Camera getCamera() { protected void getCamera(Callback onResolve, Callback onReject) {
this._on_resolve_callback = onResolve;
this._on_reject_callback = onReject;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED)
{
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, 621);
return;
}
Camera c = null; Camera c = null;
try { try {
c = Camera.open(); c = Camera.open();
onResolve.call(c);
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(this, "Error opening camera: "+e, Toast.LENGTH_SHORT).show(); onReject.call("Error opening camera: ");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != 621) return;
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getCamera(_on_resolve_callback, _on_reject_callback);
} else {
if (_on_reject_callback != null) _on_reject_callback.call( getResources().getString(R.string.camera_error) );
} }
return c;
} }
private boolean checkCameraHardware() { private boolean checkCameraHardware() {
......
<vector android:height="24dp" android:tint="@color/slate_50"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9.4,10.5l4.77,-8.26C13.47,2.09 12.75,2 12,2c-2.4,0 -4.6,0.85 -6.32,2.25l3.66,6.35 0.06,-0.1zM21.54,9c-0.92,-2.92 -3.15,-5.26 -6,-6.34L11.88,9h9.66zM21.8,10h-7.49l0.29,0.5 4.76,8.25C21,16.97 22,14.61 22,12c0,-0.69 -0.07,-1.35 -0.2,-2zM8.54,12l-3.9,-6.75C3.01,7.03 2,9.39 2,12c0,0.69 0.07,1.35 0.2,2h7.49l-1.15,-2zM2.46,15c0.92,2.92 3.15,5.26 6,6.34L12.12,15L2.46,15zM13.73,15l-3.9,6.76c0.7,0.15 1.42,0.24 2.17,0.24 2.4,0 4.6,-0.85 6.32,-2.25l-3.66,-6.35 -0.93,1.6z"/>
</vector>
<vector android:height="24dp" android:tint="@color/slate_50"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM17,15.59L15.59,17 12,13.41 8.41,17 7,15.59 10.59,12 7,8.41 8.41,7 12,10.59 15.59,7 17,8.41 13.41,12 17,15.59z"/>
</vector>
<vector android:height="24dp" android:tint="@color/slate_50"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>
<vector android:height="24dp" android:tint="#FFFFFF" <vector android:height="24dp" android:tint="@color/slate_50"
android:viewportHeight="24" android:viewportWidth="24" android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2L18,7L6,7v12zM8.46,11.88l1.41,-1.41L12,12.59l2.12,-2.12 1.41,1.41L13.41,14l2.12,2.12 -1.41,1.41L12,15.41l-2.12,2.12 -1.41,-1.41L10.59,14l-2.13,-2.12zM15.5,4l-1,-1h-5l-1,1L5,4v2h14L19,4z"/> <path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2L18,7L6,7v12zM8.46,11.88l1.41,-1.41L12,12.59l2.12,-2.12 1.41,1.41L13.41,14l2.12,2.12 -1.41,1.41L12,15.41l-2.12,2.12 -1.41,-1.41L10.59,14l-2.13,-2.12zM15.5,4l-1,-1h-5l-1,1L5,4v2h14L19,4z"/>
......
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="4dp" android:color="@color/blue_500" />
<solid android:color="@color/slate_50"/>
<corners android:radius="8dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
\ No newline at end of file
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
<LinearLayout <LinearLayout
android:id="@+id/camera_hud" android:id="@+id/camera_hud"
android:visibility="gone" android:visibility="visible"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
...@@ -65,48 +65,132 @@ ...@@ -65,48 +65,132 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" android:orientation="vertical"
android:layout_marginBottom="20dp" android:layout_marginBottom="20dp"
android:gravity="bottom|center"> android:gravity="bottom|center">
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center"
android:gravity="center"> android:layout_marginBottom="8dp"
<Button android:orientation="horizontal">
android:id="@+id/btn_nope" <LinearLayout
android:text="@string/cancel"
android:backgroundTint="@color/red_500"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"
android:background="@drawable/icon_background_full"
android:padding="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/slate_600"
android:textSize="18sp"
android:text="@string/orientation"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/slate_700"
android:textSize="18sp"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:text=":"/>
<LinearLayout
android:id="@+id/orient_layout"
android:clipToPadding="true"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/blue_500"
android:textSize="18sp"
android:textAlignment="center"
android:text="@string/north"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/blue_500"
android:textSize="18sp"
android:textAlignment="center"
android:text="@string/est"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/blue_500"
android:textSize="18sp"
android:textAlignment="center"
android:text="@string/south"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/blue_500"
android:textSize="18sp"
android:textAlignment="center"
android:text="@string/west"/>
</LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal">
android:gravity="center"> <LinearLayout
<Button android:layout_width="0dp"
android:id="@+id/btn_foto" android:layout_weight="1"
android:text="@string/done" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:orientation="horizontal"
android:layout_height="wrap_content"/> android:gravity="center"
</LinearLayout> android:padding="6dp">
<LinearLayout <ImageButton
android:layout_width="0dp" android:id="@+id/btn_nope"
android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" /> android:backgroundTint="@color/red_500"
android:background="@drawable/icon_background"
android:src="@drawable/ic_baseline_cancel_24"
android:padding="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageButton
android:id="@+id/btn_foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/blue_500"
android:background="@drawable/icon_background"
android:src="@drawable/ic_baseline_camera_24"
android:padding="16dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/edit_hud" android:id="@+id/edit_hud"
android:visibility="visible" android:visibility="gone"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
...@@ -174,11 +258,13 @@ ...@@ -174,11 +258,13 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:gravity="center"
android:orientation="horizontal"> android:orientation="horizontal">
<Button <Button
android:id="@+id/btn_photo" android:id="@+id/btn_photo"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/change_photo"/> android:text="@string/edit"
app:icon="@drawable/ic_baseline_edit_24" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="0dp"
...@@ -186,11 +272,14 @@ ...@@ -186,11 +272,14 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:gravity="center"
android:orientation="horizontal"> android:orientation="horizontal">
<Button <Button
android:id="@+id/btn_done" android:id="@+id/btn_done"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/done"/> android:text="@string/done"
android:backgroundTint="@color/blue_500"
app:icon="@drawable/ic_baseline_check_24" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
......
...@@ -97,4 +97,5 @@ ...@@ -97,4 +97,5 @@
<string name="no_directions_desc">Aucun itinéraire disponible entre ces pièces</string> <string name="no_directions_desc">Aucun itinéraire disponible entre ces pièces</string>
<string name="go_to_value">Allez dans la pièce {{value}}</string> <string name="go_to_value">Allez dans la pièce {{value}}</string>
<string name="go_to_value_desc">Allez dans la pièce {{value1}} en utilisant le passage {{value2}}</string> <string name="go_to_value_desc">Allez dans la pièce {{value1}} en utilisant le passage {{value2}}</string>
<string name="camera_error">Erreur : La camera ne peux pas être utilisée\n(Vérifiez peut-être les permissions de l\'app)</string>
</resources> </resources>
\ No newline at end of file
...@@ -97,4 +97,5 @@ ...@@ -97,4 +97,5 @@
<string name="no_directions_desc">No route available between those rooms</string> <string name="no_directions_desc">No route available between those rooms</string>
<string name="go_to_value">Go to the room {{value}}</string> <string name="go_to_value">Go to the room {{value}}</string>
<string name="go_to_value_desc">Go to the room {{value1}} by using the path {{value2}}</string> <string name="go_to_value_desc">Go to the room {{value1}} by using the path {{value2}}</string>
<string name="camera_error">Error : The camera cannot be open !\n(Maybe check the app\'s permissions)</string>
</resources> </resources>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment