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

Commit 354a5164 authored by Rhed Jao's avatar Rhed Jao
Browse files

Fix NPE caused by invalid ContentScope in FocusManager.

Bug: 79662134
Test: Small test in FocusManagerTest.
Change-Id: I0076dd3897c295358d5d22a2282cb442c6ff2929
parent 4d85336b
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -129,14 +129,14 @@ public final class FocusManager implements FocusHandler {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // Remember focus events on items.
        if (hasFocus && v.getParent() == mScope.view) {
        if (hasFocus && mScope.isValid() && v.getParent() == mScope.view) {
            mScope.lastFocusPosition = mScope.view.getChildAdapterPosition(v);
        }
    }

    @Override
    public boolean focusDirectoryList() {
        if (mScope.adapter.getItemCount() == 0) {
        if (!mScope.isValid() || mScope.adapter.getItemCount() == 0) {
            if (DEBUG) Log.v(TAG, "Nothing to focus.");
            return false;
        }
@@ -180,8 +180,10 @@ public final class FocusManager implements FocusHandler {

    @Override
    public void clearFocus() {
        if (mScope.isValid()) {
            mScope.view.clearFocus();
        }
    }

    /*
     * Attempts to put focus on the document associated with the given modelId. If item does not
@@ -190,6 +192,10 @@ public final class FocusManager implements FocusHandler {
     */
    @Override
    public void focusDocument(String modelId) {
        if (!mScope.isValid()) {
            if (DEBUG) Log.v(TAG, "Invalid mScope. No focus will be done.");
            return;
        }
        int pos = mScope.adapter.getAdapterPosition(modelId);
        if (pos != -1 && mScope.view.findViewHolderForAdapterPosition(pos) != null) {
            focusItem(pos);
@@ -663,5 +669,9 @@ public final class FocusManager implements FocusHandler {

        private @Nullable String pendingFocusId;
        private int lastFocusPosition = RecyclerView.NO_POSITION;

        boolean isValid() {
            return (view != null && model != null);
        }
    }
}
+29 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import com.android.documentsui.testing.SelectionHelpers;
import com.android.documentsui.testing.TestFeatures;
import com.android.documentsui.testing.TestGridLayoutManager;
import com.android.documentsui.testing.TestRecyclerView;
import com.android.documentsui.testing.Views;

import java.util.ArrayList;
import java.util.List;
@@ -88,4 +89,32 @@ public class FocusManagerTest extends AndroidTestCase {
        mSelectionMgr.select("0");
        assertFalse(mManager.focusDirectoryList());
    }

    public void testFocusDirectoryList_invalidContentScope() {
        mManager = new FocusManager(
                mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
        // pass if no exception is thrown.
        mManager.focusDirectoryList();
    }

    public void testOnFocusChange_invalidContentScope() {
        mManager = new FocusManager(
                mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
        // pass if no exception is thrown.
        mManager.onFocusChange(Views.createTestView(), true);
    }

    public void testClearFocus_invalidContentScope() {
        mManager = new FocusManager(
                mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
        // pass if no exception is thrown.
        mManager.clearFocus();
    }

    public void testFocusDocument_invalidContentScope() {
        mManager = new FocusManager(
                mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
        // pass if no exception is thrown.
        mManager.focusDocument(Integer.toString(0));
    }
}