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.net.wifi.WifiConfiguration;
21 import android.net.wifi.WifiManager;
22 import android.os.Binder;
23 import android.os.IBinder;
24 import android.os.Message;
25 import android.os.Messenger;
26 import android.os.RemoteException;
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 public class LocalOnlyHotspotRequestInfo implements IBinder.DeathRecipient {
36     static final int HOTSPOT_NO_ERROR = -1;
37 
38     private final int mPid;
39     private final IBinder mBinder;
40     private final RequestingApplicationDeathCallback mCallback;
41     private final Messenger mMessenger;
42 
43     /**
44      * Callback for use with LocalOnlyHotspot to unregister requesting applications upon death.
45      */
46     public interface RequestingApplicationDeathCallback {
47         /**
48          * Called when requesting app has died.
49          */
onLocalOnlyHotspotRequestorDeath(LocalOnlyHotspotRequestInfo requestor)50         void onLocalOnlyHotspotRequestorDeath(LocalOnlyHotspotRequestInfo requestor);
51     }
52 
LocalOnlyHotspotRequestInfo(@onNull IBinder binder, @NonNull Messenger messenger, @NonNull RequestingApplicationDeathCallback callback)53     LocalOnlyHotspotRequestInfo(@NonNull IBinder binder, @NonNull Messenger messenger,
54             @NonNull RequestingApplicationDeathCallback callback) {
55         mPid = Binder.getCallingPid();
56         mBinder = Preconditions.checkNotNull(binder);
57         mMessenger = Preconditions.checkNotNull(messenger);
58         mCallback = Preconditions.checkNotNull(callback);
59 
60         try {
61             mBinder.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         mBinder.unlinkToDeath(this, 0);
72     }
73 
74     /**
75      * Application requesting LocalOnlyHotspot died
76      */
77     @Override
binderDied()78     public void binderDied() {
79         mCallback.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         Message message = Message.obtain();
91         message.what = WifiManager.HOTSPOT_FAILED;
92         message.arg1 = reasonCode;
93         mMessenger.send(message);
94     }
95 
96     /**
97      * Send a HOTSPOT_STARTED message to WifiManager for the calling application with the config.
98      *
99      * @param config WifiConfiguration for the callback
100      *
101      * @throws RemoteException
102      */
sendHotspotStartedMessage(WifiConfiguration config)103     public void sendHotspotStartedMessage(WifiConfiguration config) throws RemoteException {
104         Message message = Message.obtain();
105         message.what = WifiManager.HOTSPOT_STARTED;
106         message.obj = config;
107         mMessenger.send(message);
108     }
109 
110     /**
111      * Send a HOTSPOT_STOPPED message to WifiManager for the calling application.
112      *
113      * @throws RemoteException
114      */
sendHotspotStoppedMessage()115     public void sendHotspotStoppedMessage() throws RemoteException {
116         Message message = Message.obtain();
117         message.what = WifiManager.HOTSPOT_STOPPED;
118         mMessenger.send(message);
119     }
120 
getPid()121     public int getPid() {
122         return mPid;
123     }
124 }
125