Skip to content

Commit

Permalink
Merge pull request #12257 from nextcloud/bugfix/receiver-crash
Browse files Browse the repository at this point in the history
Fix Crash Register Broadcast
  • Loading branch information
AndyScherzinger authored Dec 11, 2023
2 parents fee31e6 + e8fb0f1 commit e524f1a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import android.os.BatteryManager
import android.os.PowerManager
import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.client.preferences.AppPreferencesImpl
import com.nextcloud.utils.extensions.registerBroadcastReceiver
import com.owncloud.android.datamodel.ReceiverFlag

internal class PowerManagementServiceImpl(
private val context: Context,
Expand Down Expand Up @@ -67,7 +69,11 @@ internal class PowerManagementServiceImpl(
@Suppress("MagicNumber") // 100% is 100, we're not doing Cobol
override val battery: BatteryStatus
get() {
val intent: Intent? = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
val intent: Intent? = context.registerBroadcastReceiver(
null,
IntentFilter(Intent.ACTION_BATTERY_CHANGED),
ReceiverFlag.NotExported
)
val isCharging = intent?.let {
when (it.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)) {
BatteryManager.BATTERY_PLUGGED_USB -> true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Nextcloud Android client application
*
* @author Alper Ozturk
* Copyright (C) 2023 Alper Ozturk
* Copyright (C) 2023 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.nextcloud.utils.extensions

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.owncloud.android.datamodel.ReceiverFlag

@SuppressLint("UnspecifiedRegisterReceiverFlag")
fun Context.registerBroadcastReceiver(receiver: BroadcastReceiver?, filter: IntentFilter, flag: ReceiverFlag): Intent? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(receiver, filter, flag.getId())
} else {
registerReceiver(receiver, filter)
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/owncloud/android/datamodel/ReceiverFlag.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Nextcloud Android client application
*
* @author Alper Ozturk
* Copyright (C) 2023 Alper Ozturk
* Copyright (C) 2023 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owncloud.android.datamodel

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi

enum class ReceiverFlag {
NotExported;

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun getId(): Int {
return Context.RECEIVER_NOT_EXPORTED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import com.nextcloud.client.network.ConnectivityService;
import com.nextcloud.client.network.WalledCheckCache;
import com.nextcloud.common.DNSCache;
import com.nextcloud.utils.extensions.ContextExtensionsKt;
import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.ReceiverFlag;
import com.owncloud.android.datamodel.UploadsStorageManager;

/**
Expand Down Expand Up @@ -69,7 +71,7 @@ public void onReceive(Context context, Intent intent) {
}
};

context.registerReceiver(broadcastReceiver, intentFilter);
ContextExtensionsKt.registerBroadcastReceiver(context, broadcastReceiver, intentFilter, ReceiverFlag.NotExported);
}

public static void registerPowerChangeReceiver(
Expand All @@ -96,7 +98,7 @@ public void onReceive(Context context, Intent intent) {
}
};

context.registerReceiver(broadcastReceiver, intentFilter);
ContextExtensionsKt.registerBroadcastReceiver(context, broadcastReceiver, intentFilter, ReceiverFlag.NotExported);
}

public static void registerPowerSaveReceiver(
Expand All @@ -122,6 +124,6 @@ public void onReceive(Context context, Intent intent) {
}
};

context.registerReceiver(broadcastReceiver, intentFilter);
ContextExtensionsKt.registerBroadcastReceiver(context, broadcastReceiver, intentFilter, ReceiverFlag.NotExported);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import android.os.BatteryManager
import android.os.Build
import android.os.PowerManager
import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.utils.extensions.registerBroadcastReceiver
import com.owncloud.android.datamodel.ReceiverFlag
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -137,7 +139,9 @@ class TestPowerManagementService {

@Before
fun setUp() {
whenever(context.registerReceiver(anyOrNull(), anyOrNull())).thenReturn(intent)
whenever(context.registerBroadcastReceiver(anyOrNull(), anyOrNull(), ReceiverFlag.NotExported)).thenReturn(
intent
)
}

@Test
Expand Down Expand Up @@ -193,12 +197,14 @@ class TestPowerManagementService {
// device has API level P or below
// battery status sticky intent is NOT available
whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.P)
whenever(context.registerReceiver(anyOrNull(), anyOrNull())).thenReturn(null)
whenever(context.registerBroadcastReceiver(anyOrNull(), anyOrNull(), ReceiverFlag.NotExported)).thenReturn(
null
)

// THEN
// charging flag is false
assertFalse(powerManagementService.battery.isCharging)
verify(context).registerReceiver(anyOrNull(), any())
verify(context).registerBroadcastReceiver(anyOrNull(), any(), ReceiverFlag.NotExported)
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion scripts/analysis/lint-results.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DO NOT TOUCH; GENERATED BY DRONE
<span class="mdl-layout-title">Lint Report: 3 errors and 73 warnings</span>
<span class="mdl-layout-title">Lint Report: 3 errors and 70 warnings</span>

0 comments on commit e524f1a

Please sign in to comment.