• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(AutoLauncherStrategy.class);
44         registerLauncherStrategy(GoogleExperienceLauncherStrategy.class);
45         registerLauncherStrategy(Launcher3Strategy.class);
46         registerLauncherStrategy(NexusLauncherStrategy.class);
47         registerLauncherStrategy(PixelCLauncherStrategy.class);
48         registerLauncherStrategy(LeanbackLauncherStrategy.class);
49         registerLauncherStrategy(WearLauncherStrategy.class);
50         registerLauncherStrategy(TvLauncherStrategy.class);
51     }
52 
53     /**
54      * Retrieves an instance of the {@link LauncherStrategyFactory}
55      * @param uiDevice
56      * @return
57      */
getInstance(UiDevice uiDevice)58     public static LauncherStrategyFactory getInstance(UiDevice uiDevice) {
59         if (sInstance == null) {
60             sInstance = new LauncherStrategyFactory(uiDevice);
61         }
62         return sInstance;
63     }
64 
65     /**
66      * Registers an {@link ILauncherStrategy}.
67      * <p>Note that the registration is by class so that the caller does not need to instantiate
68      * multiple instances of the same class.
69      * @param launcherStrategy
70      */
registerLauncherStrategy(Class<? extends ILauncherStrategy> launcherStrategy)71     public void registerLauncherStrategy(Class<? extends ILauncherStrategy> launcherStrategy) {
72         // ignore repeated registering attempts
73         if (!mKnownLauncherStrategies.contains(launcherStrategy)) {
74             try {
75                 ILauncherStrategy strategy = launcherStrategy.newInstance();
76                 strategy.setUiDevice(mUiDevice);
77                 mInstanceMap.put(strategy.getSupportedLauncherPackage(), strategy);
78             } catch (InstantiationException | IllegalAccessException e) {
79                 Log.e(LOG_TAG, "exception while creating instance: "
80                         + launcherStrategy.getCanonicalName());
81             }
82         }
83     }
84 
85     /**
86      * Retrieves a {@link ILauncherStrategy} that supports the current default launcher
87      * <p>
88      * {@link ILauncherStrategy} maybe registered via
89      * {@link LauncherStrategyFactory#registerLauncherStrategy(Class)} by identifying the
90      * launcher package name supported
91      * @throw RuntimeException if no valid launcher strategy is found
92      * @return
93      */
getLauncherStrategy()94     public ILauncherStrategy getLauncherStrategy() {
95         String launcherPkg = mUiDevice.getLauncherPackageName();
96         if (mInstanceMap.containsKey(launcherPkg)) {
97             return mInstanceMap.get(launcherPkg);
98         } else {
99             throw new RuntimeException(String.format(
100                     "Could not find a launcher strategy for package, %s", launcherPkg));
101         }
102     }
103 
104     /**
105      * Retrieves a {@link ILeanbackLauncherStrategy} that supports the current default launcher
106      * for TV. Either Leanback Launcher or new TV Launcher
107      * @return
108      */
getLeanbackLauncherStrategy()109     public ILeanbackLauncherStrategy getLeanbackLauncherStrategy() {
110         ILauncherStrategy launcherStrategy = getLauncherStrategy();
111         if (launcherStrategy instanceof ILeanbackLauncherStrategy) {
112             return (ILeanbackLauncherStrategy)launcherStrategy;
113         }
114         throw new RuntimeException("This LauncherStrategy is suitable for TV.");
115     }
116 
117     /**
118      * Retrieves a {@link IAutoLauncherStrategy} that supports the current default auto
119      * launcher
120      * @return
121      */
getAutoLauncherStrategy()122     public IAutoLauncherStrategy getAutoLauncherStrategy() {
123         ILauncherStrategy launcherStrategy = getLauncherStrategy();
124         if (launcherStrategy instanceof IAutoLauncherStrategy) {
125             return (IAutoLauncherStrategy) launcherStrategy;
126         }
127         throw new RuntimeException("This LauncherStrategy is not for auto launcher.");
128     }
129 }
130