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