package androidx.documentfile.provider; 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; /* loaded from: classes.dex */ class DocumentsContractApi19 { private static final int FLAG_VIRTUAL_DOCUMENT = 512; private static final String TAG = "DocumentFile"; public static boolean isVirtual(Context context, Uri uri) { return DocumentsContract.isDocumentUri(context, uri) && (getFlags(context, uri) & 512) != 0; } public static String getName(Context context, Uri uri) { return queryForString(context, uri, "_display_name", null); } private static String getRawType(Context context, Uri uri) { return queryForString(context, uri, "mime_type", null); } public static String getType(Context context, Uri uri) { String rawType = getRawType(context, uri); if ("vnd.android.document/directory".equals(rawType)) { return null; } return rawType; } public static long getFlags(Context context, Uri uri) { return queryForLong(context, uri, "flags", 0L); } public static boolean isDirectory(Context context, Uri uri) { return "vnd.android.document/directory".equals(getRawType(context, uri)); } public static boolean isFile(Context context, Uri uri) { String rawType = getRawType(context, uri); return ("vnd.android.document/directory".equals(rawType) || TextUtils.isEmpty(rawType)) ? false : true; } public static long lastModified(Context context, Uri uri) { return queryForLong(context, uri, "last_modified", 0L); } public static long length(Context context, Uri uri) { return queryForLong(context, uri, "_size", 0L); } public static boolean canRead(Context context, Uri uri) { return context.checkCallingOrSelfUriPermission(uri, 1) == 0 && !TextUtils.isEmpty(getRawType(context, uri)); } public static boolean canWrite(Context context, Uri uri) { if (context.checkCallingOrSelfUriPermission(uri, 2) != 0) { return false; } String rawType = getRawType(context, uri); int queryForInt = queryForInt(context, uri, "flags", 0); if (TextUtils.isEmpty(rawType)) { return false; } if ((queryForInt & 4) != 0) { return true; } if (!"vnd.android.document/directory".equals(rawType) || (queryForInt & 8) == 0) { return (TextUtils.isEmpty(rawType) || (queryForInt & 2) == 0) ? false : true; } return true; } public static boolean exists(Context context, Uri uri) { Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, new String[]{"document_id"}, null, null, null); return cursor.getCount() > 0; } catch (Exception e) { Log.w(TAG, "Failed query: " + e); return false; } finally { closeQuietly(cursor); } } private static String queryForString(Context context, Uri uri, String str, String str2) { ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { cursor = contentResolver.query(uri, new String[]{str}, null, null, null); return (!cursor.moveToFirst() || cursor.isNull(0)) ? str2 : cursor.getString(0); } catch (Exception e) { Log.w(TAG, "Failed query: " + e); return str2; } finally { closeQuietly(cursor); } } private static int queryForInt(Context context, Uri uri, String str, int i) { return (int) queryForLong(context, uri, str, i); } private static long queryForLong(Context context, Uri uri, String str, long j) { ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { cursor = contentResolver.query(uri, new String[]{str}, null, null, null); return (!cursor.moveToFirst() || cursor.isNull(0)) ? j : cursor.getLong(0); } catch (Exception e) { Log.w(TAG, "Failed query: " + e); return j; } finally { closeQuietly(cursor); } } private static void closeQuietly(AutoCloseable autoCloseable) { if (autoCloseable != null) { try { autoCloseable.close(); } catch (RuntimeException e) { throw e; } catch (Exception unused) { } } } private DocumentsContractApi19() { } }