/**
   * Implementation is provided by the parent class. Can be overridden to provide additional
   * functionality, but subclasses <em>must</em> always call the superclass. If the superclass
   * returns {@code null}, the subclass may implement custom behavior.
   *
   * @see #openDocument(String, String, CancellationSignal)
   * @see #deleteDocument(String)
   */
  @Override
  public Bundle call(String method, String arg, Bundle extras) {
    final Context context = getContext();

    if (!method.startsWith("android:")) {
      // Let non-platform methods pass through
      return super.call(method, arg, extras);
    }

    final String documentId = extras.getString(Document.COLUMN_DOCUMENT_ID);
    final Uri documentUri = DocumentsContract.buildDocumentUri(mAuthority, documentId);

    // Require that caller can manage requested document
    final boolean callerHasManage =
        context.checkCallingOrSelfPermission(android.Manifest.permission.MANAGE_DOCUMENTS)
            == PackageManager.PERMISSION_GRANTED;
    if (!callerHasManage) {
      getContext()
          .enforceCallingOrSelfUriPermission(
              documentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION, method);
    }

    final Bundle out = new Bundle();
    try {
      if (METHOD_CREATE_DOCUMENT.equals(method)) {
        final String mimeType = extras.getString(Document.COLUMN_MIME_TYPE);
        final String displayName = extras.getString(Document.COLUMN_DISPLAY_NAME);

        final String newDocumentId = createDocument(documentId, mimeType, displayName);
        out.putString(Document.COLUMN_DOCUMENT_ID, newDocumentId);

        // Extend permission grant towards caller if needed
        if (!callerHasManage) {
          final Uri newDocumentUri = DocumentsContract.buildDocumentUri(mAuthority, newDocumentId);
          context.grantUriPermission(
              getCallingPackage(),
              newDocumentUri,
              Intent.FLAG_GRANT_READ_URI_PERMISSION
                  | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                  | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        }

      } else if (METHOD_DELETE_DOCUMENT.equals(method)) {
        deleteDocument(documentId);

        // Document no longer exists, clean up any grants
        context.revokeUriPermission(
            documentUri,
            Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

      } else {
        throw new UnsupportedOperationException("Method not supported " + method);
      }
    } catch (FileNotFoundException e) {
      throw new IllegalStateException("Failed call " + method, e);
    }
    return out;
  }
Beispiel #2
0
 public static Uri getSoundUri(int not) {
   switch (not) {
     case 0:
       return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
     default:
       try {
         File file =
             new File(
                 Environment.getExternalStorageDirectory() + "/Animeflv/cache/.sounds",
                 getSoundsFileName(not));
         if (file.exists()) {
           file.setReadable(true, false);
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             Uri uri =
                 FileProvider.getUriForFile(context, "knf.animeflv.RequestsBackground", file);
             context.grantUriPermission(
                 "com.android.systemui", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
             return uri;
           } else {
             return Uri.fromFile(file);
           }
         } else {
           Log.d("Sound Uri", "Not found");
           return getSoundUri(0);
         }
       } catch (Exception e) {
         e.printStackTrace();
         return getSoundUri(0);
       }
   }
 }