enable/disable BroadcastReceivers via PackageManager when Socket is connected/disconnected

This does not work with NotificationListener unfortunately.
This commit is contained in:
Andreas Shimokawa 2015-02-02 21:16:42 +01:00
parent fafcdc1d78
commit ab233279e1
2 changed files with 35 additions and 3 deletions

View File

@ -35,17 +35,17 @@
</service> </service>
<service android:name=".BluetoothCommunicationService" > <service android:name=".BluetoothCommunicationService" >
</service> </service>
<receiver android:name=".PhoneCallReceiver"> <receiver android:name=".PhoneCallReceiver" android:enabled="false">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/> <action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name=".SMSReceiver"> <receiver android:name=".SMSReceiver" android:enabled="false">
<intent-filter> <intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/> <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name=".K9Receiver"> <receiver android:name=".K9Receiver" android:enabled="false">
<intent-filter> <intent-filter>
<data android:scheme="email" /> <data android:scheme="email" />
<action android:name="com.fsck.k9.intent.action.EMAIL_RECEIVED" /> <action android:name="com.fsck.k9.intent.action.EMAIL_RECEIVED" />

View File

@ -6,8 +6,10 @@ import android.app.Service;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket; import android.bluetooth.BluetoothSocket;
import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
@ -47,6 +49,30 @@ public class BluetoothCommunicationService extends Service {
private BtSocketIoThread mBtSocketIoThread = null; private BtSocketIoThread mBtSocketIoThread = null;
private static final UUID PEBBLE_UUID = UUID.fromString("00000000-deca-fade-deca-deafdecacafe"); private static final UUID PEBBLE_UUID = UUID.fromString("00000000-deca-fade-deca-deafdecacafe");
private void setReceiversEnableState(boolean enable) {
final Class[] receiverClasses = {
PhoneCallReceiver.class,
SMSReceiver.class,
K9Receiver.class,
};
int newState;
if (enable) {
newState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
} else {
newState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
PackageManager pm = getPackageManager();
for (Class receiverClass : receiverClasses) {
ComponentName compName = new ComponentName(getApplicationContext(), receiverClass);
pm.setComponentEnabledSetting(compName, newState, PackageManager.DONT_KILL_APP);
}
}
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
@ -102,6 +128,8 @@ public class BluetoothCommunicationService extends Service {
mBtSocket.connect(); mBtSocket.connect();
mBtSocketIoThread = new BtSocketIoThread(mBtSocket.getInputStream(), mBtSocket.getOutputStream()); mBtSocketIoThread = new BtSocketIoThread(mBtSocket.getInputStream(), mBtSocket.getOutputStream());
mBtSocketIoThread.start(); mBtSocketIoThread.start();
setReceiversEnableState(true); // enable BroadcastReceivers
} }
} catch (IOException e) { } catch (IOException e) {
@ -142,6 +170,9 @@ public class BluetoothCommunicationService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
setReceiversEnableState(false); // disable BroadcastReceivers
if (mBtSocketIoThread != null) { if (mBtSocketIoThread != null) {
try { try {
mBtSocketIoThread.quit(); mBtSocketIoThread.quit();
@ -249,6 +280,7 @@ public class BluetoothCommunicationService extends Service {
} catch (IOException e) { } catch (IOException e) {
if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right
mBtSocket = null; mBtSocket = null;
setReceiversEnableState(false);
Log.i(TAG, "Bluetooth socket closed, will quit IO Thread"); Log.i(TAG, "Bluetooth socket closed, will quit IO Thread");
mQuit = true; mQuit = true;
} }