forked from upstream-mirrors/quik
Add button to select contact, parse response as vCard string
This commit is contained in:
@@ -27,6 +27,7 @@ import android.content.Intent
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.provider.ContactsContract
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import android.text.format.DateFormat
|
import android.text.format.DateFormat
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
@@ -68,8 +69,9 @@ import javax.inject.Inject
|
|||||||
class ComposeActivity : QkThemedActivity(), ComposeView {
|
class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val CAMERA_REQUEST_CODE = 0
|
private const val CAMERA_REQUEST_CODE = 0
|
||||||
const val GALLERY_REQUEST_CODE = 1
|
private const val GALLERY_REQUEST_CODE = 1
|
||||||
|
private const val CONTACT_REQUEST_CODE = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject lateinit var attachmentAdapter: AttachmentAdapter
|
@Inject lateinit var attachmentAdapter: AttachmentAdapter
|
||||||
@@ -97,7 +99,9 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
override val cameraIntent by lazy { Observable.merge(camera.clicks(), cameraLabel.clicks()) }
|
override val cameraIntent by lazy { Observable.merge(camera.clicks(), cameraLabel.clicks()) }
|
||||||
override val galleryIntent by lazy { Observable.merge(gallery.clicks(), galleryLabel.clicks()) }
|
override val galleryIntent by lazy { Observable.merge(gallery.clicks(), galleryLabel.clicks()) }
|
||||||
override val scheduleIntent by lazy { Observable.merge(schedule.clicks(), scheduleLabel.clicks()) }
|
override val scheduleIntent by lazy { Observable.merge(schedule.clicks(), scheduleLabel.clicks()) }
|
||||||
|
override val attachContactIntent by lazy { Observable.merge(contact.clicks(), contactLabel.clicks()) }
|
||||||
override val attachmentSelectedIntent: Subject<Uri> = PublishSubject.create()
|
override val attachmentSelectedIntent: Subject<Uri> = PublishSubject.create()
|
||||||
|
override val contactSelectedIntent: Subject<Uri> = PublishSubject.create()
|
||||||
override val inputContentIntent by lazy { message.inputContentSelected }
|
override val inputContentIntent by lazy { message.inputContentSelected }
|
||||||
override val scheduleSelectedIntent: Subject<Long> = PublishSubject.create()
|
override val scheduleSelectedIntent: Subject<Long> = PublishSubject.create()
|
||||||
override val changeSimIntent by lazy { sim.clicks() }
|
override val changeSimIntent by lazy { sim.clicks() }
|
||||||
@@ -232,9 +236,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
send.imageAlpha = if (state.canSend) 255 else 128
|
send.imageAlpha = if (state.canSend) 255 else 128
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun clearSelection() {
|
override fun clearSelection() = messageAdapter.clearSelection()
|
||||||
messageAdapter.clearSelection()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun showDetails(details: String) {
|
override fun showDetails(details: String) {
|
||||||
AlertDialog.Builder(this)
|
AlertDialog.Builder(this)
|
||||||
@@ -268,6 +270,13 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show()
|
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun requestContact() {
|
||||||
|
val intent = Intent(Intent.ACTION_PICK)
|
||||||
|
.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE)
|
||||||
|
|
||||||
|
startActivityForResult(Intent.createChooser(intent, null), CONTACT_REQUEST_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
override fun requestCamera() {
|
override fun requestCamera() {
|
||||||
cameraDestination = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
cameraDestination = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
||||||
.let { timestamp -> ContentValues().apply { put(MediaStore.Images.Media.TITLE, timestamp) } }
|
.let { timestamp -> ContentValues().apply { put(MediaStore.Images.Media.TITLE, timestamp) } }
|
||||||
@@ -288,9 +297,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
startActivityForResult(Intent.createChooser(intent, null), GALLERY_REQUEST_CODE)
|
startActivityForResult(Intent.createChooser(intent, null), GALLERY_REQUEST_CODE)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setDraft(draft: String) {
|
override fun setDraft(draft: String) = message.setText(draft)
|
||||||
message.setText(draft)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun scrollToMessage(id: Long) {
|
override fun scrollToMessage(id: Long) {
|
||||||
messageAdapter.data?.second
|
messageAdapter.data?.second
|
||||||
@@ -323,15 +330,13 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
if (resultCode == Activity.RESULT_OK) {
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
when (requestCode) {
|
when (requestCode) {
|
||||||
CAMERA_REQUEST_CODE -> cameraDestination
|
CAMERA_REQUEST_CODE -> cameraDestination?.let(attachmentSelectedIntent::onNext)
|
||||||
GALLERY_REQUEST_CODE -> data?.data
|
GALLERY_REQUEST_CODE -> data?.data?.let(attachmentSelectedIntent::onNext)
|
||||||
else -> null
|
CONTACT_REQUEST_CODE -> data?.data?.let(contactSelectedIntent::onNext)
|
||||||
}?.let(attachmentSelectedIntent::onNext)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBackPressed() {
|
override fun onBackPressed() = backPressedIntent.onNext(Unit)
|
||||||
backPressedIntent.onNext(Unit)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -48,7 +48,9 @@ interface ComposeView : QkView<ComposeState> {
|
|||||||
val cameraIntent: Observable<*>
|
val cameraIntent: Observable<*>
|
||||||
val galleryIntent: Observable<*>
|
val galleryIntent: Observable<*>
|
||||||
val scheduleIntent: Observable<*>
|
val scheduleIntent: Observable<*>
|
||||||
|
val attachContactIntent: Observable<*>
|
||||||
val attachmentSelectedIntent: Observable<Uri>
|
val attachmentSelectedIntent: Observable<Uri>
|
||||||
|
val contactSelectedIntent: Observable<Uri>
|
||||||
val inputContentIntent: Observable<InputContentInfoCompat>
|
val inputContentIntent: Observable<InputContentInfoCompat>
|
||||||
val scheduleSelectedIntent: Observable<Long>
|
val scheduleSelectedIntent: Observable<Long>
|
||||||
val scheduleCancelIntent: Observable<*>
|
val scheduleCancelIntent: Observable<*>
|
||||||
@@ -64,6 +66,7 @@ interface ComposeView : QkView<ComposeState> {
|
|||||||
fun requestCamera()
|
fun requestCamera()
|
||||||
fun requestGallery()
|
fun requestGallery()
|
||||||
fun requestDatePicker()
|
fun requestDatePicker()
|
||||||
|
fun requestContact()
|
||||||
fun setDraft(draft: String)
|
fun setDraft(draft: String)
|
||||||
fun scrollToMessage(id: Long)
|
fun scrollToMessage(id: Long)
|
||||||
fun showQksmsPlusSnackbar(@StringRes message: Int)
|
fun showQksmsPlusSnackbar(@StringRes message: Int)
|
||||||
|
|||||||
@@ -19,6 +19,8 @@
|
|||||||
package com.moez.QKSMS.feature.compose
|
package com.moez.QKSMS.feature.compose
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.ContactsContract
|
||||||
import android.telephony.PhoneNumberUtils
|
import android.telephony.PhoneNumberUtils
|
||||||
import android.telephony.SmsMessage
|
import android.telephony.SmsMessage
|
||||||
import android.view.inputmethod.EditorInfo
|
import android.view.inputmethod.EditorInfo
|
||||||
@@ -68,10 +70,12 @@ import io.reactivex.subjects.BehaviorSubject
|
|||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
import io.realm.RealmList
|
import io.realm.RealmList
|
||||||
|
import timber.log.Timber
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Named
|
import javax.inject.Named
|
||||||
|
|
||||||
|
|
||||||
class ComposeViewModel @Inject constructor(
|
class ComposeViewModel @Inject constructor(
|
||||||
@Named("query") private val query: String,
|
@Named("query") private val query: String,
|
||||||
@Named("threadId") private val threadId: Long,
|
@Named("threadId") private val threadId: Long,
|
||||||
@@ -519,6 +523,21 @@ class ComposeViewModel @Inject constructor(
|
|||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { scheduled -> newState { copy(scheduled = scheduled) } }
|
.subscribe { scheduled -> newState { copy(scheduled = scheduled) } }
|
||||||
|
|
||||||
|
// Attach a contact
|
||||||
|
view.attachContactIntent
|
||||||
|
.doOnNext { newState { copy(attaching = false) } }
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { view.requestContact() }
|
||||||
|
|
||||||
|
// Contact was selected for attachment
|
||||||
|
view.contactSelectedIntent
|
||||||
|
.map(::vCard)
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe({}, { error ->
|
||||||
|
context.makeToast(R.string.compose_contact_error)
|
||||||
|
Timber.w(error)
|
||||||
|
})
|
||||||
|
|
||||||
// Detach a photo
|
// Detach a photo
|
||||||
view.attachmentDeletedIntent
|
view.attachmentDeletedIntent
|
||||||
.withLatestFrom(attachments) { bitmap, attachments -> attachments.filter { it !== bitmap } }
|
.withLatestFrom(attachments) { bitmap, attachments -> attachments.filter { it !== bitmap } }
|
||||||
@@ -678,4 +697,17 @@ class ComposeViewModel @Inject constructor(
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun vCard(contactData: Uri): String? {
|
||||||
|
val lookupKey = context.contentResolver.query(contactData, null, null, null, null)?.use { cursor ->
|
||||||
|
cursor.moveToFirst()
|
||||||
|
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY))
|
||||||
|
}
|
||||||
|
|
||||||
|
val vCardUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey)
|
||||||
|
return context.contentResolver.openAssetFileDescriptor(vCardUri, "r")
|
||||||
|
?.createInputStream()
|
||||||
|
?.readBytes()
|
||||||
|
?.let { bytes -> String(bytes) }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -397,7 +397,8 @@
|
|||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
app:constraint_referenced_ids="schedule, scheduleLabel, gallery, galleryLabel, camera, cameraLabel, attachingBackground" />
|
app:constraint_referenced_ids="contact, contactLabel, schedule, scheduleLabel, gallery, galleryLabel, camera,
|
||||||
|
cameraLabel, attachingBackground" />
|
||||||
|
|
||||||
<View
|
<View
|
||||||
android:id="@+id/attachingBackground"
|
android:id="@+id/attachingBackground"
|
||||||
@@ -410,6 +411,40 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/contact"
|
||||||
|
android:layout_width="40dp"
|
||||||
|
android:layout_height="40dp"
|
||||||
|
android:layout_marginBottom="12dp"
|
||||||
|
android:background="@drawable/circle"
|
||||||
|
android:backgroundTint="?attr/bubbleColor"
|
||||||
|
android:contentDescription="@string/compose_contact_cd"
|
||||||
|
android:elevation="4dp"
|
||||||
|
android:padding="10dp"
|
||||||
|
android:src="@drawable/ic_person_black_24dp"
|
||||||
|
android:tint="?android:attr/textColorSecondary"
|
||||||
|
android:visibility="visible"
|
||||||
|
app:layout_constraintBottom_toTopOf="@id/schedule"
|
||||||
|
app:layout_constraintEnd_toEndOf="@id/attach"
|
||||||
|
app:layout_constraintStart_toStartOf="@id/attach" />
|
||||||
|
|
||||||
|
<com.moez.QKSMS.common.widget.QkTextView
|
||||||
|
android:id="@+id/contactLabel"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="32dp"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:background="@drawable/rounded_rectangle_4dp"
|
||||||
|
android:backgroundTint="?attr/bubbleColor"
|
||||||
|
android:elevation="4dp"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
android:paddingStart="8dp"
|
||||||
|
android:text="@string/compose_contact_cd"
|
||||||
|
android:textColor="?android:attr/textColorPrimary"
|
||||||
|
app:layout_constraintBottom_toBottomOf="@id/contact"
|
||||||
|
app:layout_constraintStart_toEndOf="@id/contact"
|
||||||
|
app:layout_constraintTop_toTopOf="@id/contact" />
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/schedule"
|
android:id="@+id/schedule"
|
||||||
android:layout_width="40dp"
|
android:layout_width="40dp"
|
||||||
|
|||||||
@@ -127,6 +127,8 @@
|
|||||||
<string name="compose_gallery_cd">Attach a photo</string>
|
<string name="compose_gallery_cd">Attach a photo</string>
|
||||||
<string name="compose_camera_cd">Take a photo</string>
|
<string name="compose_camera_cd">Take a photo</string>
|
||||||
<string name="compose_schedule_cd">Schedule message</string>
|
<string name="compose_schedule_cd">Schedule message</string>
|
||||||
|
<string name="compose_contact_cd">Attach a contact</string>
|
||||||
|
<string name="compose_contact_error">Error reading contact</string>
|
||||||
<string name="compose_sim_cd">%s selected, change SIM card</string>
|
<string name="compose_sim_cd">%s selected, change SIM card</string>
|
||||||
<string name="compose_send_cd">Send message</string>
|
<string name="compose_send_cd">Send message</string>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user