Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Android 14 Runtime registered broadcast receivers export behavior #1858

Merged
merged 13 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/hello_sdl_android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 33
compileSdkVersion 34
defaultConfig {
applicationId "com.sdl.hellosdlandroid"
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
versionCode 1
versionName "1.0"
resValue "string", "app_name", "Hello Sdl Android"
Expand Down
4 changes: 2 additions & 2 deletions android/sdl_android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 33
compileSdkVersion 34
defaultConfig {
minSdkVersion 16
targetSdkVersion 33
targetSdkVersion 34
versionCode 25
versionName new File(projectDir.path, ('/../../VERSION')).text.trim()
buildConfigField "String", "VERSION_NAME", '\"' + versionName + '\"'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.smartdevicelink.proxy.rpc.enums.PredefinedWindows;
import com.smartdevicelink.proxy.rpc.enums.RequestType;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.util.AndroidTools;
import com.smartdevicelink.util.DebugTool;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -134,7 +135,9 @@ public void start(CompletionListener listener) {
public void dispose() {
// send broadcast to close lock screen if open
if (context.get() != null) {
context.get().sendBroadcast(new Intent(SDLLockScreenActivity.CLOSE_LOCK_SCREEN_ACTION));
Intent intent = new Intent(SDLLockScreenActivity.CLOSE_LOCK_SCREEN_ACTION)
.setPackage(context.get().getPackageName());
context.get().sendBroadcast(intent);
try {
context.get().unregisterReceiver(mLockscreenDismissedReceiver);
lockscreenDismissReceiverRegistered = false;
Expand Down Expand Up @@ -332,7 +335,9 @@ private void launchLockScreenActivity() {
// pass in icon, background color, and custom view
if (lockScreenEnabled && isApplicationForegrounded && context.get() != null) {
if (isLockscreenDismissible && !lockscreenDismissReceiverRegistered) {
context.get().registerReceiver(mLockscreenDismissedReceiver, new IntentFilter(SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED));
AndroidTools.registerReceiver(context.get(), mLockscreenDismissedReceiver,
new IntentFilter(SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED),
Context.RECEIVER_NOT_EXPORTED);
lockscreenDismissReceiverRegistered = true;

}
Expand Down Expand Up @@ -373,7 +378,9 @@ private void closeLockScreenActivity() {
if (context.get() != null) {
LockScreenStatus status = getLockScreenStatus();
if (status == LockScreenStatus.OFF || (status == LockScreenStatus.OPTIONAL && displayMode != LockScreenConfig.DISPLAY_MODE_OPTIONAL_OR_REQUIRED)) {
context.get().sendBroadcast(new Intent(SDLLockScreenActivity.CLOSE_LOCK_SCREEN_ACTION));
Intent intent = new Intent(SDLLockScreenActivity.CLOSE_LOCK_SCREEN_ACTION)
.setPackage(context.get().getPackageName());
context.get().sendBroadcast(intent);
}
}
lastIntentUsed = null;
Expand Down Expand Up @@ -428,6 +435,7 @@ public void onImageRetrieved(Bitmap icon) {
intent.putExtra(SDLLockScreenActivity.LOCKSCREEN_DEVICE_LOGO_EXTRA, deviceLogoEnabled);
intent.putExtra(SDLLockScreenActivity.LOCKSCREEN_DEVICE_LOGO_BITMAP, deviceLogo);
if (context.get() != null) {
intent.setPackage(context.get().getPackageName());
context.get().sendBroadcast(intent);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import android.widget.TextView;

import com.smartdevicelink.R;
import com.smartdevicelink.util.AndroidTools;

public class SDLLockScreenActivity extends Activity {

Expand Down Expand Up @@ -106,7 +107,8 @@ protected void onCreate(Bundle savedInstanceState) {
lockscreenFilter.addAction(LOCKSCREEN_DEVICE_LOGO_DOWNLOADED);

// register broadcast receivers
registerReceiver(lockScreenBroadcastReceiver, lockscreenFilter);
AndroidTools.registerReceiver(this, lockScreenBroadcastReceiver, lockscreenFilter,
RECEIVER_NOT_EXPORTED);
}

@Override
Expand Down Expand Up @@ -284,7 +286,9 @@ private class SwipeUpGestureListener extends GestureDetector.SimpleOnGestureList
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
if ((event2.getY() - event1.getY()) > MIN_SWIPE_DISTANCE) {
sendBroadcast(new Intent(KEY_LOCKSCREEN_DISMISSED));
Intent intent = new Intent(KEY_LOCKSCREEN_DISMISSED)
.setPackage(getPackageName());
sendBroadcast(intent);
finish();
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ public void startUpSequence() {

IntentFilter filter = new IntentFilter();
filter.addAction(REGISTER_WITH_ROUTER_ACTION);
registerReceiver(mainServiceReceiver, filter);
AndroidTools.registerReceiver(this, mainServiceReceiver, filter, RECEIVER_EXPORTED);

if (!connectAsClient) {
if (bluetoothAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.transport.utl.SdlDeviceListener;
import com.smartdevicelink.transport.utl.TransportRecord;
import com.smartdevicelink.util.AndroidTools;
import com.smartdevicelink.util.DebugTool;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -412,7 +413,8 @@ synchronized void enterLegacyMode(final String info) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
contextWeakReference.get().registerReceiver(legacyDisconnectReceiver, intentFilter);
AndroidTools.registerReceiver(contextWeakReference.get(),
legacyDisconnectReceiver, intentFilter, Context.RECEIVER_EXPORTED);
}
} else {
new Handler(Looper.myLooper()).postDelayed(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

package com.smartdevicelink.util;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -392,4 +394,27 @@ public static void saveVehicleType(Context context, VehicleType vehicleType, Str
return null;
}
}

/**
* A helper method to handle adding flags to registering a run time broadcast receiver.
*
* @param context a context that will be used to register the receiver with
* @param receiver the receiver that will be registered
* @param filter the filter that will be use to filter intents sent to the broadcast receiver
* @param flags any flags that should be used to register the receiver. In most cases this
* will be {@link Context#RECEIVER_NOT_EXPORTED} or
* {@link Context#RECEIVER_EXPORTED}
* @see Context#registerReceiver(BroadcastReceiver, IntentFilter)
* @see Context#registerReceiver(BroadcastReceiver, IntentFilter, int)
*/
@SuppressLint("UnspecifiedRegisterReceiverFlag")
public static void registerReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter, int flags) {
if (context != null && receiver != null && filter != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.registerReceiver(receiver, filter, flags);
} else {
context.registerReceiver(receiver, filter);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ private void updateBroadcastReceiver() {
}
unregisterBroadcastReceiver();
//Re-register receiver
context.registerReceiver(broadcastReceiver, intentFilter);

AndroidTools.registerReceiver(context, broadcastReceiver, intentFilter,
Context.RECEIVER_EXPORTED);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public ServiceFinder(Context context, String packageName, final ServiceFinderCal

this.sdlMultiMap = AndroidTools.getSdlEnabledApps(context, packageName);

this.context.registerReceiver(mainServiceReceiver, new IntentFilter(this.receiverLocation));
AndroidTools.registerReceiver(this.context, mainServiceReceiver,
new IntentFilter(this.receiverLocation), Context.RECEIVER_EXPORTED);

timeoutRunnable = new Runnable() {
@Override
Expand Down