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
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

android {
compileSdk 34
compileSdk 36

defaultConfig {
applicationId "otus.gpb.homework.activities"
Expand Down Expand Up @@ -60,4 +60,5 @@ dependencies {
implementation 'androidx.activity:activity-ktx:1.9.0'
implementation 'androidx.fragment:fragment-ktx:1.7.1'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.activity:activity:1.11.0'
}
10 changes: 9 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<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" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -12,7 +18,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.Activities"
tools:targetApi="31">

<activity
android:name=".FillFormActivity"
android:exported="false" />
<activity
android:name=".EditProfileActivity"
android:exported="true">
Expand Down
156 changes: 153 additions & 3 deletions app/src/main/java/otus/gpb/homework/activities/EditProfileActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
package otus.gpb.homework.activities

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import otus.gpb.homework.alertsdialogs.EditProfileDialog
import otus.gpb.homework.alertsdialogs.EditProfileListener
import otus.gpb.homework.alertsdialogs.PermissionDialogListener
import otus.gpb.homework.alertsdialogs.PermissionsAlertDialog

class EditProfileActivity : AppCompatActivity() {
open class EditProfileActivity : AppCompatActivity(), EditProfileListener,
PermissionDialogListener {

private lateinit var imageView: ImageView
private lateinit var textviewName: TextView
private lateinit var textviewSurname: TextView
private lateinit var textviewAge: TextView

private val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
takePhoto()
}
}

private val pickImageLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
uri?.let {
populateImage(uri)
}
}

private val getFormLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val data = result.data
val code = result.resultCode

when (code) {
RESULT_OK -> {
fillTextFields(data)
}

RESULT_CANCELED -> {}
else -> {}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit_profile)
imageView = findViewById(R.id.imageview_photo)
textviewName = findViewById<TextView>(R.id.textview_name)
textviewSurname = findViewById<TextView>(R.id.textview_surname)
textviewAge = findViewById<TextView>(R.id.textview_age)

val btn4 = findViewById<Button>(R.id.button4)

findViewById<Toolbar>(R.id.toolbar).apply {
inflateMenu(R.menu.menu)
Expand All @@ -24,10 +75,20 @@ class EditProfileActivity : AppCompatActivity() {
openSenderApp()
true
}

else -> false
}
}
}

imageView.setOnClickListener {
EditProfileDialog(this).show(supportFragmentManager, "CHOOSING")
}

btn4.setOnClickListener {
val intent = Intent(this, FillFormActivity::class.java)
getFormLauncher.launch(intent)
}
}

/**
Expand All @@ -39,6 +100,95 @@ class EditProfileActivity : AppCompatActivity() {
}

private fun openSenderApp() {
TODO("В качестве реализации метода отправьте неявный Intent чтобы поделиться профилем. В качестве extras передайте заполненные строки и картинку")
// TODO(
// "В качестве реализации метода отправьте неявный Intent чтобы поделиться профилем. " +
// "В качестве extras передайте заполненные строки и картинку"
// )

val name = textviewName.text?.toString() ?: ""
val surname = textviewSurname.text?.toString() ?: ""
val age = textviewAge.text?.toString() ?: ""
val imageUri = imageView.id

val intent = Intent(Intent.ACTION_SEND)
intent.apply {
setPackage("org.telegram.messenger")
putExtra("name", name)
putExtra("surname", surname)
putExtra("age", age)
putExtra(Intent.EXTRA_STREAM, imageUri)
}

try {
startActivity(intent)
} catch (e: Exception) {
Toast.makeText(this, "Telegram не найден: $e", Toast.LENGTH_SHORT).show()
}

}

private fun requestCameraPermissions() {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}

private fun showPermissionsRational() {
PermissionsAlertDialog(this).show(supportFragmentManager, "PERMISSIONS")
}

private fun takePhoto() {
imageView.setImageResource(R.drawable.cat)
}

override fun callCameraPermissionLauncher() {
when {
(ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED) -> {
takePhoto()
}

shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> {
showPermissionsRational()
}

else -> {
requestCameraPermissions()
}
}
}

override fun openGallery() {
pickImageLauncher.launch("image/*")
}

override fun getCameraPermissionLauncher() {
when {
(ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED) -> {
takePhoto()
}

else -> {
val intent = Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", packageName, null)
)
startActivity(intent)
}
}
}

private fun fillTextFields(intent: Intent?) {
val bundle = intent?.getBundleExtra("BundleTitle")
val name = bundle?.getString("name")
val surname = bundle?.getString("surname")
val age = bundle?.getString("age")

textviewName.text = name
textviewSurname.text = surname
textviewAge.text = age
}
}
}
50 changes: 50 additions & 0 deletions app/src/main/java/otus/gpb/homework/activities/FillFormActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package otus.gpb.homework.activities

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class FillFormActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_fill_form)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

val nameField = findViewById<EditText>(R.id.name)
val surnameField = findViewById<EditText>(R.id.surname)
val ageField = findViewById<EditText>(R.id.age)
val getDataBtn = findViewById<Button>(R.id.btn_get)

getDataBtn.setOnClickListener {
val name = nameField.text?.toString() ?: ""
val surname = surnameField.text?.toString() ?: ""
val age = ageField.text?.toString() ?: ""

val bundle = Bundle()
bundle.apply {
putString("name", name)
putString("surname", surname)
putString("age", age)
}

val intent = Intent()
intent.putExtra("BundleTitle", bundle)

setResult(Activity.RESULT_OK, intent)

finish()
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package otus.gpb.homework.alertsdialogs

import android.app.Dialog
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment

interface EditProfileListener {
fun callCameraPermissionLauncher()
fun openGallery()
}

class EditProfileDialog(private val listener: EditProfileListener) : DialogFragment() {

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
builder
.setTitle("Выберите действие")
.setNegativeButton("Отменить") { _, _ ->
}
.setItems(arrayOf("Сделать фото", "Выбрать фото")) { dialog, which ->
when (which) {
0 -> {listener.callCameraPermissionLauncher()}
1 -> {listener.openGallery()}
}
}
builder.create()
} ?: throw IllegalStateException()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package otus.gpb.homework.alertsdialogs

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment

interface PermissionDialogListener {
fun getCameraPermissionLauncher()
}

class PermissionsAlertDialog(private val listener: PermissionDialogListener): DialogFragment() {

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
builder
.setMessage("Приложению нужен доступ к камере, чтобы сделать фото")
.setTitle("Разрешения камеры")
.setPositiveButton("Дать доступ") { _, _ ->
listener.getCameraPermissionLauncher()
}
.setNegativeButton("Отмена") { _, _ ->

}
builder.create()
} ?: throw IllegalStateException()
}
}
Loading