Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Issue #2079. Fix a crash when targeting Android 12 #2090

Merged
merged 1 commit into from
Mar 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import leakcanary.internal.NotificationReceiver.Action.CANCEL_NOTIFICATION
import leakcanary.internal.NotificationReceiver.Action.DUMP_HEAP
import shark.SharkLog
Expand Down Expand Up @@ -39,7 +40,12 @@ internal class NotificationReceiver : BroadcastReceiver() {
): PendingIntent {
val broadcastIntent = Intent(context, NotificationReceiver::class.java)
broadcastIntent.action = action.name
return PendingIntent.getBroadcast(context, 0, broadcastIntent, 0)
val flags = if (Build.VERSION.SDK_INT > 30) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Build.VERSION_CODES.R instead of "30" requires updating compileSDK from 29 to 30. This update caused other issues. So, it is better to do compileSDK update in a separate PR

PendingIntent.FLAG_IMMUTABLE
} else {
0
}
return PendingIntent.getBroadcast(context, 0, broadcastIntent, flags)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import android.annotation.TargetApi
import android.app.Activity
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Build.VERSION_CODES.M
import android.os.Bundle
import android.os.Build
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import com.squareup.leakcanary.core.R

@TargetApi(M) //
@TargetApi(Build.VERSION_CODES.M) //
internal class RequestStoragePermissionActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -74,7 +75,12 @@ internal class RequestStoragePermissionActivity : Activity() {
fun createPendingIntent(context: Context): PendingIntent {
val intent = Intent(context, RequestStoragePermissionActivity::class.java)
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TOP
return PendingIntent.getActivity(context, 1, intent, FLAG_UPDATE_CURRENT)
val flags = if (Build.VERSION.SDK_INT > 30) {
FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
} else {
FLAG_UPDATE_CURRENT
}
return PendingIntent.getActivity(context, 1, intent, flags)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Intent
import android.net.Uri
import android.os.AsyncTask
import android.os.Bundle
import android.os.Build
import android.view.View
import com.squareup.leakcanary.core.R
import leakcanary.internal.HeapAnalyzerService
Expand Down Expand Up @@ -178,7 +179,12 @@ internal class LeakActivity : NavigatingActivity() {
val intent = Intent(context, LeakActivity::class.java)
intent.putExtra("screens", screens)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
return PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val flags = if (Build.VERSION.SDK_INT > 30) {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}
return PendingIntent.getActivity(context, 1, intent, flags)
}

fun createIntent(context: Context): Intent {
Expand Down