1 /* 2 * Copyright 2020 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 android.media; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.text.TextUtils; 22 import android.util.Log; 23 24 /** 25 * @hide 26 */ 27 public class MediaRouter2Utils { 28 29 static final String TAG = "MR2Utils"; 30 static final String SEPARATOR = ":"; 31 32 @NonNull toUniqueId(@onNull String providerId, @NonNull String id)33 public static String toUniqueId(@NonNull String providerId, @NonNull String id) { 34 if (TextUtils.isEmpty(providerId)) { 35 Log.w(TAG, "toUniqueId: providerId shouldn't be empty"); 36 return null; 37 } 38 if (TextUtils.isEmpty(id)) { 39 Log.w(TAG, "toUniqueId: id shouldn't be null"); 40 return null; 41 } 42 43 return providerId + SEPARATOR + id; 44 } 45 46 /** 47 * Gets provider ID from unique ID. 48 * If the corresponding provider ID could not be generated, it will return null. 49 */ 50 @Nullable getProviderId(@onNull String uniqueId)51 public static String getProviderId(@NonNull String uniqueId) { 52 if (TextUtils.isEmpty(uniqueId)) { 53 Log.w(TAG, "getProviderId: uniqueId shouldn't be empty"); 54 return null; 55 } 56 57 int firstIndexOfSeparator = uniqueId.indexOf(SEPARATOR); 58 if (firstIndexOfSeparator == -1) { 59 return null; 60 } 61 62 String providerId = uniqueId.substring(0, firstIndexOfSeparator); 63 if (TextUtils.isEmpty(providerId)) { 64 return null; 65 } 66 67 return providerId; 68 } 69 70 /** 71 * Gets the original ID (i.e. non-unique route/session ID) from unique ID. 72 * If the corresponding ID could not be generated, it will return null. 73 */ 74 @Nullable getOriginalId(@onNull String uniqueId)75 public static String getOriginalId(@NonNull String uniqueId) { 76 if (TextUtils.isEmpty(uniqueId)) { 77 Log.w(TAG, "getOriginalId: uniqueId shouldn't be empty"); 78 return null; 79 } 80 81 int firstIndexOfSeparator = uniqueId.indexOf(SEPARATOR); 82 if (firstIndexOfSeparator == -1 || firstIndexOfSeparator + 1 >= uniqueId.length()) { 83 return null; 84 } 85 86 String providerId = uniqueId.substring(firstIndexOfSeparator + 1); 87 if (TextUtils.isEmpty(providerId)) { 88 return null; 89 } 90 91 return providerId; 92 } 93 } 94