1 /*
2  * Copyright (C) 2022 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.server.connectivity.wear;
18 
19 import android.annotation.SuppressLint;
20 import android.companion.AssociationInfo;
21 import android.companion.CompanionDeviceManager;
22 import android.content.Context;
23 import android.net.wear.ICompanionDeviceManagerProxy;
24 import android.os.Binder;
25 import android.os.Build;
26 
27 import androidx.annotation.RequiresApi;
28 
29 import com.android.net.module.util.PermissionUtils;
30 
31 import java.util.List;
32 
33 /**
34  * A proxy for {@link CompanionDeviceManager}, for use by Tethering with NetworkStack permissions.
35  */
36 public class CompanionDeviceManagerProxyService extends ICompanionDeviceManagerProxy.Stub {
37     private final Context mContext;
38 
39     @RequiresApi(Build.VERSION_CODES.TIRAMISU)
CompanionDeviceManagerProxyService(Context context)40     public CompanionDeviceManagerProxyService(Context context) {
41         mContext = context;
42     }
43 
44     // TODO(b/193460475): Android Lint handles change from SystemApi to public incorrectly.
45     // CompanionDeviceManager#getAllAssociations() is made public in U,
46     // but existed in T as an identical SystemApi.
47     @SuppressLint("NewApi")
48     @Override
getAllAssociations()49     public List<AssociationInfo> getAllAssociations() {
50         PermissionUtils.enforceNetworkStackPermission(mContext);
51         final long token = Binder.clearCallingIdentity();
52         try {
53             final CompanionDeviceManager cdm = mContext.getSystemService(
54                     CompanionDeviceManager.class);
55             return cdm.getAllAssociations();
56         } finally {
57             Binder.restoreCallingIdentity(token);
58         }
59     }
60 }
61