Skip to content

Commit

Permalink
Merge branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmir1 committed Nov 30, 2023
2 parents ee7fca6 + 0a9f358 commit 587d5b2
Show file tree
Hide file tree
Showing 43 changed files with 709 additions and 117 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
./buildall.sh --arch x86_64 mpv
./buildall.sh --arch arm64 mpv
./buildall.sh
env:
GRADLE_OPTS: "-Xmx1G"
- name: Get tag name
if: startsWith(github.ref, 'refs/tags/')
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# mpv for Aniyomi

[![Build Status](https://api.travis-ci.com/mpv-android/mpv-android.svg?branch=master)](https://app.travis-ci.com/github/mpv-android/mpv-android)
[![Build Status](https://github.com/mpv-android/mpv-android/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/mpv-android/mpv-android/actions/workflows/build.yml)

mpv-android is a video player for Android based on [libmpv](https://github.com/mpv-player/mpv).

Expand Down
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'

ext.abiCodes = ["armeabi-v7a": 1, "arm64-v8a": 2, "x86":3, "x86_64":4]
ext.universalBase = 8000

android {
namespace 'is.xyz.mpv'
Expand All @@ -11,8 +12,13 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 33
<<<<<<< HEAD
versionCode 31
versionName "1.10.n"
=======
versionCode 32
versionName "2023-08-27-release"
>>>>>>> upstream/master
vectorDrawables.useSupportLibrary = true
}

Expand Down Expand Up @@ -68,7 +74,7 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.recyclerview:recyclerview:1.3.1'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'androidx.media:media:1.6.0'
}

Expand Down
7 changes: 7 additions & 0 deletions app/src/api29/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

</manifest>
13 changes: 13 additions & 0 deletions app/src/default/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<!-- SDK 33 and later -->
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

</manifest>
88 changes: 59 additions & 29 deletions app/src/main/java/is/xyz/filepicker/FilePickerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
import is.xyz.mpv.R;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.FileObserver;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.loader.content.AsyncTaskLoader;
import androidx.core.content.ContextCompat;
import androidx.loader.content.Loader;
import android.widget.Toast;

import java.io.File;
import java.io.FileFilter;
Expand All @@ -32,7 +35,13 @@
*/
public class FilePickerFragment extends AbstractFilePickerFragment<File> {

protected static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
protected static final int PERMISSIONS_REQUEST_ID = 1001;
protected static final String PERMISSION_PRE33 = Manifest.permission.WRITE_EXTERNAL_STORAGE;
protected static final String[] PERMISSIONS_POST33 = {
Manifest.permission.READ_MEDIA_AUDIO,
Manifest.permission.READ_MEDIA_IMAGES,
Manifest.permission.READ_MEDIA_VIDEO,
};
protected boolean showHiddenItems = false;
protected FileFilter filterPredicate = null;
private File mRequestedPath = null;
Expand Down Expand Up @@ -81,27 +90,38 @@ public void setFilterPredicate(@Nullable FileFilter predicate) {
/**
* @return true if app has been granted permission to write to the SD-card.
*/
public static boolean hasPermission(@NonNull Context context, @NonNull File path) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// user can choose to grant at least one
for (String permission : PERMISSIONS_POST33) {
if (PackageManager.PERMISSION_GRANTED ==
ContextCompat.checkSelfPermission(context, permission)) {
return true;
}
}
return false;
} else {
return PackageManager.PERMISSION_GRANTED ==
ContextCompat.checkSelfPermission(context, PERMISSION_PRE33);
}
}

@Override
protected boolean hasPermission(@NonNull File path) {
return PackageManager.PERMISSION_GRANTED ==
ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
return hasPermission(requireContext(), path);
}

/**
* Request permission to write to the SD-card.
*/
@Override
protected void handlePermission(@NonNull File path) {
// Should we show an explanation?
// if (shouldShowRequestPermissionRationale(
// Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Explain to the user why we need permission
// }

mRequestedPath = path;
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissions(PERMISSIONS_POST33, PERMISSIONS_REQUEST_ID);
} else {
requestPermissions(new String[]{PERMISSION_PRE33}, PERMISSIONS_REQUEST_ID);
}
}

/**
Expand All @@ -116,26 +136,32 @@ protected void handlePermission(@NonNull File path) {
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode != PERMISSIONS_REQUEST_ID)
return;
// If arrays are empty, then process was cancelled
if (permissions.length == 0) {
// Treat this as a cancel press
if (mListener != null) {
if (mListener != null)
mListener.onCancelled();
return;
}
boolean ok = false;
for (int r : grantResults) {
if (PackageManager.PERMISSION_GRANTED == r) {
ok = true;
break;
}
} else { // if (requestCode == PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
// Do refresh
if (mRequestedPath != null) {
refresh(mRequestedPath);
}
} else {
Toast.makeText(getContext(), R.string.nnf_permission_external_write_denied,
Toast.LENGTH_SHORT).show();
// Treat this as a cancel press
if (mListener != null) {
mListener.onCancelled();
}
}
}
if (ok) {
// Do refresh
if (mRequestedPath != null)
refresh(mRequestedPath);
} else {
Toast.makeText(getContext(), R.string.nnf_permission_external_write_denied,
Toast.LENGTH_SHORT).show();
// Treat this as a cancel press
if (mListener != null)
mListener.onCancelled();
}
}

Expand Down Expand Up @@ -239,8 +265,10 @@ public Loader<List<File>> getLoader() {
@Override
public List<File> loadInBackground() {
File[] listFiles = mCurrentPath.listFiles();
if (listFiles == null)
if (listFiles == null) {
Log.e(TAG, "FilePickerFragment: IO error while listing files");
return new ArrayList<>(0);
}

ArrayList<File> files = new ArrayList<>(listFiles.length);

Expand Down Expand Up @@ -329,4 +357,6 @@ protected int compareFiles(@NonNull File lhs, @NonNull File rhs) {
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
}

private static final String TAG = "mpv";
}
11 changes: 8 additions & 3 deletions app/src/main/java/is/xyz/mpv/BackgroundPlaybackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.IBinder
import android.support.v4.media.session.MediaSessionCompat
import android.util.Log
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
Expand Down Expand Up @@ -77,6 +78,8 @@ class BackgroundPlaybackService : Service(), MPVLib.EventObserver {
else
buildNotificationAction(R.drawable.ic_pause_black_24dp, R.string.btn_pause, "PLAY_PAUSE")

val style = MediaStyle()
mediaToken?.let { style.setMediaSession(it) }
if (shouldShowPrevNext) {
builder.addAction(buildNotificationAction(
R.drawable.ic_skip_previous_black_24dp, R.string.dialog_prev, "ACTION_PREV"
Expand All @@ -85,11 +88,11 @@ class BackgroundPlaybackService : Service(), MPVLib.EventObserver {
builder.addAction(buildNotificationAction(
R.drawable.ic_skip_next_black_24dp, R.string.dialog_next, "ACTION_NEXT"
))
builder.setStyle(MediaStyle().setShowActionsInCompactView(0, 2))
style.setShowActionsInCompactView(0, 2)
} else {
builder.addAction(playPauseAction)
builder.setStyle(MediaStyle())
}
builder.setStyle(style)

return builder.build()
}
Expand Down Expand Up @@ -143,7 +146,7 @@ class BackgroundPlaybackService : Service(), MPVLib.EventObserver {
}

override fun event(eventId: Int) {
if (eventId == MPVLib.mpvEventId.MPV_EVENT_IDLE)
if (eventId == MPVLib.mpvEventId.MPV_EVENT_SHUTDOWN)
stopSelf()
}

Expand All @@ -154,6 +157,8 @@ class BackgroundPlaybackService : Service(), MPVLib.EventObserver {
/* Using this property MPVActivity gives us a thumbnail
to display alongside the permanent notification */
var thumbnail: Bitmap? = null
/* Same but for connecting the notification to the media session */
var mediaToken: MediaSessionCompat.Token? = null

private const val NOTIFICATION_ID = 12345
private const val NOTIFICATION_CHANNEL_ID = "background_playback"
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/is/xyz/mpv/FilePickerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import `is`.xyz.filepicker.FilePickerFragment
import `is`.xyz.mpv.databinding.FragmentFilepickerChoiceBinding
import java.io.File
import java.io.FileFilter
Expand Down Expand Up @@ -176,9 +176,7 @@ class FilePickerActivity : AppCompatActivity(), AbstractFilePickerFragment.OnFil
}
}

if (PackageManager.PERMISSION_GRANTED !=
ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (!FilePickerFragment.hasPermission(this, File("/"))) {
Log.v(TAG, "FilePickerActivity: waiting for file picker permission")
return
}
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/is/xyz/mpv/MPVActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
return
}

player.initialize(applicationContext.filesDir.path)
player.initialize(applicationContext.filesDir.path, applicationContext.cacheDir.path)
player.addObserver(this)
player.playFile(filepath)

Expand All @@ -303,6 +303,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse

mediaSession = initMediaSession()
updateMediaSession()
BackgroundPlaybackService.mediaToken = mediaSession?.sessionToken

audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager

Expand Down Expand Up @@ -339,8 +340,12 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
// Suppress any further callbacks
activityIsForeground = false

mediaSession?.isActive = false
mediaSession?.release()
BackgroundPlaybackService.mediaToken = null
mediaSession?.let {
it.isActive = false
it.release()
}
mediaSession = null

@Suppress("DEPRECATION")
audioManager?.abandonAudioFocus(audioFocusChangeListener)
Expand Down Expand Up @@ -1594,7 +1599,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
if (!activityIsForeground) return
when (property) {
"track-list" -> player.loadTracks()
"video-params" -> updateOrientation()
"video-params/aspect" -> updateOrientation()
"video-format" -> updateAudioUI()
"hwdec-current" -> updateDecoderButton()
}
Expand Down
Loading

0 comments on commit 587d5b2

Please sign in to comment.