package com.yanzhenjie.permission; import android.content.ContentProvider; import android.content.ContentValues; import android.content.Context; import android.content.pm.ProviderInfo; import android.content.res.XmlResourceParser; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.os.ParcelFileDescriptor; import android.text.TextUtils; import android.webkit.MimeTypeMap; import com.liulishuo.filedownloader.model.FileDownloadModel; import com.ubtrobot.jimu.robotapi.PeripheralType; import com.unity3d.ads.metadata.MediationMetaData; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import org.xmlpull.v1.XmlPullParserException; /* loaded from: classes2.dex */ public class FileProvider extends ContentProvider { private static final String[] b = {"_display_name", "_size"}; private static final File c = new File("/"); private static final HashMap d = new HashMap<>(); private PathStrategy a; interface PathStrategy { File a(Uri uri); } private static int a(String str) { if ("r".equals(str)) { return 268435456; } if ("w".equals(str) || "wt".equals(str)) { return 738197504; } if ("wa".equals(str)) { return 704643072; } if ("rw".equals(str)) { return 939524096; } if ("rwt".equals(str)) { return 1006632960; } throw new IllegalArgumentException("Invalid mode: " + str); } private static PathStrategy b(Context context, String str) { PathStrategy pathStrategy; synchronized (d) { pathStrategy = d.get(str); if (pathStrategy == null) { try { pathStrategy = c(context, str); d.put(str, pathStrategy); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse android.support.FILE_PROVIDER_PATHS meta-data", e); } catch (XmlPullParserException e2) { throw new IllegalArgumentException("Failed to parse android.support.FILE_PROVIDER_PATHS meta-data", e2); } } } return pathStrategy; } private static PathStrategy c(Context context, String str) throws IOException, XmlPullParserException { SimplePathStrategy simplePathStrategy = new SimplePathStrategy(str); XmlResourceParser loadXmlMetaData = context.getPackageManager().resolveContentProvider(str, PeripheralType.SERVO).loadXmlMetaData(context.getPackageManager(), "android.support.FILE_PROVIDER_PATHS"); if (loadXmlMetaData == null) { throw new IllegalArgumentException("Missing android.support.FILE_PROVIDER_PATHS meta-data"); } while (true) { int next = loadXmlMetaData.next(); if (next == 1) { return simplePathStrategy; } if (next == 2) { String name = loadXmlMetaData.getName(); File file = null; String attributeValue = loadXmlMetaData.getAttributeValue(null, MediationMetaData.KEY_NAME); String attributeValue2 = loadXmlMetaData.getAttributeValue(null, FileDownloadModel.PATH); if ("root-path".equals(name)) { file = c; } else if ("files-path".equals(name)) { file = context.getFilesDir(); } else if ("cache-path".equals(name)) { file = context.getCacheDir(); } else if ("external-path".equals(name)) { file = Environment.getExternalStorageDirectory(); } else if ("external-files-path".equals(name)) { File[] a = a(context, (String) null); if (a.length > 0) { file = a[0]; } } else if ("external-cache-path".equals(name)) { File[] a2 = a(context); if (a2.length > 0) { file = a2[0]; } } else if (Build.VERSION.SDK_INT >= 21 && "external-media-path".equals(name)) { File[] externalMediaDirs = context.getExternalMediaDirs(); if (externalMediaDirs.length > 0) { file = externalMediaDirs[0]; } } if (file != null) { simplePathStrategy.a(attributeValue, a(file, attributeValue2)); } } } } @Override // android.content.ContentProvider public void attachInfo(Context context, ProviderInfo providerInfo) { super.attachInfo(context, providerInfo); if (providerInfo.exported) { throw new SecurityException("Provider must not be exported"); } if (!providerInfo.grantUriPermissions) { throw new SecurityException("Provider must grant uri permissions"); } this.a = b(context, providerInfo.authority); } @Override // android.content.ContentProvider public int delete(Uri uri, String str, String[] strArr) { return this.a.a(uri).delete() ? 1 : 0; } @Override // android.content.ContentProvider public String getType(Uri uri) { File a = this.a.a(uri); int lastIndexOf = a.getName().lastIndexOf(46); if (lastIndexOf < 0) { return "application/octet-stream"; } String mimeTypeFromExtension = MimeTypeMap.getSingleton().getMimeTypeFromExtension(a.getName().substring(lastIndexOf + 1)); return mimeTypeFromExtension != null ? mimeTypeFromExtension : "application/octet-stream"; } @Override // android.content.ContentProvider public Uri insert(Uri uri, ContentValues contentValues) { throw new UnsupportedOperationException("No external inserts"); } @Override // android.content.ContentProvider public boolean onCreate() { return true; } @Override // android.content.ContentProvider public ParcelFileDescriptor openFile(Uri uri, String str) throws FileNotFoundException { return ParcelFileDescriptor.open(this.a.a(uri), a(str)); } @Override // android.content.ContentProvider public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) { int i; File a = this.a.a(uri); if (strArr == null) { strArr = b; } String[] strArr3 = new String[strArr.length]; Object[] objArr = new Object[strArr.length]; int i2 = 0; for (String str3 : strArr) { if ("_display_name".equals(str3)) { strArr3[i2] = "_display_name"; i = i2 + 1; objArr[i2] = a.getName(); } else if ("_size".equals(str3)) { strArr3[i2] = "_size"; i = i2 + 1; objArr[i2] = Long.valueOf(a.length()); } i2 = i; } String[] a2 = a(strArr3, i2); Object[] a3 = a(objArr, i2); MatrixCursor matrixCursor = new MatrixCursor(a2, 1); matrixCursor.addRow(a3); return matrixCursor; } @Override // android.content.ContentProvider public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) { throw new UnsupportedOperationException("No external updates"); } static class SimplePathStrategy implements PathStrategy { private final HashMap a = new HashMap<>(); SimplePathStrategy(String str) { } void a(String str, File file) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("Name must not be empty"); } try { this.a.put(str, file.getCanonicalFile()); } catch (IOException e) { throw new IllegalArgumentException("Failed to resolve canonical path for " + file, e); } } @Override // com.yanzhenjie.permission.FileProvider.PathStrategy public File a(Uri uri) { String encodedPath = uri.getEncodedPath(); int indexOf = encodedPath.indexOf(47, 1); String decode = Uri.decode(encodedPath.substring(1, indexOf)); String decode2 = Uri.decode(encodedPath.substring(indexOf + 1)); File file = this.a.get(decode); if (file != null) { File file2 = new File(file, decode2); try { File canonicalFile = file2.getCanonicalFile(); if (canonicalFile.getPath().startsWith(file.getPath())) { return canonicalFile; } throw new SecurityException("Resolved path jumped beyond configured root"); } catch (IOException unused) { throw new IllegalArgumentException("Failed to resolve canonical path for " + file2); } } throw new IllegalArgumentException("Unable to find configured root for " + uri); } } private static File a(File file, String... strArr) { for (String str : strArr) { if (str != null) { file = new File(file, str); } } return file; } private static String[] a(String[] strArr, int i) { String[] strArr2 = new String[i]; System.arraycopy(strArr, 0, strArr2, 0, i); return strArr2; } private static Object[] a(Object[] objArr, int i) { Object[] objArr2 = new Object[i]; System.arraycopy(objArr, 0, objArr2, 0, i); return objArr2; } private static File[] a(Context context, String str) { return Build.VERSION.SDK_INT >= 19 ? context.getExternalFilesDirs(str) : new File[]{context.getExternalFilesDir(str)}; } public static File[] a(Context context) { return Build.VERSION.SDK_INT >= 19 ? context.getExternalCacheDirs() : new File[]{context.getExternalCacheDir()}; } }