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.media.AudioAttributesCompat; 31 import androidx.media2.common.UriMediaItem; 32 import androidx.media2.player.MediaPlayer; 33 import androidx.media2.widget.VideoView; 34 35 import com.android.pump.R; 36 import com.android.pump.db.Album; 37 import com.android.pump.db.Artist; 38 import com.android.pump.db.Audio; 39 import com.android.pump.db.Genre; 40 import com.android.pump.db.Playlist; 41 import com.android.pump.util.Clog; 42 import com.android.pump.util.IntentUtils; 43 44 @UiThread 45 public class AudioPlayerActivity extends AppCompatActivity { 46 private static final String TAG = Clog.tag(AudioPlayerActivity.class); 47 48 private VideoView mVideoView; 49 private MediaPlayer mMediaPlayer; 50 start(@onNull Context context, @NonNull Audio audio)51 public static void start(@NonNull Context context, @NonNull Audio audio) { 52 // TODO(b/123702587) Find a better URI (audio.getUri()?) 53 Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 54 audio.getId()); 55 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 56 intent.setDataAndTypeAndNormalize(uri, audio.getMimeType()); 57 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 58 IntentUtils.startExternalActivity(context, intent); 59 } 60 start(@onNull Context context, @NonNull Album album)61 public static void start(@NonNull Context context, @NonNull Album album) { 62 // TODO(b/123702587) Find a better URI (album.getUri()?) 63 Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 64 album.getId()); 65 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 66 // TODO Should the mime type be MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE? 67 intent.setDataAndTypeAndNormalize(uri, MediaStore.Audio.Albums.CONTENT_TYPE); 68 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 69 IntentUtils.startExternalActivity(context, intent); 70 } 71 start(@onNull Context context, @NonNull Artist artist)72 public static void start(@NonNull Context context, @NonNull Artist artist) { 73 // TODO(b/123702587) Find a better URI (artist.getUri()?) 74 Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, 75 artist.getId()); 76 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 77 // TODO Should the mime type be MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE? 78 intent.setDataAndTypeAndNormalize(uri, MediaStore.Audio.Artists.CONTENT_TYPE); 79 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 80 IntentUtils.startExternalActivity(context, intent); 81 } 82 start(@onNull Context context, @NonNull Genre genre)83 public static void start(@NonNull Context context, @NonNull Genre genre) { 84 // TODO(b/123702587) Find a better URI (genre.getUri()?) 85 Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, 86 genre.getId()); 87 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 88 // TODO Should the mime type be MediaStore.Audio.Genres.ENTRY_CONTENT_TYPE? 89 intent.setDataAndTypeAndNormalize(uri, MediaStore.Audio.Genres.CONTENT_TYPE); 90 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 91 IntentUtils.startExternalActivity(context, intent); 92 } 93 start(@onNull Context context, @NonNull Playlist playlist)94 public static void start(@NonNull Context context, @NonNull Playlist playlist) { 95 // TODO(b/123702587) Find a better URI (playlist.getUri()?) 96 Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 97 playlist.getId()); 98 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 99 // TODO Should the mime type be MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE? 100 intent.setDataAndTypeAndNormalize(uri, MediaStore.Audio.Playlists.CONTENT_TYPE); 101 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 102 IntentUtils.startExternalActivity(context, intent); 103 } 104 start(@onNull Context context, @NonNull Playlist playlist, int position)105 public static void start(@NonNull Context context, @NonNull Playlist playlist, int position) { 106 // TODO(b/123702587) Find a better URI? 107 Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 108 playlist.getId()).buildUpon() 109 .appendEncodedPath(MediaStore.Audio.Playlists.Members.CONTENT_DIRECTORY) 110 .appendEncodedPath(String.valueOf(position)).build(); 111 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 112 // TODO Should the mime type be MediaStore.Audio.Playlists.CONTENT_TYPE? 113 intent.setDataAndTypeAndNormalize(uri, MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE); 114 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 115 IntentUtils.startExternalActivity(context, intent); 116 } 117 118 @Override onCreate(@ullable Bundle savedInstanceState)119 protected void onCreate(@Nullable Bundle savedInstanceState) { 120 super.onCreate(savedInstanceState); 121 setContentView(R.layout.activity_audio_player); 122 mVideoView = findViewById(R.id.video_view); 123 124 mMediaPlayer = new MediaPlayer(AudioPlayerActivity.this); 125 AudioAttributesCompat audioAttributes = new AudioAttributesCompat.Builder() 126 .setUsage(AudioAttributesCompat.USAGE_MEDIA) 127 .setContentType(AudioAttributesCompat.CONTENT_TYPE_MOVIE).build(); 128 129 mMediaPlayer.setAudioAttributes(audioAttributes); 130 mVideoView.setPlayer(mMediaPlayer); 131 132 handleIntent(); 133 } 134 135 @Override onNewIntent(@ullable Intent intent)136 protected void onNewIntent(@Nullable Intent intent) { 137 super.onNewIntent(intent); 138 setIntent(intent); 139 140 handleIntent(); 141 } 142 handleIntent()143 private void handleIntent() { 144 Intent intent = getIntent(); 145 Uri uri = intent.getData(); 146 if (uri == null) { 147 Clog.e(TAG, "The intent has no uri. Finishing activity..."); 148 finish(); 149 return; 150 } 151 UriMediaItem mediaItem = new UriMediaItem.Builder(uri).build(); 152 mMediaPlayer.setMediaItem(mediaItem); 153 } 154 155 @Override onDestroy()156 protected void onDestroy() { 157 super.onDestroy(); 158 try { 159 if (mMediaPlayer != null) { 160 mMediaPlayer.close(); 161 } 162 } catch (Exception e) { } 163 } 164 } 165