• 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     private boolean mIsDeviceSet;
38 
LauncherStrategyFactory(UiDevice uiDevice)39     private LauncherStrategyFactory(UiDevice uiDevice) {
40         mUiDevice = uiDevice;
41         mInstanceMap = new HashMap<>();
42         mKnownLauncherStrategies = new HashSet<>();
43         registerLauncherStrategy(AospLauncherStrategy.class);
44         registerLauncherStrategy(AutoLauncherStrategy.class);
45         registerLauncherStrategy(GoogleExperienceLauncherStrategy.class);
46         registerLauncherStrategy(Launcher3Strategy.class);
47         registerLauncherStrategy(NexusLauncherStrategy.class);
48         registerLauncherStrategy(PixelCLauncherStrategy.class);
49         registerLauncherStrategy(LeanbackLauncherStrategy.class);
50         registerLauncherStrategy(WearLauncherStrategy.class);
51         registerLauncherStrategy(TvLauncherStrategy.class);
52     }
53 
54     /**
55      * Retrieves an instance of the {@link LauncherStrategyFactory}
56      * @param uiDevice
57      * @return
58      */
getInstance(UiDevice uiDevice)59     public static LauncherStrategyFactory getInstance(UiDevice uiDevice) {
60         if (sInstance == null) {
61             sInstance = new LauncherStrategyFactory(uiDevice);
62         }
63         return sInstance;
64     }
65 
66     /**
67      * Registers an {@link ILauncherStrategy}.
68      * <p>Note that the registration is by class so that the caller does not need to instantiate
69      * multiple instances of the same class.
70      * @param launcherStrategy
71      */
registerLauncherStrategy(Class<? extends ILauncherStrategy> launcherStrategy)72     public void registerLauncherStrategy(Class<? extends ILauncherStrategy> launcherStrategy) {
73         // ignore repeated registering attempts
74         if (!mKnownLauncherStrategies.contains(launcherStrategy)) {
75             try {
76                 ILauncherStrategy strategy = launcherStrategy.newInstance();
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             ILauncherStrategy strategy = mInstanceMap.get(launcherPkg);
98             if (!mIsDeviceSet) {
99                 strategy.setUiDevice(mUiDevice);
100                 mIsDeviceSet = true;
101             }
102             return strategy;
103         } else {
104             throw new RuntimeException(String.format(
105                     "Could not find a launcher strategy for package, %s", launcherPkg));
106         }
107     }
108 
109     /**
110      * Retrieves a {@link ILeanbackLauncherStrategy} that supports the current default launcher
111      * for TV. Either Leanback Launcher or new TV Launcher
112      * @return
113      */
getLeanbackLauncherStrategy()114     public ILeanbackLauncherStrategy getLeanbackLauncherStrategy() {
115         ILauncherStrategy launcherStrategy = getLauncherStrategy();
116         if (launcherStrategy instanceof ILeanbackLauncherStrategy) {
117             return (ILeanbackLauncherStrategy)launcherStrategy;
118         }
119         throw new RuntimeException("This LauncherStrategy is suitable for TV.");
120     }
121 
122     /**
123      * Retrieves a {@link IAutoLauncherStrategy} that supports the current default auto
124      * launcher
125      * @return
126      */
getAutoLauncherStrategy()127     public IAutoLauncherStrategy getAutoLauncherStrategy() {
128         ILauncherStrategy launcherStrategy = getLauncherStrategy();
129         if (launcherStrategy instanceof IAutoLauncherStrategy) {
130             return (IAutoLauncherStrategy) launcherStrategy;
131         }
132         throw new RuntimeException("This LauncherStrategy is not for auto launcher.");
133     }
134 }
135