Java tutorial
package android.support.v4.content; 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.net.Uri.Builder; import android.os.Environment; import android.os.ParcelFileDescriptor; import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; import android.text.TextUtils; import android.webkit.MimeTypeMap; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map.Entry; import org.apache.http.protocol.HTTP; import org.xmlpull.v1.XmlPullParserException; public class FileProvider extends ContentProvider { private static final String ATTR_NAME = "name"; private static final String ATTR_PATH = "path"; private static final String[] COLUMNS; private static final File DEVICE_ROOT; private static final String META_DATA_FILE_PROVIDER_PATHS = "android.support.FILE_PROVIDER_PATHS"; private static final String TAG_CACHE_PATH = "cache-path"; private static final String TAG_EXTERNAL = "external-path"; private static final String TAG_FILES_PATH = "files-path"; private static final String TAG_ROOT_PATH = "root-path"; private static HashMap<String, PathStrategy> sCache; private PathStrategy mStrategy; interface PathStrategy { File getFileForUri(Uri uri); Uri getUriForFile(File file); } static class SimplePathStrategy implements PathStrategy { private final String mAuthority; private final HashMap<String, File> mRoots; public SimplePathStrategy(String str) { this.mRoots = new HashMap(); this.mAuthority = str; } public void addRoot(String str, File file) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("Name must not be empty"); } try { this.mRoots.put(str, file.getCanonicalFile()); } catch (Throwable e) { throw new IllegalArgumentException("Failed to resolve canonical path for " + file, e); } } public Uri getUriForFile(File file) { try { String path; String canonicalPath = file.getCanonicalPath(); Entry entry = null; for (Entry entry2 : this.mRoots.entrySet()) { Entry entry22; path = ((File) entry22.getValue()).getPath(); if (!canonicalPath.startsWith(path) || (entry != null && path.length() <= ((File) entry.getValue()).getPath().length())) { entry22 = entry; } entry = entry22; } if (entry == null) { throw new IllegalArgumentException( "Failed to find configured root that contains " + canonicalPath); } String path2 = ((File) entry.getValue()).getPath(); if (path2.endsWith("/")) { path = canonicalPath.substring(path2.length()); } else { path = canonicalPath.substring(path2.length() + 1); } return new Builder().scheme("content").authority(this.mAuthority) .encodedPath(Uri.encode((String) entry.getKey()) + '/' + Uri.encode(path, "/")).build(); } catch (IOException e) { throw new IllegalArgumentException("Failed to resolve canonical path for " + file); } } public File getFileForUri(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 = (File) this.mRoots.get(decode); if (file == null) { throw new IllegalArgumentException("Unable to find configured root for " + uri); } 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 e) { throw new IllegalArgumentException("Failed to resolve canonical path for " + file2); } } } static { COLUMNS = new String[] { "_display_name", "_size" }; DEVICE_ROOT = new File("/"); sCache = new HashMap(); } public boolean onCreate() { return true; } public void attachInfo(Context context, ProviderInfo providerInfo) { super.attachInfo(context, providerInfo); if (providerInfo.exported) { throw new SecurityException("Provider must not be exported"); } else if (providerInfo.grantUriPermissions) { this.mStrategy = getPathStrategy(context, providerInfo.authority); } else { throw new SecurityException("Provider must grant uri permissions"); } } public static Uri getUriForFile(Context context, String str, File file) { return getPathStrategy(context, str).getUriForFile(file); } public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) { File fileForUri = this.mStrategy.getFileForUri(uri); if (strArr == null) { strArr = COLUMNS; } String[] strArr3 = new String[strArr.length]; Object[] objArr = new Object[strArr.length]; int length = strArr.length; int i = 0; int i2 = 0; while (i < length) { int i3; Object obj = strArr[i]; if ("_display_name".equals(obj)) { strArr3[i2] = "_display_name"; i3 = i2 + 1; objArr[i2] = fileForUri.getName(); } else if ("_size".equals(obj)) { strArr3[i2] = "_size"; i3 = i2 + 1; objArr[i2] = Long.valueOf(fileForUri.length()); } else { i3 = i2; } i++; i2 = i3; } String[] copyOf = copyOf(strArr3, i2); Object[] copyOf2 = copyOf(objArr, i2); Cursor matrixCursor = new MatrixCursor(copyOf, 1); matrixCursor.addRow(copyOf2); return matrixCursor; } public String getType(Uri uri) { File fileForUri = this.mStrategy.getFileForUri(uri); int lastIndexOf = fileForUri.getName().lastIndexOf(46); if (lastIndexOf >= 0) { String mimeTypeFromExtension = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(fileForUri.getName().substring(lastIndexOf + 1)); if (mimeTypeFromExtension != null) { return mimeTypeFromExtension; } } return HTTP.OCTET_STREAM_TYPE; } public Uri insert(Uri uri, ContentValues contentValues) { throw new UnsupportedOperationException("No external inserts"); } public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) { throw new UnsupportedOperationException("No external updates"); } public int delete(Uri uri, String str, String[] strArr) { return this.mStrategy.getFileForUri(uri).delete() ? 1 : 0; } public ParcelFileDescriptor openFile(Uri uri, String str) throws FileNotFoundException { return ParcelFileDescriptor.open(this.mStrategy.getFileForUri(uri), modeToMode(str)); } private static PathStrategy getPathStrategy(Context context, String str) { PathStrategy pathStrategy; synchronized (sCache) { pathStrategy = (PathStrategy) sCache.get(str); if (pathStrategy == null) { try { pathStrategy = parsePathStrategy(context, str); sCache.put(str, pathStrategy); } catch (Throwable e) { throw new IllegalArgumentException( "Failed to parse android.support.FILE_PROVIDER_PATHS meta-data", e); } catch (Throwable e2) { throw new IllegalArgumentException( "Failed to parse android.support.FILE_PROVIDER_PATHS meta-data", e2); } } } return pathStrategy; } private static PathStrategy parsePathStrategy(Context context, String str) throws IOException, XmlPullParserException { PathStrategy simplePathStrategy = new SimplePathStrategy(str); XmlResourceParser loadXmlMetaData = context.getPackageManager() .resolveContentProvider(str, AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS) .loadXmlMetaData(context.getPackageManager(), META_DATA_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) { File buildPath; String name = loadXmlMetaData.getName(); String attributeValue = loadXmlMetaData.getAttributeValue(null, ATTR_NAME); String attributeValue2 = loadXmlMetaData.getAttributeValue(null, ATTR_PATH); if (TAG_ROOT_PATH.equals(name)) { buildPath = buildPath(DEVICE_ROOT, attributeValue2); } else if (TAG_FILES_PATH.equals(name)) { buildPath = buildPath(context.getFilesDir(), attributeValue2); } else if (TAG_CACHE_PATH.equals(name)) { buildPath = buildPath(context.getCacheDir(), attributeValue2); } else if (TAG_EXTERNAL.equals(name)) { buildPath = buildPath(Environment.getExternalStorageDirectory(), attributeValue2); } else { buildPath = null; } if (buildPath != null) { simplePathStrategy.addRoot(attributeValue, buildPath); } } } } private static int modeToMode(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 File buildPath(File file, String... strArr) { int length = strArr.length; int i = 0; File file2 = file; while (i < length) { File file3; String str = strArr[i]; if (str != null) { file3 = new File(file2, str); } else { file3 = file2; } i++; file2 = file3; } return file2; } private static String[] copyOf(String[] strArr, int i) { Object obj = new String[i]; System.arraycopy(strArr, 0, obj, 0, i); return obj; } private static Object[] copyOf(Object[] objArr, int i) { Object obj = new Object[i]; System.arraycopy(objArr, 0, obj, 0, i); return obj; } }