Skip to content

Commit

Permalink
Trigger MediaScanner scans when using MediaStore
Browse files Browse the repository at this point in the history
  • Loading branch information
moneytoo committed Sep 7, 2024
1 parent 96745a0 commit 2f4d6f5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
applicationId "com.brouken.player"
minSdkVersion 21
targetSdkVersion 34
versionCode 169
versionCode 170
versionName "0.${versionCode}"
archivesBaseName = "Just.Player.v${versionName}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@RequiresApi(api = Build.VERSION_CODES.R)
private void start() {
if (bucketId == null) {
Utils.scanMediaStorage(this);
showBuckets();
} else {
showFiles(bucketId);
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/brouken/player/PlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,12 @@ public void onAnimationEnd(Animator animation) {
});
}
});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (useMediaStore()) {
Utils.scanMediaStorage(this);
}
}
}

@Override
Expand Down Expand Up @@ -1597,9 +1603,13 @@ private void enableRotation() {
}
}

private void openFile(Uri pickerInitialUri) {
boolean useMediaStore() {
final int targetSdkVersion = getApplicationContext().getApplicationInfo().targetSdkVersion;
if ((isTvBox && Build.VERSION.SDK_INT >= 30 && targetSdkVersion >= 30 && mPrefs.fileAccess.equals("auto")) || mPrefs.fileAccess.equals("mediastore")) {
return (isTvBox && Build.VERSION.SDK_INT >= 30 && targetSdkVersion >= 30 && mPrefs.fileAccess.equals("auto")) || mPrefs.fileAccess.equals("mediastore");
}

private void openFile(Uri pickerInitialUri) {
if (useMediaStore()) {
Intent intent = new Intent(this, MediaStoreChooserActivity.class);
startActivityForResult(intent, REQUEST_CHOOSER_VIDEO_MEDIASTORE);
} else if ((isTvBox && mPrefs.fileAccess.equals("auto")) || mPrefs.fileAccess.equals("legacy")) {
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/brouken/player/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import android.content.res.Resources;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.LocaleList;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import android.util.Log;
Expand Down Expand Up @@ -705,4 +708,18 @@ public static <K, V> void orderByValue(LinkedHashMap<K, V> m, final Comparator<?
m.put(e.getKey(), e.getValue());
}
}

@RequiresApi(api = Build.VERSION_CODES.R)
public static void scanMediaStorage(Context context) {
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
List<String> storagePaths = new ArrayList<>();
for (StorageVolume volume : storageVolumes) {
File directory = volume.getDirectory();
if (directory != null) {
storagePaths.add(directory.getAbsolutePath());
}
}
MediaScannerConnection.scanFile(context, storagePaths.toArray(new String[0]), new String[]{"*/*"}, null);
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<string name="button_pip">Obraz v obraze</string>
<string name="pref_file_access">Přístup k souborům</string>
<string name="pref_file_access_auto">Automaticky</string>
<string name="mediastore_empty">Nebyly nalezeny žádné video soubory. Pro přístup k video souborům v daném úložišti, povolte automatické vyhledávání médii v systémovém nastavení.</string>
<string name="mediastore_empty">Nebyly nalezeny žádné video soubory</string>
<string name="pref_dangerous_header">Nebezpečné ⚠️</string>
<string name="pref_dangerous_warning">Změna voleb v této kategorii může rozbít přehrávání normálně fungujícího obsahu</string>
<string name="pref_shortcuts_header">Zkratky</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<string name="button_pip">Bild-im-Bild</string>
<string name="pref_file_access_auto">Automatisch</string>
<string name="pref_file_access">Dateizugriff</string>
<string name="mediastore_empty">Keine indizierten Videodateien gefunden. Um auf Videodateien im angegebenen Speicher zuzugreifen, aktivieren Sie die automatische Mediensuche in den Systemeinstellungen.</string>
<string name="mediastore_empty">Keine Videodateien gefunden</string>
<string name="pref_shortcuts_header">Verknüpfungen</string>
<string name="pref_captioning_preferences">Untertitel-Einstellungen</string>
<string name="pref_decoder_priority_prefer_device">Gerätedecoder bevorzugen (Standard)</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<string name="button_pip">Picture-in-picture</string>
<string name="pref_file_access">File access</string>
<string name="pref_file_access_auto">Auto</string>
<string name="mediastore_empty">No indexed video files found. To access video files in given storage, enable automatic media scanning in system settings.</string>
<string name="mediastore_empty">No video files found</string>
<string name="pref_dangerous_header">Dangerous ⚠️</string>
<string name="pref_dangerous_warning">Switching options in this category may break playback of normally compatible content</string>
<string name="pref_shortcuts_header">Shortcuts</string>
Expand Down

0 comments on commit 2f4d6f5

Please sign in to comment.