Skip to content

Commit

Permalink
Updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascraigschmidt committed Jun 28, 2015
1 parent d75ed78 commit 3caf819
Show file tree
Hide file tree
Showing 31 changed files with 610 additions and 76 deletions.
2 changes: 1 addition & 1 deletion ex/ContactsContentProviderAsync/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="18"
android:minSdkVersion="19"
android:targetSdkVersion="22" />

<uses-permission android:name="android.permission.READ_CONTACTS" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
</Button>

<Button android:layout_width="wrap_content"
android:text="@string/query_text"
android:text="@string/modify_text"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:onClick="queryContacts">
android:onClick="modifyContacts">
</Button>

<Button android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions ex/ContactsContentProviderAsync/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<string name="app_name">ContractsContentProviderAsync</string>
<string name="insert_text">Insert\nContacts</string>
<string name="query_text">Query\nContacts</string>
<string name="modify_text">Modify\nContacts</string>
<string name="delete_text">Delete\nContacts</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void onCreate(Bundle savedInstanceState) {
*/
public void insertContacts(View v) {
// Insert contacts.
getOps().runInsertContactAsyncCommands();
getOps().runInsertContactsAsyncCommands();
}

/**
Expand All @@ -100,13 +100,22 @@ public void queryContacts(View v) {
getOps().runQueryContactsAsyncCommands();
}

/**
* This click handler method modifies contacts in the
* ContactsContentProvider.
*/
public void modifyContacts(View v) {
// Modify contacts.
getOps().runModifyContactsAsyncCommands();
}

/**
* This click handler method deletes contacts from the
* ContactsContentProvider.
*/
public void deleteContacts(View v) {
// Delete contacts.
getOps().runDeleteContactAsyncCommands();
getOps().runDeleteContactsAsyncCommands();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.os.Looper;
import android.provider.ContactsContract;
import android.util.Log;

/**
Expand Down Expand Up @@ -55,6 +59,8 @@ public class ContactsOps implements ConfigurableOps {
*/
private Cursor mCursor;

public int mCounter;

/**
* The list of contacts that we'll insert, query, and delete.
*/
Expand All @@ -74,6 +80,57 @@ public class ContactsOps implements ConfigurableOps {
"Jimmy Swaggart",
}));

/**
* The list of contacts that we'll modify.
*/
protected final List<String> mModifyContacts =
new ArrayList<String>(Arrays.asList(new String[]
{
"Jiminy Cricket",
"James Cricket",
"Jimi Hendrix",
"James Hendix",
"Jimmy Buffett",
"James Buffett",
"Jimmy Carter",
"James Carter",
"Jimmy Choo",
"James Choo",
"Jimmy Connors",
"James Connors",
"Jimmy Durante",
"James Durante",
"Jimmy Fallon",
"James Fallon",
"Jimmy Kimmel",
"James Kimmel",
"Jimmy Johns",
"James Johns",
"Jimmy Johnson",
"James Johnson",
"Jimmy Page",
"James Page",
"Jimmy Swaggart",
"James Swaggart",
}));

private final Handler handler = new Handler(Looper.getMainLooper());

/**
* Observer that's dispatched by the ContentResolver when Contacts
* change (e.g., are inserted or deleted).
*/
private final ContentObserver contactsChangeContentObserver =
new ContentObserver(handler) {
/**
* Trigger a query and display.
*/
@Override
public void onChange (boolean selfChange) {
runQueryContactsAsyncCommands();
}
};

/**
* This default constructor must be public for the GenericOps
* class to work properly.
Expand All @@ -97,31 +154,72 @@ public void onConfiguration(Activity activity,
mActivity =
new WeakReference<>((ContactsActivity) activity);

if (firstTimeIn)
if (firstTimeIn) {
// Initialize the Google account information.
initializeAccount();
else if (mCursor != null)

// Initialize the ContentObserver.
initializeContentObserver();
} else if (mCursor != null)
// Redisplay the contents of the cursor after a runtime
// configuration change.
mActivity.get().displayCursor(mCursor);
}

/**
* Initialize the Google account.
*/
private void initializeAccount() {
// Get Account information. Must have a Google account
// configured on the device.
mAccountList =
AccountManager.get(mActivity.get()
.getApplicationContext())
.getAccountsByType("com.google");
if (mAccountList == null)
Utils.showToast(mActivity.get(),
"google account not configured");
else {
try {
mAccountType = mAccountList[0].type;
mAccountName = mAccountList[0].name;
} catch (Exception e) {
Log.d(TAG,
"google account not configured"
+ e);
}
}
}

/**
* Initialize the ContentObserver.
*/
public void initializeContentObserver() {
// Register a ContentObserver that's notified when Contacts
// change (e.g., are inserted or deleted).
mActivity.get().getContentResolver().registerContentObserver
(ContactsContract.Contacts.CONTENT_URI,
true,
contactsChangeContentObserver);
}

/**
* Insert the contacts asynchronously.
*/
public void runInsertContactAsyncCommands() {
public void runInsertContactsAsyncCommands() {
// Reset mCursor and reset the display to show nothing.
mActivity.get().displayCursor(mCursor = null);

// Start executing InsertAsyncCommand to insert the contacts
// into the Contacts Provider and then execute the
// QueryAsyncCommand to print out the inserted contacts. Both
// commands run asynchronously.
// QueryAsyncCommand to print out the number of inserted
// contacts. Both commands run asynchronously.
executeAsyncCommands
(new AsyncCommand[]{
new InsertAsyncCommand(this,
mContacts.iterator()),
new QueryAsyncCommand(this, true)
new QueryAsyncCommand(this,
true)
});
}

Expand All @@ -133,25 +231,52 @@ public void runQueryContactsAsyncCommands() {
// print out the inserted contacts when the query is done.
executeAsyncCommands
(new AsyncCommand[]{
new QueryAsyncCommand(this, false)
new QueryAsyncCommand(this,
false)
});
}

/**
* Delete the contacts asynchronously.
*/
public void runDeleteContactAsyncCommands() {
public void runDeleteContactsAsyncCommands() {
mCounter = 0;

// Start executing the DeleteAsyncCommand, which runs
// asynchronously.
executeAsyncCommands
(new AsyncCommand[]{
new DeleteAsyncCommand(this,
mContacts.iterator()),
mModifyContacts.iterator()),
// Print a toast after all the contacts are deleted.
new AsyncCommand(null) {
public void execute() {
Utils.showToast(mActivity.get(),
"Contacts deleted");
mCounter
+ " contact(s) deleted");
}
}
});
}

/**
* Modify the contacts asynchronously.
*/
public void runModifyContactsAsyncCommands() {
mCounter = 0;

// Start executing the ModifyAsyncCommand, which runs
// asynchronously.
executeAsyncCommands
(new AsyncCommand[]{
new ModifyAsyncCommand(this,
mModifyContacts.iterator()),
// Print a toast after all the contacts are deleted.
new AsyncCommand(null) {
public void execute() {
Utils.showToast(mActivity.get(),
mCounter
+ " contact(s) modified");
}
}
});
Expand All @@ -174,31 +299,6 @@ protected void executeAsyncCommands(AsyncCommand[] asyncCommands) {
asyncCommandsIter.next().execute();
}

/**
* Initialize the Google account.
*/
private void initializeAccount() {
// Get Account information. Must have a Google account
// configured on the device.
mAccountList =
AccountManager.get(mActivity.get()
.getApplicationContext())
.getAccountsByType("com.google");
if (mAccountList == null)
Utils.showToast(mActivity.get(),
"google account not configured");
else {
try {
mAccountType = mAccountList[0].type;
mAccountName = mAccountList[0].name;
} catch (Exception e) {
Log.d(TAG,
"google account not configured"
+ e);
}
}
}

/*
* Getters and settings for fields contained in ContactsOps.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public void execute() {
public void onDeleteComplete(int token,
Object cookie,
int result) {
// Increment the count of deletions.
mOps.mCounter += result;

// Call the execute() method to trigger the deletion of next
// contact (if any) in the Iterator.
this.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Iterator;

import vandy.mooc.common.AsyncCommand;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
Expand Down Expand Up @@ -117,12 +116,13 @@ public void onInsertComplete(int token,
Uri uri) {
if (token == RAW_CONTACT) {
// If the token is RAW_CONTACT then make a ContentValues
// object containing the data associated with RawContact
// and initiate an asynchronous insert on the Contacts
// Provider.
// object containing the data associated with RawContact.
ContentValues values =
makeRawContactData(mContactsIter.next(),
uri);

// Initiate an asynchronous insert on the Contacts
// Provider.
this.startInsert(RAW_CONTACT_DATA,
null,
Data.CONTENT_URI,
Expand Down
Loading

0 comments on commit 3caf819

Please sign in to comment.