Join activity is converted to loaders/fragments

Change-Id: I3d02fac52daffa5e0921a05fcad6932bc94e7274
diff --git a/src/com/android/contacts/JoinContactActivity.java b/src/com/android/contacts/JoinContactActivity.java
index 9024e0b..501739d 100644
--- a/src/com/android/contacts/JoinContactActivity.java
+++ b/src/com/android/contacts/JoinContactActivity.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007 The Android Open Source Project
+ * Copyright (C) 2009 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,28 +17,21 @@
 package com.android.contacts;
 
 
-import com.android.contacts.list.JoinContactListAdapter;
 import com.android.contacts.list.JoinContactListFragment;
-import com.android.internal.telephony.gsm.stk.ResultCode;
+import com.android.contacts.list.OnContactPickerActionListener;
 
-import android.content.ContentUris;
+import android.app.Activity;
+import android.app.FragmentTransaction;
 import android.content.Intent;
-import android.database.Cursor;
-import android.database.MatrixCursor;
 import android.net.Uri;
-import android.net.Uri.Builder;
+import android.os.Bundle;
 import android.provider.ContactsContract;
-import android.provider.ContactsContract.Contacts;
-import android.provider.ContactsContract.Contacts.AggregationSuggestions;
-import android.text.TextUtils;
 import android.util.Log;
-import android.widget.ListView;
-import android.widget.TextView;
 
 /**
  * An activity that shows a list of contacts that can be joined with the target contact.
  */
-public class JoinContactActivity extends ContactsListActivity {
+public class JoinContactActivity extends Activity {
 
     private static final String TAG = "JoinContactActivity";
 
@@ -57,153 +50,43 @@
      */
     public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID";
 
-    /** Maximum number of suggestions shown for joining aggregates */
-    private static final int MAX_SUGGESTIONS = 4;
-
     private long mTargetContactId;
 
-    /**
-     * The ID of the special item described above.
-     */
-    private static final long JOIN_MODE_SHOW_ALL_CONTACTS_ID = -2;
-
-    private boolean mLoadingJoinSuggestions;
-
-    private JoinContactListAdapter mAdapter;
-
     @Override
-    protected boolean resolveIntent(Intent intent) {
-        mMode = MODE_PICK_CONTACT;
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        Intent intent = getIntent();
         mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1);
         if (mTargetContactId == -1) {
             Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: "
                     + EXTRA_TARGET_CONTACT_ID);
             setResult(RESULT_CANCELED);
             finish();
-            return false;
+            return;
         }
 
-        mListFragment = new JoinContactListFragment();
-
-        return true;
-    }
-
-    @Override
-    protected void onResume() {
-        super.onResume();
-
-        // TODO move this to onAttach of the corresponding fragment
-        TextView blurbView = (TextView)findViewById(R.id.join_contact_blurb);
-
-        String blurb = getString(R.string.blurbJoinContactDataWith,
-                getContactDisplayName(mTargetContactId));
-        blurbView.setText(blurb);
-
-        ListView listView = (ListView)findViewById(android.R.id.list);
-        mAdapter = (JoinContactListAdapter)listView.getAdapter();
-        mAdapter.setJoinModeShowAllContacts(true);
-    }
-
-    @Override
-    public void onListItemClick(int position, long id) {
-        if (id == JOIN_MODE_SHOW_ALL_CONTACTS_ID) {
-            mAdapter.setJoinModeShowAllContacts(false);
-            startQuery();
-        } else {
-            final Uri uri = getSelectedUri(position);
-            setResult(RESULT_OK, new Intent(null, uri));
-            finish();
-        }
-    }
-
-    @Override
-    protected Uri getUriToQuery() {
-        return getJoinSuggestionsUri(null);
-    }
-
-    /*
-     * TODO: move to a background thread.
-     */
-    private String getContactDisplayName(long contactId) {
-        String contactName = null;
-        Cursor c = getContentResolver().query(
-                ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
-                new String[] {Contacts.DISPLAY_NAME}, null, null, null);
-        try {
-            if (c != null && c.moveToFirst()) {
-                contactName = c.getString(0);
-            }
-        } finally {
-            if (c != null) {
-                c.close();
-            }
-        }
-
-        if (contactName == null) {
-            contactName = "";
-        }
-
-        return contactName;
-    }
-
-    private Uri getJoinSuggestionsUri(String filter) {
-        Builder builder = Contacts.CONTENT_URI.buildUpon();
-        builder.appendEncodedPath(String.valueOf(mTargetContactId));
-        builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
-        if (!TextUtils.isEmpty(filter)) {
-            builder.appendEncodedPath(Uri.encode(filter));
-        }
-        builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS));
-        return builder.build();
-    }
-
-    @Override
-    public
-    Cursor doFilter(String filter) {
-        throw new UnsupportedOperationException();
-    }
-
-    private Cursor getShowAllContactsLabelCursor(String[] projection) {
-        MatrixCursor matrixCursor = new MatrixCursor(projection);
-        Object[] row = new Object[projection.length];
-        // The only columns we care about is the id
-        row[SUMMARY_ID_COLUMN_INDEX] = JOIN_MODE_SHOW_ALL_CONTACTS_ID;
-        matrixCursor.addRow(row);
-        return matrixCursor;
-    }
-
-    @Override
-    protected void startQuery(Uri uri, String[] projection) {
-        mLoadingJoinSuggestions = true;
-        startQuery(uri, projection, null, null, null);
-    }
-
-    @Override
-    protected void onQueryComplete(Cursor cursor) {
-        // Whenever we get a suggestions cursor, we need to immediately kick off
-        // another query for the complete list of contacts
-        if (cursor != null && mLoadingJoinSuggestions) {
-            mLoadingJoinSuggestions = false;
-            if (cursor.getCount() > 0) {
-                mAdapter.setSuggestionsCursor(cursor);
-            } else {
-                cursor.close();
-                mAdapter.setSuggestionsCursor(null);
+        JoinContactListFragment fragment = new JoinContactListFragment();
+        fragment.setTargetContactId(mTargetContactId);
+        fragment.setOnContactPickerActionListener(new OnContactPickerActionListener() {
+            public void onPickContactAction(Uri contactUri) {
+                Intent intent = new Intent(null, contactUri);
+                setResult(RESULT_OK, intent);
+                finish();
             }
 
-            if (mAdapter.getSuggestionsCursorCount() == 0
-                    || !mAdapter.isJoinModeShowAllContacts()) {
-                startQuery(getContactFilterUri(mListFragment.getQueryString()),
-                        CONTACTS_SUMMARY_PROJECTION,
-                        Contacts._ID + " != " + mTargetContactId
-                                + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null,
-                        getSortOrder(CONTACTS_SUMMARY_PROJECTION));
-                return;
+            public void onSearchAllContactsAction(String string) {
             }
 
-            cursor = getShowAllContactsLabelCursor(CONTACTS_SUMMARY_PROJECTION);
-        }
+            public void onShortcutIntentCreated(Intent intent) {
+            }
 
-        super.onQueryComplete(cursor);
+            public void onCreateNewContactAction() {
+            }
+        });
+
+        FragmentTransaction transaction = openFragmentTransaction();
+        transaction.add(fragment, android.R.id.content);
+        transaction.commit();
     }
 }