1 /*
2  * Copyright (C) 2019 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 package android.car.cluster;
17 
18 import static com.android.car.apps.common.util.LiveDataFunctions.mapNonNull;
19 
20 import android.app.Application;
21 import android.graphics.Bitmap;
22 
23 import androidx.lifecycle.AndroidViewModel;
24 import androidx.lifecycle.LiveData;
25 
26 import com.android.car.media.common.source.MediaSource;
27 import com.android.car.media.common.source.MediaSourceViewModel;
28 
29 /**
30  * View model for {@link MusicFragment}
31  */
32 public final class MusicFragmentViewModel extends AndroidViewModel {
33 
34     private LiveData<MediaSource> mMediaSource;
35     private LiveData<CharSequence> mAppName;
36     private LiveData<Bitmap> mAppIcon;
37 
38     private MediaSourceViewModel mMediaSourceViewModel;
39 
MusicFragmentViewModel(Application application)40     public MusicFragmentViewModel(Application application) {
41         super(application);
42     }
43 
init(MediaSourceViewModel mediaSourceViewModel)44     void init(MediaSourceViewModel mediaSourceViewModel) {
45         if (mMediaSourceViewModel == mediaSourceViewModel) {
46             return;
47         }
48         mMediaSourceViewModel = mediaSourceViewModel;
49         mMediaSource = mMediaSourceViewModel.getPrimaryMediaSource();
50         mAppName = mapNonNull(mMediaSource, source -> source.getDisplayName(getApplication()));
51         mAppIcon = mapNonNull(mMediaSource, MediaSource::getCroppedPackageIcon);
52     }
53 
getAppName()54     LiveData<CharSequence> getAppName() {
55         return mAppName;
56     }
57 
getAppIcon()58     LiveData<Bitmap> getAppIcon() {
59         return mAppIcon;
60     }
61 }
62