• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, 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 com.android.car.stream.media;
17 
18 import android.content.ComponentName;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 import android.media.MediaDescription;
24 import android.service.media.MediaBrowserService;
25 
26 import java.util.List;
27 import java.util.Objects;
28 
29 /**
30  * Media related utility functions
31  */
32 public final class MediaUtils {
33     /**
34      * @return True if the two media descriptions are the same.
35      */
isSameMediaDescription(MediaDescription description1, MediaDescription description2)36     public static boolean isSameMediaDescription(MediaDescription description1,
37             MediaDescription description2) {
38         if ((description1 == null) && (description2 == null)) {
39             return true;
40         }
41 
42         if (description1 != null && description2 != null) {
43             return Objects.equals(description1.getTitle(), description2.getTitle())
44                     && Objects.equals(description1.getSubtitle(), description2.getSubtitle());
45         }
46         return false;
47     }
48 
49     /**
50      * @return The component name of the {@link MediaBrowserService} for the given package name.
51      */
getMediaBrowserService(String packageName, Context context)52     public static ComponentName getMediaBrowserService(String packageName,
53             Context context) {
54         Intent intent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
55         List<ResolveInfo> mediaApps = context.getPackageManager()
56                 .queryIntentServices(intent, PackageManager.GET_RESOLVED_FILTER);
57 
58         for (int i = 0; i < mediaApps.size(); i++) {
59             ResolveInfo info = mediaApps.get(i);
60             if (packageName.equals(info.serviceInfo.packageName)) {
61                 return new ComponentName(packageName, info.serviceInfo.name /* className */);
62             }
63         }
64         return null;
65     }
66 }
67