MP1_CalebFontenot
1
Book Files/14_LocalDatabases/CriminalIntent/app/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
32
Book Files/14_LocalDatabases/CriminalIntent/app/build.gradle
Normal file
@@ -0,0 +1,32 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 25
|
||||
buildToolsVersion '25.0.2'
|
||||
defaultConfig {
|
||||
applicationId "com.bignerdranch.android.criminalintent"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 25
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(include: ['*.jar'], dir: 'libs')
|
||||
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
|
||||
exclude group: 'com.android.support', module: 'support-annotations'
|
||||
})
|
||||
compile 'com.android.support:appcompat-v7:25.3.0'
|
||||
testCompile 'junit:junit:4.12'
|
||||
compile 'com.android.support:support-v4:25.3.0'
|
||||
compile 'com.android.support:recyclerview-v7:25.3.0'
|
||||
compile 'com.android.support.constraint:constraint-layout:1.0.2'
|
||||
}
|
17
Book Files/14_LocalDatabases/CriminalIntent/app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# By default, the flags in this file are appended to flags specified
|
||||
# in /Users/chris/AndroidDeveloper/sdk/tools/proguard/proguard-android.txt
|
||||
# You can edit the include path and order by changing the proguardFiles
|
||||
# directive in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
@@ -0,0 +1,26 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumentation test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() throws Exception {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("com.bignerdranch.android.criminalintent", appContext.getPackageName());
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest package="com.bignerdranch.android.criminalintent"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".CrimeListActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".CrimePagerActivity"
|
||||
android:parentActivityName=".CrimeListActivity">
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
@@ -0,0 +1,49 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Crime {
|
||||
|
||||
private UUID mId;
|
||||
private String mTitle;
|
||||
private Date mDate;
|
||||
private boolean mSolved;
|
||||
|
||||
public Crime() {
|
||||
this(UUID.randomUUID());
|
||||
}
|
||||
|
||||
public Crime(UUID id) {
|
||||
mId = id;
|
||||
mDate = new Date();
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return mId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return mTitle;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
mTitle = title;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return mDate;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
mDate = date;
|
||||
}
|
||||
|
||||
public boolean isSolved() {
|
||||
return mSolved;
|
||||
}
|
||||
|
||||
public void setSolved(boolean solved) {
|
||||
mSolved = solved;
|
||||
}
|
||||
}
|
@@ -0,0 +1,126 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import static android.widget.CompoundButton.*;
|
||||
|
||||
public class CrimeFragment extends Fragment {
|
||||
|
||||
private static final String ARG_CRIME_ID = "crime_id";
|
||||
private static final String DIALOG_DATE = "DialogDate";
|
||||
|
||||
private static final int REQUEST_DATE = 0;
|
||||
|
||||
private Crime mCrime;
|
||||
private EditText mTitleField;
|
||||
private Button mDateButton;
|
||||
private CheckBox mSolvedCheckbox;
|
||||
|
||||
public static CrimeFragment newInstance(UUID crimeId) {
|
||||
Bundle args = new Bundle();
|
||||
args.putSerializable(ARG_CRIME_ID, crimeId);
|
||||
|
||||
CrimeFragment fragment = new CrimeFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
|
||||
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View v = inflater.inflate(R.layout.fragment_crime, container, false);
|
||||
|
||||
mTitleField = (EditText) v.findViewById(R.id.crime_title);
|
||||
mTitleField.setText(mCrime.getTitle());
|
||||
mTitleField.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
mCrime.setTitle(s.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
mDateButton = (Button) v.findViewById(R.id.crime_date);
|
||||
updateDate();
|
||||
mDateButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
FragmentManager manager = getFragmentManager();
|
||||
DatePickerFragment dialog = DatePickerFragment
|
||||
.newInstance(mCrime.getDate());
|
||||
dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
|
||||
dialog.show(manager, 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) {
|
||||
mCrime.setSolved(isChecked);
|
||||
}
|
||||
});
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
|
||||
CrimeLab.get(getActivity())
|
||||
.updateCrime(mCrime);
|
||||
}
|
||||
|
||||
@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);
|
||||
updateDate();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDate() {
|
||||
mDateButton.setText(mCrime.getDate().toString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,107 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import com.bignerdranch.android.criminalintent.database.CrimeBaseHelper;
|
||||
import com.bignerdranch.android.criminalintent.database.CrimeCursorWrapper;
|
||||
import com.bignerdranch.android.criminalintent.database.CrimeDbSchema;
|
||||
import com.bignerdranch.android.criminalintent.database.CrimeDbSchema.CrimeTable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.bignerdranch.android.criminalintent.database.CrimeDbSchema.CrimeTable.*;
|
||||
import static com.bignerdranch.android.criminalintent.database.CrimeDbSchema.CrimeTable.Cols.*;
|
||||
|
||||
public class CrimeLab {
|
||||
private static CrimeLab sCrimeLab;
|
||||
private Context mContext;
|
||||
private SQLiteDatabase mDatabase;
|
||||
|
||||
public static CrimeLab get(Context context) {
|
||||
if (sCrimeLab == null) {
|
||||
sCrimeLab = new CrimeLab(context);
|
||||
}
|
||||
|
||||
return sCrimeLab;
|
||||
}
|
||||
|
||||
private CrimeLab(Context context) {
|
||||
mContext = context.getApplicationContext();
|
||||
mDatabase = new CrimeBaseHelper(mContext)
|
||||
.getWritableDatabase();
|
||||
|
||||
}
|
||||
|
||||
public void addCrime(Crime c) {
|
||||
ContentValues values = getContentValues(c);
|
||||
mDatabase.insert(CrimeTable.NAME, null, values);
|
||||
}
|
||||
|
||||
public List<Crime> getCrimes() {
|
||||
List<Crime> crimes = new ArrayList<>();
|
||||
CrimeCursorWrapper cursor = queryCrimes(null, null);
|
||||
try {
|
||||
cursor.moveToFirst();
|
||||
while (!cursor.isAfterLast()) {
|
||||
crimes.add(cursor.getCrime());
|
||||
cursor.moveToNext();
|
||||
}
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
return crimes;
|
||||
}
|
||||
|
||||
public Crime getCrime(UUID id) {
|
||||
CrimeCursorWrapper cursor = queryCrimes(
|
||||
CrimeTable.Cols.UUID + " = ?",
|
||||
new String[]{id.toString()}
|
||||
);
|
||||
try {
|
||||
if (cursor.getCount() == 0) {
|
||||
return null;
|
||||
}
|
||||
cursor.moveToFirst();
|
||||
return cursor.getCrime();
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateCrime(Crime crime) {
|
||||
String uuidString = crime.getId().toString();
|
||||
ContentValues values = getContentValues(crime);
|
||||
mDatabase.update(CrimeTable.NAME, values,
|
||||
CrimeTable.Cols.UUID + " = ?",
|
||||
new String[]{uuidString});
|
||||
}
|
||||
|
||||
private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs) {
|
||||
Cursor cursor = mDatabase.query(
|
||||
CrimeTable.NAME,
|
||||
null, // Columns - null selects all columns
|
||||
whereClause,
|
||||
whereArgs,
|
||||
null, // groupBy
|
||||
null, // having
|
||||
null // orderBy
|
||||
);
|
||||
return new CrimeCursorWrapper(cursor);
|
||||
}
|
||||
|
||||
private static ContentValues getContentValues(Crime crime) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(UUID, crime.getId().toString());
|
||||
values.put(TITLE, crime.getTitle());
|
||||
values.put(DATE, crime.getDate().getTime());
|
||||
values.put(SOLVED, crime.isSolved() ? 1 : 0);
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
public class CrimeListActivity extends SingleFragmentActivity {
|
||||
|
||||
@Override
|
||||
protected Fragment createFragment() {
|
||||
return new CrimeListFragment();
|
||||
}
|
||||
}
|
@@ -0,0 +1,186 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CrimeListFragment extends Fragment {
|
||||
|
||||
private static final String SAVED_SUBTITLE_VISIBLE = "subtitle";
|
||||
|
||||
private RecyclerView mCrimeRecyclerView;
|
||||
private CrimeAdapter mAdapter;
|
||||
private boolean mSubtitleVisible;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
|
||||
|
||||
mCrimeRecyclerView = (RecyclerView) view
|
||||
.findViewById(R.id.crime_recycler_view);
|
||||
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
|
||||
}
|
||||
|
||||
updateUI();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
updateUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putBoolean(SAVED_SUBTITLE_VISIBLE, mSubtitleVisible);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
inflater.inflate(R.menu.fragment_crime_list, menu);
|
||||
|
||||
MenuItem subtitleItem = menu.findItem(R.id.show_subtitle);
|
||||
if (mSubtitleVisible) {
|
||||
subtitleItem.setTitle(R.string.hide_subtitle);
|
||||
} else {
|
||||
subtitleItem.setTitle(R.string.show_subtitle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.new_crime:
|
||||
Crime crime = new Crime();
|
||||
CrimeLab.get(getActivity()).addCrime(crime);
|
||||
Intent intent = CrimePagerActivity
|
||||
.newIntent(getActivity(), crime.getId());
|
||||
startActivity(intent);
|
||||
return true;
|
||||
case R.id.show_subtitle:
|
||||
mSubtitleVisible = !mSubtitleVisible;
|
||||
getActivity().invalidateOptionsMenu();
|
||||
updateSubtitle();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSubtitle() {
|
||||
CrimeLab crimeLab = CrimeLab.get(getActivity());
|
||||
int crimeCount = crimeLab.getCrimes().size();
|
||||
String subtitle = getString(R.string.subtitle_format, crimeCount);
|
||||
|
||||
if (!mSubtitleVisible) {
|
||||
subtitle = null;
|
||||
}
|
||||
|
||||
AppCompatActivity activity = (AppCompatActivity) getActivity();
|
||||
activity.getSupportActionBar().setSubtitle(subtitle);
|
||||
}
|
||||
|
||||
private void updateUI() {
|
||||
CrimeLab crimeLab = CrimeLab.get(getActivity());
|
||||
List<Crime> crimes = crimeLab.getCrimes();
|
||||
|
||||
if (mAdapter == null) {
|
||||
mAdapter = new CrimeAdapter(crimes);
|
||||
mCrimeRecyclerView.setAdapter(mAdapter);
|
||||
} else {
|
||||
mAdapter.setCrimes(crimes);
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
updateSubtitle();
|
||||
}
|
||||
|
||||
private class CrimeHolder extends RecyclerView.ViewHolder
|
||||
implements View.OnClickListener {
|
||||
|
||||
private Crime mCrime;
|
||||
|
||||
private TextView mTitleTextView;
|
||||
private TextView mDateTextView;
|
||||
private ImageView mSolvedImageView;
|
||||
|
||||
public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
|
||||
super(inflater.inflate(R.layout.list_item_crime, parent, false));
|
||||
itemView.setOnClickListener(this);
|
||||
|
||||
mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
|
||||
mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
|
||||
mSolvedImageView = (ImageView) itemView.findViewById(R.id.crime_solved);
|
||||
}
|
||||
|
||||
public void bind(Crime crime) {
|
||||
mCrime = crime;
|
||||
mTitleTextView.setText(mCrime.getTitle());
|
||||
mDateTextView.setText(mCrime.getDate().toString());
|
||||
mSolvedImageView.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Intent intent = CrimePagerActivity.newIntent(getActivity(), mCrime.getId());
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
|
||||
|
||||
private List<Crime> mCrimes;
|
||||
|
||||
public CrimeAdapter(List<Crime> crimes) {
|
||||
mCrimes = crimes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
|
||||
return new CrimeHolder(layoutInflater, parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(CrimeHolder holder, int position) {
|
||||
Crime crime = mCrimes.get(position);
|
||||
holder.bind(crime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mCrimes.size();
|
||||
}
|
||||
|
||||
public void setCrimes(List<Crime> crimes) {
|
||||
mCrimes = crimes;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CrimePagerActivity extends AppCompatActivity {
|
||||
|
||||
private static final String EXTRA_CRIME_ID =
|
||||
"com.bignerdranch.android.criminalintent.crime_id";
|
||||
|
||||
private ViewPager mViewPager;
|
||||
private List<Crime> mCrimes;
|
||||
|
||||
public static Intent newIntent(Context packageContext, UUID crimeId) {
|
||||
Intent intent = new Intent(packageContext, CrimePagerActivity.class);
|
||||
intent.putExtra(EXTRA_CRIME_ID, crimeId);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_crime_pager);
|
||||
|
||||
UUID crimeId = (UUID) getIntent()
|
||||
.getSerializableExtra(EXTRA_CRIME_ID);
|
||||
|
||||
mViewPager = (ViewPager) findViewById(R.id.crime_view_pager);
|
||||
|
||||
mCrimes = CrimeLab.get(this).getCrimes();
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
Crime crime = mCrimes.get(position);
|
||||
return CrimeFragment.newInstance(crime.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mCrimes.size();
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < mCrimes.size(); i++) {
|
||||
if (mCrimes.get(i).getId().equals(crimeId)) {
|
||||
mViewPager.setCurrentItem(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.DatePicker;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class DatePickerFragment extends DialogFragment {
|
||||
|
||||
public static final String EXTRA_DATE =
|
||||
"com.bignerdranch.android.criminalintent.date";
|
||||
|
||||
private static final String ARG_DATE = "date";
|
||||
|
||||
private DatePicker mDatePicker;
|
||||
|
||||
public static DatePickerFragment newInstance(Date date) {
|
||||
Bundle args = new Bundle();
|
||||
args.putSerializable(ARG_DATE, date);
|
||||
|
||||
DatePickerFragment fragment = new DatePickerFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
Date date = (Date) getArguments().getSerializable(ARG_DATE);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
int month = calendar.get(Calendar.MONTH);
|
||||
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
View v = LayoutInflater.from(getActivity())
|
||||
.inflate(R.layout.dialog_date, null);
|
||||
|
||||
mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
|
||||
mDatePicker.init(year, month, day, null);
|
||||
|
||||
return new AlertDialog.Builder(getActivity())
|
||||
.setView(v)
|
||||
.setTitle(R.string.date_picker_title)
|
||||
.setPositiveButton(android.R.string.ok,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
int year = mDatePicker.getYear();
|
||||
int month = mDatePicker.getMonth();
|
||||
int day = mDatePicker.getDayOfMonth();
|
||||
Date date = new GregorianCalendar(year, month, day).getTime();
|
||||
sendResult(Activity.RESULT_OK, date);
|
||||
}
|
||||
})
|
||||
.create();
|
||||
}
|
||||
|
||||
private void sendResult(int resultCode, Date date) {
|
||||
if (getTargetFragment() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(EXTRA_DATE, date);
|
||||
|
||||
getTargetFragment()
|
||||
.onActivityResult(getTargetRequestCode(), resultCode, intent);
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
public abstract class SingleFragmentActivity extends AppCompatActivity {
|
||||
|
||||
protected abstract Fragment createFragment();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_fragment);
|
||||
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
|
||||
|
||||
if (fragment == null) {
|
||||
fragment = createFragment();
|
||||
fm.beginTransaction()
|
||||
.add(R.id.fragment_container, fragment)
|
||||
.commit();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package com.bignerdranch.android.criminalintent.database;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
import com.bignerdranch.android.criminalintent.database.CrimeDbSchema.CrimeTable;
|
||||
|
||||
public class CrimeBaseHelper extends SQLiteOpenHelper {
|
||||
private static final int VERSION = 1;
|
||||
private static final String DATABASE_NAME = "crimeBase.db";
|
||||
|
||||
public CrimeBaseHelper(Context context) {
|
||||
super(context, DATABASE_NAME, null, VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL("create table " + CrimeTable.NAME + "(" +
|
||||
" _id integer primary key autoincrement, " +
|
||||
CrimeTable.Cols.UUID + ", " +
|
||||
CrimeTable.Cols.TITLE + ", " +
|
||||
CrimeTable.Cols.DATE + ", " +
|
||||
CrimeTable.Cols.SOLVED +
|
||||
")"
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.bignerdranch.android.criminalintent.database;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.CursorWrapper;
|
||||
|
||||
import com.bignerdranch.android.criminalintent.Crime;
|
||||
import com.bignerdranch.android.criminalintent.database.CrimeDbSchema.CrimeTable;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CrimeCursorWrapper extends CursorWrapper {
|
||||
|
||||
public CrimeCursorWrapper(Cursor cursor) {
|
||||
super(cursor);
|
||||
}
|
||||
|
||||
public Crime getCrime() {
|
||||
String uuidString = getString(getColumnIndex(CrimeTable.Cols.UUID));
|
||||
String title = getString(getColumnIndex(CrimeTable.Cols.TITLE));
|
||||
long date = getLong(getColumnIndex(CrimeTable.Cols.DATE));
|
||||
int isSolved = getInt(getColumnIndex(CrimeTable.Cols.SOLVED));
|
||||
|
||||
Crime crime = new Crime(UUID.fromString(uuidString));
|
||||
crime.setTitle(title);
|
||||
crime.setDate(new Date(date));
|
||||
crime.setSolved(isSolved != 0);
|
||||
|
||||
return crime;
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.bignerdranch.android.criminalintent.database;
|
||||
|
||||
public class CrimeDbSchema {
|
||||
public static final class CrimeTable {
|
||||
public static final String NAME = "crimes";
|
||||
|
||||
public static final class Cols {
|
||||
public static final String UUID = "uuid";
|
||||
public static final String TITLE = "title";
|
||||
public static final String DATE = "date";
|
||||
public static final String SOLVED = "solved";
|
||||
}
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 223 B |
After Width: | Height: | Size: 2.7 KiB |
After Width: | Height: | Size: 152 B |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 197 B |
After Width: | Height: | Size: 3.8 KiB |
After Width: | Height: | Size: 351 B |
After Width: | Height: | Size: 5.9 KiB |
After Width: | Height: | Size: 8.2 KiB |
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/crime_view_pager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
@@ -0,0 +1,4 @@
|
||||
<FrameLayout android:id="@+id/fragment_container"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/dialog_date_picker"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:calendarViewShown="false"/>
|
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="16dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
style="?android:listSeparatorTextViewStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/crime_title_label"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/crime_title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/crime_title_hint"/>
|
||||
|
||||
<TextView
|
||||
style="?android:listSeparatorTextViewStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/crime_details_label"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/crime_date"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/crime_solved"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/crime_solved_label"/>
|
||||
|
||||
</LinearLayout>
|
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.v7.widget.RecyclerView android:id="@+id/crime_recycler_view"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/crime_title"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Crime Title"
|
||||
android:textColor="@android:color/black"
|
||||
android:textSize="18sp"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toLeftOf="@+id/crime_solved"
|
||||
app:layout_constraintTop_toTopOf="parent"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/crime_date"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Crime Date"
|
||||
android:layout_marginStart="16dp"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
android:layout_marginTop="8dp"
|
||||
app:layout_constraintTop_toBottomOf="@+id/crime_title"
|
||||
app:layout_constraintRight_toLeftOf="@+id/crime_solved"
|
||||
android:layout_marginEnd="8dp"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/crime_solved"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:srcCompat="@drawable/ic_solved"
|
||||
android:layout_marginTop="16dp"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
app:layout_constraintRight_toRightOf="parent"/>
|
||||
</android.support.constraint.ConstraintLayout>
|
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/new_crime"
|
||||
android:icon="@drawable/ic_menu_add"
|
||||
android:title="@string/new_crime"
|
||||
app:showAsAction="ifRoom|withText"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/show_subtitle"
|
||||
android:title="@string/show_subtitle"
|
||||
app:showAsAction="ifRoom"/>
|
||||
</menu>
|
After Width: | Height: | Size: 3.3 KiB |
After Width: | Height: | Size: 2.2 KiB |
After Width: | Height: | Size: 4.7 KiB |
After Width: | Height: | Size: 7.5 KiB |
After Width: | Height: | Size: 10 KiB |
@@ -0,0 +1,6 @@
|
||||
<resources>
|
||||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
|
||||
(such as screen margins) for screens with more than 820dp of available width. This
|
||||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
|
||||
<dimen name="activity_horizontal_margin">64dp</dimen>
|
||||
</resources>
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
</resources>
|
@@ -0,0 +1,5 @@
|
||||
<resources>
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
</resources>
|
@@ -0,0 +1,12 @@
|
||||
<resources>
|
||||
<string name="app_name">CriminalIntent</string>
|
||||
<string name="crime_title_hint">Enter a title for the crime.</string>
|
||||
<string name="crime_title_label">Title</string>
|
||||
<string name="crime_details_label">Details</string>
|
||||
<string name="crime_solved_label">Solved</string>
|
||||
<string name="date_picker_title">Date of crime:</string>
|
||||
<string name="new_crime">New Crime</string>
|
||||
<string name="show_subtitle">Show Subtitle</string>
|
||||
<string name="hide_subtitle">Hide Subtitle</string>
|
||||
<string name="subtitle_format">%1$d crimes</string>
|
||||
</resources>
|
@@ -0,0 +1,11 @@
|
||||
<resources>
|
||||
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@@ -0,0 +1,17 @@
|
||||
package com.bignerdranch.android.criminalintent;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|