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