Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ android {
}
buildFeatures {
viewBinding true
buildConfig true
}
}

Expand Down
20 changes: 9 additions & 11 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature
android:name="android.hardware.camera"
android:required="false" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -12,20 +20,10 @@
android:supportsRtl="true"
android:theme="@style/Theme.OTUSLocationMapsHW"
tools:targetApi="31">
<!--
TODO: Before you run your application, you need a Google Maps API key.

To get one, follow the directions here:

https://developers.google.com/maps/documentation/android-sdk/get-api-key

Once you have your API key (it starts with "AIza"), define a new property in your
project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the
"YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />
android:value="${MAPS_API_KEY}" />

<activity
android:name=".MapsActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == CameraActivity.SUCCESS_RESULT_CODE) {
// TODO("Обновить точки на карте при получении результата от камеры")
showPreviewsOnMap()
}
}

Expand All @@ -43,7 +43,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
// TODO("Вызвать инициализацию карты")
mapFragment.getMapAsync(this)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
Expand Down Expand Up @@ -83,12 +83,13 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
inPreferredConfig = Bitmap.Config.ARGB_8888
}), 64, 64, false
)
// TODO("Указать pinBitmap как иконку для маркера")

map.addMarker(
MarkerOptions()
.position(point)
.icon(BitmapDescriptorFactory.fromBitmap(pinBitmap))
)
// TODO("Передвинуть карту к местоположению последнего фото")
map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 15f))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.sample.otuslocationmapshw.camera

import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.Manifest.permission.CAMERA
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.hardware.SensorManager.SENSOR_DELAY_UI
import android.location.Location
import android.os.Bundle
import android.util.Log
Expand All @@ -14,10 +18,13 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import androidx.camera.core.ImageCaptureException
import androidx.camera.core.Preview
import androidx.camera.core.takePicture
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.google.android.gms.location.CurrentLocationRequest
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.common.util.concurrent.ListenableFuture
Expand Down Expand Up @@ -58,9 +65,9 @@ class CameraActivity : AppCompatActivity() {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
cameraProviderFuture = ProcessCameraProvider.getInstance(this)

// TODO("Получить экземпляр SensorManager")
// TODO("Добавить проверку на наличие датчика акселерометра и присвоить значение tiltSensor")
tiltSensor = TODO("Get tilt sensor")
sensorManager = getSystemService(SensorManager::class.java)
tiltSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)

cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
}, ContextCompat.getMainExecutor(this))
Expand All @@ -81,9 +88,17 @@ class CameraActivity : AppCompatActivity() {
}
}

// TODO("Подписаться на получение событий обновления датчика")
override fun onResume() {
super.onResume()
if (tiltSensor == null) return
sensorManager.registerListener(sensorEventListener, tiltSensor, SENSOR_DELAY_UI)
}

// TODO("Остановить получение событий от датчика")
override fun onPause() {
super.onPause()
if (tiltSensor == null) return
sensorManager.unregisterListener(sensorEventListener)
}

override fun onRequestPermissionsResult(
requestCode: Int,
Expand Down Expand Up @@ -115,21 +130,43 @@ class CameraActivity : AppCompatActivity() {
if (!folder.exists()) {
folder.mkdirs()
}
val filePath = folderPath + SimpleDateFormat(FILENAME_FORMAT, Locale.getDefault()).format(Date())
val filePath =
folderPath + SimpleDateFormat(FILENAME_FORMAT, Locale.getDefault()).format(Date())

// TODO("4. Добавить установку местоположения в метаданные фото")
val outputFileOptions = ImageCapture.OutputFileOptions.Builder(File(filePath))
.setMetadata(ImageCapture.Metadata().apply { this.location = location })
.build()

// TODO("Добавить вызов CameraX для фото")
// TODO("Вывести Toast о том, что фото успешно сохранено и закрыть текущее активити c указанием кода результата SUCCESS_RESULT_CODE")
// imageCapture...
imageCapture.takePicture(
outputFileOptions,
ContextCompat.getMainExecutor(this),
object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
Toast.makeText(
this@CameraActivity,
"saved",
Toast.LENGTH_SHORT
).show()
setResult(SUCCESS_RESULT_CODE)
finish()
}

override fun onError(e: ImageCaptureException) {
Toast.makeText(
this@CameraActivity,
"Error: ${e.message}",
Toast.LENGTH_LONG
).show()
}
})
}
}

@SuppressLint("MissingPermission")
private fun getLastLocation(callback: (location: Location?) -> Unit) {
// TODO("Добавить получение местоположения от fusedLocationClient и передать результат в callback после получения")

fusedLocationClient.getCurrentLocation(CurrentLocationRequest.Builder().build(), null)
.addOnCompleteListener { callback.invoke(it.result) }
}

private fun startCamera() {
Expand Down Expand Up @@ -168,9 +205,11 @@ class CameraActivity : AppCompatActivity() {
private const val TAG = "CameraXApp"
private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
private const val REQUEST_CODE_PERMISSIONS = 10
// TODO("Указать набор требуемых разрешений")

private val REQUIRED_PERMISSIONS: Array<String> = mutableListOf<String>(
// TODO("Добавить требуемые разрешения")
ACCESS_COARSE_LOCATION,
ACCESS_FINE_LOCATION,
CAMERA
).toTypedArray()

const val SUCCESS_RESULT_CODE = 15
Expand Down
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
dependencies {
classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
}
}

plugins {
id 'com.android.application' version '8.7.3' apply false
id 'com.android.library' version '8.7.3' apply false
Expand Down