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

Commit f1694ee1 authored by Colin Cross's avatar Colin Cross Committed by Xin Li
Browse files

Change switches on resource IDs to if statements

Prepare for building the DocumentsUI code once as a library by using
if statements instead of switches on resource IDs.  Libraries are
compiled with non-final reosurce IDs, as the final IDs will be assigned
later during app linking.

This relands Ia48b0b35a891a6a427b67a7a09bfe6cab2663fc4.

Test: m DocumentsUI
Bug: 294256649
Merged-In: Iac561933980b5089347a451c6738596317eccf76
Change-Id: Iac561933980b5089347a451c6738596317eccf76
parent e203586d
Loading
Loading
Loading
Loading
+11 −15
Original line number Diff line number Diff line
@@ -16,11 +16,11 @@

package com.android.documentsui.sorting;

import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import android.view.View;

import com.android.documentsui.BaseActivity;
import com.android.documentsui.Injector;
import com.android.documentsui.MetricConsts;
@@ -67,19 +67,15 @@ public final class SortController {
        final Injector<?> injector = ((BaseActivity)activity).getInjector();
        sortModel.setMetricRecorder((SortDimension dimension) -> {
            int sortType = MetricConsts.USER_ACTION_UNKNOWN;
            switch (dimension.getId()) {
                case SortModel.SORT_DIMENSION_ID_TITLE:
            final int id = dimension.getId();
            if (id == SortModel.SORT_DIMENSION_ID_TITLE) {
                sortType = MetricConsts.USER_ACTION_SORT_NAME;
                    break;
                case SortModel.SORT_DIMENSION_ID_SIZE:
            } else if (id == SortModel.SORT_DIMENSION_ID_SIZE) {
                sortType = MetricConsts.USER_ACTION_SORT_SIZE;
                    break;
                case SortModel.SORT_DIMENSION_ID_DATE:
            } else if (id == SortModel.SORT_DIMENSION_ID_DATE) {
                sortType = MetricConsts.USER_ACTION_SORT_DATE;
                    break;
                case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
            } else if (id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
                sortType = MetricConsts.USER_ACTION_SORT_TYPE;
                    break;
            }

            Metrics.logUserAction(sortType);
+23 −27
Original line number Diff line number Diff line
@@ -65,18 +65,15 @@ public class SortListFragment extends DialogFragment {
        for (int i = 0; i < mModel.getSize(); ++i) {
            SortDimension dimension = mModel.getDimensionAt(i);
            if (dimension.getSortCapability() != SortDimension.SORT_CAPABILITY_NONE) {
                switch (dimension.getId()) {
                    case SortModel.SORT_DIMENSION_ID_TITLE:
                    case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
                final int id = dimension.getId();
                if (id == SortModel.SORT_DIMENSION_ID_TITLE
                        || id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
                    addBothDirectionDimension(dimension, true);
                        break;
                    case SortModel.SORT_DIMENSION_ID_DATE:
                    case SortModel.SORT_DIMENSION_ID_SIZE:
                } else if (id == SortModel.SORT_DIMENSION_ID_DATE
                        || id == SortModel.SORT_DIMENSION_ID_SIZE) {
                    addBothDirectionDimension(dimension, false);
                        break;
                    default:
                } else {
                    mSortingList.add(new SortItem(dimension));
                        break;
                }
            }
        }
@@ -95,22 +92,21 @@ public class SortListFragment extends DialogFragment {

    public static @StringRes int getSheetLabelId(SortDimension dimension, @SortDirection int direction) {
        boolean isAscending = direction == SortDimension.SORT_DIRECTION_ASCENDING;
        switch (dimension.getId()) {
            case SortModel.SORT_DIMENSION_ID_TITLE:
        final int id = dimension.getId();
        if (id == SortModel.SORT_DIMENSION_ID_TITLE) {
            return isAscending ? R.string.sort_dimension_name_ascending :
                    R.string.sort_dimension_name_descending;
            case SortModel.SORT_DIMENSION_ID_DATE:
        } else if (id == SortModel.SORT_DIMENSION_ID_DATE) {
            return isAscending ? R.string.sort_dimension_date_ascending :
                    R.string.sort_dimension_date_descending;
            case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
        } else if (id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
            return isAscending ? R.string.sort_dimension_file_type_ascending :
                    R.string.sort_dimension_file_type_descending;
            case SortModel.SORT_DIMENSION_ID_SIZE:
        } else if (id == SortModel.SORT_DIMENSION_ID_SIZE) {
            return isAscending ? R.string.sort_dimension_size_ascending :
                    R.string.sort_dimension_size_descending;
            default:
                return dimension.getLabelId();
        }
        return dimension.getLabelId();
    }

    @Override
+38 −46
Original line number Diff line number Diff line
@@ -229,30 +229,26 @@ public class SortModel implements Parcelable {
        // should only be called when R.bool.feature_content_paging is true

        final int id = getSortedDimensionId();
        switch (id) {
            case SORT_DIMENSION_ID_UNKNOWN:
        if (id == SORT_DIMENSION_ID_UNKNOWN) {
            return;
            case SortModel.SORT_DIMENSION_ID_TITLE:
        } else if (id == SortModel.SORT_DIMENSION_ID_TITLE) {
            queryArgs.putStringArray(
                    ContentResolver.QUERY_ARG_SORT_COLUMNS,
                    new String[]{Document.COLUMN_DISPLAY_NAME});
                break;
            case SortModel.SORT_DIMENSION_ID_DATE:
        } else if (id == SortModel.SORT_DIMENSION_ID_DATE) {
            queryArgs.putStringArray(
                    ContentResolver.QUERY_ARG_SORT_COLUMNS,
                    new String[]{Document.COLUMN_LAST_MODIFIED});
                break;
            case SortModel.SORT_DIMENSION_ID_SIZE:
        } else if (id == SortModel.SORT_DIMENSION_ID_SIZE) {
            queryArgs.putStringArray(
                    ContentResolver.QUERY_ARG_SORT_COLUMNS,
                    new String[]{Document.COLUMN_SIZE});
                break;
            case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
        } else if (id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
            // Unfortunately sorting by mime type is pretty much guaranteed different from
            // sorting by user-friendly type, so there is no point to guide the provider to sort
            // in a particular order.
            return;
            default:
        } else {
            throw new IllegalStateException(
                    "Unexpected sort dimension id: " + id);
        }
@@ -286,24 +282,20 @@ public class SortModel implements Parcelable {

        final int id = getSortedDimensionId();
        final String columnName;
        switch (id) {
            case SORT_DIMENSION_ID_UNKNOWN:
        if (id == SORT_DIMENSION_ID_UNKNOWN) {
            return null;
            case SortModel.SORT_DIMENSION_ID_TITLE:
        } else if (id == SortModel.SORT_DIMENSION_ID_TITLE) {
            columnName = Document.COLUMN_DISPLAY_NAME;
                break;
            case SortModel.SORT_DIMENSION_ID_DATE:
        } else if (id == SortModel.SORT_DIMENSION_ID_DATE) {
            columnName = Document.COLUMN_LAST_MODIFIED;
                break;
            case SortModel.SORT_DIMENSION_ID_SIZE:
        } else if (id == SortModel.SORT_DIMENSION_ID_SIZE) {
            columnName = Document.COLUMN_SIZE;
                break;
            case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
        } else if (id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
            // Unfortunately sorting by mime type is pretty much guaranteed different from
            // sorting by user-friendly type, so there is no point to guide the provider to sort
            // in a particular order.
            return null;
            default:
        } else {
            throw new IllegalStateException(
                    "Unexpected sort dimension id: " + id);
        }
+22 −33
Original line number Diff line number Diff line
@@ -49,15 +49,12 @@ class SortingCursorWrapper extends AbstractCursor {
        String[] ids = new String[count];

        final int id = dimension.getId();
        switch (id) {
            case SortModel.SORT_DIMENSION_ID_TITLE:
            case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
        if (id == SortModel.SORT_DIMENSION_ID_TITLE
                || id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
            stringValues = new String[count];
                break;
            case SortModel.SORT_DIMENSION_ID_DATE:
            case SortModel.SORT_DIMENSION_ID_SIZE:
        } else if (id == SortModel.SORT_DIMENSION_ID_DATE
                || id == SortModel.SORT_DIMENSION_ID_SIZE) {
            longValues = new long[count];
                break;
        }

        cursor.moveToPosition(-1);
@@ -69,34 +66,26 @@ class SortingCursorWrapper extends AbstractCursor {
            isDirs[i] = Document.MIME_TYPE_DIR.equals(mimeType);
            ids[i] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);

            switch(id) {
                case SortModel.SORT_DIMENSION_ID_TITLE:
            if (id == SortModel.SORT_DIMENSION_ID_TITLE) {
                final String displayName = getCursorString(
                        mCursor, Document.COLUMN_DISPLAY_NAME);
                stringValues[i] = displayName;
                    break;
                case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
            } else if (id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
                stringValues[i] = fileTypeLookup.lookup(mimeType);
                    break;
                case SortModel.SORT_DIMENSION_ID_DATE:
            } else if (id == SortModel.SORT_DIMENSION_ID_DATE) {
                longValues[i] = getLastModified(mCursor);
                    break;
                case SortModel.SORT_DIMENSION_ID_SIZE:
            } else if (id == SortModel.SORT_DIMENSION_ID_SIZE) {
                longValues[i] = getCursorLong(mCursor, Document.COLUMN_SIZE);
                    break;
            }

        }

        switch (id) {
            case SortModel.SORT_DIMENSION_ID_TITLE:
            case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
        if (id == SortModel.SORT_DIMENSION_ID_TITLE
                || id == SortModel.SORT_DIMENSION_ID_FILE_TYPE) {
            binarySort(stringValues, isDirs, mPosition, ids, dimension.getSortDirection());
                break;
            case SortModel.SORT_DIMENSION_ID_DATE:
            case SortModel.SORT_DIMENSION_ID_SIZE:
        } else if (id == SortModel.SORT_DIMENSION_ID_DATE
                || id == SortModel.SORT_DIMENSION_ID_SIZE) {
            binarySort(longValues, isDirs, mPosition, ids, dimension.getSortDirection());
                break;
        }

    }