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