1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.pump.activity;
18 
19 import android.content.ContentUris;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.provider.MediaStore;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.annotation.UiThread;
29 import androidx.appcompat.app.AppCompatActivity;
30 import androidx.core.content.ContextCompat;
31 import androidx.media.AudioAttributesCompat;
32 import androidx.media2.common.SessionPlayer;
33 import androidx.media2.common.UriMediaItem;
34 import androidx.media2.player.MediaPlayer;
35 import androidx.media2.widget.VideoView;
36 
37 import com.android.pump.R;
38 import com.android.pump.db.Video;
39 import com.android.pump.util.Clog;
40 import com.android.pump.util.IntentUtils;
41 
42 @UiThread
43 public class VideoPlayerActivity extends AppCompatActivity {
44     private static final String TAG = Clog.tag(VideoPlayerActivity.class);
45     private static final String SAVED_POSITION_KEY = "SavedPosition";
46 
47     private VideoView mVideoView;
48     private MediaPlayer mMediaPlayer;
49     private long mSavedPosition = SessionPlayer.UNKNOWN_TIME;
50 
start(@onNull Context context, @NonNull Video video)51     public static void start(@NonNull Context context, @NonNull Video video) {
52         // TODO(b/123703220) Find a better URI (video.getUri()?)
53         Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
54                 video.getId());
55         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
56         intent.setDataAndTypeAndNormalize(uri, video.getMimeType());
57         intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
58         IntentUtils.startExternalActivity(context, intent);
59     }
60 
61     @Override
onCreate(@ullable Bundle savedInstanceState)62     protected void onCreate(@Nullable Bundle savedInstanceState) {
63         super.onCreate(savedInstanceState);
64         setContentView(R.layout.activity_video_player);
65         mVideoView = findViewById(R.id.video_view);
66 
67         if (savedInstanceState != null) {
68             mSavedPosition = savedInstanceState.getLong(SAVED_POSITION_KEY,
69                     SessionPlayer.UNKNOWN_TIME);
70         }
71 
72         mMediaPlayer = new MediaPlayer(VideoPlayerActivity.this);
73         AudioAttributesCompat audioAttributes = new AudioAttributesCompat.Builder()
74                 .setUsage(AudioAttributesCompat.USAGE_MEDIA)
75                 .setContentType(AudioAttributesCompat.CONTENT_TYPE_MOVIE).build();
76 
77         mMediaPlayer.setAudioAttributes(audioAttributes);
78         mVideoView.setPlayer(mMediaPlayer);
79 
80         handleIntent();
81     }
82 
83     @Override
onSaveInstanceState(@onNull Bundle outState)84     public void onSaveInstanceState(@NonNull Bundle outState) {
85         outState.putLong(SAVED_POSITION_KEY, mMediaPlayer.getCurrentPosition());
86         super.onSaveInstanceState(outState);
87     }
88 
89     @Override
onNewIntent(@ullable Intent intent)90     protected void onNewIntent(@Nullable Intent intent) {
91         super.onNewIntent(intent);
92         setIntent(intent);
93 
94         handleIntent();
95     }
96 
handleIntent()97     private void handleIntent() {
98         Intent intent = getIntent();
99         Uri uri = intent.getData();
100         if (uri == null) {
101             Clog.e(TAG, "The intent has no uri. Finishing activity...");
102             finish();
103             return;
104         }
105         UriMediaItem mediaItem = new UriMediaItem.Builder(uri).build();
106         mMediaPlayer.setMediaItem(mediaItem)
107                 .addListener(new Runnable() {
108                     @Override
109                     public void run() {
110                         if (mSavedPosition != SessionPlayer.UNKNOWN_TIME) {
111                             mMediaPlayer.seekTo(mSavedPosition);
112                             mSavedPosition = SessionPlayer.UNKNOWN_TIME;
113                         }
114                         mMediaPlayer.play();
115                     }
116                 }, ContextCompat.getMainExecutor(this));
117     }
118 
119     @Override
onDestroy()120     protected void onDestroy() {
121         super.onDestroy();
122         try {
123             if (mMediaPlayer != null) {
124                 mMediaPlayer.close();
125             }
126         } catch (Exception e) { }
127     }
128 }
129