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