<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package com.yasirkula.unity;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList;

public class NativeFilePickerSAFEntry {
    private static final String TAG = "DocumentFile";
    private Context mContext;
    private Uri mUri;

    public static NativeFilePickerSAFEntry fromTreeUri(Context context, Uri uri) {
        Uri buildDocumentUriUsingTree = DocumentsContract.buildDocumentUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri));
        if (buildDocumentUriUsingTree == null) {
            return null;
        }
        return new NativeFilePickerSAFEntry(context, buildDocumentUriUsingTree);
    }

    public NativeFilePickerSAFEntry(Context context, Uri uri) {
        this.mContext = context;
        this.mUri = uri;
    }

    public NativeFilePickerSAFEntry createFile(String str, String str2) {
        try {
            Uri createDocument = DocumentsContract.createDocument(this.mContext.getContentResolver(), this.mUri, str, str2);
            if (createDocument != null) {
                return new NativeFilePickerSAFEntry(this.mContext, createDocument);
            }
            return null;
        } catch (Exception e) {
            Log.e("Unity", "Exception:", e);
            return null;
        }
    }

    public NativeFilePickerSAFEntry createDirectory(String str) {
        try {
            Uri createDocument = DocumentsContract.createDocument(this.mContext.getContentResolver(), this.mUri, "vnd.android.document/directory", str);
            if (createDocument != null) {
                return new NativeFilePickerSAFEntry(this.mContext, createDocument);
            }
            return null;
        } catch (Exception e) {
            Log.e("Unity", "Exception:", e);
            return null;
        }
    }

    public Uri getUri() {
        return this.mUri;
    }

    public String getName() {
        return queryForString("_display_name", (String) null);
    }

    public String getType() {
        String rawType = getRawType();
        if ("vnd.android.document/directory".equals(rawType)) {
            return null;
        }
        return rawType;
    }

    public boolean isDirectory() {
        return "vnd.android.document/directory".equals(getRawType());
    }

    public boolean isFile() {
        String rawType = getRawType();
        return !"vnd.android.document/directory".equals(rawType) &amp;&amp; !TextUtils.isEmpty(rawType);
    }

    public long lastModified() {
        return queryForLong("last_modified", 0);
    }

    public long length() {
        return queryForLong("_size", 0);
    }

    public boolean canRead() {
        return this.mContext.checkCallingOrSelfUriPermission(this.mUri, 1) == 0 &amp;&amp; !TextUtils.isEmpty(getRawType());
    }

    public boolean canWrite() {
        if (this.mContext.checkCallingOrSelfUriPermission(this.mUri, 2) != 0) {
            return false;
        }
        String rawType = getRawType();
        int queryForInt = queryForInt("flags", 0);
        if (TextUtils.isEmpty(rawType)) {
            return false;
        }
        if ((queryForInt &amp; 4) != 0) {
            return true;
        }
        if ("vnd.android.document/directory".equals(rawType) &amp;&amp; (queryForInt &amp; 8) != 0) {
            return true;
        }
        if (TextUtils.isEmpty(rawType) || (queryForInt &amp; 2) == 0) {
            return false;
        }
        return true;
    }

    public boolean delete() {
        try {
            return DocumentsContract.deleteDocument(this.mContext.getContentResolver(), this.mUri);
        } catch (Exception e) {
            Log.e("Unity", "Exception:", e);
            return false;
        }
    }

    public boolean exists() {
        boolean z = false;
        Cursor cursor = null;
        try {
            Cursor query = this.mContext.getContentResolver().query(this.mUri, new String[]{"document_id"}, (String) null, (String[]) null, (String) null);
            if (query.getCount() &gt; 0) {
                z = true;
            }
            if (query != null) {
                try {
                    query.close();
                } catch (Exception e) {
                    Log.e(TAG, "Exception:", e);
                }
            }
            return z;
        } catch (Exception e2) {
            Log.w(TAG, "Failed query: " + e2);
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e3) {
                    Log.e(TAG, "Exception:", e3);
                }
            }
            return false;
        } catch (Throwable th) {
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e4) {
                    Log.e(TAG, "Exception:", e4);
                }
            }
            throw th;
        }
    }

    public ArrayList&lt;NativeFilePickerSAFEntry&gt; listFiles() {
        ContentResolver contentResolver = this.mContext.getContentResolver();
        Uri uri = this.mUri;
        Uri buildChildDocumentsUriUsingTree = DocumentsContract.buildChildDocumentsUriUsingTree(uri, DocumentsContract.getDocumentId(uri));
        ArrayList&lt;NativeFilePickerSAFEntry&gt; arrayList = new ArrayList&lt;&gt;();
        Cursor cursor = null;
        try {
            Cursor query = contentResolver.query(buildChildDocumentsUriUsingTree, new String[]{"document_id"}, (String) null, (String[]) null, (String) null);
            while (query.moveToNext()) {
                arrayList.add(new NativeFilePickerSAFEntry(this.mContext, DocumentsContract.buildDocumentUriUsingTree(this.mUri, query.getString(0))));
            }
            if (query != null) {
                try {
                    query.close();
                } catch (Exception e) {
                    Log.e(TAG, "Exception:", e);
                }
            }
        } catch (Exception e2) {
            Log.w("Unity", "Failed query: " + e2);
            if (cursor != null) {
                cursor.close();
            }
        } catch (Throwable th) {
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e3) {
                    Log.e(TAG, "Exception:", e3);
                }
            }
            throw th;
        }
        return arrayList;
    }

    public String renameTo(String str) {
        try {
            Uri renameDocument = DocumentsContract.renameDocument(this.mContext.getContentResolver(), this.mUri, str);
            if (renameDocument != null) {
                this.mUri = renameDocument;
            }
        } catch (Exception e) {
            Log.e("Unity", "Exception:", e);
        }
        return this.mUri.toString();
    }

    private String getRawType() {
        return queryForString("mime_type", (String) null);
    }

    private String queryForString(String str, String str2) {
        ContentResolver contentResolver = this.mContext.getContentResolver();
        Cursor cursor = null;
        try {
            Cursor query = contentResolver.query(this.mUri, new String[]{str}, (String) null, (String[]) null, (String) null);
            if (!query.moveToFirst() || query.isNull(0)) {
                if (query != null) {
                    try {
                        query.close();
                    } catch (Exception e) {
                        Log.e(TAG, "Exception:", e);
                    }
                }
                return str2;
            }
            String string = query.getString(0);
            if (query != null) {
                try {
                    query.close();
                } catch (Exception e2) {
                    Log.e(TAG, "Exception:", e2);
                }
            }
            return string;
        } catch (Exception e3) {
            Log.w(TAG, "Failed query: " + e3);
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e4) {
                    Log.e(TAG, "Exception:", e4);
                }
            }
            return str2;
        } catch (Throwable th) {
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e5) {
                    Log.e(TAG, "Exception:", e5);
                }
            }
            throw th;
        }
    }

    private int queryForInt(String str, int i) {
        return (int) queryForLong(str, (long) i);
    }

    private long queryForLong(String str, long j) {
        ContentResolver contentResolver = this.mContext.getContentResolver();
        Cursor cursor = null;
        try {
            Cursor query = contentResolver.query(this.mUri, new String[]{str}, (String) null, (String[]) null, (String) null);
            if (!query.moveToFirst() || query.isNull(0)) {
                if (query != null) {
                    try {
                        query.close();
                    } catch (Exception e) {
                        Log.e(TAG, "Exception:", e);
                    }
                }
                return j;
            }
            long j2 = query.getLong(0);
            if (query != null) {
                try {
                    query.close();
                } catch (Exception e2) {
                    Log.e(TAG, "Exception:", e2);
                }
            }
            return j2;
        } catch (Exception e3) {
            Log.w(TAG, "Failed query: " + e3);
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e4) {
                    Log.e(TAG, "Exception:", e4);
                }
            }
            return j;
        } catch (Throwable th) {
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e5) {
                    Log.e(TAG, "Exception:", e5);
                }
            }
            throw th;
        }
    }
}
</pre></body></html>