mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-26 02:25:50 +01:00
register/unregister receivers at runtime instead of enabling/disabling them via packagemanger
This commit is contained in:
parent
85777f99e4
commit
a9186791dc
@ -210,56 +210,7 @@
|
||||
</intent-filter>
|
||||
</service>
|
||||
<service android:name=".service.DeviceCommunicationService" />
|
||||
|
||||
<receiver
|
||||
android:name=".externalevents.PhoneCallReceiver"
|
||||
android:enabled="false">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.PHONE_STATE" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".externalevents.SMSReceiver"
|
||||
android:enabled="false">
|
||||
<intent-filter>
|
||||
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".externalevents.TimeChangeReceiver"
|
||||
android:enabled="false">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
|
||||
<action android:name="android.intent.action.TIME_SET" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".externalevents.K9Receiver"
|
||||
android:enabled="false">
|
||||
<intent-filter>
|
||||
<data android:scheme="email" />
|
||||
|
||||
<action android:name="com.fsck.k9.intent.action.EMAIL_RECEIVED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".externalevents.PebbleReceiver"
|
||||
android:enabled="false">
|
||||
<intent-filter>
|
||||
<action android:name="com.getpebble.action.SEND_NOTIFICATION" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".externalevents.MusicPlaybackReceiver"
|
||||
android:enabled="false">
|
||||
<intent-filter>
|
||||
<action android:name="com.android.music.metachanged"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
<receiver
|
||||
android:name=".externalevents.BluetoothStateChangeReceiver"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
|
@ -24,6 +24,12 @@ import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
||||
@ -80,6 +86,13 @@ public class DeviceCommunicationService extends Service {
|
||||
private GBDevice mGBDevice = null;
|
||||
private DeviceSupport mDeviceSupport;
|
||||
|
||||
private PhoneCallReceiver mPhoneCallReceiver = null;
|
||||
private SMSReceiver mSMSReceiver = null;
|
||||
private K9Receiver mK9Receiver = null;
|
||||
private PebbleReceiver mPebbleReceiver = null;
|
||||
private MusicPlaybackReceiver mMusicPlaybackReceiver = null;
|
||||
private TimeChangeReceiver mTimeChangeReceiver = null;
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
@ -89,7 +102,7 @@ public class DeviceCommunicationService extends Service {
|
||||
if (mGBDevice.equals(device)) {
|
||||
mGBDevice = device;
|
||||
boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized());
|
||||
GB.setReceiversEnableState(enableReceivers, context);
|
||||
setReceiversEnableState(enableReceivers);
|
||||
GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), context);
|
||||
} else {
|
||||
LOG.error("Got ACTION_DEVICE_CHANGED from unexpected device: " + mGBDevice);
|
||||
@ -321,13 +334,79 @@ public class DeviceCommunicationService extends Service {
|
||||
return mGBDevice != null && mGBDevice.isInitialized();
|
||||
}
|
||||
|
||||
|
||||
private void setReceiversEnableState(boolean enable) {
|
||||
LOG.info("Setting broadcast receivers to: " + enable);
|
||||
|
||||
if (enable) {
|
||||
if (mPhoneCallReceiver == null) {
|
||||
mPhoneCallReceiver = new PhoneCallReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction("android.intent.action.PHONE_STATE");
|
||||
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
|
||||
registerReceiver(mPhoneCallReceiver, filter);
|
||||
}
|
||||
if (mSMSReceiver == null) {
|
||||
mSMSReceiver = new SMSReceiver();
|
||||
registerReceiver(mSMSReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
|
||||
}
|
||||
if (mK9Receiver == null) {
|
||||
mK9Receiver = new K9Receiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addDataScheme("email");
|
||||
filter.addAction("com.fsck.k9.intent.action.EMAIL_RECEIVED");
|
||||
registerReceiver(mK9Receiver, filter);
|
||||
}
|
||||
if (mPebbleReceiver == null) {
|
||||
mPebbleReceiver = new PebbleReceiver();
|
||||
registerReceiver(mPebbleReceiver, new IntentFilter("com.getpebble.action.SEND_NOTIFICATION"));
|
||||
}
|
||||
if (mMusicPlaybackReceiver == null) {
|
||||
mMusicPlaybackReceiver = new MusicPlaybackReceiver();
|
||||
registerReceiver(mMusicPlaybackReceiver, new IntentFilter("com.android.music.metachanged"));
|
||||
}
|
||||
if (mTimeChangeReceiver == null) {
|
||||
mTimeChangeReceiver = new TimeChangeReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction("android.intent.action.TIME_SET");
|
||||
filter.addAction("android.intent.action.TIMEZONE_CHANGED");
|
||||
registerReceiver(mTimeChangeReceiver, filter);
|
||||
}
|
||||
} else {
|
||||
if (mPhoneCallReceiver != null) {
|
||||
unregisterReceiver(mPhoneCallReceiver);
|
||||
mPhoneCallReceiver = null;
|
||||
}
|
||||
if (mSMSReceiver != null) {
|
||||
unregisterReceiver(mSMSReceiver);
|
||||
mSMSReceiver = null;
|
||||
}
|
||||
if (mK9Receiver != null) {
|
||||
unregisterReceiver(mK9Receiver);
|
||||
mK9Receiver = null;
|
||||
}
|
||||
if (mPebbleReceiver != null) {
|
||||
unregisterReceiver(mPebbleReceiver);
|
||||
mPebbleReceiver = null;
|
||||
}
|
||||
if (mMusicPlaybackReceiver != null) {
|
||||
unregisterReceiver(mMusicPlaybackReceiver);
|
||||
mMusicPlaybackReceiver = null;
|
||||
}
|
||||
if (mTimeChangeReceiver != null) {
|
||||
unregisterReceiver(mTimeChangeReceiver);
|
||||
mTimeChangeReceiver = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
LOG.debug("DeviceCommunicationService is being destroyed");
|
||||
super.onDestroy();
|
||||
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
||||
GB.setReceiversEnableState(false, this); // disable BroadcastReceivers
|
||||
setReceiversEnableState(false); // disable BroadcastReceivers
|
||||
|
||||
setDeviceSupport(null);
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
@ -5,7 +5,6 @@ import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
@ -29,12 +28,6 @@ import nodomain.freeyourgadget.gadgetbridge.GBEnvironment;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
|
||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver;
|
||||
|
||||
public class GB {
|
||||
public static final int NOTIFICATION_ID = 1;
|
||||
@ -86,35 +79,6 @@ public class GB {
|
||||
nm.cancel(id);
|
||||
}
|
||||
|
||||
public static void setReceiversEnableState(boolean enable, Context context) {
|
||||
LOG.info("Setting broadcast receivers to: " + enable);
|
||||
final Class<?>[] receiverClasses = {
|
||||
PhoneCallReceiver.class,
|
||||
SMSReceiver.class,
|
||||
K9Receiver.class,
|
||||
PebbleReceiver.class,
|
||||
MusicPlaybackReceiver.class,
|
||||
TimeChangeReceiver.class,
|
||||
//NotificationListener.class, // disabling this leads to loss of permission to read notifications
|
||||
};
|
||||
|
||||
int newState;
|
||||
|
||||
if (enable) {
|
||||
newState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
|
||||
} else {
|
||||
newState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
|
||||
}
|
||||
|
||||
PackageManager pm = context.getPackageManager();
|
||||
|
||||
for (Class<?> receiverClass : receiverClasses) {
|
||||
ComponentName compName = new ComponentName(context, receiverClass);
|
||||
|
||||
pm.setComponentEnabledSetting(compName, newState, PackageManager.DONT_KILL_APP);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isBluetoothEnabled() {
|
||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
return adapter != null && adapter.isEnabled();
|
||||
|
Loading…
Reference in New Issue
Block a user