IAFileProvider.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package epson.provider;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.content.pm.ProviderInfo;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.os.ParcelFileDescriptor;
  8. import androidx.core.content.FileProvider;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. public abstract class IAFileProvider extends FileProvider {
  12. private static String mAuthority;
  13. public boolean onCreate() {
  14. return true;
  15. }
  16. public void attachInfo(Context context, ProviderInfo providerInfo) {
  17. super.attachInfo(context, providerInfo);
  18. mAuthority = providerInfo.authority;
  19. }
  20. public ParcelFileDescriptor openFile(Uri uri, String str) throws FileNotFoundException {
  21. if (checkUriPath(uri)) {
  22. return super.openFile(uri, str);
  23. }
  24. throw new FileNotFoundException();
  25. }
  26. public static Uri getUriForFile(Context context, File file) throws FileNotFoundException {
  27. return getUriForFile(context, mAuthority, file);
  28. }
  29. public String getType(Uri uri) {
  30. if (!checkUriPath(uri)) {
  31. return null;
  32. }
  33. return super.getType(uri);
  34. }
  35. public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
  36. if (!checkUriPath(uri)) {
  37. return null;
  38. }
  39. return super.query(uri, strArr, str, strArr2, str2);
  40. }
  41. static boolean checkUriPath(Uri uri) {
  42. if (uri == null) {
  43. return true;
  44. }
  45. String path = uri.getPath();
  46. if (path.contains("/../") || path.endsWith("/..")) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. public Uri insert(Uri uri, ContentValues contentValues) {
  52. throw new UnsupportedOperationException("No external insert");
  53. }
  54. public int delete(Uri uri, String str, String[] strArr) {
  55. throw new UnsupportedOperationException("No external delete");
  56. }
  57. public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) {
  58. throw new UnsupportedOperationException("No external updates");
  59. }
  60. }