• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.bluetooth.btservice;
18 
19 import java.util.HashMap;
20 
21 import com.android.bluetooth.Utils;
22 
23 import android.app.Service;
24 import android.bluetooth.BluetoothAdapter;
25 import android.bluetooth.BluetoothDevice;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.os.IBinder;
29 import android.util.Log;
30 
31 public abstract class ProfileService extends Service {
32     private static final boolean DBG = false;
33     private static final String TAG = "BluetoothProfileService";
34 
35     //For Debugging only
36     private static HashMap<String, Integer> sReferenceCount = new HashMap<String,Integer>();
37 
38     public static final String BLUETOOTH_ADMIN_PERM =
39             android.Manifest.permission.BLUETOOTH_ADMIN;
40     public static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
41     public static final String BLUETOOTH_PRIVILEGED =
42         android.Manifest.permission.BLUETOOTH_PRIVILEGED;
43 
44     public static interface IProfileServiceBinder extends IBinder {
cleanup()45         public boolean cleanup();
46     }
47     //Profile services will not be automatically restarted.
48     //They must be explicitly restarted by AdapterService
49     private static final int PROFILE_SERVICE_MODE=Service.START_NOT_STICKY;
50     protected String mName;
51     protected BluetoothAdapter mAdapter;
52     protected IProfileServiceBinder mBinder;
53     protected boolean mStartError=false;
54     private boolean mCleaningUp = false;
55 
getName()56     protected String getName() {
57         return getClass().getSimpleName();
58     }
59 
isAvailable()60     protected boolean isAvailable() {
61         return !mStartError && !mCleaningUp;
62     }
63 
initBinder()64     protected abstract IProfileServiceBinder initBinder();
65 
start()66     protected abstract boolean start();
stop()67     protected abstract boolean stop();
create()68     protected void create() {}
cleanup()69     protected boolean cleanup() {
70         return true;
71     }
72 
ProfileService()73     protected ProfileService() {
74         mName = getName();
75         if (DBG) {
76             synchronized (sReferenceCount) {
77                 Integer refCount = sReferenceCount.get(mName);
78                 if (refCount==null) {
79                     refCount = 1;
80                 } else {
81                     refCount = refCount+1;
82                 }
83                 sReferenceCount.put(mName, refCount);
84                 if (DBG) log("REFCOUNT: CREATED. INSTANCE_COUNT=" +refCount);
85             }
86         }
87     }
88 
finalize()89     protected void finalize() {
90         if (DBG) {
91             synchronized (sReferenceCount) {
92                 Integer refCount = sReferenceCount.get(mName);
93                 if (refCount!=null) {
94                     refCount = refCount-1;
95                 } else {
96                     refCount = 0;
97                 }
98                 sReferenceCount.put(mName, refCount);
99                 log("REFCOUNT: FINALIZED. INSTANCE_COUNT=" +refCount);
100             }
101         }
102     }
103 
104     @Override
onCreate()105     public void onCreate() {
106         if (DBG) log("onCreate");
107         super.onCreate();
108         mAdapter = BluetoothAdapter.getDefaultAdapter();
109         mBinder = initBinder();
110         create();
111     }
112 
onStartCommand(Intent intent, int flags, int startId)113     public int onStartCommand(Intent intent, int flags, int startId) {
114         if (DBG) log("onStartCommand()");
115         AdapterService adapterService = AdapterService.getAdapterService();
116         if (adapterService != null) {
117             adapterService.addProfile(this);
118         } else {
119             Log.w(TAG, "Could not add this profile because AdapterService is null.");
120         }
121 
122         if (mStartError || mAdapter == null) {
123             Log.w(mName, "Stopping profile service: device does not have BT");
124             doStop(intent);
125             return PROFILE_SERVICE_MODE;
126         }
127 
128         if (checkCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM)!=PackageManager.PERMISSION_GRANTED) {
129             Log.e(mName, "Permission denied!");
130             return PROFILE_SERVICE_MODE;
131         }
132 
133         if (intent == null) {
134             Log.d(mName, "Restarting profile service...");
135             return PROFILE_SERVICE_MODE;
136         } else {
137             String action = intent.getStringExtra(AdapterService.EXTRA_ACTION);
138             if (AdapterService.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
139                 int state= intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
140                 if(state==BluetoothAdapter.STATE_OFF) {
141                     Log.d(mName, "Received stop request...Stopping profile...");
142                     doStop(intent);
143                 } else if (state == BluetoothAdapter.STATE_ON) {
144                     Log.d(mName, "Received start request. Starting profile...");
145                     doStart(intent);
146                 }
147             }
148         }
149         return PROFILE_SERVICE_MODE;
150     }
151 
onBind(Intent intent)152     public IBinder onBind(Intent intent) {
153         if (DBG) log("onBind");
154         if (mAdapter != null && mBinder == null) {
155             // initBinder returned null, you can't bind
156             throw new UnsupportedOperationException("Cannot bind to " + mName);
157         }
158         return mBinder;
159     }
160 
onUnbind(Intent intent)161     public boolean onUnbind(Intent intent) {
162         if (DBG) log("onUnbind");
163         return super.onUnbind(intent);
164     }
165 
166     // for dumpsys support
dump(StringBuilder sb)167     public void dump(StringBuilder sb) {
168         sb.append("\nProfile: " + mName + "\n");
169     }
170 
dumpProto(BluetoothProto.BluetoothLog proto)171     public void dumpProto(BluetoothProto.BluetoothLog proto) {
172         // Do nothing
173     }
174 
175     // with indenting for subclasses
println(StringBuilder sb, String s)176     public static void println(StringBuilder sb, String s) {
177         sb.append("  ");
178         sb.append(s);
179         sb.append("\n");
180     }
181 
182     @Override
onDestroy()183     public void onDestroy() {
184         if (DBG) log("Destroying service.");
185         AdapterService adapterService = AdapterService.getAdapterService();
186         if (adapterService != null) adapterService.removeProfile(this);
187 
188         if (mCleaningUp) {
189             if (DBG) log("Cleanup already started... Skipping cleanup()...");
190         } else {
191             if (DBG) log("cleanup()");
192             mCleaningUp = true;
193             cleanup();
194             if (mBinder != null) {
195                 mBinder.cleanup();
196                 mBinder= null;
197             }
198         }
199         super.onDestroy();
200         mAdapter = null;
201     }
202 
doStart(Intent intent)203     private void doStart(Intent intent) {
204         //Start service
205         if (mAdapter == null) {
206             Log.e(mName, "Error starting profile. BluetoothAdapter is null");
207         } else {
208             if (DBG) log("start()");
209             mStartError = !start();
210             if (!mStartError) {
211                 notifyProfileServiceStateChanged(BluetoothAdapter.STATE_ON);
212             } else {
213                 Log.e(mName, "Error starting profile. BluetoothAdapter is null");
214             }
215         }
216     }
217 
doStop(Intent intent)218     private void doStop(Intent intent) {
219         if (stop()) {
220             if (DBG) log("stop()");
221             notifyProfileServiceStateChanged(BluetoothAdapter.STATE_OFF);
222             stopSelf();
223         } else {
224             Log.e(mName, "Unable to stop profile");
225         }
226     }
227 
notifyProfileServiceStateChanged(int state)228     protected void notifyProfileServiceStateChanged(int state) {
229         //Notify adapter service
230         AdapterService adapterService = AdapterService.getAdapterService();
231         if (adapterService != null) {
232             adapterService.onProfileServiceStateChanged(getClass().getName(), state);
233         }
234     }
235 
getDevice(byte[] address)236     protected BluetoothDevice getDevice(byte[] address) {
237         if(mAdapter != null){
238             return mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address));
239         }
240         return null;
241     }
242 
log(String msg)243     protected void log(String msg) {
244         Log.d(mName, msg);
245     }
246 }
247