Browse Source

Migrate player service and preview fragment to User model

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
Chris Narkiewicz 4 years ago
parent
commit
277f08b941

+ 8 - 19
src/main/java/com/nextcloud/client/media/Player.kt

@@ -19,24 +19,23 @@
  */
 package com.nextcloud.client.media
 
-import android.accounts.Account
 import android.content.Context
 import android.media.AudioManager
 import android.media.MediaPlayer
 import android.os.PowerManager
 import android.widget.MediaController
+import com.nextcloud.client.account.User
 import com.nextcloud.client.media.PlayerStateMachine.Event
 import com.nextcloud.client.media.PlayerStateMachine.State
+import com.nextcloud.client.network.ClientFactory
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile
-import com.owncloud.android.lib.common.OwnCloudAccount
-import com.owncloud.android.lib.common.OwnCloudClient
-import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
 import com.owncloud.android.lib.common.utils.Log_OC
 
 @Suppress("TooManyFunctions")
 internal class Player(
     private val context: Context,
+    private val clientFactory: ClientFactory,
     private val listener: Listener? = null,
     audioManager: AudioManager,
     private val mediaPlayerCreator: () -> MediaPlayer = { MediaPlayer() }
@@ -64,7 +63,7 @@ internal class Player(
     private var playedFile: OCFile? = null
     private var startPositionMs: Int = 0
     private var autoPlay = true
-    private var account: Account? = null
+    private var user: User? = null
     private var dataSource: String? = null
     private var lastError: PlayerError? = null
     private var mediaPlayer: MediaPlayer? = null
@@ -82,7 +81,7 @@ internal class Player(
                     playedFile = it.file
                     startPositionMs = it.startPositionMs
                     autoPlay = it.autoPlay
-                    account = it.account
+                    user = it.user
                     dataSource = if (it.file.isDown) it.file.storagePath else null
                     listener?.onRunning(it.file)
                 } else {
@@ -94,8 +93,9 @@ internal class Player(
         override fun onStartDownloading() {
             trace("onStartDownloading()")
             checkNotNull(playedFile) { "File not set." }
+            checkNotNull(user)
             playedFile?.let {
-                val client = buildClient()
+                val client = clientFactory.create(user)
                 val task = LoadUrlTask(client, it.remoteId, this@Player::onDownloaded)
                 task.execute()
                 loadUrlTask = task
@@ -125,7 +125,7 @@ internal class Player(
 
             playedFile = null
             startPositionMs = 0
-            account = null
+            user = null
             autoPlay = true
             dataSource = null
             loadUrlTask?.cancel(true)
@@ -235,17 +235,6 @@ internal class Player(
         }
     }
 
-    // this should be refactored into a proper, injectable factory
-    private fun buildClient(): OwnCloudClient {
-        val account = this.account
-        if (account != null) {
-            val ocAccount = OwnCloudAccount(account, context)
-            return OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context)
-        } else {
-            throw IllegalArgumentException("Account not set")
-        }
-    }
-
     private fun trace(fmt: String, vararg args: Any?) {
         Log_OC.v(javaClass.simpleName, fmt.format(args))
     }

+ 10 - 6
src/main/java/com/nextcloud/client/media/PlayerService.kt

@@ -19,7 +19,6 @@
  */
 package com.nextcloud.client.media
 
-import android.accounts.Account
 import android.app.PendingIntent
 import android.app.Service
 import android.content.Intent
@@ -29,6 +28,8 @@ import android.os.IBinder
 import android.widget.MediaController
 import android.widget.Toast
 import androidx.core.app.NotificationCompat
+import com.nextcloud.client.account.User
+import com.nextcloud.client.network.ClientFactory
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.ui.notifications.NotificationUtils
@@ -40,7 +41,7 @@ import javax.inject.Inject
 class PlayerService : Service() {
 
     companion object {
-        const val EXTRA_ACCOUNT = "ACCOUNT"
+        const val EXTRA_USER = "USER"
         const val EXTRA_FILE = "FILE"
         const val EXTRA_AUTO_PLAY = "EXTRA_AUTO_PLAY"
         const val EXTRA_START_POSITION_MS = "START_POSITION_MS"
@@ -84,13 +85,16 @@ class PlayerService : Service() {
     @Inject
     protected lateinit var audioManager: AudioManager
 
+    @Inject
+    protected lateinit var clientFactory: ClientFactory
+
     private lateinit var player: Player
     private lateinit var notificationBuilder: NotificationCompat.Builder
 
     override fun onCreate() {
         super.onCreate()
         AndroidInjection.inject(this)
-        player = Player(applicationContext, playerListener, audioManager)
+        player = Player(applicationContext, clientFactory, playerListener, audioManager)
         notificationBuilder = NotificationCompat.Builder(this)
         notificationBuilder.color = ThemeUtils.primaryColor(this)
         val stop = Intent(this, PlayerService::class.java)
@@ -113,11 +117,11 @@ class PlayerService : Service() {
     }
 
     private fun onActionPlay(intent: Intent) {
-        val account: Account = intent.getParcelableExtra(EXTRA_ACCOUNT)
-        val file: OCFile = intent.getParcelableExtra(EXTRA_FILE)
+        val user: User = intent.getParcelableExtra(EXTRA_USER) as User
+        val file: OCFile = intent.getParcelableExtra(EXTRA_FILE) as OCFile
         val startPos = intent.getIntExtra(EXTRA_START_POSITION_MS, 0)
         val autoPlay = intent.getBooleanExtra(EXTRA_AUTO_PLAY, true)
-        val item = PlaylistItem(file = file, startPositionMs = startPos, autoPlay = autoPlay, account = account)
+        val item = PlaylistItem(file = file, startPositionMs = startPos, autoPlay = autoPlay, user = user)
         player.play(item)
     }
 

+ 3 - 3
src/main/java/com/nextcloud/client/media/PlayerServiceConnection.kt

@@ -19,13 +19,13 @@
  */
 package com.nextcloud.client.media
 
-import android.accounts.Account
 import android.content.ComponentName
 import android.content.Context
 import android.content.Intent
 import android.content.ServiceConnection
 import android.os.IBinder
 import android.widget.MediaController
+import com.nextcloud.client.account.User
 import com.owncloud.android.datamodel.OCFile
 
 @Suppress("TooManyFunctions") // implementing large interface
@@ -49,9 +49,9 @@ class PlayerServiceConnection(private val context: Context) : MediaController.Me
         }
     }
 
-    fun start(account: Account, file: OCFile, playImmediately: Boolean, position: Int) {
+    fun start(user: User, file: OCFile, playImmediately: Boolean, position: Int) {
         val i = Intent(context, PlayerService::class.java)
-        i.putExtra(PlayerService.EXTRA_ACCOUNT, account)
+        i.putExtra(PlayerService.EXTRA_USER, user)
         i.putExtra(PlayerService.EXTRA_FILE, file)
         i.putExtra(PlayerService.EXTRA_AUTO_PLAY, playImmediately)
         i.putExtra(PlayerService.EXTRA_START_POSITION_MS, position)

+ 2 - 2
src/main/java/com/nextcloud/client/media/PlaylistItem.kt

@@ -1,6 +1,6 @@
 package com.nextcloud.client.media
 
-import android.accounts.Account
+import com.nextcloud.client.account.User
 import com.owncloud.android.datamodel.OCFile
 
-data class PlaylistItem(val file: OCFile, val startPositionMs: Int, val autoPlay: Boolean, val account: Account)
+data class PlaylistItem(val file: OCFile, val startPositionMs: Int, val autoPlay: Boolean, val user: User)

+ 6 - 2
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -522,7 +522,7 @@ public class FileDisplayActivity extends FileActivity
             if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)) {
                 int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
                 boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
-                secondFragment = PreviewMediaFragment.newInstance(file, user.toPlatformAccount(), startPlaybackPosition, autoplay);
+                secondFragment = PreviewMediaFragment.newInstance(file, user, startPlaybackPosition, autoplay);
             } else if (file.isDown() && PreviewTextFileFragment.canBePreviewed(file)) {
                 secondFragment = null;
             } else {
@@ -2130,8 +2130,12 @@ public class FileDisplayActivity extends FileActivity
      */
     public void startMediaPreview(OCFile file, int startPlaybackPosition, boolean autoplay, boolean showPreview,
                                   boolean streamMedia) {
+        Optional<User> user = getUser();
+        if (!user.isPresent()) {
+            return; // not reachable under normal conditions
+        }
         if (showPreview && file.isDown() && !file.isDownloading() || streamMedia) {
-            Fragment mediaFragment = PreviewMediaFragment.newInstance(file, getAccount(), startPlaybackPosition, autoplay);
+            Fragment mediaFragment = PreviewMediaFragment.newInstance(file, user.get(), startPlaybackPosition, autoplay);
             setSecondFragment(mediaFragment);
             updateFragmentsVisibility(true);
             updateActionBarTitleAndHomeButton(file);

+ 24 - 25
src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java

@@ -52,11 +52,13 @@ import android.widget.RelativeLayout;
 import android.widget.TextView;
 import android.widget.VideoView;
 
+import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.device.DeviceInfo;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.media.ErrorFormat;
 import com.nextcloud.client.media.PlayerServiceConnection;
+import com.nextcloud.client.network.ClientFactory;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileMenuFilter;
@@ -97,17 +99,17 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
     private static final String TAG = PreviewMediaFragment.class.getSimpleName();
 
     public static final String EXTRA_FILE = "FILE";
-    public static final String EXTRA_ACCOUNT = "ACCOUNT";
+    public static final String EXTRA_USER = "USER";
     private static final String EXTRA_PLAY_POSITION = "PLAY_POSITION";
     private static final String EXTRA_PLAYING = "PLAYING";
     private static final double MIN_DENSITY_RATIO = 24.0;
 
     private static final String FILE = "FILE";
-    private static final String ACCOUNT = "ACCOUNT";
+    private static final String USER = "USER";
     private static final String PLAYBACK_POSITION = "PLAYBACK_POSITION";
     private static final String AUTOPLAY = "AUTOPLAY";
 
-    private Account mAccount;
+    private User user;
     private ImageView mImagePreview;
     private VideoView mVideoPreview;
     private int mSavedPlaybackPosition;
@@ -126,6 +128,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
     private PlayerServiceConnection mMediaPlayerServiceConnection;
 
     private Uri mVideoUri;
+    @Inject ClientFactory clientFactory;
     @Inject UserAccountManager accountManager;
     @Inject DeviceInfo deviceInfo;
 
@@ -135,15 +138,15 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
      * When 'fileToDetail' or 'ocAccount' are null
      *
      * @param fileToDetail An {@link OCFile} to preview in the fragment
-     * @param ocAccount    An ownCloud account; needed to start downloads
+     * @param user    Currently active user
      */
-    public static PreviewMediaFragment newInstance(OCFile fileToDetail, Account ocAccount, int startPlaybackPosition,
+    public static PreviewMediaFragment newInstance(OCFile fileToDetail, User user, int startPlaybackPosition,
                                                    boolean autoplay) {
         PreviewMediaFragment previewMediaFragment = new PreviewMediaFragment();
 
         Bundle bundle = new Bundle();
         bundle.putParcelable(FILE, fileToDetail);
-        bundle.putParcelable(ACCOUNT, ocAccount);
+        bundle.putParcelable(USER, user);
         bundle.putInt(PLAYBACK_POSITION, startPlaybackPosition);
         bundle.putBoolean(AUTOPLAY, autoplay);
 
@@ -163,7 +166,6 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
      */
     public PreviewMediaFragment() {
         super();
-        mAccount = null;
         mSavedPlaybackPosition = 0;
         mAutoplay = true;
     }
@@ -176,7 +178,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
         Bundle bundle = getArguments();
 
         setFile(bundle.getParcelable(FILE));
-        mAccount = bundle.getParcelable(ACCOUNT);
+        user = bundle.getParcelable(USER);
         mSavedPlaybackPosition = bundle.getInt(PLAYBACK_POSITION);
         mAutoplay = bundle.getBoolean(AUTOPLAY);
         mMediaPlayerServiceConnection = new PlayerServiceConnection(getContext());
@@ -242,15 +244,15 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
             if (file == null) {
                 throw new IllegalStateException("Instanced with a NULL OCFile");
             }
-            if (mAccount == null) {
+            if (user == null) {
                 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
             }
         } else {
-            file = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
+            file = savedInstanceState.getParcelable(EXTRA_FILE);
             setFile(file);
-            mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT);
-            mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
-            mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING);
+            user = savedInstanceState.getParcelable(EXTRA_USER);
+            mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_PLAY_POSITION);
+            mAutoplay = savedInstanceState.getBoolean(EXTRA_PLAYING);
         }
 
         if (file != null) {
@@ -295,19 +297,19 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
         super.onSaveInstanceState(outState);
         Log_OC.v(TAG, "onSaveInstanceState");
         toggleDrawerLockMode(containerActivity, DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
-        outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, getFile());
-        outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
+        outState.putParcelable(EXTRA_FILE, getFile());
+        outState.putParcelable(EXTRA_USER, user);
 
         if (MimeTypeUtil.isVideo(getFile())) {
             if (mVideoPreview != null) {
                 mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
                 mAutoplay = mVideoPreview.isPlaying();
-                outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION, mSavedPlaybackPosition);
-                outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING, mAutoplay);
+                outState.putInt(EXTRA_PLAY_POSITION, mSavedPlaybackPosition);
+                outState.putBoolean(EXTRA_PLAYING, mAutoplay);
             }
         } else if(mMediaPlayerServiceConnection.isConnected()) {
-            outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION, mMediaPlayerServiceConnection.getCurrentPosition());
-            outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING, mMediaPlayerServiceConnection.isPlaying());
+            outState.putInt(EXTRA_PLAY_POSITION, mMediaPlayerServiceConnection.getCurrentPosition());
+            outState.putBoolean(EXTRA_PLAYING, mMediaPlayerServiceConnection.isPlaying());
         }
     }
 
@@ -320,7 +322,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
             if (MimeTypeUtil.isAudio(file)) {
                 mMediaController.setMediaPlayer(mMediaPlayerServiceConnection);
                 mMediaPlayerServiceConnection.bind();
-                mMediaPlayerServiceConnection.start(mAccount, file, mAutoplay, mSavedPlaybackPosition);
+                mMediaPlayerServiceConnection.start(user, file, mAutoplay, mSavedPlaybackPosition);
                 mMultiListContainer.setVisibility(View.GONE);
                 mPreviewContainer.setVisibility(View.VISIBLE);
             } else if (MimeTypeUtil.isVideo(file)) {
@@ -481,10 +483,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
             mVideoPreview.setVideoURI(getFile().getStorageUri());
         } else {
             try {
-                OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, getContext());
-                OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
-                        getClientFor(ocAccount, getContext());
-
+                OwnCloudClient client = clientFactory.create(user);
                 new LoadStreamUrl(this, client).execute(getFile().getLocalId());
             } catch (Exception e) {
                 Log_OC.e(TAG, "Loading stream url not possible: " + e);
@@ -636,7 +635,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
 
     private void startFullScreenVideo() {
         Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
-        i.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount);
+        i.putExtra(FileActivity.EXTRA_ACCOUNT, user.toPlatformAccount());
         i.putExtra(FileActivity.EXTRA_FILE, getFile());
         i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
         i.putExtra(PreviewVideoActivity.EXTRA_STREAM_URL, mVideoUri);