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