Skip to content

Commit

Permalink
Update CH9 from previous.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonostrander committed Feb 22, 2012
1 parent a07d5ef commit e070b58
Show file tree
Hide file tree
Showing 19 changed files with 385 additions and 173 deletions.
31 changes: 22 additions & 9 deletions Chapter9/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,50 @@
android:targetSdkVersion="14" />

<application
android:name=".TimeTrackerApplication"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:name=".TimeTrackerApplication" >
android:label="@string/app_name" >
<activity
android:launchMode="singleTop"
android:name=".TaskListActivity" >
android:name=".TaskListActivity"
android:launchMode="singleTop" >
</activity>
<activity
android:name=".TimeTrackerActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:name=".TimeTrackerActivity"
android:windowSoftInputMode="stateUnchanged" >
<intent-filter >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EditTaskActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:name=".EditTaskActivity"
android:windowSoftInputMode="stateUnchanged" >
</activity>

<provider
android:authorities="com.example.TimeTracker"
android:name="com.example.provider.TaskProvider" >
android:name="com.example.provider.TaskProvider"
android:authorities="com.example.TimeTracker" >
</provider>

<service android:name="TimerService" />

<receiver android:name=".TimerWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.ActionTimeUpdate" />
<action android:name="com.example.ActionTimerFinished" />
<action android:name="com.example.ActionTimerStopped" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/time_tracker_appwidget_info" />
</receiver>
</application>

</manifest>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions Chapter9/res/drawable/widget_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#80000000" />

<corners android:radius="4dp" />

</shape>
2 changes: 2 additions & 0 deletions Chapter9/res/layout/digit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:text="0"
android:textSize="50sp" />

Expand All @@ -16,6 +17,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:text="0"
android:textSize="50sp"
/>
Expand Down
26 changes: 26 additions & 0 deletions Chapter9/res/layout/timer_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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:orientation="horizontal"
android:background="@drawable/widget_background" >

<TextView
android:id="@+id/counter"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="00:00"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
android:id="@+id/start_stop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:src="@android:drawable/ic_media_play" />

</LinearLayout>
4 changes: 0 additions & 4 deletions Chapter9/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/new_task"
android:title="@string/new_task"/>

<item
android:id="@+id/clear_all"
android:title="@string/clear_all"/>
Expand Down
8 changes: 0 additions & 8 deletions Chapter9/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,4 @@
<string name="timer">Timer</string>
<string name="tasks">Tasks</string>

<string name="lorem_ipsum">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec purus felis. In hac habitasse platea dictumst. Morbi arcu mauris, hendrerit ut ultrices in, auctor id odio. Aliquam vitae facilisis sapien. Donec commodo egestas leo nec luctus. Sed pulvinar justo nec purus pulvinar ut interdum lectus condimentum. Suspendisse commodo tincidunt elit, sit amet porttitor dui pellentesque at. Nunc ut purus ut elit laoreet vehicula a eu velit. Sed vestibulum molestie lacus, malesuada lacinia orci hendrerit a. Nulla et sem dictum diam rhoncus laoreet.

Nullam eget nunc nisl, et feugiat massa. Duis dictum nunc et ante dapibus commodo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac urna leo. Nulla facilisi. Quisque bibendum pellentesque nunc, sollicitudin congue turpis ullamcorper a. Sed vestibulum sollicitudin ornare. Donec vitae magna arcu. Suspendisse euismod augue sit amet massa bibendum ultricies aliquam mi pretium. Fusce et arcu neque. Donec lacinia vulputate orci in consequat. Curabitur mattis interdum sem et convallis. Morbi elementum porttitor nunc dictum luctus. Quisque quis lectus massa, at ullamcorper purus. Ut lorem massa, porta vulputate luctus sit amet, sagittis fringilla lacus. Vivamus nec nulla urna.

Cras sagittis, magna a volutpat feugiat, nisi ipsum accumsan ipsum, ut lacinia quam diam et tellus. Fusce sit amet nisi sit amet lacus adipiscing eleifend in at nulla. Aenean ac enim at justo elementum tincidunt non ac ipsum. Quisque sed malesuada enim. Vivamus velit nunc, euismod sed consequat non, posuere dapibus purus. Proin ac erat sed neque vehicula lobortis. Integer a metus a tellus rutrum rutrum. Quisque eget orci eu magna volutpat ornare ac nec felis.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tincidunt consequat sapien, ac auctor orci hendrerit at. Morbi laoreet orci eget eros elementum vitae bibendum nibh volutpat. Morbi sit amet lorem tellus, sit amet posuere augue. Nullam nibh orci, tristique a iaculis non, sagittis quis felis. Mauris volutpat dui nec enim suscipit lobortis. Pellentesque purus odio, mattis interdum fermentum nec, tincidunt convallis purus. Integer cursus elit sed ipsum condimentum vitae tincidunt risus fermentum. Suspendisse eu magna vitae odio vulputate mollis vel ullamcorper neque. Maecenas suscipit ligula lacinia lectus ullamcorper non elementum quam interdum. Proin ipsum tortor, posuere in ullamcorper eget, placerat quis nunc. Praesent pharetra pulvinar neque sed pellentesque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc ligula ligula, faucibus eget ornare id, elementum vel erat. Nulla ut vestibulum tortor.</string>

</resources>
7 changes: 7 additions & 0 deletions Chapter9/res/xml/time_tracker_appwidget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="110dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/time_tracker_widget_preview"
android:initialLayout="@layout/timer_widget" >
</appwidget-provider>
19 changes: 9 additions & 10 deletions Chapter9/src/com/example/ConfirmClearDialogFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.AsyncQueryHandler;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import com.example.provider.TaskProvider;

public class ConfirmClearDialogFragment extends DialogFragment {
private TimeListAdapter mAdapter;

public static ConfirmClearDialogFragment newInstance(TimeListAdapter adapter) {

public static ConfirmClearDialogFragment newInstance() {
ConfirmClearDialogFragment frag = new ConfirmClearDialogFragment();
frag.mAdapter = adapter;
return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
Expand All @@ -30,7 +26,10 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// mAdapter.clear();

// Delete all existint tasks from the DB
AsyncQueryHandler handler = new AsyncQueryHandler(getActivity().getContentResolver()) {};
handler.startDelete(0, null, TaskProvider.getContentUri(), null, null);
}
})
.setNegativeButton(R.string.cancel, null)
Expand Down
89 changes: 51 additions & 38 deletions Chapter9/src/com/example/EditTaskActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example;

import java.util.Calendar;

import android.content.AsyncQueryHandler;
import android.content.ContentValues;
import android.content.Intent;
Expand All @@ -21,11 +23,26 @@
public class EditTaskActivity extends FragmentActivity implements OnClickListener, LoaderCallbacks<Cursor> {

public static final String TASK_ID = "TaskId";
public static final String TASK_TIME = "TaskTime";
public static final String TASK_NAME = "TaskName";
public static final String TASK_DATE = "TaskDATE";
public static final String TASK_DESCRIPTION = "TaskDescription";

private long mTaskId = -1;
private long mTime = 0;
private EditText mName;
private EditText mDescription;
private DatePicker mDate;

private long getDateMillis() {
int year = mDate.getYear();
int month = mDate.getMonth();
int dayOfMonth = mDate.getDayOfMonth();
Calendar c = Calendar.getInstance();
c.set(year, month, dayOfMonth);
return c.getTimeInMillis();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -35,6 +52,7 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle extras = intent.getExtras();
if (extras != null) {
mTaskId = extras.getLong(TASK_ID);
mTime = extras.getLong(TASK_TIME);
}

Button finish = (Button) findViewById(R.id.finished);
Expand All @@ -48,46 +66,35 @@ protected void onCreate(Bundle savedInstanceState) {
}

@Override
protected void onPause() {
super.onPause();

// Save newly entered data to the database
// Don't block the UI thread
AsyncQueryHandler handler = new AsyncQueryHandler(getContentResolver()) {
@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
super.onInsertComplete(token, cookie, uri);
long task = Long.parseLong(uri.getLastPathSegment());
Intent data = new Intent();
data.putExtra(TASK_ID, task);
setResult(RESULT_OK, data);
}
public void onClick(View v) {
if (v.getId() == R.id.finished) {
// Save newly entered data to the database
// Don't block the UI thread
AsyncQueryHandler handler = new AsyncQueryHandler(getContentResolver()) {};

Uri uri = TaskProvider.getContentUri();
ContentValues cv = new ContentValues();
cv.put(TaskProvider.Task.NAME, mName.getText().toString());
cv.put(TaskProvider.Task.DESCRIPTION, mDescription.getText().toString());
cv.put(TaskProvider.Task.DATE, getDateMillis());

@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
super.onUpdateComplete(token, cookie, result);
if (mTaskId > -1) {
String selection = TaskProvider.Task._ID + " = ?";
String[] selectionArgs = new String[] {Long.toString(mTaskId)};
handler.startUpdate(0, null, uri, cv, selection, selectionArgs);
} else {
cv.put(TaskProvider.Task.TIME, 0);
handler.startInsert(0, null, uri, cv);
}
};

Uri uri = TaskProvider.getContentUri();
ContentValues cv = new ContentValues();
cv.put(TaskProvider.Task.NAME, mName.getText().toString());
cv.put(TaskProvider.Task.DESCRIPTION, mDescription.getText().toString());

if (mTaskId > -1) {
String selection = TaskProvider.Task._ID + " = ?";
String[] selectionArgs = new String[] {Long.toString(mTaskId)};
handler.startUpdate(0, null, uri, cv, selection, selectionArgs);
} else {
cv.put(TaskProvider.Task.DATE, System.currentTimeMillis());
cv.put(TaskProvider.Task.TIME, 0);
handler.startInsert(0, null, uri, cv);
}
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.finished) {
// Now finish the task, returning a result to the TimeTrackerActivity
Intent data = new Intent();
data.putExtra(TASK_ID, mTaskId);
data.putExtra(TASK_TIME, mTime);
data.putExtra(TASK_NAME, mName.getText().toString());
data.putExtra(TASK_DATE, getDateMillis());
data.putExtra(TASK_DESCRIPTION, mDescription.getText().toString());
setResult(RESULT_OK, data);
finish();
}
}
Expand All @@ -98,7 +105,7 @@ public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
String[] projection = new String[] {
TaskProvider.Task.NAME,
TaskProvider.Task.DESCRIPTION,
TaskProvider.Task.DATE
TaskProvider.Task.DATE,
};
String selection = TaskProvider.Task._ID + " = ?";
String[] selectionArgs = new String[] {Long.toString(mTaskId)};
Expand All @@ -114,8 +121,14 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(TaskProvider.Task.NAME));
String desc = cursor.getString(cursor.getColumnIndexOrThrow(TaskProvider.Task.DESCRIPTION));
long date = cursor.getLong(cursor.getColumnIndexOrThrow(TaskProvider.Task.DATE));

mName.setText(name);
mDescription.setText(desc);

Calendar c = Calendar.getInstance();
c.setTimeInMillis(date);
mDate.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
}

@Override
Expand Down
8 changes: 2 additions & 6 deletions Chapter9/src/com/example/PagerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@

public class PagerAdapter extends FragmentPagerAdapter {
public static final int PAGE_COUNT = 2;
TimerFragment mTimerFragment;
TaskListFragment mTaskListFragment;

public PagerAdapter(FragmentManager fm) {
super(fm);
mTimerFragment = new TimerFragment();
mTaskListFragment = new TaskListFragment();
}

@Override
public Fragment getItem(int arg0) {
switch(arg0) {
case 0:
return mTimerFragment;
return new TimerFragment();
case 1:
return mTaskListFragment;
return new TaskListFragment();
}
return null;
}
Expand Down
Loading

0 comments on commit e070b58

Please sign in to comment.