|
@@ -0,0 +1,126 @@
|
|
|
|
|
+package com.example.david.libretasker.InputSelector;
|
|
|
|
|
+
|
|
|
|
|
+import android.content.Context;
|
|
|
|
|
+import android.content.Intent;
|
|
|
|
|
+import android.database.Cursor;
|
|
|
|
|
+import android.net.Uri;
|
|
|
|
|
+import android.os.Bundle;
|
|
|
|
|
+import android.provider.ContactsContract;
|
|
|
|
|
+import android.support.v4.app.Fragment;
|
|
|
|
|
+import android.view.LayoutInflater;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+import android.view.ViewGroup;
|
|
|
|
|
+import android.widget.Button;
|
|
|
|
|
+import android.widget.EditText;
|
|
|
|
|
+import android.widget.TextView;
|
|
|
|
|
+
|
|
|
|
|
+import com.example.david.libretasker.R;
|
|
|
|
|
+
|
|
|
|
|
+import static android.app.Activity.RESULT_OK;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * A simple {@link Fragment} subclass.
|
|
|
|
|
+ * Activities that contain this fragment must implement the
|
|
|
|
|
+ * {@link Contact.OnFragmentInteractionListener} interface
|
|
|
|
|
+ * to handle interaction events.
|
|
|
|
|
+ * Use the {@link Contact#newInstance} factory method to
|
|
|
|
|
+ * create an instance of this fragment.
|
|
|
|
|
+ */
|
|
|
|
|
+public class Contact extends Fragment {
|
|
|
|
|
+ private OnFragmentInteractionListener mListener;
|
|
|
|
|
+ private static final int REQUEST_CODE = 1;
|
|
|
|
|
+
|
|
|
|
|
+ String name = "";
|
|
|
|
|
+ String number = "";
|
|
|
|
|
+ View thisView;
|
|
|
|
|
+
|
|
|
|
|
+ public Contact() {
|
|
|
|
|
+ // Required empty public constructor
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Contact newInstance() {
|
|
|
|
|
+ return new Contact();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
|
+ Bundle savedInstanceState) {
|
|
|
|
|
+ View v = inflater.inflate(R.layout.fragment_contact, container, false);
|
|
|
|
|
+ thisView = v;
|
|
|
|
|
+ Button b = (Button)v.findViewById(R.id.pickContact);
|
|
|
|
|
+ b.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onClick(View view) {
|
|
|
|
|
+ Uri uri = Uri.parse("content://contacts");
|
|
|
|
|
+ Intent intent = new Intent(Intent.ACTION_PICK, uri);
|
|
|
|
|
+ intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
|
|
|
|
|
+ startActivityForResult(intent, REQUEST_CODE);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ return v;
|
|
|
|
|
+ }
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
|
+ if (requestCode == REQUEST_CODE) {
|
|
|
|
|
+ if (resultCode == RESULT_OK) {
|
|
|
|
|
+ Uri uri = intent.getData();
|
|
|
|
|
+ String[] projection = { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
|
|
|
|
|
+
|
|
|
|
|
+ Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(uri, projection,
|
|
|
|
|
+ null, null, null);
|
|
|
|
|
+ cursor.moveToFirst();
|
|
|
|
|
+
|
|
|
|
|
+ int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
|
|
|
|
|
+ number = cursor.getString(numberColumnIndex);
|
|
|
|
|
+
|
|
|
|
|
+ int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
|
|
|
|
|
+ name = cursor.getString(nameColumnIndex);
|
|
|
|
|
+
|
|
|
|
|
+ EditText tvName = (EditText) thisView.findViewById(R.id.contactName);
|
|
|
|
|
+ EditText tvNumber = (EditText) thisView.findViewById(R.id.contactNumber);
|
|
|
|
|
+
|
|
|
|
|
+ tvName.setText(name);
|
|
|
|
|
+ tvNumber.setText(number);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: Rename method, update argument and hook method into UI event
|
|
|
|
|
+ public void onButtonPressed(Uri uri) {
|
|
|
|
|
+ if (mListener != null) {
|
|
|
|
|
+ mListener.onFragmentInteraction(uri);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onAttach(Context context) {
|
|
|
|
|
+ super.onAttach(context);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDetach() {
|
|
|
|
|
+ super.onDetach();
|
|
|
|
|
+ mListener = null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * This interface must be implemented by activities that contain this
|
|
|
|
|
+ * fragment to allow an interaction in this fragment to be communicated
|
|
|
|
|
+ * to the activity and potentially other fragments contained in that
|
|
|
|
|
+ * activity.
|
|
|
|
|
+ * <p>
|
|
|
|
|
+ * See the Android Training lesson <a href=
|
|
|
|
|
+ * "http://developer.android.com/training/basics/fragments/communicating.html"
|
|
|
|
|
+ * >Communicating with Other Fragments</a> for more information.
|
|
|
|
|
+ */
|
|
|
|
|
+ public interface OnFragmentInteractionListener {
|
|
|
|
|
+ // TODO: Update argument type and name
|
|
|
|
|
+ void onFragmentInteraction(Uri uri);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|