1 /** 2 * Copyright (C) 2022 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.systemui.media.controls.util; 18 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageManager; 23 import android.os.Bundle; 24 import android.text.TextUtils; 25 26 import androidx.core.math.MathUtils; 27 import androidx.media.utils.MediaConstants; 28 29 /** 30 * Utility class with common methods for media controls 31 */ 32 public class MediaDataUtils { 33 34 /** 35 * Get the application label for a given package 36 * @param context the context to use 37 * @param packageName Package to check 38 * @param unknownName Fallback string if application is not found 39 * @return The label or fallback string 40 */ getAppLabel(Context context, String packageName, String unknownName)41 public static String getAppLabel(Context context, String packageName, String unknownName) { 42 if (TextUtils.isEmpty(packageName)) { 43 return null; 44 } 45 final PackageManager packageManager = context.getPackageManager(); 46 ApplicationInfo applicationInfo; 47 try { 48 applicationInfo = packageManager.getApplicationInfo(packageName, 0); 49 } catch (PackageManager.NameNotFoundException e) { 50 applicationInfo = null; 51 } 52 final String applicationName = 53 (String) (applicationInfo != null 54 ? packageManager.getApplicationLabel(applicationInfo) 55 : unknownName); 56 return applicationName; 57 } 58 59 /** 60 * Check the bundle for extras indicating the progress percentage 61 * 62 * @param extras 63 * @return the progress value between 0-1 inclusive if prsent, otherwise null 64 */ getDescriptionProgress(@ullable Bundle extras)65 public static Double getDescriptionProgress(@Nullable Bundle extras) { 66 if (extras == null 67 || !extras.containsKey(MediaConstants.DESCRIPTION_EXTRAS_KEY_COMPLETION_STATUS)) { 68 return null; 69 } 70 71 int status = extras.getInt(MediaConstants.DESCRIPTION_EXTRAS_KEY_COMPLETION_STATUS); 72 switch (status) { 73 case MediaConstants.DESCRIPTION_EXTRAS_VALUE_COMPLETION_STATUS_NOT_PLAYED: 74 return 0.0; 75 case MediaConstants.DESCRIPTION_EXTRAS_VALUE_COMPLETION_STATUS_FULLY_PLAYED: 76 return 1.0; 77 case MediaConstants.DESCRIPTION_EXTRAS_VALUE_COMPLETION_STATUS_PARTIALLY_PLAYED: { 78 if (extras 79 .containsKey(MediaConstants.DESCRIPTION_EXTRAS_KEY_COMPLETION_PERCENTAGE)) { 80 double percent = extras 81 .getDouble(MediaConstants.DESCRIPTION_EXTRAS_KEY_COMPLETION_PERCENTAGE); 82 return MathUtils.clamp(percent, 0.0, 1.0); 83 } else { 84 return 0.5; 85 } 86 } 87 } 88 return null; 89 } 90 } 91