Skip to content

Commit

Permalink
Merge pull request #192 from Countly/sdk34-broadcast-receiver-fix
Browse files Browse the repository at this point in the history
Fixed crash for SDK34 BroadcastReceivers
  • Loading branch information
ArtursKadikis authored Oct 17, 2023
2 parents 9c37f53 + a107868 commit d0d4a9f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Added a call to enroll users to A/B tests when getting all remote config values: 'getAllValuesAndEnroll'
* Adding app version to every request

* Fixed crash for SDK34 BroadcastReceivers, now declared broadcast receiver not exported if API is 34 and up.

* Mitigated an issue where users could not enroll to an A/B tests if enrollment request has failed
* Mitigated an issue where users could not exit from A/B tests if removal request has failed

Expand Down
13 changes: 7 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ repositories {

//required for huawei push
maven {
url "https://developer.huawei.com/repo/"
url "https://developer.huawei.com/repo/"
}
}

android {
compileSdkVersion 33
buildToolsVersion '33.0.0'
compileSdk 34
namespace 'ly.count.android.demo'

signingConfigs {
release {
Expand All @@ -45,8 +45,8 @@ android {

defaultConfig {
applicationId "ly.count.android.demo"
minSdkVersion 17
targetSdkVersion 33
minSdk 17
targetSdk 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -91,13 +91,14 @@ dependencies {
//implementation 'ly.count.android:sdk:21.11.0'
//implementation 'ly.count.android:sdk-plugin:21.11.0'


//required for FCM push
implementation 'com.google.firebase:firebase-messaging:22.0.0'
//required for huawei push
implementation 'com.huawei.hms:push:6.3.0.304'

implementation 'com.google.android.material:material:1.6.0-alpha02'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.annotation:annotation:1.3.0'
implementation 'androidx.core:core-ktx:1.7.0'
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/ly/count/android/demo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ public void onReceive(Context context, Intent intent) {
};
IntentFilter filter = new IntentFilter();
filter.addAction(CountlyPush.SECURE_NOTIFICATION_BROADCAST);
registerReceiver(messageReceiver, filter, getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
registerReceiver(messageReceiver, filter, getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null, Context.RECEIVER_NOT_EXPORTED);
}
else {
registerReceiver(messageReceiver, filter, getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
}
}
}
8 changes: 4 additions & 4 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ buildscript {
}

android {
compileSdkVersion 33
buildToolsVersion "33.0.0"
compileSdk 34
buildToolsVersion "34.0.0"

defaultConfig {
minSdkVersion 17
targetSdkVersion 33
minSdk 17
targetSdk 34

testInstrumentationRunner 'ly.count.android.sdk.test.InstrumentationTestRunner'
testHandleProfiling true
Expand Down
8 changes: 7 additions & 1 deletion sdk/src/main/java/ly/count/android/sdk/DeviceInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,13 @@ public String getDiskTotal() {
@Override
public String getBatteryLevel(Context context) {
try {
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Intent batteryIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED), null, null, Context.RECEIVER_NOT_EXPORTED);
}
else {
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
if (batteryIntent != null) {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,12 @@ public void onActivityDestroyed(Activity activity) {
IntentFilter filter = new IntentFilter();
filter.addAction(Countly.CONSENT_BROADCAST);
BroadcastReceiver consentReceiver = new ConsentBroadcastReceiver();
countlyConfigPush.application.registerReceiver(consentReceiver, filter, countlyConfigPush.application.getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
countlyConfigPush.application.registerReceiver(consentReceiver, filter, countlyConfigPush.application.getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null, Context.RECEIVER_NOT_EXPORTED);
}
else {
countlyConfigPush.application.registerReceiver(consentReceiver, filter, countlyConfigPush.application.getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
}
}

if (countlyConfigPush.provider == Countly.CountlyMessagingProvider.HMS && getPushConsent(countlyConfigPush.application)) {
Expand Down

0 comments on commit d0d4a9f

Please sign in to comment.