Unverified Commit 77ae37ce authored by Lucio Maciel's avatar Lucio Maciel Committed by GitHub

Merge pull request #1852 from RocketChat/new/take-a-photo

[NEW] Take a photo
parents 6f0a22e3 cb67c120
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="rocket_chat_images"
path="Android/data/chat.rocket.android.dev/files/Pictures" />
</paths>
\ No newline at end of file
......@@ -105,6 +105,15 @@
<meta-data
android:name="io.fabric.ApiKey"
android:value="12ac6e94f850aaffcdff52001af77ca415d06a43" />
</application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="chat.rocket.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
......@@ -34,11 +34,9 @@ import chat.rocket.android.server.domain.uploadMimeTypeFilter
import chat.rocket.android.server.domain.useRealName
import chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import chat.rocket.android.server.infraestructure.state
import chat.rocket.android.util.extension.compressImageAndGetByteArray
import chat.rocket.android.util.extension.getByteArray
import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.extensions.avatarUrl
import chat.rocket.android.util.extensions.exhaustive
import chat.rocket.android.util.retryIO
import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType
......@@ -82,7 +80,6 @@ import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import org.threeten.bp.Instant
import timber.log.Timber
import java.io.InputStream
import java.util.*
import javax.inject.Inject
......
......@@ -8,8 +8,10 @@ import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.provider.MediaStore
import android.text.SpannableStringBuilder
import android.view.KeyEvent
import android.view.LayoutInflater
......@@ -23,6 +25,7 @@ import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.FileProvider
import androidx.core.text.bold
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
......@@ -73,6 +76,8 @@ import chat.rocket.android.util.extensions.rotateBy
import chat.rocket.android.util.extensions.showToast
import chat.rocket.android.util.extensions.textContent
import chat.rocket.android.util.extensions.ui
import chat.rocket.android.util.extension.createImageFile
import chat.rocket.android.util.extensions.getBitmpap
import chat.rocket.common.model.RoomType
import chat.rocket.common.model.roomTypeOf
import chat.rocket.core.internal.realtime.socket.model.State
......@@ -86,6 +91,9 @@ import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_attachment_options.*
import kotlinx.android.synthetic.main.message_composer.*
import kotlinx.android.synthetic.main.message_list.*
import timber.log.Timber
import java.io.File
import java.io.IOException
import kotlinx.android.synthetic.main.reaction_praises_list_item.*
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
......@@ -125,6 +133,7 @@ private const val BUNDLE_CHAT_ROOM_TYPE = "chat_room_type"
private const val BUNDLE_IS_CHAT_ROOM_READ_ONLY = "is_chat_room_read_only"
private const val REQUEST_CODE_FOR_PERFORM_SAF = 42
private const val REQUEST_CODE_FOR_DRAW = 101
private const val REQUEST_CODE_FOR_PERFORM_CAMERA = 102
private const val BUNDLE_CHAT_ROOM_LAST_SEEN = "chat_room_last_seen"
private const val BUNDLE_CHAT_ROOM_IS_SUBSCRIBED = "chat_room_is_subscribed"
private const val BUNDLE_CHAT_ROOM_IS_CREATOR = "chat_room_is_creator"
......@@ -196,6 +205,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
internal val description by lazy { dialogView.findViewById<EditText>(R.id.text_file_description) }
internal val audioVideoAttachment by lazy { dialogView.findViewById<FrameLayout>(R.id.audio_video_attachment) }
internal val textFile by lazy { dialogView.findViewById<TextView>(R.id.text_file_name) }
private var takenPhotoUri: Uri? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -284,12 +294,19 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
if (resultData != null && resultCode == Activity.RESULT_OK) {
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
REQUEST_CODE_FOR_PERFORM_SAF -> showFileAttachmentDialog(resultData.data)
REQUEST_CODE_FOR_DRAW -> showDrawAttachmentDialog(
resultData.getByteArrayExtra(DRAWING_BYTE_ARRAY_EXTRA_DATA)
)
REQUEST_CODE_FOR_PERFORM_CAMERA -> takenPhotoUri?.let { uri ->
uri.getBitmpap(requireContext())?.let { bitmap ->
presenter.uploadImage(chatRoomId, "image/png", uri, bitmap, "")
}
}
REQUEST_CODE_FOR_PERFORM_SAF -> resultData?.data?.let {
showFileAttachmentDialog(it)
}
REQUEST_CODE_FOR_DRAW -> resultData?.getByteArrayExtra(DRAWING_BYTE_ARRAY_EXTRA_DATA)?.let {
showDrawAttachmentDialog(it)
}
}
}
}
......@@ -884,7 +901,19 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
hideAttachmentOptions()
}
button_files.setOnClickListener {
button_add_reaction.setOnClickListener { _ ->
openEmojiKeyboardPopup()
}
button_take_a_photo.setOnClickListener {
dispatchTakePictureIntent()
handler.postDelayed({
hideAttachmentOptions()
}, 400)
}
button_attach_a_file.setOnClickListener {
handler.postDelayed({
presenter.selectFile()
}, 200)
......@@ -894,10 +923,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
}, 400)
}
button_add_reaction.setOnClickListener { _ ->
openEmojiKeyboardPopup()
}
button_drawing.setOnClickListener {
activity?.let { fragmentActivity ->
if (!ImageHelper.canWriteToExternalStorage(fragmentActivity)) {
......@@ -915,6 +940,26 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
}
}
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Create the File where the photo should go
val photoFile: File? = try {
activity?.createImageFile()
} catch (ex: IOException) {
Timber.e(ex)
null
}
// Continue only if the File was successfully created
photoFile?.also {
takenPhotoUri = FileProvider.getUriForFile(
requireContext(), "chat.rocket.android.fileprovider", it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, takenPhotoUri)
startActivityForResult(takePictureIntent, REQUEST_CODE_FOR_PERFORM_CAMERA)
}
}
}
private fun getUnfinishedMessage() {
val unfinishedMessage = presenter.getUnfinishedMessage(chatRoomId)
if (unfinishedMessage.isNotBlank()) {
......
......@@ -217,7 +217,7 @@ class ProfileFragment : Fragment(), ProfileView, ActionMode.Callback {
hideUpdateAvatarOptions()
}
button_take_photo.setOnClickListener {
button_take_a_photo.setOnClickListener {
dispatchTakePicture(REQUEST_CODE_FOR_PERFORM_CAMERA)
hideUpdateAvatarOptions()
}
......
......@@ -6,14 +6,24 @@
android:orientation="vertical">
<Button
android:id="@+id/button_files"
android:id="@+id/button_take_a_photo"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="20dp"
android:drawableStart="@drawable/ic_photo_camera_black_24dp"
android:gravity="start|center"
android:text="@string/action_take_a_photo" />
<Button
android:id="@+id/button_attach_a_file"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="20dp"
android:drawableStart="@drawable/ic_files_24dp"
android:gravity="start|center"
android:text="@string/action_files" />
android:text="@string/action_attach_a_files" />
<Button
android:id="@+id/button_drawing"
......
......@@ -16,14 +16,14 @@
android:text="@string/action_select_photo_from_gallery" />
<Button
android:id="@+id/button_take_photo"
android:id="@+id/button_take_a_photo"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_photo_camera_black_24dp"
android:drawablePadding="20dp"
android:gravity="start|center"
android:text="@string/action_take_photo" />
android:text="@string/action_take_a_photo" />
<Button
android:id="@+id/button_reset_avatar"
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">Erstelle Raum</string>
<string name="action_create">Erstelle</string>
<string name="action_logout">Abmelden</string>
<string name="action_files">Dateien</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">Bestätige Passwort Änderung</string>
<string name="action_join_chat">Trete Chat bei</string>
<string name="action_add_account">Erstelle Account</string>
......@@ -46,7 +46,7 @@
<string name="action_drawing">Zeichnung</string>
<string name="action_save_to_gallery">Sichern in Gallerie</string>
<string name="action_select_photo_from_gallery">Select photo from gallery</string> <!-- TODO Add translation -->
<string name="action_take_photo">Select photo from gallery</string> <!-- TODO Add translation -->
<string name="action_take_a_photo">Take a photo from galery</string> <!-- TODO Add translation -->
<string name="action_reset_avatar">Reset avatar</string> <!-- TODO Add translation -->
<string name="action_connect_server">Connect with a server</string> <!-- TODO Add translation -->
<string name="action_join_community">Join in the community</string> <!-- TODO Add translation -->
......
......@@ -34,7 +34,7 @@
<string name="action_create_channel">Crear canal</string>
<string name="action_create">Create</string>
<string name="action_logout">Cerrar sesión</string>
<string name="action_files">Archivos</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">Confirmar cambio de contraseña</string>
<string name="action_join_chat">Unirse al chat</string>
<string name="action_add_account">Añadir cuenta</string>
......@@ -45,7 +45,7 @@
<string name="action_drawing">Dibujo</string>
<string name="action_save_to_gallery">Guardar en la galería</string>
<string name="action_select_photo_from_gallery">Select photo from gallery</string> <!-- TODO Add translation -->
<string name="action_take_photo">Select photo from gallery</string> <!-- TODO Add translation -->
<string name="action_take_a_photo">Take a photo</string> <!-- TODO Add translation -->
<string name="action_reset_avatar">Reset avatar</string> <!-- TODO Add translation -->
<string name="action_connect_server">Connect with a server</string> <!-- TODO Add translation -->
<string name="action_join_community">Join in the community</string> <!-- TODO Add translation -->
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">Créer salon</string>
<string name="action_create">Créer</string>
<string name="action_logout">Se déconnecter</string>
<string name="action_files">Fichiers</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">Confirmer le mot de passe</string>
<string name="action_join_chat">Rejoignez le chat</string>
<string name="action_add_account">Ajouter un compte</string>
......@@ -46,7 +46,7 @@
<string name="action_drawing">Dessin</string>
<string name="action_save_to_gallery">Sauvegarder vers la gallerie</string>
<string name="action_select_photo_from_gallery">Sélectionner depuis la gallerie</string>
<string name="action_take_photo">Prendre une photo</string>
<string name="action_take_a_photo">Prendre une photo</string>
<string name="action_reset_avatar">Réinitialiser l\'avatar</string>
<string name="action_connect_server">Connect with a server</string> <!-- TODO Add translation -->
<string name="action_join_community">Join in the community</string> <!-- TODO Add translation -->
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">चैनल बनाएं</string>
<string name="action_create">बनाएं</string>
<string name="action_logout">लोग आउट करें</string>
<string name="action_files">फ़ाइलें</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">पासवर्ड परिवर्तन की पुष्टि करें</string>
<string name="action_join_chat">चैट में शामिल हों</string>
<string name="action_add_account">खाता जोड़ो</string>
......@@ -46,7 +46,7 @@
<string name="action_save_to_gallery">गैलरी में सहेजें</string>
<string name="action_drawing">चित्रकारी</string>
<string name="action_select_photo_from_gallery">गैलरी से फोटो का चयन करें</string>
<string name="action_take_photo">फोटो खेचिये</string>
<string name="action_take_a_photo">फोटो खेचिये</string>
<string name="action_reset_avatar">अवतार रीसेट करें</string>
<string name="action_connect_server">सर्वर से कनेक्ट करें</string>
<string name="action_join_community">समुदाय में शामिल हों</string>
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">Crea canale</string>
<string name="action_create">Crea</string>
<string name="action_logout">Disconnettersi</string>
<string name="action_files">Documenti</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO ADD translation -->
<string name="action_confirm_password">Conferma Cambio Password</string>
<string name="action_join_chat">Iscriviti alla stanza</string>
<string name="action_add_account">Aggiungi utente</string>
......@@ -46,7 +46,7 @@
<string name="action_drawing">Disegno</string>
<string name="action_save_to_gallery">Salva in galleria</string>
<string name="action_select_photo_from_gallery">Seleziona foto dalla galleria</string>
<string name="action_take_photo">Fare foto</string>
<string name="action_take_a_photo">Take a photo</string> <!-- TODO ADD translation -->
<string name="action_reset_avatar">Reimposta avatar</string>
<string name="action_connect_server">Collegati a un server</string>
<string name="action_join_community">Entra nella community</string>
......
......@@ -26,9 +26,6 @@
<string name="title_create_channel">新しいチャネルを作成</string>
<string name="title_are_you_sure">本気ですか?</string>
<!-- Actions -->
<string name="action_connect">接続</string>
<string name="action_use_this_username">このユーザー名を使用する</string>
......@@ -40,7 +37,7 @@
<string name="action_create_channel">チャンネル作成</string>
<string name="action_create">作ります</string>
<string name="action_logout">ログアウト</string>
<string name="action_files">ファイル</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">変更したパスワードの確認</string>
<string name="action_join_chat">チャットに参加</string>
<string name="action_add_account">サーバーの追加</string>
......@@ -51,7 +48,7 @@
<string name="action_drawing">絵を描く</string>
<string name="action_save_to_gallery">ギャラリーに保存</string>
<string name="action_select_photo_from_gallery">ギャラリーの写真を選択</string>
<string name="action_take_photo">写真を撮る</string>
<string name="action_take_a_photo">写真を撮る</string>
<string name="action_reset_avatar">アバターをリセット</string>
<string name="action_connect_server">サーバーに接続</string>
<string name="action_join_community">コミュニティに参加</string>
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">Criar chat</string>
<string name="action_create">Criar</string>
<string name="action_logout">Sair</string>
<string name="action_files">Arquivos</string>
<string name="action_attach_a_files">Anexar um arquivo</string>
<string name="action_confirm_password">Confirme a nova senha</string>
<string name="action_join_chat">Entrar no Chat</string>
<string name="action_add_account">Adicionar conta</string>
......@@ -46,7 +46,7 @@
<string name="action_drawing">Desenhando</string>
<string name="action_save_to_gallery">Salvar na galeria</string>
<string name="action_select_photo_from_gallery">Escolher foto da galeria</string>
<string name="action_take_photo">Tirar foto</string>
<string name="action_take_a_photo">Tirar uma foto</string>
<string name="action_reset_avatar">Resetar avatar</string>
<string name="action_connect_server">Conectar com um servidor</string>
<string name="action_join_community">Junte-se à comunidade</string>
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">Создать канал</string>
<string name="action_create">Создать</string>
<string name="action_logout">Выйти</string>
<string name="action_files">Файлы</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">Подтверждение изменения пароля</string>
<string name="action_join_chat">Присоединиться к чату</string>
<string name="action_add_account">Добавить аккаунт</string>
......@@ -46,7 +46,7 @@
<string name="action_drawing">Рисунок</string>
<string name="action_save_to_gallery">Сохранить в галерею</string>
<string name="action_select_photo_from_gallery">Выбрать из галереи</string>
<string name="action_take_photo">Сделать снимок</string>
<string name="action_take_a_photo">Сделать снимок</string>
<string name="action_reset_avatar">Восстановить аватар</string>
<string name="action_connect_server">Соединиться с сервером</string>
<string name="action_join_community">Сообщество</string>
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">Yeni Kanal Oluştur</string>
<string name="action_create">Oluştur</string>
<string name="action_logout">Çıkış Yap</string>
<string name="action_files">Dosyalar</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">Şifre Değişikliğini Onaylayın</string>
<string name="action_join_chat">Sohbete Bağlan</string>
<string name="action_add_account">Hesap Ekle</string>
......@@ -46,7 +46,7 @@
<string name="action_drawing">Çizim</string>
<string name="action_save_to_gallery">Galeriye kaydet</string>
<string name="action_select_photo_from_gallery">Galeriden resim seç</string>
<string name="action_take_photo">Fotoğraf çek</string>
<string name="action_take_a_photo">Fotoğraf çek</string>
<string name="action_reset_avatar">Avatarı kaldır</string>
<string name="action_connect_server">Connect with a server</string> <!-- TODO Add translation -->
<string name="action_join_community">Join in the community</string> <!-- TODO Add translation -->
......
......@@ -35,7 +35,7 @@
<string name="action_create_channel">Створити канал</string>
<string name="action_create">Створити</string>
<string name="action_logout">Вийти</string>
<string name="action_files">Файли</string>
<string name="action_attach_a_files">Attach a file</string> <!-- TODO Add translation -->
<string name="action_confirm_password">Підтвердження зміни пароля</string>
<string name="action_join_chat">Приєднатися до чату</string>
<string name="action_add_account">Додати аккаунт</string>
......@@ -46,7 +46,7 @@
<string name="action_drawing">Малюнок</string>
<string name="action_save_to_gallery">Зберегти до галереї</string>
<string name="action_select_photo_from_gallery">Вибрати з галереї</string>
<string name="action_take_photo">Зробити знімок</string>
<string name="action_take_a_photo">Зробити знімок</string>
<string name="action_reset_avatar">Відновити аватар</string>
<string name="action_connect_server">Connect with a server</string> <!-- TODO Add translation -->
<string name="action_join_community">Join in the community</string> <!-- TODO Add translation -->
......
......@@ -47,7 +47,7 @@ https://github.com/RocketChat/java-code-styles/blob/master/CODING_STYLE.md#strin
<string name="action_create_channel">Create channel</string>
<string name="action_create">Create</string>
<string name="action_logout">Logout</string>
<string name="action_files">Files</string>
<string name="action_attach_a_files">Attach a file</string>
<string name="action_confirm_password">Confirm Password Change</string>
<string name="action_join_chat">Join Chat</string>
<string name="action_add_account">Add account</string>
......@@ -58,7 +58,7 @@ https://github.com/RocketChat/java-code-styles/blob/master/CODING_STYLE.md#strin
<string name="action_drawing">Drawing</string>
<string name="action_save_to_gallery">Save to gallery</string>
<string name="action_select_photo_from_gallery">Select photo from gallery</string>
<string name="action_take_photo">Take photo</string>
<string name="action_take_a_photo">Take a photo</string>
<string name="action_reset_avatar">Reset avatar</string>
<string name="action_connect_server">Connect with a server</string>
<string name="action_join_community">Join in the community</string>
......
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="rocket_chat_images"
path="Android/data/chat.rocket.android/files/Pictures" />
</paths>
\ No newline at end of file
......@@ -2,13 +2,19 @@ package chat.rocket.android.util.extension
import android.content.Intent
import android.graphics.Bitmap
import android.os.Environment
import android.provider.MediaStore
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.experimental.DefaultDispatcher
import kotlinx.coroutines.experimental.withContext
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.IOException
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
/**
* Compress a [Bitmap] image.
......@@ -104,4 +110,16 @@ fun Fragment.dispatchTakePicture(requestCode: Int) {
if (takePictureIntent.resolveActivity(context?.packageManager) != null) {
startActivityForResult(takePictureIntent, requestCode)
}
}
@Throws(IOException::class)
fun FragmentActivity.createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"PNG_${timeStamp}_", /* prefix */
".png", /* suffix */
storageDir /* directory */
)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment