diff --git a/Sources/app/src/main/java/Views/CameraPreview.java b/Sources/app/src/main/java/Views/CameraPreview.java index 4a8dc4c1cff2d520608a4b1585a1e3c867c22606..a2377a03e64711dfb8f3804c1ddae0e84fbb3d53 100644 --- a/Sources/app/src/main/java/Views/CameraPreview.java +++ b/Sources/app/src/main/java/Views/CameraPreview.java @@ -10,26 +10,24 @@ import android.widget.Toast; import java.io.IOException; import java.util.List; -/** A basic Camera preview class */ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; + // Constructor public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera; - // Install a SurfaceHolder.Callback so we get notified when the - // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); - // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } + // Get the optimal preview size (based of camera's supported preview sizes and the view's size) private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) { final double ASPECT_TOLERANCE = 0.1; - double targetRatio=(double)h / w; + double targetRatio = (double)h / w; if (sizes == null) return null; @@ -59,6 +57,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback return optimalSize; } + // Bind the camera preview to the SurfaceHolder protected void setCameraPreview(SurfaceHolder holder) { Camera.Parameters params = mCamera.getParameters(); params.set("orientation", "portrait"); @@ -78,33 +77,24 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback } } + // Event holder for surface creation public void surfaceCreated(SurfaceHolder holder) { setCameraPreview(holder); } + // Event holder for surface destruction public void surfaceDestroyed(SurfaceHolder holder) { - // empty. Take care of releasing the Camera preview in your activity. + mCamera.stopPreview(); + mCamera.release(); } + // Event holder for surface change public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { - // If your preview can change or rotate, take care of those events here. - // Make sure to stop the preview before resizing or reformatting it. + if (mHolder.getSurface() == null) return; - if (mHolder.getSurface() == null){ - // preview surface does not exist - return; - } - - // stop preview before making changes try { mCamera.stopPreview(); - } catch (Exception e){ - // ignore: tried to stop a non-existent preview - } - - // set preview size and make any resize, rotate or - // reformatting changes here - + } catch (Exception e) {} setCameraPreview(holder); } } diff --git a/Sources/app/src/main/java/Views/ClickDetector.java b/Sources/app/src/main/java/Views/ClickDetector.java index c21cf8f5b5665e4967f8962e8645b75ff9c13747..e88221ba6039a323b376fb35a4f431148d412e67 100644 --- a/Sources/app/src/main/java/Views/ClickDetector.java +++ b/Sources/app/src/main/java/Views/ClickDetector.java @@ -13,24 +13,29 @@ import Common.Coord; public class ClickDetector extends View { Callback cb = null; + // set the on touch callback function protected void init() { setOnTouchListener(this::touchListener); } + // constructor public ClickDetector(Context context) { super(context); this.init(); } + // constructor public ClickDetector(Context context, AttributeSet attrs) { super(context, attrs); this.init(); } + // set the callback function public void setOnClickCallback(Callback c) { this.cb = c; } + // on touch, if the touch is valid (only one finger, and the action is up), call the callback function with the touch coordinates protected boolean touchListener(View v, MotionEvent ev) { if (ev.getPointerCount() != 1) return true; if (ev.getAction() != MotionEvent.ACTION_DOWN) return true; diff --git a/Sources/app/src/main/java/Views/DoorsSelector.java b/Sources/app/src/main/java/Views/DoorsSelector.java index cae28f57639bba1b3ec01a7db37d0ab6bff6608c..6c9fb6f185c2f968e3a34c2148e00cd8b95f06dd 100644 --- a/Sources/app/src/main/java/Views/DoorsSelector.java +++ b/Sources/app/src/main/java/Views/DoorsSelector.java @@ -30,20 +30,24 @@ public class DoorsSelector extends View { RectF editedRect = null; List<RectF> btns = new ArrayList<>(); + // set the on touch callback function protected void init() { setOnTouchListener(this::touchListener); } + // constructor public DoorsSelector(Context context) { super(context); this.init(); } + // constructor public DoorsSelector(Context context, AttributeSet attrs) { super(context, attrs); this.init(); } + // draw all the pathviews of the current photo (+ the current edited pathview) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); @@ -66,6 +70,7 @@ public class DoorsSelector extends View { ); } + // draw one pathview on the canvas protected void drawDoorZone(Canvas c, RectF rect, boolean showButton, String name) { Paint p = new Paint(); int width = getWidth(); @@ -129,6 +134,7 @@ public class DoorsSelector extends View { } } + // checks if the click at the given position is on a pathview, if so, show the edit popup (or remove the pathview if the click is on the remove button) protected void propagateClick(Coord c) { List<PathView> pathviews = photo.getPathViews(); for(int i = 0; i < pathviews.size(); i++) { @@ -148,6 +154,7 @@ public class DoorsSelector extends View { } } + // touch listener (for all interactions: click, drag, etc.) protected boolean touchListener(View v, MotionEvent ev) { if (ev.getPointerCount() != 1) return true; @@ -185,14 +192,17 @@ public class DoorsSelector extends View { return true; } + // set the doors selector's current room public void setRoom(RoomInfo room) { this.room = room; } + // set the doors selector's current photo public void setPhoto(PhotoInfo photo) { this.photo = photo; } + // show the add popup for a new door protected void showAddDoorPopup() { RectF rect = new RectF(editedRect.left, editedRect.top, editedRect.right, editedRect.bottom); new PathviewPopup( @@ -219,6 +229,7 @@ public class DoorsSelector extends View { ).show(); } + // show the edit popup for a door protected void showEditPathviewPopup(int index) { PathView pv = photo.getPathView(index); new PathviewPopup( diff --git a/Sources/app/src/main/java/Views/InfoAdapter.java b/Sources/app/src/main/java/Views/InfoAdapter.java index fed05b2bd573cd04f021aa2fcdf8e6c31a43b949..8ff9ae16604bf2c99f70b16d51a5ba829ec58810 100644 --- a/Sources/app/src/main/java/Views/InfoAdapter.java +++ b/Sources/app/src/main/java/Views/InfoAdapter.java @@ -36,6 +36,7 @@ public class InfoAdapter<A> extends BaseAdapter { private int tile = 0; + // constructor public InfoAdapter(Context context, List<A> data, View noDataLayout, int tile) { this.context = context; this.data = data; @@ -44,6 +45,7 @@ public class InfoAdapter<A> extends BaseAdapter { this.tile = tile; } + // sets the adapter data list (and updates it) public void setData(List<A> data) { this.data = data; this.updateQueryList(); @@ -64,6 +66,7 @@ public class InfoAdapter<A> extends BaseAdapter { return i; } + // update the displayed data list based on the search query public void updateQueryList() { this.queryList.clear(); for(A info: this.data) { @@ -80,6 +83,7 @@ public class InfoAdapter<A> extends BaseAdapter { this.notifyDataSetChanged(); } + // returns the view of the given item (with the correct data, based on the binding list) @Override public View getView(int i, View view, ViewGroup viewGroup) { view = LayoutInflater.from(context).inflate(this.tile, viewGroup, false); @@ -100,11 +104,13 @@ public class InfoAdapter<A> extends BaseAdapter { return view; } + // setup the click listeners for the given view private void setupOnClick(View view, int i) { LinearLayout ll = view.findViewById(R.id.tile_box); Button b1 = view.findViewById(R.id.tile_remove); Button b2 = view.findViewById(R.id.tile_edit); + // if there is no edit button, then the view button is the edit button if (b2 == null) { ll.setOnClickListener(view1 -> { if (this.editCallback != null) @@ -133,11 +139,13 @@ public class InfoAdapter<A> extends BaseAdapter { }); } + // set the bindings list (and update the query list) public void setBindings(InfoBinding[] objs) { this.objs = objs; this.updateQueryList(); } + // add a binding to the bindings list (and update the query list) public void addBinding(InfoBinding binding) { InfoBinding[] newObjs = new InfoBinding[this.objs.length + 1]; for (int i = 0; i < this.objs.length; i++) @@ -147,18 +155,22 @@ public class InfoAdapter<A> extends BaseAdapter { this.updateQueryList(); } + // set the edit callback public void setOnEditListener(Callback cb) { this.editCallback = cb; } + // set the view callback public void setOnViewListener(Callback cb) { this.viewCallback = cb; } + // set the remove callback public void setOnRemoveListener(Callback cb) { this.removeCallback = cb; } + // set the search query (and update the query list) public void setSearchQuery(String query) { this.searchQuery = query; this.updateQueryList(); diff --git a/Sources/app/src/main/java/Views/LevelCanvas.java b/Sources/app/src/main/java/Views/LevelCanvas.java index 682e21b6243cf2d77e7be1dd63a051cf00eff6f4..1c7de26486714fa553b96fb1e2e68cd49b1ec22b 100644 --- a/Sources/app/src/main/java/Views/LevelCanvas.java +++ b/Sources/app/src/main/java/Views/LevelCanvas.java @@ -16,18 +16,21 @@ public class LevelCanvas extends View { Rect dims = new Rect(0, 0, 10, 10); float r; + // constructor public LevelCanvas(Context context, AttributeSet attrs) { super(context, attrs); p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setColor(Color.BLACK); } + // set the canvas dimensions @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); dims = new Rect(0, 0, w, h); } + // draw the phone's level based on the given accelerometer values (using askForDraw) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); @@ -64,9 +67,10 @@ public class LevelCanvas extends View { canvas.drawLine(radius, radius, p_x, p_y, p); } + // ask for a refresh of the level, with the given rotation (smooth it a little bit) public void askForDraw(float rot) { float newRot = - rot; - this.r += (newRot - this.r) / 4; // to smooth it a little bit + this.r += (newRot - this.r) / 4; // to smooth it this.invalidate(); } } \ No newline at end of file