SimpleNavigatorProvider.java revision 6acf1d8307805fb382e8cc4883ddae89f1c21eb0
1/* 2 * Copyright (C) 2017 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 17package androidx.navigation; 18 19import android.support.annotation.NonNull; 20import android.support.annotation.Nullable; 21import android.support.annotation.RestrictTo; 22 23import java.util.HashMap; 24 25/** 26 * Simple implementation of a {@link NavigatorProvider} that stores instances of 27 * {@link Navigator navigators} by name, using the {@link Navigator.Name} when given a class name. 28 * 29 * @hide 30 */ 31@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 32public class SimpleNavigatorProvider implements NavigatorProvider { 33 private static final HashMap<Class, String> sAnnotationNames = new HashMap<>(); 34 35 private final HashMap<String, Navigator<? extends NavDestination>> mNavigators = 36 new HashMap<>(); 37 38 @NonNull 39 private String getNameForNavigator(@NonNull Class<? extends Navigator> navigatorClass) { 40 String name = sAnnotationNames.get(navigatorClass); 41 if (name == null) { 42 Navigator.Name annotation = navigatorClass.getAnnotation(Navigator.Name.class); 43 name = annotation != null ? annotation.value() : null; 44 if (!validateName(name)) { 45 throw new IllegalArgumentException("No @Navigator.Name annotation found for " 46 + navigatorClass.getSimpleName()); 47 } 48 sAnnotationNames.put(navigatorClass, name); 49 } 50 return name; 51 } 52 53 @NonNull 54 @Override 55 public <D extends NavDestination, T extends Navigator<? extends D>> T getNavigator( 56 @NonNull Class<T> navigatorClass) { 57 String name = getNameForNavigator(navigatorClass); 58 return getNavigator(name); 59 } 60 61 @NonNull 62 @Override 63 public <D extends NavDestination, T extends Navigator<? extends D>> T getNavigator( 64 @NonNull String name) { 65 if (!validateName(name)) { 66 throw new IllegalArgumentException("navigator name cannot be an empty string"); 67 } 68 69 Navigator<? extends NavDestination> navigator = mNavigators.get(name); 70 if (navigator == null) { 71 throw new IllegalStateException("Could not find Navigator with name \"" + name 72 + "\". You must call NavController.addNavigator() for each navigation type."); 73 } 74 //noinspection unchecked 75 return (T) navigator; 76 } 77 78 @Nullable 79 @Override 80 public Navigator<? extends NavDestination> addNavigator( 81 @NonNull Navigator<? extends NavDestination> navigator) { 82 String name = getNameForNavigator(navigator.getClass()); 83 84 return addNavigator(name, navigator); 85 } 86 87 @Nullable 88 @Override 89 public Navigator<? extends NavDestination> addNavigator(@NonNull String name, 90 @NonNull Navigator<? extends NavDestination> navigator) { 91 if (!validateName(name)) { 92 throw new IllegalArgumentException("navigator name cannot be an empty string"); 93 } 94 return mNavigators.put(name, navigator); 95 } 96 97 private boolean validateName(String name) { 98 return name != null && !name.isEmpty(); 99 } 100} 101