1 /*
2  * Copyright (C) 2017 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.wifi;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.wifi.ILocalOnlyHotspotCallback;
22 import android.net.wifi.SoftApConfiguration;
23 import android.os.Binder;
24 import android.os.IBinder;
25 import android.os.RemoteException;
26 
27 import com.android.internal.util.Preconditions;
28 
29 /**
30  * Tracks information about applications requesting use of the LocalOnlyHotspot.
31  *
32  * @hide
33  */
34 class LocalOnlyHotspotRequestInfo implements IBinder.DeathRecipient {
35     static final int HOTSPOT_NO_ERROR = -1;
36 
37     private final int mPid;
38     private final ILocalOnlyHotspotCallback mCallback;
39     private final RequestingApplicationDeathCallback mDeathCallback;
40     private final SoftApConfiguration mCustomConfig;
41 
42     /**
43      * Callback for use with LocalOnlyHotspot to unregister requesting applications upon death.
44      */
45     interface RequestingApplicationDeathCallback {
46         /**
47          * Called when requesting app has died.
48          */
onLocalOnlyHotspotRequestorDeath(LocalOnlyHotspotRequestInfo requestor)49         void onLocalOnlyHotspotRequestorDeath(LocalOnlyHotspotRequestInfo requestor);
50     }
51 
LocalOnlyHotspotRequestInfo(@onNull ILocalOnlyHotspotCallback callback, @NonNull RequestingApplicationDeathCallback deathCallback, @Nullable SoftApConfiguration customConfig)52     LocalOnlyHotspotRequestInfo(@NonNull ILocalOnlyHotspotCallback callback,
53             @NonNull RequestingApplicationDeathCallback deathCallback,
54             @Nullable SoftApConfiguration customConfig) {
55         mPid = Binder.getCallingPid();
56         mCallback = Preconditions.checkNotNull(callback);
57         mDeathCallback = Preconditions.checkNotNull(deathCallback);
58         mCustomConfig = customConfig;
59 
60         try {
61             mCallback.asBinder().linkToDeath(this, 0);
62         } catch (RemoteException e) {
63             binderDied();
64         }
65     }
66 
67     /**
68      * Allow caller to unlink this object from binder death.
69      */
unlinkDeathRecipient()70     public void unlinkDeathRecipient() {
71         mCallback.asBinder().unlinkToDeath(this, 0);
72     }
73 
74     /**
75      * Application requesting LocalOnlyHotspot died
76      */
77     @Override
binderDied()78     public void binderDied() {
79         mDeathCallback.onLocalOnlyHotspotRequestorDeath(this);
80     }
81 
82     /**
83      * Send a HOTSPOT_FAILED message to WifiManager for the calling application with the error code.
84      *
85      * @param reasonCode error code for the message
86      *
87      * @throws RemoteException
88      */
sendHotspotFailedMessage(int reasonCode)89     public void sendHotspotFailedMessage(int reasonCode) throws RemoteException {
90         mCallback.onHotspotFailed(reasonCode);
91     }
92 
93     /**
94      * Send a HOTSPOT_STARTED message to WifiManager for the calling application with the config.
95      *
96      * @param config SoftApConfiguration for the callback
97      *
98      * @throws RemoteException
99      */
sendHotspotStartedMessage(SoftApConfiguration config)100     public void sendHotspotStartedMessage(SoftApConfiguration config) throws RemoteException {
101         mCallback.onHotspotStarted(config);
102     }
103 
104     /**
105      * Send a HOTSPOT_STOPPED message to WifiManager for the calling application.
106      *
107      * @throws RemoteException
108      */
sendHotspotStoppedMessage()109     public void sendHotspotStoppedMessage() throws RemoteException {
110         mCallback.onHotspotStopped();
111     }
112 
getPid()113     public int getPid() {
114         return mPid;
115     }
116 
getCustomConfig()117     public SoftApConfiguration getCustomConfig() {
118         return mCustomConfig;
119     }
120 }
121