package com.example.lenovo.addressbook;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;

public class DetailActivity extends AppCompatActivity {
    private TextView mName, mPhone, mEmail, mWeixin, mQQ, mOrganization, mAddress;
    private ImageButton imageButton;
    private String telNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        //获取Intent传入的数据
        ArrayList<String> person = getIntent().getStringArrayListExtra("Person");
        //获取子试图
        mName = (TextView) findViewById(R.id.DETAILS_name);
        mPhone = (TextView) findViewById(R.id.DETAILS_phone);
        mEmail = (TextView) findViewById(R.id.DETAILS_email);
        mWeixin = (TextView) findViewById(R.id.DETAILS_weixin);
        mQQ = (TextView) findViewById(R.id.DETAILS_qq);
        mOrganization = (TextView) findViewById(R.id.DETAILS_organization);
        mAddress = (TextView) findViewById(R.id.DETAILS_address);
        imageButton = (ImageButton) findViewById(R.id.callButton);
        //设置子视图属性值
        mName.setText(person.get(0));
        mPhone.setText(person.get(1));
        telNumber = person.get(1);
        mEmail.setText(person.get(2));
        mWeixin.setText(person.get(3));
        mQQ.setText(person.get(4));
        mOrganization.setText(person.get(5));
        mAddress.setText(person.get(6));
        //创建按钮的监听器
        imageButton.setOnClickListener(new ImageButton.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeCall(telNumber);
            }
        });
    }
    //拨打电话
    private void makeCall(String telNumber) {
        Uri uri = Uri.parse("tel:" + telNumber.trim());
        Intent intent = new Intent(Intent.ACTION_CALL, uri);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            startActivity(intent);
        }
    }
}
