• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.settings.network;
18 
19 import android.os.Binder;
20 import android.os.IBinder;
21 import android.os.IInterface;
22 import android.os.ServiceManager;
23 
24 import org.robolectric.annotation.Implementation;
25 import org.robolectric.annotation.Implements;
26 import org.robolectric.shadows.ShadowServiceManager;
27 import org.robolectric.util.ReflectionHelpers;
28 
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 @Implements(value = ServiceManager.class)
33 public class ShadowServiceManagerExtend extends ShadowServiceManager {
34     private static final Map<String, IBinder> EXTRA_SERVICES = new HashMap<>();
35 
36     @Implementation
getService(String name)37     public static IBinder getService(String name) {
38         final IBinder iBinder = ShadowServiceManager.getService(name);
39         if (iBinder == null) {
40             return EXTRA_SERVICES.get(name);
41         }
42         return iBinder;
43     }
44 
addService(String name, Class<? extends IInterface> classService, String fullNameClass)45     public static void addService(String name, Class<? extends IInterface> classService,
46             String fullNameClass) {
47         final Binder service = new Binder();
48         service.attachInterface(ReflectionHelpers.createNullProxy(classService), fullNameClass);
49 
50         ShadowServiceManager.addService(name, service);
51         EXTRA_SERVICES.put(name, service);
52     }
53 }
54