Skip to content

Commit

Permalink
Fix DeadSystemException in android 12 (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec authored Oct 24, 2021
1 parent af58116 commit 90f2c1b
Showing 1 changed file with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.s2msp = SecureSmsProxyFacade.instance(this);
registerReceiver();
registerBroadcastReceiver();

new NotificationService(this).registerChannel();

Expand Down Expand Up @@ -192,14 +192,22 @@ private void updateBottomNavigation() {
item.setVisible(ApplicationPreferences.instance().getTestAlarmEnabled(this));
}

@Override
protected void onPause() {
unregisterBroadcastReceiver();
super.onPause();
}

@Override
protected void onResume() {
super.onResume();
registerBroadcastReceiver();
updateBottomNavigation();
BillingManager billingManager = this.billingAdapter.getBillingManager();
if (billingManager != null && billingManager.getBillingClientResponseCode() == BillingResponseCode.OK) {
billingManager.queryPurchases();
}
refresh();
}

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Expand Down Expand Up @@ -297,25 +305,6 @@ private boolean isAcquireFragmentShown() {
return donationFragment != null && donationFragment.isVisible();
}

private void registerReceiver() {
broadcastReceiver = new BroadcastReceiver() {
@SuppressLint("DefaultLocale")
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction()) &&
new ShiftService(context).getState() == OnOffState.ON) {
LowSignalService.enqueueWork(context);
}
refresh();
}
};
IntentFilter filter = new IntentFilter(Action.REFRESH.getId());
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
filter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
registerReceiver(broadcastReceiver, filter);
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Expand All @@ -324,13 +313,38 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {

@Override
protected void onDestroy() {
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
unregisterBroadcastReceiver();
if (billingAdapter != null) {
billingAdapter.destroy();
}
super.onDestroy();
}

private void registerBroadcastReceiver() {
if (broadcastReceiver == null) {
broadcastReceiver = new BroadcastReceiver() {
@SuppressLint("DefaultLocale")
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction()) &&
new ShiftService(context).getState() == OnOffState.ON) {
LowSignalService.enqueueWork(context);
}
refresh();
}
};
IntentFilter filter = new IntentFilter(Action.REFRESH.getId());
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
filter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
registerReceiver(broadcastReceiver, filter);
}
}

private void unregisterBroadcastReceiver() {
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
}
}

0 comments on commit 90f2c1b

Please sign in to comment.