Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
Check boot completed receiver via package info.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Savin committed May 12, 2015
1 parent 24ed245 commit 2f054a8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ Common utils used among OPF libraries. Intended for the internal use.

Download [the latest JAR][opfutils-latest-jar] or grab via Gradle:
```groovy
compile 'org.onepf:opfutils:0.1.21'
compile 'org.onepf:opfutils:0.1.22'
```

or Maven:
```xml
<dependency>
<groupId>org.onepf</groupId>
<artifactId>opfutils</artifactId>
<version>0.1.21</version>
<version>0.1.22</version>
</dependency>
```

[opfutils-latest-jar]: https://github.com/onepf/OPFUtils/releases/download/v0.1.21/opfutils-0.1.21.jar
[opfutils-latest-jar]: https://github.com/onepf/OPFUtils/releases/download/v0.1.22/opfutils-0.1.22.jar
2 changes: 1 addition & 1 deletion opfutils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "0.1.21"
versionName "0.1.22"
}

buildTypes {
Expand Down
36 changes: 36 additions & 0 deletions opfutils/src/main/java/org/onepf/opfutils/OPFChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
Expand Down Expand Up @@ -170,6 +171,14 @@ public static void checkReceiver(@NonNull final Context context,
@Nullable final String receiverName,
@NonNull final Intent broadcastIntent,
@Nullable final String permission) {
if (Intent.ACTION_BOOT_COMPLETED.equals(broadcastIntent.getAction())) {
//If user has device with root rights, he can disable auto start for your application.
//In this case queryBroadcastReceivers() method doesn't return any receivers which is registered on ACTION_BOOT_COMPLETED.
//So we check it via package info by receiverName without intent filter checking.
checkReceiverInManifest(context, receiverName);
return;
}

final PackageManager packageManager = context.getPackageManager();

final List<ResolveInfo> receivers = packageManager
Expand Down Expand Up @@ -203,4 +212,31 @@ public static void checkReceiver(@NonNull final Context context,
+ receiverName);
}
}

private static void checkReceiverInManifest(@NonNull final Context context,
@Nullable final String receiverName) {
if (receiverName == null) {
return;
}

final String packageName = context.getPackageName();
final PackageManager packageManager = context.getPackageManager();
PackageInfo receiversInfo;
try {
receiversInfo = packageManager.getPackageInfo(
packageName, PackageManager.GET_RECEIVERS);
} catch (PackageManager.NameNotFoundException e) {
OPFLog.e(e.getMessage());
return;
}
final ActivityInfo[] receivers = receiversInfo.receivers;

for (ActivityInfo receiver : receivers) {
if (receiverName.equals(receiver.name) && packageName.equals(receiver.packageName)) {
return;
}
}

throw new IllegalStateException("Receiver " + receiverName + " hasn't been declared in AndroidManifest.xml");
}
}

0 comments on commit 2f054a8

Please sign in to comment.