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