Clean-up after extracting nested classes from CallLogFragment.

A few fields were made package accessible: fix those up.

Also, move around a couple of interfaces to the most appropriate
location.

Bug: 5101753
Change-Id: I34af0dc6401874d9c8c71932bce239852d42f703
diff --git a/src/com/android/contacts/calllog/CallLogAdapter.java b/src/com/android/contacts/calllog/CallLogAdapter.java
index 4d33eea..cbe075b 100644
--- a/src/com/android/contacts/calllog/CallLogAdapter.java
+++ b/src/com/android/contacts/calllog/CallLogAdapter.java
@@ -21,8 +21,6 @@
 import com.android.contacts.PhoneCallDetails;
 import com.android.contacts.PhoneCallDetailsHelper;
 import com.android.contacts.R;
-import com.android.contacts.calllog.CallLogFragment.CallFetcher;
-import com.android.contacts.calllog.CallLogFragment.GroupCreator;
 import com.android.contacts.util.ExpirableCache;
 import com.google.common.annotations.VisibleForTesting;
 
@@ -50,10 +48,18 @@
  * Adapter class to fill in data for the Call Log.
  */
 public final class CallLogAdapter extends GroupingListAdapter
-        implements Runnable, ViewTreeObserver.OnPreDrawListener, GroupCreator {
+        implements Runnable, ViewTreeObserver.OnPreDrawListener, CallLogGroupBuilder.GroupCreator {
+    /** Interface used to initiate a refresh of the content. */
+    public interface CallFetcher {
+        public void startCallsQuery();
+    }
+
     /** The time in millis to delay starting the thread processing requests. */
     private static final int START_PROCESSING_REQUESTS_DELAY_MILLIS = 1000;
 
+    /** The size of the cache of contact info. */
+    private static final int CONTACT_INFO_CACHE_SIZE = 100;
+
     private final Context mContext;
     private final String mCurrentCountryIso;
     private final CallFetcher mCallFetcher;
@@ -64,7 +70,7 @@
      * The content of the cache is expired (but not purged) whenever the application comes to
      * the foreground.
      */
-    ExpirableCache<String, ContactInfo> mContactInfoCache;
+    private ExpirableCache<String, ContactInfo> mContactInfoCache;
 
     /**
      * List of requests to update contact details.
@@ -76,7 +82,7 @@
 
     private volatile boolean mDone;
     private boolean mLoading = true;
-    ViewTreeObserver.OnPreDrawListener mPreDrawListener;
+    private ViewTreeObserver.OnPreDrawListener mPreDrawListener;
     private static final int REDRAW = 1;
     private static final int START_THREAD = 2;
     private boolean mFirst;
@@ -148,7 +154,7 @@
         mCurrentCountryIso = currentCountryIso;
         mCallFetcher = callFetcher;
 
-        mContactInfoCache = ExpirableCache.create(CallLogFragment.CONTACT_INFO_CACHE_SIZE);
+        mContactInfoCache = ExpirableCache.create(CONTACT_INFO_CACHE_SIZE);
         mRequests = new LinkedList<String>();
         mPreDrawListener = null;
 
@@ -172,7 +178,7 @@
     protected void onContentChanged() {
         // When the content changes, always fetch all the calls, in case a new missed call came
         // in and we were filtering over voicemail only, so that we see the missed call.
-        mCallFetcher.fetchAllCalls();
+        mCallFetcher.startCallsQuery();
     }
 
     void setLoading(boolean loading) {
@@ -220,6 +226,8 @@
 
     public void invalidateCache() {
         mContactInfoCache.expireAll();
+        // Let it restart the thread after next draw
+        mPreDrawListener = null;
     }
 
     private void enqueueRequest(String number, boolean immediate) {
@@ -684,4 +692,42 @@
         }
         return PhoneNumberUtils.formatNumber(number, normalizedNumber, countryIso);
     }
-}
\ No newline at end of file
+
+    /*
+     * Get the number from the Contacts, if available, since sometimes
+     * the number provided by caller id may not be formatted properly
+     * depending on the carrier (roaming) in use at the time of the
+     * incoming call.
+     * Logic : If the caller-id number starts with a "+", use it
+     *         Else if the number in the contacts starts with a "+", use that one
+     *         Else if the number in the contacts is longer, use that one
+     */
+    public String getBetterNumberFromContacts(String number) {
+        String matchingNumber = null;
+        // Look in the cache first. If it's not found then query the Phones db
+        ContactInfo ci = mContactInfoCache.getPossiblyExpired(number);
+        if (ci != null && ci != ContactInfo.EMPTY) {
+            matchingNumber = ci.number;
+        } else {
+            try {
+                Cursor phonesCursor = mContext.getContentResolver().query(
+                        Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number),
+                        PhoneQuery._PROJECTION, null, null, null);
+                if (phonesCursor != null) {
+                    if (phonesCursor.moveToFirst()) {
+                        matchingNumber = phonesCursor.getString(PhoneQuery.MATCHED_NUMBER);
+                    }
+                    phonesCursor.close();
+                }
+            } catch (Exception e) {
+                // Use the number from the call log
+            }
+        }
+        if (!TextUtils.isEmpty(matchingNumber) &&
+                (matchingNumber.startsWith("+")
+                        || matchingNumber.length() > number.length())) {
+            number = matchingNumber;
+        }
+        return number;
+    }
+}
diff --git a/src/com/android/contacts/calllog/CallLogFragment.java b/src/com/android/contacts/calllog/CallLogFragment.java
index dcd4fee..a40379d 100644
--- a/src/com/android/contacts/calllog/CallLogFragment.java
+++ b/src/com/android/contacts/calllog/CallLogFragment.java
@@ -37,7 +37,6 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.provider.CallLog.Calls;
-import android.provider.ContactsContract.PhoneLookup;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
@@ -57,12 +56,9 @@
  * Displays a list of call log entries.
  */
 public class CallLogFragment extends ListFragment implements ViewPagerVisibilityListener,
-        CallLogQueryHandler.Listener {
+        CallLogQueryHandler.Listener, CallLogAdapter.CallFetcher {
     private static final String TAG = "CallLogFragment";
 
-    /** The size of the cache of contact info. */
-    static final int CONTACT_INFO_CACHE_SIZE = 100;
-
     private CallLogAdapter mAdapter;
     private CallLogQueryHandler mCallLogQueryHandler;
     private String mVoiceMailNumber;
@@ -78,14 +74,6 @@
     private TextView mStatusMessageAction;
     private KeyguardManager mKeyguardManager;
 
-    public interface GroupCreator {
-        public void addGroup(int cursorPosition, int size, boolean expanded);
-    }
-
-    public interface CallFetcher {
-        public void fetchAllCalls();
-    }
-
     @Override
     public void onCreate(Bundle state) {
         super.onCreate(state);
@@ -141,13 +129,7 @@
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
         String currentCountryIso = ContactsUtils.getCurrentCountryIso(getActivity());
-        mAdapter = new CallLogAdapter(getActivity(),
-                new CallFetcher() {
-                    @Override
-                    public void fetchAllCalls() {
-                        startCallsQuery();
-                    }
-                }, currentCountryIso, mVoiceMailNumber);
+        mAdapter = new CallLogAdapter(getActivity(), this, currentCountryIso, mVoiceMailNumber);
         setListAdapter(mAdapter);
         getListView().setItemsCanFocus(true);
     }
@@ -213,7 +195,8 @@
         mAdapter.changeCursor(null);
     }
 
-    private void startCallsQuery() {
+    @Override
+    public void startCallsQuery() {
         mAdapter.setLoading(true);
         mCallLogQueryHandler.fetchAllCalls();
         if (mShowingVoicemailOnly) {
@@ -265,45 +248,6 @@
                 return false;
         }
     }
-
-    /*
-     * Get the number from the Contacts, if available, since sometimes
-     * the number provided by caller id may not be formatted properly
-     * depending on the carrier (roaming) in use at the time of the
-     * incoming call.
-     * Logic : If the caller-id number starts with a "+", use it
-     *         Else if the number in the contacts starts with a "+", use that one
-     *         Else if the number in the contacts is longer, use that one
-     */
-    private String getBetterNumberFromContacts(String number) {
-        String matchingNumber = null;
-        // Look in the cache first. If it's not found then query the Phones db
-        ContactInfo ci = mAdapter.mContactInfoCache.getPossiblyExpired(number);
-        if (ci != null && ci != ContactInfo.EMPTY) {
-            matchingNumber = ci.number;
-        } else {
-            try {
-                Cursor phonesCursor = getActivity().getContentResolver().query(
-                        Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number),
-                        PhoneQuery._PROJECTION, null, null, null);
-                if (phonesCursor != null) {
-                    if (phonesCursor.moveToFirst()) {
-                        matchingNumber = phonesCursor.getString(PhoneQuery.MATCHED_NUMBER);
-                    }
-                    phonesCursor.close();
-                }
-            } catch (Exception e) {
-                // Use the number from the call log
-            }
-        }
-        if (!TextUtils.isEmpty(matchingNumber) &&
-                (matchingNumber.startsWith("+")
-                        || matchingNumber.length() > number.length())) {
-            number = matchingNumber;
-        }
-        return number;
-    }
-
     public void callSelectedEntry() {
         int position = getListView().getSelectedItemPosition();
         if (position < 0) {
@@ -335,7 +279,7 @@
                        (callType == Calls.INCOMING_TYPE
                                 || callType == Calls.MISSED_TYPE)) {
                     // If the caller-id matches a contact with a better qualified number, use it
-                    number = getBetterNumberFromContacts(number);
+                    number = mAdapter.getBetterNumberFromContacts(number);
                 }
                 intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
                                     Uri.fromParts("tel", number, null));
@@ -375,7 +319,6 @@
         mAdapter.invalidateCache();
         startCallsQuery();
         startVoicemailStatusQuery();
-        mAdapter.mPreDrawListener = null; // Let it restart the thread after next draw
         updateOnEntry();
     }
 
diff --git a/src/com/android/contacts/calllog/CallLogGroupBuilder.java b/src/com/android/contacts/calllog/CallLogGroupBuilder.java
index 284e668..a7f8fa3 100644
--- a/src/com/android/contacts/calllog/CallLogGroupBuilder.java
+++ b/src/com/android/contacts/calllog/CallLogGroupBuilder.java
@@ -29,15 +29,19 @@
  * This class is meant to be used in conjunction with {@link GroupingListAdapter}.
  */
 public class CallLogGroupBuilder {
+    public interface GroupCreator {
+        public void addGroup(int cursorPosition, int size, boolean expanded);
+    }
+
     /** Reusable char array buffer. */
     private CharArrayBuffer mBuffer1 = new CharArrayBuffer(128);
     /** Reusable char array buffer. */
     private CharArrayBuffer mBuffer2 = new CharArrayBuffer(128);
 
     /** The object on which the groups are created. */
-    private final CallLogFragment.GroupCreator mGroupCreator;
+    private final GroupCreator mGroupCreator;
 
-    public CallLogGroupBuilder(CallLogFragment.GroupCreator groupCreator) {
+    public CallLogGroupBuilder(GroupCreator groupCreator) {
         mGroupCreator = groupCreator;
     }
 
diff --git a/tests/src/com/android/contacts/calllog/CallLogGroupBuilderTest.java b/tests/src/com/android/contacts/calllog/CallLogGroupBuilderTest.java
index d05dc0b..8e97fe3 100644
--- a/tests/src/com/android/contacts/calllog/CallLogGroupBuilderTest.java
+++ b/tests/src/com/android/contacts/calllog/CallLogGroupBuilderTest.java
@@ -271,7 +271,7 @@
     }
 
     /** Fake implementation of a GroupCreator which stores the created groups in a member field. */
-    private static class FakeGroupCreator implements CallLogFragment.GroupCreator {
+    private static class FakeGroupCreator implements CallLogGroupBuilder.GroupCreator {
         /** The list of created groups. */
         public final List<GroupSpec> groups = newArrayList();