Back to project page CriminalIntent.
The source code is released under:
MIT License
If you think the Android project CriminalIntent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.ambergleam.criminalintent.controller; // w ww. ja v a 2s . c o m import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.UUID; import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.NavUtils; import android.text.Editable; import android.text.TextWatcher; import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import com.ambergleam.criminalintent.R; import com.ambergleam.criminalintent.model.Crime; import com.ambergleam.criminalintent.model.CrimeLab; public class CrimeFragment extends Fragment { public static final String EXTRA_CRIME_ID = "com.ambergleam.criminalintent.crime_id"; private static final int REQUEST_CONTACT = 2; private static final String DIALOG_DATE = "date"; private static final int REQUEST_DATE = 0; private Crime mCrime; private EditText mTitleField; private Button mDateButton, mSuspectButton, mReportButton; private CheckBox mSolvedCheckBox; private Callbacks mCallbacks; /* * Required interface for hosting activities */ public interface Callbacks { void onCrimeUpdated(Crime crime); } @Override public void onAttach(Activity activity) { super.onAttach(activity); mCallbacks = (Callbacks) activity; } @Override public void onDetach() { super.onDetach(); mCallbacks = null; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); getActivity().setTitle(R.string.crime_title); UUID crimeId = (UUID) getArguments().getSerializable(EXTRA_CRIME_ID); mCrime = CrimeLab.get(getActivity()).getCrime(crimeId); setRetainInstance(true); } public static CrimeFragment newInstance(UUID crimeId) { Bundle args = new Bundle(); args.putSerializable(EXTRA_CRIME_ID, crimeId); CrimeFragment fragment = new CrimeFragment(); fragment.setArguments(args); return fragment; } public void returnResult() { getActivity().setResult(Activity.RESULT_OK, null); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, parent, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (NavUtils.getParentActivityName(getActivity()) != null) { getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); } } mTitleField = (EditText) v.findViewById(R.id.crime_title); mTitleField.setText(mCrime.getTitle()); mTitleField.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mCrime.setTitle(s.toString()); mCallbacks.onCrimeUpdated(mCrime); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // Intentionally left blank } @Override public void afterTextChanged(Editable s) { // Intentionally left blank } }); mDateButton = (Button) v.findViewById(R.id.crime_date); updateDate(); mDateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getActivity().getSupportFragmentManager(); DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate()); dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE); dialog.show(fm, DIALOG_DATE); } }); mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved); mSolvedCheckBox.setChecked(mCrime.isSolved()); mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // Set the crime's solved property mCrime.setSolved(isChecked); mCallbacks.onCrimeUpdated(mCrime); } }); mReportButton = (Button) v.findViewById(R.id.crime_reportButton); mReportButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(Intent.EXTRA_TEXT, getCrimeReport()); i.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.crime_report_subject)); i = Intent.createChooser(i, getString(R.string.send_report)); startActivity(i); } }); mSuspectButton = (Button) v.findViewById(R.id.crime_suspectButton); mSuspectButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(i, REQUEST_CONTACT); } }); if (mCrime.getSuspect() != null) { mSuspectButton.setText(mCrime.getSuspect()); } return v; } private void updateDate() { SimpleDateFormat sdf = new SimpleDateFormat(getActivity().getString(R.string.date_formatting), Locale.US); String date = sdf.format(mCrime.getDate()); mDateButton.setText(date); } private String getCrimeReport() { String solvedString = null; if (mCrime.isSolved()) { solvedString = getString(R.string.crime_report_solved); } else { solvedString = getString(R.string.crime_report_unsolved); } String dateFormat = "EEE, MMM dd"; String dateString = DateFormat.format(dateFormat, mCrime.getDate()).toString(); String suspect = mCrime.getSuspect(); if (suspect == null) { suspect = getString(R.string.crime_report_no_suspect); } else { suspect = getString(R.string.crime_report_suspect, suspect); } String report = getString(R.string.crime_report, mCrime.getTitle(), dateString, solvedString, suspect); return report; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) return; if (requestCode == REQUEST_DATE) { Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE); mCrime.setDate(date); mCallbacks.onCrimeUpdated(mCrime); updateDate(); } else if (requestCode == REQUEST_CONTACT) { Uri contactUri = data.getData(); // Specify which fields you want your query to return values for String[] queryFields = new String[] { ContactsContract.Contacts.DISPLAY_NAME }; // Perform your query - the contactUri is like a "where" clause here Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null); // Double-check that you actually got your results if (c.getCount() == 0) { c.close(); return; } // Pull out the first column of the first row of data = that is the name c.moveToFirst(); String suspect = c.getString(0); mCrime.setSuspect(suspect); mSuspectButton.setText(suspect); mCallbacks.onCrimeUpdated(mCrime); c.close(); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: if (NavUtils.getParentActivityName(getActivity()) != null) { NavUtils.navigateUpFromSameTask(getActivity()); } return true; case R.id.menu_item_remove_crime: CrimeLab.get(getActivity()).deleteCrime(mCrime); NavUtils.navigateUpFromSameTask(getActivity()); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onPause() { super.onPause(); CrimeLab.get(getActivity()).saveCrimes(); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.fragment_crime, menu); } }