Search in sources :

Example 1 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project kdeconnect-android by KDE.

the class CompositeReceiveFileJob method openFile.

private void openFile(DocumentFile fileDocument) {
    String mimeType = FilesHelper.getMimeTypeFromFile(fileDocument.getName());
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT >= 24) {
        // Nougat and later require "content://" uris instead of "file://" uris
        File file = new File(fileDocument.getUri().getPath());
        Uri contentUri = FileProvider.getUriForFile(getDevice().getContext(), "org.kde.kdeconnect_tp.fileprovider", file);
        intent.setDataAndType(contentUri, mimeType);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
    } else {
        intent.setDataAndType(fileDocument.getUri(), mimeType);
    }
    // Open files for KDE Itinerary explicitly because Android's activity resolution sucks
    if (fileDocument.getName().endsWith(".itinerary")) {
        intent.setClassName("org.kde.itinerary", "org.kde.itinerary.Activity");
    }
    getDevice().getContext().startActivity(intent);
}
Also used : Intent(android.content.Intent) File(java.io.File) DocumentFile(androidx.documentfile.provider.DocumentFile) Uri(android.net.Uri)

Example 2 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class LocalFeedUpdaterTest method callUpdateFeed.

/**
 * Calls the method {@link LocalFeedUpdater#updateFeed(Feed, Context)} with
 * the given local feed folder.
 *
 * @param localFeedDir assets local feed folder with media files
 */
private void callUpdateFeed(@NonNull String localFeedDir) {
    DocumentFile documentFile = new AssetsDocumentFile(localFeedDir, context.getAssets());
    try (MockedStatic<DocumentFile> dfMock = Mockito.mockStatic(DocumentFile.class)) {
        // mock external storage
        dfMock.when(() -> DocumentFile.fromTreeUri(any(), any())).thenReturn(documentFile);
        // call method to test
        Feed feed = new Feed(FEED_URL, null);
        LocalFeedUpdater.updateFeed(feed, context);
    }
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) Feed(de.danoeh.antennapod.model.feed.Feed)

Example 3 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class LocalFeedUpdaterTest method testGetImageUrl_OtherImageFilenamePng.

@Test
public void testGetImageUrl_OtherImageFilenamePng() {
    DocumentFile documentFolder = mockDocumentFolder(mockDocumentFile("audio.mp3", "audio/mp3"), mockDocumentFile("my-image.png", "image/png"));
    String imageUrl = LocalFeedUpdater.getImageUrl(context, documentFolder);
    assertThat(imageUrl, endsWith("my-image.png"));
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) Test(org.junit.Test)

Example 4 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class LocalFeedUpdaterTest method mockDocumentFile.

/**
 * Create a DocumentFile mock object.
 */
@NonNull
private static DocumentFile mockDocumentFile(@NonNull String fileName, @NonNull String mimeType) {
    DocumentFile file = mock(DocumentFile.class);
    when(file.getName()).thenReturn(fileName);
    when(file.getUri()).thenReturn(Uri.parse("file:///path/" + fileName));
    when(file.getType()).thenReturn(mimeType);
    return file;
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) NonNull(androidx.annotation.NonNull)

Example 5 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class LocalFeedUpdaterTest method testGetImageUrl_OtherImageFilenameJpeg.

@Test
public void testGetImageUrl_OtherImageFilenameJpeg() {
    DocumentFile documentFolder = mockDocumentFolder(mockDocumentFile("audio.mp3", "audio/mp3"), mockDocumentFile("my-image.jpeg", "image/jpeg"));
    String imageUrl = LocalFeedUpdater.getImageUrl(context, documentFolder);
    assertThat(imageUrl, endsWith("my-image.jpeg"));
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) Test(org.junit.Test)

Aggregations

DocumentFile (androidx.documentfile.provider.DocumentFile)20 AssetsDocumentFile (androidx.documentfile.provider.AssetsDocumentFile)10 Test (org.junit.Test)7 Uri (android.net.Uri)6 IOException (java.io.IOException)4 NonNull (androidx.annotation.NonNull)2 Feed (de.danoeh.antennapod.model.feed.Feed)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 MenuItem (android.view.MenuItem)1 SubMenu (android.view.SubMenu)1 CheckBox (android.widget.CheckBox)1 RequiresApi (androidx.annotation.RequiresApi)1 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)1 MediaType (de.danoeh.antennapod.model.playback.MediaType)1 BufferedOutputStream (java.io.BufferedOutputStream)1 InputStream (java.io.InputStream)1