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

Commit 91bc40e8 authored by Jon Mann's avatar Jon Mann Committed by Android (Google) Code Review
Browse files

Merge "Add more descriptive UI for conflicts while renaming." into nyc-andromeda-dev

parents c041a94e 60d96a2a
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -19,11 +19,16 @@
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:padding="?android:attr/listPreferredItemPaddingEnd">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/rename_input_wrapper"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
        <EditText
            android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"/>

    </android.support.design.widget.TextInputLayout>
</FrameLayout>
+3 −0
Original line number Diff line number Diff line
@@ -268,4 +268,7 @@

    <!-- Error message shown when an archive fails to load -->
    <string name="archive_loading_failed">Unable to open archive for browsing. File is either corrupt, or an unsupported format.</string>

    <!-- Error message displayed in rename dialog when there is a conflict with an existing file. -->
    <string name="name_conflict">A file with this name already exists.</string>
</resources>
+1 −1
Original line number Diff line number Diff line
@@ -779,7 +779,7 @@ public class DirectoryFragment extends Fragment

        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
        List<DocumentInfo> docs = mModel.getDocuments(selected);
        RenameDocumentFragment.show(getFragmentManager(), docs.get(0));
        RenameDocumentFragment.show(getFragmentManager(), docs.get(0), mModel::hasFileWithName);
    }

    private boolean isDocumentEnabled(String mimeType, int flags) {
+13 −3
Original line number Diff line number Diff line
@@ -44,8 +44,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

/**
@@ -81,12 +83,14 @@ public class Model {

    private static final String TAG = "Model";

    /** Maps Model ID to cursor positions, for looking up items by Model ID. */
    private final Map<String, Integer> mPositions = new HashMap<>();
    private final Set<String> mFileNames = new HashSet<>();

    private boolean mIsLoading;
    private List<EventListener<Update>> mUpdateListeners = new ArrayList<>();
    @Nullable private Cursor mCursor;
    private int mCursorCount;
    /** Maps Model ID to cursor positions, for looking up items by Model ID. */
    private Map<String, Integer> mPositions = new HashMap<>();
    private String mIds[] = new String[0];

    @Nullable String info;
@@ -132,6 +136,7 @@ public class Model {
        error = null;
        doc = null;
        mIsLoading = false;
        mFileNames.clear();
        notifyUpdateListeners();
    }

@@ -173,7 +178,7 @@ public class Model {
     */
    private void updateModelData() {
        mIds = new String[mCursorCount];

        mFileNames.clear();
        mCursor.moveToPosition(-1);
        for (int pos = 0; pos < mCursorCount; ++pos) {
            if (!mCursor.moveToNext()) {
@@ -190,6 +195,7 @@ public class Model {
            } else {
                mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
            }
            mFileNames.add(getCursorString(mCursor, Document.COLUMN_DISPLAY_NAME));
        }

        // Populate the positions.
@@ -199,6 +205,10 @@ public class Model {
        }
    }

    public boolean hasFileWithName(String name) {
        return mFileNames.contains(name);
    }

    public @Nullable Cursor getItem(String modelId) {
        Integer pos = mPositions.get(modelId);
        if (pos == null) {
+34 −16
Original line number Diff line number Diff line
@@ -33,11 +33,13 @@ import android.os.Bundle;
import android.provider.DocumentsContract;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
@@ -50,6 +52,8 @@ import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.Shared;
import com.android.documentsui.ui.Snackbars;

import java.util.function.Predicate;

/**
 * Dialog to rename file or directory.
 */
@@ -57,10 +61,15 @@ public class RenameDocumentFragment extends DialogFragment {
    private static final String TAG_RENAME_DOCUMENT = "rename_document";
    private DocumentInfo mDocument;
    private EditText mEditText;
    private TextInputLayout mRenameInputWrapper;
    private Predicate<String> mHasFileNamed;
    private @Nullable DialogInterface mDialog;

    public static void show(FragmentManager fm, DocumentInfo document) {
    public static void show(
            FragmentManager fm, DocumentInfo document, Predicate<String> hasFileNamed) {
        final RenameDocumentFragment dialog = new RenameDocumentFragment();
        dialog.mDocument = document;
        dialog.mHasFileNamed = hasFileNamed;
        dialog.show(fm, TAG_RENAME_DOCUMENT);
    }

@@ -77,22 +86,16 @@ public class RenameDocumentFragment extends DialogFragment {
        View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);

        mEditText = (EditText) view.findViewById(android.R.id.text1);
        mRenameInputWrapper = (TextInputLayout) view.findViewById(R.id.rename_input_wrapper);
        builder.setTitle(R.string.menu_rename);
        builder.setView(view);

        builder.setPositiveButton(
                android.R.string.ok,
                new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        renameDocuments(mEditText.getText().toString());
                    }
                });

        builder.setPositiveButton(android.R.string.ok, null);
        builder.setNegativeButton(android.R.string.cancel, null);

        final AlertDialog dialog = builder.create();

        dialog.setOnShowListener(this::onShowDialog);

        // Workaround for the problem - virtual keyboard doesn't show on the phone.
        Shared.ensureKeyboardPresent(context, dialog);

@@ -105,8 +108,6 @@ public class RenameDocumentFragment extends DialogFragment {
                                && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                                && event.hasNoModifiers())) {
                            renameDocuments(mEditText.getText().toString());
                            dialog.dismiss();
                            return true;
                        }
                        return false;
                    }
@@ -114,6 +115,16 @@ public class RenameDocumentFragment extends DialogFragment {
        return dialog;
    }

    private void onShowDialog(DialogInterface dialog){
        mDialog = dialog;
        Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
        button.setOnClickListener(this::onClickDialog);
    }

    private void onClickDialog(View view) {
        renameDocuments(mEditText.getText().toString());
    }

    /**
     * Sets/Restores the data.
     * @param savedInstanceState
@@ -182,12 +193,16 @@ public class RenameDocumentFragment extends DialogFragment {
    private void renameDocuments(String newDisplayName) {
        BaseActivity activity = (BaseActivity) getActivity();

        if (isValidDocumentName(newDisplayName)) {
            new RenameDocumentsTask(activity, newDisplayName).execute(mDocument);
        } else {
        if (!isValidDocumentName(newDisplayName)) {
            Log.w(TAG, "Failed to rename file - invalid name:" + newDisplayName);
            Snackbars.makeSnackbar(getActivity(), R.string.rename_error,
                    Snackbar.LENGTH_SHORT).show();
        } else if (mHasFileNamed.test(newDisplayName)){
            mRenameInputWrapper.setError(getContext().getString(R.string.name_conflict));
            selectFileName(mEditText);
            Metrics.logRenameFileError(getContext());
        } else {
            new RenameDocumentsTask(activity, newDisplayName).execute(mDocument);
        }

    }
@@ -234,6 +249,9 @@ public class RenameDocumentFragment extends DialogFragment {
                Snackbars.showRenameFailed(mActivity);
                Metrics.logRenameFileError(getContext());
            }
            if (mDialog != null) {
                mDialog.dismiss();
            }
            mActivity.setPending(false);
        }
    }
Loading