Home | History | Annotate | Download | only in testutils
      1 /*
      2  * Copyright (C) 2018 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 
     17 package com.android.tv.settings.testutils;
     18 
     19 import android.app.ActivityThread;
     20 import android.content.ComponentName;
     21 import android.content.pm.PackageManager;
     22 import android.os.RemoteException;
     23 
     24 import org.robolectric.RuntimeEnvironment;
     25 import org.robolectric.annotation.Implementation;
     26 import org.robolectric.annotation.Implements;
     27 
     28 import java.lang.reflect.InvocationHandler;
     29 import java.lang.reflect.Method;
     30 import java.lang.reflect.Proxy;
     31 
     32 import javax.annotation.Nonnull;
     33 
     34 /**
     35  * A copy of roboletric ShadowActivityThread, added getActivityInfo, getServiceInfo
     36  * TODO: Remove this class when b/74008159 is fixed.
     37  */
     38 @Implements(value = ActivityThread.class)
     39 public class TvShadowActivityThread {
     40 
     41     @Implementation
     42     public static Object getPackageManager() {
     43         ClassLoader classLoader = TvShadowActivityThread.class.getClassLoader();
     44         Class<?> iPackageManagerClass;
     45         try {
     46             iPackageManagerClass = classLoader.loadClass("android.content.pm.IPackageManager");
     47         } catch (ClassNotFoundException e) {
     48             throw new RuntimeException(e);
     49         }
     50         return Proxy.newProxyInstance(
     51                 classLoader,
     52                 new Class[]{iPackageManagerClass},
     53                 new InvocationHandler() {
     54                     @Override
     55                     public Object invoke(Object proxy, @Nonnull Method method, Object[] args)
     56                             throws Exception {
     57                         if (method.getName().equals("getApplicationInfo")) {
     58                             String packageName = (String) args[0];
     59                             int flags = (Integer) args[1];
     60 
     61                             try {
     62                                 return RuntimeEnvironment.application
     63                                         .getPackageManager()
     64                                         .getApplicationInfo(packageName, flags);
     65                             } catch (PackageManager.NameNotFoundException e) {
     66                                 throw new RemoteException(e.getMessage());
     67                             }
     68                         } else if (method.getName().equals("getActivityInfo")) {
     69                             ComponentName className = (ComponentName) args[0];
     70                             int flags = (Integer) args[1];
     71 
     72                             try {
     73                                 return RuntimeEnvironment.application
     74                                         .getPackageManager()
     75                                         .getActivityInfo(className, flags);
     76                             } catch (PackageManager.NameNotFoundException e) {
     77                                 throw new RemoteException(e.getMessage());
     78                             }
     79                         } else if (method.getName().equals("getServiceInfo")) {
     80                             ComponentName className = (ComponentName) args[0];
     81                             int flags = (Integer) args[1];
     82 
     83                             try {
     84                                 return RuntimeEnvironment.application
     85                                         .getPackageManager()
     86                                         .getServiceInfo(className, flags);
     87                             } catch (PackageManager.NameNotFoundException e) {
     88                                 throw new RemoteException(e.getMessage());
     89                             }
     90                         } else if (method.getName().equals("notifyPackageUse")) {
     91                             return null;
     92                         } else if (method.getName().equals("getPackageInstaller")) {
     93                             return null;
     94                         }
     95                         throw new UnsupportedOperationException("sorry, not supporting " + method
     96                                 + " yet!");
     97                     }
     98                 });
     99     }
    100 
    101     @Implementation
    102     public static Object currentActivityThread() {
    103         return RuntimeEnvironment.getActivityThread();
    104     }
    105 
    106 }
    107