Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e33014cd authored by Flavio Lerda's avatar Flavio Lerda Committed by Android (Google) Code Review
Browse files

Merge "Clean-up after extracting nested classes from CallLogFragment."

parents 242040e8 de4f16aa
Loading
Loading
Loading
Loading
+54 −8
Original line number Diff line number Diff line
@@ -21,8 +21,6 @@ import com.android.contacts.ContactPhotoManager;
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 @@ import java.util.LinkedList;
 * 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 @@ public final class CallLogAdapter extends GroupingListAdapter
     * 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 @@ public final class CallLogAdapter extends GroupingListAdapter

    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 @@ public final class CallLogAdapter extends GroupingListAdapter
        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 @@ public final class CallLogAdapter extends GroupingListAdapter
    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 final class CallLogAdapter extends GroupingListAdapter

    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 @@ public final class CallLogAdapter extends GroupingListAdapter
        }
        return PhoneNumberUtils.formatNumber(number, normalizedNumber, countryIso);
    }

    /*
     * 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;
    }
}
+5 −62
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ import android.os.Bundle;
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 @@ import java.util.List;
 * 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 @@ public class CallLogFragment extends ListFragment implements ViewPagerVisibility
    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 class CallLogFragment extends ListFragment implements ViewPagerVisibility
    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 @@ public class CallLogFragment extends ListFragment implements ViewPagerVisibility
        mAdapter.changeCursor(null);
    }

    private void startCallsQuery() {
    @Override
    public void startCallsQuery() {
        mAdapter.setLoading(true);
        mCallLogQueryHandler.fetchAllCalls();
        if (mShowingVoicemailOnly) {
@@ -265,45 +248,6 @@ public class CallLogFragment extends ListFragment implements ViewPagerVisibility
                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 @@ public class CallLogFragment extends ListFragment implements ViewPagerVisibility
                       (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 @@ public class CallLogFragment extends ListFragment implements ViewPagerVisibility
        mAdapter.invalidateCache();
        startCallsQuery();
        startVoicemailStatusQuery();
        mAdapter.mPreDrawListener = null; // Let it restart the thread after next draw
        updateOnEntry();
    }

+6 −2
Original line number Diff line number Diff line
@@ -29,15 +29,19 @@ import android.telephony.PhoneNumberUtils;
 * 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;
    }

+1 −1
Original line number Diff line number Diff line
@@ -271,7 +271,7 @@ public class CallLogGroupBuilderTest extends AndroidTestCase {
    }

    /** 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();