lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.fuban.user.ui.trip
 
import android.Manifest
import android.app.Activity
import android.content.Intent
import android.database.Cursor
import android.net.Uri
import android.provider.ContactsContract
import cn.sinata.xldutils.utils.isValidPhone
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.utils.optString
import com.fuban.user.R
import com.fuban.user.network.HttpManager
import com.fuban.user.network.request
import com.fuban.user.ui.H5Activity
import com.fuban.user.ui.TransparentStatusBarActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_order_other.*
import org.jetbrains.anko.startActivity
 
class OrderOtherActivity:TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_order_other
 
    override fun initClick() {
        tv_contact.setOnClickListener {
            RxPermissions(this).request(Manifest.permission.READ_CONTACTS).subscribe {
                if (it) {
                    startActivityForResult(Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 1)
                } else {
                    myToast("没有读取联系人权限")
                }
            }
        }
        tv_action.setOnClickListener {
            val phone = et_phone.text.toString()
            if (!phone.isValidPhone()){
                myToast("请输入正确的手机号码")
                return@setOnClickListener
            }
            val name = et_name.text.toString()
            if (name.isEmpty()){
                myToast("请输入姓名")
                return@setOnClickListener
            }
            setResult(Activity.RESULT_OK,intent.putExtra("name", name).putExtra("phone", phone))
            finish()
        }
    }
 
    override fun initView() {
        title = "乘车人信息"
        HttpManager.getH5(2).request(this){ _, data->
            data?.let {
                tv_rule.setOnClickListener {_->
                    startActivity<H5Activity>("title" to "用户服务协议","url" to it.optString("content"))
                }
            }
        }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data!=null&&resultCode == Activity.RESULT_OK){
            val uri = data.data
            if (uri!=null){
                val contacts = getPhoneContacts (uri)
                et_name.setText(contacts[0])
                et_phone.setText(contacts[1].replace(" ",""))
                et_phone.setSelection(et_phone.text.length)
 
            }
        }
    }
 
    private fun getPhoneContacts(uri: Uri): ArrayList<String> {
        val contact: ArrayList<String> = arrayListOf()
        val cr = contentResolver
        val cursor: Cursor? = cr.query(uri, null, null, null, null)
        cursor?.let {
            cursor.moveToFirst()
            val nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
            contact.add(cursor.getString(nameFieldColumnIndex))
            val contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
            val phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null)
            if (phone != null) {
                phone.moveToFirst()
                contact.add(phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))
            }
            phone?.close()
            cursor.close()
        }
 
        return contact
    }
}