1 /* 2 * Copyright (C) 2015 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 android.support.test.launcherhelper; 17 18 import android.support.test.uiautomator.UiDevice; 19 import android.util.Log; 20 21 import java.util.HashMap; 22 import java.util.HashSet; 23 import java.util.Map; 24 import java.util.Set; 25 26 /** 27 * Factory class that handles registering of {@link ILauncherStrategy} and providing a suitable 28 * launcher helper based on current launcher available 29 */ 30 public class LauncherStrategyFactory { 31 32 private static final String LOG_TAG = LauncherStrategyFactory.class.getSimpleName(); 33 private static LauncherStrategyFactory sInstance; 34 private UiDevice mUiDevice; 35 private Map<String, ILauncherStrategy> mInstanceMap; 36 private Set<Class <? extends ILauncherStrategy>> mKnownLauncherStrategies; 37 LauncherStrategyFactory(UiDevice uiDevice)38 private LauncherStrategyFactory(UiDevice uiDevice) { 39 mUiDevice = uiDevice; 40 mInstanceMap = new HashMap<>(); 41 mKnownLauncherStrategies = new HashSet<>(); 42 registerLauncherStrategy(AospLauncherStrategy.class); 43 registerLauncherStrategy(GoogleExperienceLauncherStrategy.class); 44 registerLauncherStrategy(Launcher3Strategy.class); 45 registerLauncherStrategy(NexusLauncherStrategy.class); 46 registerLauncherStrategy(PixelCLauncherStrategy.class); 47 registerLauncherStrategy(LeanbackLauncherStrategy.class); 48 registerLauncherStrategy(WearLauncherStrategy.class); 49 } 50 51 /** 52 * Retrieves an instance of the {@link LauncherStrategyFactory} 53 * @param uiDevice 54 * @return 55 */ getInstance(UiDevice uiDevice)56 public static LauncherStrategyFactory getInstance(UiDevice uiDevice) { 57 if (sInstance == null) { 58 sInstance = new LauncherStrategyFactory(uiDevice); 59 } 60 return sInstance; 61 } 62 63 /** 64 * Registers an {@link ILauncherStrategy}. 65 * <p>Note that the registration is by class so that the caller does not need to instantiate 66 * multiple instances of the same class. 67 * @param launcherStrategy 68 */ registerLauncherStrategy(Class<? extends ILauncherStrategy> launcherStrategy)69 public void registerLauncherStrategy(Class<? extends ILauncherStrategy> launcherStrategy) { 70 // ignore repeated registering attempts 71 if (!mKnownLauncherStrategies.contains(launcherStrategy)) { 72 try { 73 ILauncherStrategy strategy = launcherStrategy.newInstance(); 74 strategy.setUiDevice(mUiDevice); 75 mInstanceMap.put(strategy.getSupportedLauncherPackage(), strategy); 76 } catch (InstantiationException | IllegalAccessException e) { 77 Log.e(LOG_TAG, "exception while creating instance: " 78 + launcherStrategy.getCanonicalName()); 79 } 80 } 81 } 82 83 /** 84 * Retrieves a {@link ILauncherStrategy} that supports the current default launcher 85 * <p> 86 * {@link ILauncherStrategy} maybe registered via 87 * {@link LauncherStrategyFactory#registerLauncherStrategy(Class)} by identifying the 88 * launcher package name supported 89 * @return 90 */ getLauncherStrategy()91 public ILauncherStrategy getLauncherStrategy() { 92 String launcherPkg = mUiDevice.getLauncherPackageName(); 93 return mInstanceMap.get(launcherPkg); 94 } 95 96 /** 97 * Retrieves a {@link ILeanbackLauncherStrategy} that supports the current default leanback 98 * launcher 99 * @return 100 */ getLeanbackLauncherStrategy()101 public ILeanbackLauncherStrategy getLeanbackLauncherStrategy() { 102 ILauncherStrategy launcherStrategy = getLauncherStrategy(); 103 if (launcherStrategy instanceof ILeanbackLauncherStrategy) { 104 return (ILeanbackLauncherStrategy)launcherStrategy; 105 } 106 throw new RuntimeException("This LauncherStrategy is not for leanback launcher."); 107 } 108 } 109