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

Commit c9130049 authored by Ben Kwa's avatar Ben Kwa Committed by Android (Google) Code Review
Browse files

Merge "Implement Home and End key navigation in DocumentsUI."

parents fb884b5f 0ee0f9a0
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);