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

Commit 0ee0f9a0 authored by Ben Kwa's avatar Ben Kwa
Browse files

Implement Home and End key navigation in DocumentsUI.

Add a handler to shift focus to the first and last files when the home
and end keys (respectively) are pressed.

BUG=24682598

Change-Id: I82db67d0d0f8292b1dad966defa68800496d59f2
parent 01334b64
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.provider.DocumentsContract.Root;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@@ -465,6 +466,21 @@ abstract class BaseActivity extends Activity {
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (DEBUG) Log.d(mTag, "onKeyUp: keycode = " + keyCode);
        DirectoryFragment dir = DirectoryFragment.get(getFragmentManager());
        switch (keyCode) {
            case KeyEvent.KEYCODE_MOVE_HOME:
                dir.focusFirstFile();
                return true;
            case KeyEvent.KEYCODE_MOVE_END:
                dir.focusLastFile();
                return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    public void onStackPicked(DocumentStack stack) {
        try {
            // Update the restored stack to ensure we have freshest data
+39 −0
Original line number Diff line number Diff line
@@ -1341,6 +1341,45 @@ public class DirectoryFragment extends Fragment {
        }
    }

    /**
     * Scrolls to the top of the file list and focuses the first file.
     */
    void focusFirstFile() {
        focusFile(0);
    }

    /**
     * Scrolls to the bottom of the file list and focuses the last file.
     */
    void focusLastFile() {
        focusFile(mAdapter.getItemCount() - 1);
    }

    /**
     * Scrolls to and then focuses on the file at the given position.
     */
    private void focusFile(final int pos) {
        // Don't smooth scroll; that taxes the system unnecessarily and makes the scroll handling
        // logic below more complicated.
        mRecView.scrollToPosition(pos);

        // If the item is already in view, focus it; otherwise, set a one-time listener to focus it
        // when the scroll is completed.
        RecyclerView.ViewHolder vh = mRecView.findViewHolderForAdapterPosition(pos);
        if (vh != null) {
            vh.itemView.requestFocus();
        } else {
            mRecView.addOnScrollListener(
                    new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView view, int dx, int dy) {
                            view.findViewHolderForAdapterPosition(pos).itemView.requestFocus();
                            view.removeOnScrollListener(this);
                        }
                    });
        }
    }

    private void setupDragAndDropOnDirectoryView(View view) {
        // Listen for drops on non-directory items and empty space.
        view.setOnDragListener(mOnDragListener);