Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Sep 10, 2023
1 parent 3b27cd1 commit 1c62469
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ data class RegistrationDialogContent(
data class NoDistributorDialog(
var title: String = "No distributor found",
var message: String = "You need to install a distributor " +
"for push notifications to work.\n" +
"For more information, visit\n" +
"https://unifiedpush.org/",
"for push notifications to work.\n" +
"For more information, visit\n" +
"https://unifiedpush.org/",
var okButton: String = "OK",
var ignoreButton: String = "Ignore",
var ignoreButton: String = "Ignore"
)

data class ChooseDialog(
var title: String = "Choose a distributor"
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.Context
import android.content.SharedPreferences
import java.util.UUID

internal class Store (context: Context) {
internal class Store(context: Context) {

init {
preferences = context.getSharedPreferences(PREF_MASTER, Context.MODE_PRIVATE)
Expand Down Expand Up @@ -110,4 +110,4 @@ internal class Store (context: Context) {
private val distributorLock = Object()
private lateinit var preferences: SharedPreferences
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ object UnifiedPush {
const val FEATURE_BYTES_MESSAGE = "org.unifiedpush.android.distributor.feature.BYTES_MESSAGE"

@JvmStatic
fun registerApp(context: Context,
instance: String = INSTANCE_DEFAULT,
features: ArrayList<String> = ArrayList(),
messageForDistributor: String = ""
fun registerApp(
context: Context,
instance: String = INSTANCE_DEFAULT,
features: ArrayList<String> = ArrayList(),
messageForDistributor: String = ""
) {
val store = Store(context)
val token = store.getTokenOrNew(instance)
Expand All @@ -38,30 +39,35 @@ object UnifiedPush {
}

@JvmStatic
@Deprecated("Replace with registerAppWithDialog(" +
@Deprecated(
"Replace with registerAppWithDialog(" +
"Context, String, RegistrationDialogContent, ArrayList<String>, String" +
")")
fun registerAppWithDialog(context: Context,
instance: String = INSTANCE_DEFAULT,
dialogMessage: String,
features: ArrayList<String> = ArrayList(),
messageForDistributor: String = ""
")"
)
fun registerAppWithDialog(
context: Context,
instance: String = INSTANCE_DEFAULT,
dialogMessage: String,
features: ArrayList<String> = ArrayList(),
messageForDistributor: String = ""
) {
registerAppWithDialog(
context,
instance,
RegistrationDialogContent().apply { noDistributorDialog.message = dialogMessage },
features,
messageForDistributor)
messageForDistributor
)
}

@JvmStatic
fun registerAppWithDialog(context: Context,
instance: String = INSTANCE_DEFAULT,
registrationDialogContent: RegistrationDialogContent
= RegistrationDialogContent(),
features: ArrayList<String> = ArrayList(),
messageForDistributor: String = ""
fun registerAppWithDialog(
context: Context,
instance: String = INSTANCE_DEFAULT,
registrationDialogContent: RegistrationDialogContent =
RegistrationDialogContent(),
features: ArrayList<String> = ArrayList(),
messageForDistributor: String = ""
) {
if (getDistributor(context).isNotEmpty()) {
registerApp(context, instance)
Expand All @@ -70,7 +76,7 @@ object UnifiedPush {

val distributors = getDistributors(context, features)

when(distributors.size) {
when (distributors.size) {
0 -> {
if (!Store(context).getNoDistributorAck()) {
val message = TextView(context)
Expand All @@ -86,7 +92,8 @@ object UnifiedPush {
_, _ ->
}
builder.setNegativeButton(registrationDialogContent.noDistributorDialog.ignoreButton) {
_, _ -> Store(context).saveNoDistributorAck()
_, _ ->
Store(context).saveNoDistributorAck()
}
builder.show()
} else {
Expand All @@ -97,17 +104,18 @@ object UnifiedPush {
saveDistributor(context, distributors.first())
registerApp(context, instance, features, messageForDistributor)
}
else ->{
else -> {
val builder: AlertDialog.Builder = AlertDialog.Builder(context)
builder.setTitle(registrationDialogContent.chooseDialog.title)

val distributorsArray = distributors.toTypedArray()
val distributorsNameArray = distributorsArray.map {
try {
val ai = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager.getApplicationInfo(it,
context.packageManager.getApplicationInfo(
it,
PackageManager.ApplicationInfoFlags.of(
PackageManager.GET_META_DATA.toLong()
PackageManager.GET_META_DATA.toLong()
)
)
} else {
Expand Down Expand Up @@ -149,45 +157,49 @@ object UnifiedPush {
}

@JvmStatic
fun getDistributors(context: Context,
features: ArrayList<String> = ArrayList()
fun getDistributors(
context: Context,
features: ArrayList<String> = ArrayList()
): List<String> {
return (
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager.queryBroadcastReceivers(
Intent(ACTION_REGISTER),
PackageManager.ResolveInfoFlags.of(
PackageManager.GET_META_DATA.toLong()
+ PackageManager.GET_RESOLVED_FILTER.toLong()
)
)
} else {
context.packageManager.queryBroadcastReceivers(
Intent(ACTION_REGISTER),
PackageManager.GET_RESOLVED_FILTER
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager.queryBroadcastReceivers(
Intent(ACTION_REGISTER),
PackageManager.ResolveInfoFlags.of(
PackageManager.GET_META_DATA.toLong() +
PackageManager.GET_RESOLVED_FILTER.toLong()
)
}
).mapNotNull {
val packageName = it.activityInfo.packageName

features.forEach { feature ->
it.filter?.let { filter ->
if (!filter.hasAction(feature)){
Log.i(LOG_TAG, "Found distributor $packageName" +
" without feature $feature")
return@mapNotNull null
}
} ?: run {
Log.w(LOG_TAG, "Cannot filter distributors with features")
)
} else {
context.packageManager.queryBroadcastReceivers(
Intent(ACTION_REGISTER),
PackageManager.GET_RESOLVED_FILTER
)
}
).mapNotNull {
val packageName = it.activityInfo.packageName

features.forEach { feature ->
it.filter?.let { filter ->
if (!filter.hasAction(feature)) {
Log.i(
LOG_TAG,
"Found distributor $packageName" +
" without feature $feature"
)
return@mapNotNull null
}
} ?: run {
Log.w(LOG_TAG, "Cannot filter distributors with features")
}
if (it.activityInfo.exported || packageName == context.packageName) {
Log.d(LOG_TAG, "Found distributor with package name $packageName")
packageName
} else {
null
}
}
}
if (it.activityInfo.exported || packageName == context.packageName) {
Log.d(LOG_TAG, "Found distributor with package name $packageName")
packageName
} else {
null
}
}
}

@JvmStatic
Expand All @@ -200,7 +212,7 @@ object UnifiedPush {
val store = Store(context)
store.tryGetDistributor()?.let { distributor ->
if (distributor in getDistributors(context)) {
Log.d(LOG_TAG,"Found saved distributor.")
Log.d(LOG_TAG, "Found saved distributor.")
return distributor
}
}
Expand Down

0 comments on commit 1c62469

Please sign in to comment.