Loading android/app/src/com/android/bluetooth/btservice/ProfileService.java +117 −80 Original line number Original line Diff line number Diff line Loading @@ -16,12 +16,18 @@ package com.android.bluetooth.btservice; package com.android.bluetooth.btservice; import android.app.ActivityManager; import android.app.Service; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.content.pm.PackageManager; import android.os.IBinder; import android.os.IBinder; import android.os.UserHandle; import android.os.UserManager; import android.util.Log; import android.util.Log; import com.android.bluetooth.Utils; import com.android.bluetooth.Utils; Loading @@ -31,7 +37,6 @@ import com.android.bluetooth.Utils; */ */ public abstract class ProfileService extends Service { public abstract class ProfileService extends Service { private static final boolean DBG = false; private static final boolean DBG = false; private static final String TAG = "BluetoothProfileService"; public static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; public static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; public static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; public static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; Loading @@ -48,18 +53,19 @@ public abstract class ProfileService extends Service { //Profile services will not be automatically restarted. //Profile services will not be automatically restarted. //They must be explicitly restarted by AdapterService //They must be explicitly restarted by AdapterService private static final int PROFILE_SERVICE_MODE = Service.START_NOT_STICKY; private static final int PROFILE_SERVICE_MODE = Service.START_NOT_STICKY; protected String mName; protected final String mName; protected BluetoothAdapter mAdapter; protected BluetoothAdapter mAdapter; protected AdapterService mAdapterService; protected IProfileServiceBinder mBinder; protected IProfileServiceBinder mBinder; protected boolean mStartError = false; private BroadcastReceiver mUserSwitchedReceiver; private boolean mCleaningUp = false; private boolean mProfileStarted = false; protected String getName() { protected String getName() { return getClass().getSimpleName(); return getClass().getSimpleName(); } } protected boolean isAvailable() { protected boolean isAvailable() { return !mStartError && !mCleaningUp; return mProfileStarted; } } /** /** Loading Loading @@ -93,6 +99,16 @@ public abstract class ProfileService extends Service { */ */ protected void cleanup() {} protected void cleanup() {} /** * @param userId is equivalent to the result of ActivityManager.getCurrentUser() */ protected void setCurrentUser(int userId) {} /** * @param userId is equivalent to the result of ActivityManager.getCurrentUser() */ protected void setUserUnlocked(int userId) {} protected ProfileService() { protected ProfileService() { mName = getName(); mName = getName(); } } Loading @@ -113,18 +129,6 @@ public abstract class ProfileService extends Service { if (DBG) { if (DBG) { log("onStartCommand()"); log("onStartCommand()"); } } AdapterService adapterService = AdapterService.getAdapterService(); if (adapterService != null) { adapterService.addProfile(this); } else { Log.w(TAG, "Could not add this profile because AdapterService is null."); } if (mStartError || mAdapter == null) { Log.w(mName, "Stopping profile service: device does not have BT"); doStop(intent); return PROFILE_SERVICE_MODE; } if (checkCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM) if (checkCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM) != PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED) { Loading @@ -133,20 +137,17 @@ public abstract class ProfileService extends Service { } } if (intent == null) { if (intent == null) { Log.d(mName, "Restarting profile service..."); Log.d(mName, "onStartCommand ignoring null intent."); return PROFILE_SERVICE_MODE; return PROFILE_SERVICE_MODE; } else { } String action = intent.getStringExtra(AdapterService.EXTRA_ACTION); String action = intent.getStringExtra(AdapterService.EXTRA_ACTION); if (AdapterService.ACTION_SERVICE_STATE_CHANGED.equals(action)) { if (AdapterService.ACTION_SERVICE_STATE_CHANGED.equals(action)) { int state = int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); if (state == BluetoothAdapter.STATE_OFF) { if (state == BluetoothAdapter.STATE_OFF) { Log.d(mName, "Received stop request...Stopping profile..."); doStop(); doStop(intent); } else if (state == BluetoothAdapter.STATE_ON) { } else if (state == BluetoothAdapter.STATE_ON) { Log.d(mName, "Received start request. Starting profile..."); doStart(); doStart(intent); } } } } } return PROFILE_SERVICE_MODE; return PROFILE_SERVICE_MODE; Loading @@ -172,16 +173,32 @@ public abstract class ProfileService extends Service { return super.onUnbind(intent); return super.onUnbind(intent); } } // for dumpsys support /** * Support dumping profile-specific information for dumpsys * * @param sb StringBuilder from the profile. */ public void dump(StringBuilder sb) { public void dump(StringBuilder sb) { sb.append("\nProfile: " + mName + "\n"); sb.append("\nProfile: "); sb.append(mName); sb.append("\n"); } } /** * Support dumping scan events from GattService * * @param proto */ public void dumpProto(BluetoothProto.BluetoothLog proto) { public void dumpProto(BluetoothProto.BluetoothLog proto) { // Do nothing // Do nothing } } // with indenting for subclasses /** * Append an indented String for adding dumpsys support to subclasses. * * @param sb StringBuilder from the profile. * @param s String to indent and append. */ public static void println(StringBuilder sb, String s) { public static void println(StringBuilder sb, String s) { sb.append(" "); sb.append(" "); sb.append(s); sb.append(s); Loading @@ -190,68 +207,88 @@ public abstract class ProfileService extends Service { @Override @Override public void onDestroy() { public void onDestroy() { if (DBG) { if (mAdapterService != null) { log("Destroying service."); mAdapterService.removeProfile(this); } AdapterService adapterService = AdapterService.getAdapterService(); if (adapterService != null) { adapterService.removeProfile(this); } } if (mCleaningUp) { if (DBG) { log("Cleanup already started... Skipping cleanup()..."); } } else { if (DBG) { log("cleanup()"); } mCleaningUp = true; cleanup(); cleanup(); if (mBinder != null) { if (mBinder != null) { mBinder.cleanup(); mBinder.cleanup(); mBinder = null; mBinder = null; } } } super.onDestroy(); super.onDestroy(); mAdapter = null; mAdapter = null; } } private void doStart(Intent intent) { private void doStart() { //Start service if (mAdapter == null) { if (mAdapter == null) { Log.e(mName, "Error starting profile. BluetoothAdapter is null"); Log.w(mName, "Can't start profile service: device does not have BT"); } else { return; if (DBG) { log("start()"); } } mStartError = !start(); if (!mStartError) { mAdapterService = AdapterService.getAdapterService(); notifyProfileServiceStateChanged(BluetoothAdapter.STATE_ON); if (mAdapterService == null) { } else { Log.w(mName, "Could not add this profile because AdapterService is null."); Log.e(mName, "Error starting profile. start() returned false."); return; } } mAdapterService.addProfile(this); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_USER_SWITCHED); filter.addAction(Intent.ACTION_USER_UNLOCKED); mUserSwitchedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); if (userId == UserHandle.USER_NULL) { Log.e(mName, "userChangeReceiver received an invalid EXTRA_USER_HANDLE"); return; } if (Intent.ACTION_USER_SWITCHED.equals(action)) { Log.d(mName, "User switched to userId " + userId); setCurrentUser(userId); } else if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) { Log.d(mName, "Unlocked userId " + userId); setUserUnlocked(userId); } } }; getApplicationContext().registerReceiver(mUserSwitchedReceiver, filter); int currentUserId = ActivityManager.getCurrentUser(); setCurrentUser(currentUserId); UserManager userManager = UserManager.get(getApplicationContext()); if (userManager.isUserUnlocked(currentUserId)) { setUserUnlocked(currentUserId); } mProfileStarted = start(); if (!mProfileStarted) { Log.e(mName, "Error starting profile. start() returned false."); return; } } mAdapterService.onProfileServiceStateChanged(getClass().getName(), BluetoothAdapter.STATE_ON); } } private void doStop(Intent intent) { private void doStop() { if (stop()) { if (!mProfileStarted) { if (DBG) { Log.w(mName, "doStop() called, but the profile is not running."); log("stop()"); } } notifyProfileServiceStateChanged(BluetoothAdapter.STATE_OFF); mProfileStarted = false; stopSelf(); if (mAdapterService != null) { } else { mAdapterService.onProfileServiceStateChanged(getClass().getName(), BluetoothAdapter.STATE_OFF); } if (!stop()) { Log.e(mName, "Unable to stop profile"); Log.e(mName, "Unable to stop profile"); } } if (mUserSwitchedReceiver != null) { getApplicationContext().unregisterReceiver(mUserSwitchedReceiver); mUserSwitchedReceiver = null; } } stopSelf(); protected void notifyProfileServiceStateChanged(int state) { //Notify adapter service AdapterService adapterService = AdapterService.getAdapterService(); if (adapterService != null) { adapterService.onProfileServiceStateChanged(getClass().getName(), state); } } } protected BluetoothDevice getDevice(byte[] address) { protected BluetoothDevice getDevice(byte[] address) { Loading android/app/src/com/android/bluetooth/mapclient/MapClientService.java +1 −2 Original line number Original line Diff line number Diff line Loading @@ -270,14 +270,13 @@ public class MapClientService extends ProfileService { } } mAdapter = BluetoothAdapter.getDefaultAdapter(); mAdapter = BluetoothAdapter.getDefaultAdapter(); mStartError = false; IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_SDP_RECORD); filter.addAction(BluetoothDevice.ACTION_SDP_RECORD); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); registerReceiver(mMapReceiver, filter); registerReceiver(mMapReceiver, filter); removeUncleanAccounts(); removeUncleanAccounts(); return !mStartError; return true; } } @Override @Override Loading Loading
android/app/src/com/android/bluetooth/btservice/ProfileService.java +117 −80 Original line number Original line Diff line number Diff line Loading @@ -16,12 +16,18 @@ package com.android.bluetooth.btservice; package com.android.bluetooth.btservice; import android.app.ActivityManager; import android.app.Service; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.content.pm.PackageManager; import android.os.IBinder; import android.os.IBinder; import android.os.UserHandle; import android.os.UserManager; import android.util.Log; import android.util.Log; import com.android.bluetooth.Utils; import com.android.bluetooth.Utils; Loading @@ -31,7 +37,6 @@ import com.android.bluetooth.Utils; */ */ public abstract class ProfileService extends Service { public abstract class ProfileService extends Service { private static final boolean DBG = false; private static final boolean DBG = false; private static final String TAG = "BluetoothProfileService"; public static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; public static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; public static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; public static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; Loading @@ -48,18 +53,19 @@ public abstract class ProfileService extends Service { //Profile services will not be automatically restarted. //Profile services will not be automatically restarted. //They must be explicitly restarted by AdapterService //They must be explicitly restarted by AdapterService private static final int PROFILE_SERVICE_MODE = Service.START_NOT_STICKY; private static final int PROFILE_SERVICE_MODE = Service.START_NOT_STICKY; protected String mName; protected final String mName; protected BluetoothAdapter mAdapter; protected BluetoothAdapter mAdapter; protected AdapterService mAdapterService; protected IProfileServiceBinder mBinder; protected IProfileServiceBinder mBinder; protected boolean mStartError = false; private BroadcastReceiver mUserSwitchedReceiver; private boolean mCleaningUp = false; private boolean mProfileStarted = false; protected String getName() { protected String getName() { return getClass().getSimpleName(); return getClass().getSimpleName(); } } protected boolean isAvailable() { protected boolean isAvailable() { return !mStartError && !mCleaningUp; return mProfileStarted; } } /** /** Loading Loading @@ -93,6 +99,16 @@ public abstract class ProfileService extends Service { */ */ protected void cleanup() {} protected void cleanup() {} /** * @param userId is equivalent to the result of ActivityManager.getCurrentUser() */ protected void setCurrentUser(int userId) {} /** * @param userId is equivalent to the result of ActivityManager.getCurrentUser() */ protected void setUserUnlocked(int userId) {} protected ProfileService() { protected ProfileService() { mName = getName(); mName = getName(); } } Loading @@ -113,18 +129,6 @@ public abstract class ProfileService extends Service { if (DBG) { if (DBG) { log("onStartCommand()"); log("onStartCommand()"); } } AdapterService adapterService = AdapterService.getAdapterService(); if (adapterService != null) { adapterService.addProfile(this); } else { Log.w(TAG, "Could not add this profile because AdapterService is null."); } if (mStartError || mAdapter == null) { Log.w(mName, "Stopping profile service: device does not have BT"); doStop(intent); return PROFILE_SERVICE_MODE; } if (checkCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM) if (checkCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM) != PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED) { Loading @@ -133,20 +137,17 @@ public abstract class ProfileService extends Service { } } if (intent == null) { if (intent == null) { Log.d(mName, "Restarting profile service..."); Log.d(mName, "onStartCommand ignoring null intent."); return PROFILE_SERVICE_MODE; return PROFILE_SERVICE_MODE; } else { } String action = intent.getStringExtra(AdapterService.EXTRA_ACTION); String action = intent.getStringExtra(AdapterService.EXTRA_ACTION); if (AdapterService.ACTION_SERVICE_STATE_CHANGED.equals(action)) { if (AdapterService.ACTION_SERVICE_STATE_CHANGED.equals(action)) { int state = int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); if (state == BluetoothAdapter.STATE_OFF) { if (state == BluetoothAdapter.STATE_OFF) { Log.d(mName, "Received stop request...Stopping profile..."); doStop(); doStop(intent); } else if (state == BluetoothAdapter.STATE_ON) { } else if (state == BluetoothAdapter.STATE_ON) { Log.d(mName, "Received start request. Starting profile..."); doStart(); doStart(intent); } } } } } return PROFILE_SERVICE_MODE; return PROFILE_SERVICE_MODE; Loading @@ -172,16 +173,32 @@ public abstract class ProfileService extends Service { return super.onUnbind(intent); return super.onUnbind(intent); } } // for dumpsys support /** * Support dumping profile-specific information for dumpsys * * @param sb StringBuilder from the profile. */ public void dump(StringBuilder sb) { public void dump(StringBuilder sb) { sb.append("\nProfile: " + mName + "\n"); sb.append("\nProfile: "); sb.append(mName); sb.append("\n"); } } /** * Support dumping scan events from GattService * * @param proto */ public void dumpProto(BluetoothProto.BluetoothLog proto) { public void dumpProto(BluetoothProto.BluetoothLog proto) { // Do nothing // Do nothing } } // with indenting for subclasses /** * Append an indented String for adding dumpsys support to subclasses. * * @param sb StringBuilder from the profile. * @param s String to indent and append. */ public static void println(StringBuilder sb, String s) { public static void println(StringBuilder sb, String s) { sb.append(" "); sb.append(" "); sb.append(s); sb.append(s); Loading @@ -190,68 +207,88 @@ public abstract class ProfileService extends Service { @Override @Override public void onDestroy() { public void onDestroy() { if (DBG) { if (mAdapterService != null) { log("Destroying service."); mAdapterService.removeProfile(this); } AdapterService adapterService = AdapterService.getAdapterService(); if (adapterService != null) { adapterService.removeProfile(this); } } if (mCleaningUp) { if (DBG) { log("Cleanup already started... Skipping cleanup()..."); } } else { if (DBG) { log("cleanup()"); } mCleaningUp = true; cleanup(); cleanup(); if (mBinder != null) { if (mBinder != null) { mBinder.cleanup(); mBinder.cleanup(); mBinder = null; mBinder = null; } } } super.onDestroy(); super.onDestroy(); mAdapter = null; mAdapter = null; } } private void doStart(Intent intent) { private void doStart() { //Start service if (mAdapter == null) { if (mAdapter == null) { Log.e(mName, "Error starting profile. BluetoothAdapter is null"); Log.w(mName, "Can't start profile service: device does not have BT"); } else { return; if (DBG) { log("start()"); } } mStartError = !start(); if (!mStartError) { mAdapterService = AdapterService.getAdapterService(); notifyProfileServiceStateChanged(BluetoothAdapter.STATE_ON); if (mAdapterService == null) { } else { Log.w(mName, "Could not add this profile because AdapterService is null."); Log.e(mName, "Error starting profile. start() returned false."); return; } } mAdapterService.addProfile(this); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_USER_SWITCHED); filter.addAction(Intent.ACTION_USER_UNLOCKED); mUserSwitchedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); if (userId == UserHandle.USER_NULL) { Log.e(mName, "userChangeReceiver received an invalid EXTRA_USER_HANDLE"); return; } if (Intent.ACTION_USER_SWITCHED.equals(action)) { Log.d(mName, "User switched to userId " + userId); setCurrentUser(userId); } else if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) { Log.d(mName, "Unlocked userId " + userId); setUserUnlocked(userId); } } }; getApplicationContext().registerReceiver(mUserSwitchedReceiver, filter); int currentUserId = ActivityManager.getCurrentUser(); setCurrentUser(currentUserId); UserManager userManager = UserManager.get(getApplicationContext()); if (userManager.isUserUnlocked(currentUserId)) { setUserUnlocked(currentUserId); } mProfileStarted = start(); if (!mProfileStarted) { Log.e(mName, "Error starting profile. start() returned false."); return; } } mAdapterService.onProfileServiceStateChanged(getClass().getName(), BluetoothAdapter.STATE_ON); } } private void doStop(Intent intent) { private void doStop() { if (stop()) { if (!mProfileStarted) { if (DBG) { Log.w(mName, "doStop() called, but the profile is not running."); log("stop()"); } } notifyProfileServiceStateChanged(BluetoothAdapter.STATE_OFF); mProfileStarted = false; stopSelf(); if (mAdapterService != null) { } else { mAdapterService.onProfileServiceStateChanged(getClass().getName(), BluetoothAdapter.STATE_OFF); } if (!stop()) { Log.e(mName, "Unable to stop profile"); Log.e(mName, "Unable to stop profile"); } } if (mUserSwitchedReceiver != null) { getApplicationContext().unregisterReceiver(mUserSwitchedReceiver); mUserSwitchedReceiver = null; } } stopSelf(); protected void notifyProfileServiceStateChanged(int state) { //Notify adapter service AdapterService adapterService = AdapterService.getAdapterService(); if (adapterService != null) { adapterService.onProfileServiceStateChanged(getClass().getName(), state); } } } protected BluetoothDevice getDevice(byte[] address) { protected BluetoothDevice getDevice(byte[] address) { Loading
android/app/src/com/android/bluetooth/mapclient/MapClientService.java +1 −2 Original line number Original line Diff line number Diff line Loading @@ -270,14 +270,13 @@ public class MapClientService extends ProfileService { } } mAdapter = BluetoothAdapter.getDefaultAdapter(); mAdapter = BluetoothAdapter.getDefaultAdapter(); mStartError = false; IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_SDP_RECORD); filter.addAction(BluetoothDevice.ACTION_SDP_RECORD); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); registerReceiver(mMapReceiver, filter); registerReceiver(mMapReceiver, filter); removeUncleanAccounts(); removeUncleanAccounts(); return !mStartError; return true; } } @Override @Override Loading