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

Commit 3d9f3fcc authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Handle runtime exception in clip data

When invoking ContentProvider through binder, ContentProvider may return
any type of RuntimeException. To let ClipData fall back to URI when it
failes to obtain text explanation, the CL add a catch closure for
RuntimeException.

Bug: 37260995
Test: Manually
Change-Id: I54ffbbcb05be3f3ff1ebc1c3dc68c074e6eb9825
parent 1ce83f00
Loading
Loading
Loading
Loading
+31 −30
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import libcore.io.IoUtils;

/**
 * Representation of a clipped data on the clipboard.
@@ -335,46 +336,46 @@ public class ClipData implements Parcelable {
            // If this Item has a URI value, try using that.
            Uri uri = getUri();
            if (uri != null) {

                // First see if the URI can be opened as a plain text stream
                // (of any sub-type).  If so, this is the best textual
                // representation for it.
                final ContentResolver resolver = context.getContentResolver();
                AssetFileDescriptor descr = null;
                FileInputStream stream = null;
                InputStreamReader reader = null;
                try {
                    try {
                        // Ask for a stream of the desired type.
                    AssetFileDescriptor descr = context.getContentResolver()
                            .openTypedAssetFileDescriptor(uri, "text/*", null);
                        descr = resolver.openTypedAssetFileDescriptor(uri, "text/*", null);
                    } catch (SecurityException e) {
                        Log.w("ClipData", "Failure opening stream", e);
                    } catch (FileNotFoundException|RuntimeException e) {
                        // Unable to open content URI as text...  not really an
                        // error, just something to ignore.
                    }
                    if (descr != null) {
                        try {
                            stream = descr.createInputStream();
                    InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
                            reader = new InputStreamReader(stream, "UTF-8");

                            // Got it...  copy the stream into a local string and return it.
                    StringBuilder builder = new StringBuilder(128);
                            final StringBuilder builder = new StringBuilder(128);
                            char[] buffer = new char[8192];
                            int len;
                            while ((len=reader.read(buffer)) > 0) {
                                builder.append(buffer, 0, len);
                            }
                            return builder.toString();

                } catch (SecurityException e) {
                    Log.w("ClipData", "Failure opening stream", e);

                } catch (FileNotFoundException e) {
                    // Unable to open content URI as text...  not really an
                    // error, just something to ignore.

                        } catch (IOException e) {
                            // Something bad has happened.
                            Log.w("ClipData", "Failure loading text", e);
                            return e.toString();

                } finally {
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (IOException e) {
                        }
                    }
                } finally {
                    IoUtils.closeQuietly(descr);
                    IoUtils.closeQuietly(stream);
                    IoUtils.closeQuietly(reader);
                }

                // If we couldn't open the URI as a stream, use the URI itself as a textual