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