Search in sources :

Example 1 with DragAndDropPermissions

use of android.view.DragAndDropPermissions in project platform_frameworks_base by android.

the class Editor method onDrop.

void onDrop(DragEvent event) {
    StringBuilder content = new StringBuilder("");
    final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event);
    if (permissions != null) {
        permissions.takeTransient();
    }
    try {
        ClipData clipData = event.getClipData();
        final int itemCount = clipData.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            Item item = clipData.getItemAt(i);
            content.append(item.coerceToStyledText(mTextView.getContext()));
        }
    } finally {
        if (permissions != null) {
            permissions.release();
        }
    }
    final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
    Object localState = event.getLocalState();
    DragLocalState dragLocalState = null;
    if (localState instanceof DragLocalState) {
        dragLocalState = (DragLocalState) localState;
    }
    boolean dragDropIntoItself = dragLocalState != null && dragLocalState.sourceTextView == mTextView;
    if (dragDropIntoItself) {
        if (offset >= dragLocalState.start && offset < dragLocalState.end) {
            // A drop inside the original selection discards the drop.
            return;
        }
    }
    final int originalLength = mTextView.getText().length();
    int min = offset;
    int max = offset;
    Selection.setSelection((Spannable) mTextView.getText(), max);
    mTextView.replaceText_internal(min, max, content);
    if (dragDropIntoItself) {
        int dragSourceStart = dragLocalState.start;
        int dragSourceEnd = dragLocalState.end;
        if (max <= dragSourceStart) {
            // Inserting text before selection has shifted positions
            final int shift = mTextView.getText().length() - originalLength;
            dragSourceStart += shift;
            dragSourceEnd += shift;
        }
        mUndoInputFilter.setForceMerge(true);
        try {
            // Delete original selection
            mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
            // Make sure we do not leave two adjacent spaces.
            final int prevCharIdx = Math.max(0, dragSourceStart - 1);
            final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
            if (nextCharIdx > prevCharIdx + 1) {
                CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
                if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
                    mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
                }
            }
        } finally {
            mUndoInputFilter.setForceMerge(false);
        }
    }
}
Also used : Item(android.content.ClipData.Item) MenuItem(android.view.MenuItem) SpannableStringBuilder(android.text.SpannableStringBuilder) DragAndDropPermissions(android.view.DragAndDropPermissions) ClipData(android.content.ClipData) Paint(android.graphics.Paint)

Example 2 with DragAndDropPermissions

use of android.view.DragAndDropPermissions in project android_frameworks_base by crdroidandroid.

the class Editor method onDrop.

void onDrop(DragEvent event) {
    StringBuilder content = new StringBuilder("");
    final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event);
    if (permissions != null) {
        permissions.takeTransient();
    }
    try {
        ClipData clipData = event.getClipData();
        final int itemCount = clipData.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            Item item = clipData.getItemAt(i);
            content.append(item.coerceToStyledText(mTextView.getContext()));
        }
    } finally {
        if (permissions != null) {
            permissions.release();
        }
    }
    final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
    Object localState = event.getLocalState();
    DragLocalState dragLocalState = null;
    if (localState instanceof DragLocalState) {
        dragLocalState = (DragLocalState) localState;
    }
    boolean dragDropIntoItself = dragLocalState != null && dragLocalState.sourceTextView == mTextView;
    if (dragDropIntoItself) {
        if (offset >= dragLocalState.start && offset < dragLocalState.end) {
            // A drop inside the original selection discards the drop.
            return;
        }
    }
    final int originalLength = mTextView.getText().length();
    int min = offset;
    int max = offset;
    Selection.setSelection((Spannable) mTextView.getText(), max);
    mTextView.replaceText_internal(min, max, content);
    if (dragDropIntoItself) {
        int dragSourceStart = dragLocalState.start;
        int dragSourceEnd = dragLocalState.end;
        if (max <= dragSourceStart) {
            // Inserting text before selection has shifted positions
            final int shift = mTextView.getText().length() - originalLength;
            dragSourceStart += shift;
            dragSourceEnd += shift;
        }
        mUndoInputFilter.setForceMerge(true);
        try {
            // Delete original selection
            mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
            // Make sure we do not leave two adjacent spaces.
            final int prevCharIdx = Math.max(0, dragSourceStart - 1);
            final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
            if (nextCharIdx > prevCharIdx + 1) {
                CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
                if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
                    mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
                }
            }
        } finally {
            mUndoInputFilter.setForceMerge(false);
        }
    }
}
Also used : Item(android.content.ClipData.Item) MenuItem(android.view.MenuItem) SpannableStringBuilder(android.text.SpannableStringBuilder) DragAndDropPermissions(android.view.DragAndDropPermissions) ClipData(android.content.ClipData) Paint(android.graphics.Paint)

Example 3 with DragAndDropPermissions

use of android.view.DragAndDropPermissions in project android_frameworks_base by AOSPA.

the class Editor method onDrop.

void onDrop(DragEvent event) {
    StringBuilder content = new StringBuilder("");
    final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event);
    if (permissions != null) {
        permissions.takeTransient();
    }
    try {
        ClipData clipData = event.getClipData();
        final int itemCount = clipData.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            Item item = clipData.getItemAt(i);
            content.append(item.coerceToStyledText(mTextView.getContext()));
        }
    } finally {
        if (permissions != null) {
            permissions.release();
        }
    }
    final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
    Object localState = event.getLocalState();
    DragLocalState dragLocalState = null;
    if (localState instanceof DragLocalState) {
        dragLocalState = (DragLocalState) localState;
    }
    boolean dragDropIntoItself = dragLocalState != null && dragLocalState.sourceTextView == mTextView;
    if (dragDropIntoItself) {
        if (offset >= dragLocalState.start && offset < dragLocalState.end) {
            // A drop inside the original selection discards the drop.
            return;
        }
    }
    final int originalLength = mTextView.getText().length();
    int min = offset;
    int max = offset;
    Selection.setSelection((Spannable) mTextView.getText(), max);
    mTextView.replaceText_internal(min, max, content);
    if (dragDropIntoItself) {
        int dragSourceStart = dragLocalState.start;
        int dragSourceEnd = dragLocalState.end;
        if (max <= dragSourceStart) {
            // Inserting text before selection has shifted positions
            final int shift = mTextView.getText().length() - originalLength;
            dragSourceStart += shift;
            dragSourceEnd += shift;
        }
        mUndoInputFilter.setForceMerge(true);
        try {
            // Delete original selection
            mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
            // Make sure we do not leave two adjacent spaces.
            final int prevCharIdx = Math.max(0, dragSourceStart - 1);
            final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
            if (nextCharIdx > prevCharIdx + 1) {
                CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
                if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
                    mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
                }
            }
        } finally {
            mUndoInputFilter.setForceMerge(false);
        }
    }
}
Also used : Item(android.content.ClipData.Item) MenuItem(android.view.MenuItem) SpannableStringBuilder(android.text.SpannableStringBuilder) DragAndDropPermissions(android.view.DragAndDropPermissions) ClipData(android.content.ClipData) Paint(android.graphics.Paint)

Example 4 with DragAndDropPermissions

use of android.view.DragAndDropPermissions in project android_frameworks_base by DirtyUnicorns.

the class Editor method onDrop.

void onDrop(DragEvent event) {
    StringBuilder content = new StringBuilder("");
    final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event);
    if (permissions != null) {
        permissions.takeTransient();
    }
    try {
        ClipData clipData = event.getClipData();
        final int itemCount = clipData.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            Item item = clipData.getItemAt(i);
            content.append(item.coerceToStyledText(mTextView.getContext()));
        }
    } finally {
        if (permissions != null) {
            permissions.release();
        }
    }
    final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
    Object localState = event.getLocalState();
    DragLocalState dragLocalState = null;
    if (localState instanceof DragLocalState) {
        dragLocalState = (DragLocalState) localState;
    }
    boolean dragDropIntoItself = dragLocalState != null && dragLocalState.sourceTextView == mTextView;
    if (dragDropIntoItself) {
        if (offset >= dragLocalState.start && offset < dragLocalState.end) {
            // A drop inside the original selection discards the drop.
            return;
        }
    }
    final int originalLength = mTextView.getText().length();
    int min = offset;
    int max = offset;
    Selection.setSelection((Spannable) mTextView.getText(), max);
    mTextView.replaceText_internal(min, max, content);
    if (dragDropIntoItself) {
        int dragSourceStart = dragLocalState.start;
        int dragSourceEnd = dragLocalState.end;
        if (max <= dragSourceStart) {
            // Inserting text before selection has shifted positions
            final int shift = mTextView.getText().length() - originalLength;
            dragSourceStart += shift;
            dragSourceEnd += shift;
        }
        mUndoInputFilter.setForceMerge(true);
        try {
            // Delete original selection
            mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
            // Make sure we do not leave two adjacent spaces.
            final int prevCharIdx = Math.max(0, dragSourceStart - 1);
            final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
            if (nextCharIdx > prevCharIdx + 1) {
                CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
                if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
                    mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
                }
            }
        } finally {
            mUndoInputFilter.setForceMerge(false);
        }
    }
}
Also used : Item(android.content.ClipData.Item) MenuItem(android.view.MenuItem) SpannableStringBuilder(android.text.SpannableStringBuilder) DragAndDropPermissions(android.view.DragAndDropPermissions) ClipData(android.content.ClipData) Paint(android.graphics.Paint)

Example 5 with DragAndDropPermissions

use of android.view.DragAndDropPermissions in project android_frameworks_base by ResurrectionRemix.

the class Editor method onDrop.

void onDrop(DragEvent event) {
    StringBuilder content = new StringBuilder("");
    final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event);
    if (permissions != null) {
        permissions.takeTransient();
    }
    try {
        ClipData clipData = event.getClipData();
        final int itemCount = clipData.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            Item item = clipData.getItemAt(i);
            content.append(item.coerceToStyledText(mTextView.getContext()));
        }
    } finally {
        if (permissions != null) {
            permissions.release();
        }
    }
    final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
    Object localState = event.getLocalState();
    DragLocalState dragLocalState = null;
    if (localState instanceof DragLocalState) {
        dragLocalState = (DragLocalState) localState;
    }
    boolean dragDropIntoItself = dragLocalState != null && dragLocalState.sourceTextView == mTextView;
    if (dragDropIntoItself) {
        if (offset >= dragLocalState.start && offset < dragLocalState.end) {
            // A drop inside the original selection discards the drop.
            return;
        }
    }
    final int originalLength = mTextView.getText().length();
    int min = offset;
    int max = offset;
    Selection.setSelection((Spannable) mTextView.getText(), max);
    mTextView.replaceText_internal(min, max, content);
    if (dragDropIntoItself) {
        int dragSourceStart = dragLocalState.start;
        int dragSourceEnd = dragLocalState.end;
        if (max <= dragSourceStart) {
            // Inserting text before selection has shifted positions
            final int shift = mTextView.getText().length() - originalLength;
            dragSourceStart += shift;
            dragSourceEnd += shift;
        }
        mUndoInputFilter.setForceMerge(true);
        try {
            // Delete original selection
            mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
            // Make sure we do not leave two adjacent spaces.
            final int prevCharIdx = Math.max(0, dragSourceStart - 1);
            final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
            if (nextCharIdx > prevCharIdx + 1) {
                CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
                if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
                    mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
                }
            }
        } finally {
            mUndoInputFilter.setForceMerge(false);
        }
    }
}
Also used : Item(android.content.ClipData.Item) MenuItem(android.view.MenuItem) SpannableStringBuilder(android.text.SpannableStringBuilder) DragAndDropPermissions(android.view.DragAndDropPermissions) ClipData(android.content.ClipData) Paint(android.graphics.Paint)

Aggregations

ClipData (android.content.ClipData)5 Item (android.content.ClipData.Item)5 Paint (android.graphics.Paint)5 SpannableStringBuilder (android.text.SpannableStringBuilder)5 DragAndDropPermissions (android.view.DragAndDropPermissions)5 MenuItem (android.view.MenuItem)5