diff --git a/.idea/analyzers/pmd/ruleset.xml b/.idea/analyzers/pmd/ruleset.xml
deleted file mode 100644
index 1bc2f2bb..00000000
--- a/.idea/analyzers/pmd/ruleset.xml
+++ /dev/null
@@ -1,99 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml
index af61e7de..af5db421 100644
--- a/.idea/checkstyle-idea.xml
+++ b/.idea/checkstyle-idea.xml
@@ -3,10 +3,10 @@
diff --git a/build.gradle b/build.gradle
index 02bbbf42..5a43eaa2 100644
--- a/build.gradle
+++ b/build.gradle
@@ -29,6 +29,7 @@ buildscript {
//noinspection GradleDependency
classpath 'com.android.tools.build:gradle:1.1.2'
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.0'
+ classpath 'com.noveogroup.android:check:1.1.2'
}
}
diff --git a/.idea/analyzers/checkstyle/checkstyle.xml b/config/checkstyle.xml
similarity index 89%
rename from .idea/analyzers/checkstyle/checkstyle.xml
rename to config/checkstyle.xml
index 9aea6153..c17ef8d0 100644
--- a/.idea/analyzers/checkstyle/checkstyle.xml
+++ b/config/checkstyle.xml
@@ -32,8 +32,13 @@
+
+
+
+
+
@@ -77,6 +82,8 @@
+
+
@@ -88,7 +95,10 @@
-
+
+
+
+
@@ -125,8 +135,15 @@
+
+
+
+
+
-
+
+
+
@@ -148,7 +165,9 @@
-
+
+
+
@@ -164,10 +183,8 @@
-
-
diff --git a/config/pmd.xml b/config/pmd.xml
new file mode 100644
index 00000000..c74aa626
--- /dev/null
+++ b/config/pmd.xml
@@ -0,0 +1,116 @@
+
+
+
+
+ POM rule set file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/opfpush-providers/adm/build.gradle b/opfpush-providers/adm/build.gradle
index fa7ad06d..ab1a7515 100644
--- a/opfpush-providers/adm/build.gradle
+++ b/opfpush-providers/adm/build.gradle
@@ -16,6 +16,7 @@
apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.library'
+apply plugin: 'com.noveogroup.android.check'
apply from: 'https://raw.githubusercontent.com/onepf/OPF-mvn-repo/master/OPF-mvn-push.gradle'
android {
@@ -25,8 +26,7 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
- versionCode 4
- versionName "0.1.4"
+ versionName "0.1.5-SNAPSHOT"
}
lintOptions {
@@ -41,15 +41,29 @@ android {
}
}
+check {
+ abortOnError false
+
+ checkstyle {
+ config rootProject.file('config/checkstyle.xml')
+ }
+
+ pmd {
+ config rootProject.file('config/pmd.xml')
+ }
+}
+
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.robolectric:robolectric:2.4'
-
+
//noinspection NewerVersionAvailable
- compile('org.onepf:opfpush:0.1.4@aar') {
- transitive = true
- }
+ compile 'org.onepf:opfpush:0.1.5-SNAPSHOT@aar'
provided 'com.amazon:amazon-device-messaging:1.0.1'
+ provided 'com.android.support:support-annotations:19.1.0'
+ provided 'com.google.code.findbugs:annotations:3.0.0'
+ //noinspection NewerVersionAvailable
+ provided 'org.onepf:opfutils:0.1.15-SNAPSHOT'
}
diff --git a/opfpush-providers/adm/gradle.properties b/opfpush-providers/adm/gradle.properties
index 58b8c39c..a3ab86c4 100644
--- a/opfpush-providers/adm/gradle.properties
+++ b/opfpush-providers/adm/gradle.properties
@@ -16,7 +16,7 @@
POM_NAME = Amazon Device Messaging OPFPush provider
POM_ARTIFACT_ID = opfpush-adm
-POM_DESCRIPTION = Amazon Device Messaging push provider implementation fo OPFPush library
+POM_DESCRIPTION = Amazon Device Messaging push provider implementation for OPFPush library
POM_PACKAGING = aar
POM_SCM_URL = https://github.com/onepf/OPFPush.git
POM_SCM_CONNECTION = scm:git@github.com:onepf/OPFPush.git
diff --git a/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java b/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java
index 3314b945..c24e5906 100644
--- a/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java
+++ b/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java
@@ -33,6 +33,8 @@ public final class ADMConstants {
static final String KINDLE_STORE_APP_PACKAGE = "com.amazon.venezia";
static final String ACCOUNT_TYPE = "com.amazon.account";
+ static final String RECEIVE_MESSAGE_PERMISSION_SUFFIX = ".permission.RECEIVE_ADM_MESSAGE";
+
private ADMConstants() {
throw new UnsupportedOperationException();
}
diff --git a/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMProvider.java b/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMProvider.java
index 4ebfc5f7..87b8ae10 100644
--- a/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMProvider.java
+++ b/opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMProvider.java
@@ -16,7 +16,10 @@
package org.onepf.opfpush.adm;
+import android.accounts.AccountManager;
+import android.content.ComponentName;
import android.content.Context;
+import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -25,11 +28,18 @@
import com.amazon.device.messaging.development.ADMManifest;
import org.onepf.opfpush.BasePushProvider;
+import org.onepf.opfpush.util.ManifestUtils;
+import org.onepf.opfpush.util.ReceiverUtils;
import org.onepf.opfutils.OPFLog;
+import static android.Manifest.permission.GET_ACCOUNTS;
+import static com.amazon.device.messaging.ADMConstants.LowLevel.ACTION_APP_REGISTRATION_EVENT;
+import static com.amazon.device.messaging.ADMConstants.LowLevel.ACTION_RECEIVE_ADM_MESSAGE;
+import static com.amazon.device.messaging.development.ADMManifest.PERMISSION_RECEIVE_MESSAGES;
import static org.onepf.opfpush.adm.ADMConstants.AMAZON_MANUFACTURER;
import static org.onepf.opfpush.adm.ADMConstants.KINDLE_STORE_APP_PACKAGE;
import static org.onepf.opfpush.adm.ADMConstants.PROVIDER_NAME;
+import static org.onepf.opfpush.adm.ADMConstants.RECEIVE_MESSAGE_PERMISSION_SUFFIX;
/**
* Amazon Device Messaging push provider implementation.
@@ -60,15 +70,40 @@ public void register() {
}
@Override
- public boolean checkManifest() {
+ public void checkManifest() {
OPFLog.methodD();
- try {
- ADMManifest.checkManifestAuthoredProperly(getContext());
- } catch (IllegalStateException e) {
- OPFLog.d("Error while check manifest: " + e);
- return false;
- }
- return super.checkManifest() && Build.MANUFACTURER.equals(AMAZON_MANUFACTURER);
+ super.checkManifest();
+ final Context context = getContext();
+ ADMManifest.checkManifestAuthoredProperly(context);
+ context.enforceCallingOrSelfPermission(PERMISSION_RECEIVE_MESSAGES,
+ ManifestUtils.getSecurityExceptionMessage(PERMISSION_RECEIVE_MESSAGES));
+ context.enforceCallingOrSelfPermission(GET_ACCOUNTS,
+ ManifestUtils.getSecurityExceptionMessage(GET_ACCOUNTS));
+ final String admMessagePermission = context.getPackageName() + RECEIVE_MESSAGE_PERMISSION_SUFFIX;
+ context.enforceCallingOrSelfPermission(admMessagePermission,
+ ManifestUtils.getSecurityExceptionMessage(admMessagePermission));
+
+ ManifestUtils.checkService(context, new ComponentName(context, ADMService.class));
+
+ final Intent registrationBroadcastIntent = new Intent(ACTION_APP_REGISTRATION_EVENT);
+ final Intent receiveBroadcastIntent = new Intent(ACTION_RECEIVE_ADM_MESSAGE);
+ final Intent loginChangedBroadcastIntent = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION);
+ ReceiverUtils.checkReceiver(context, registrationBroadcastIntent,
+ ADMReceiver.class.getName(), ADMManifest.PERMISSION_SEND_MESSAGES);
+ ReceiverUtils.checkReceiver(context, receiveBroadcastIntent,
+ ADMReceiver.class.getName(), ADMManifest.PERMISSION_SEND_MESSAGES);
+ ReceiverUtils.checkReceiver(context, loginChangedBroadcastIntent,
+ LoginAccountsChangedReceiver.class.getName(), null);
+ }
+
+ @Override
+ public void onRegistrationInvalid() {
+ //nothing
+ }
+
+ @Override
+ public void onUnavailable() {
+ //nothing
}
@Override
@@ -86,7 +121,9 @@ public void unregister() {
@Override
public boolean isAvailable() {
- return super.isAvailable() && adm.isSupported();
+ return super.isAvailable()
+ && adm.isSupported()
+ && Build.MANUFACTURER.equals(AMAZON_MANUFACTURER);
}
@Override
diff --git a/opfpush-providers/adm/src/test/java/org/onepf/opfpush/adm/PreferencesProviderTest.java b/opfpush-providers/adm/src/test/java/org/onepf/opfpush/adm/PreferencesProviderTest.java
index bfc51c46..1c029726 100644
--- a/opfpush-providers/adm/src/test/java/org/onepf/opfpush/adm/PreferencesProviderTest.java
+++ b/opfpush-providers/adm/src/test/java/org/onepf/opfpush/adm/PreferencesProviderTest.java
@@ -33,10 +33,12 @@
import java.lang.reflect.Field;
import java.util.Random;
+import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
+
/**
* Created by antonpp on 02.03.15.
*/
-@Config(emulateSdk = 18, manifest = Config.NONE)
+@Config(emulateSdk = JELLY_BEAN_MR2, manifest = Config.NONE)
@RunWith(RobolectricTestRunner.class)
public class PreferencesProviderTest extends Assert {
diff --git a/opfpush-providers/gcm/build.gradle b/opfpush-providers/gcm/build.gradle
index 2c9e4c2c..837089e1 100644
--- a/opfpush-providers/gcm/build.gradle
+++ b/opfpush-providers/gcm/build.gradle
@@ -16,6 +16,7 @@
apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.library'
+apply plugin: 'com.noveogroup.android.check'
apply from: 'https://raw.githubusercontent.com/onepf/OPF-mvn-repo/master/OPF-mvn-push.gradle'
android {
@@ -25,8 +26,7 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
- versionCode 4
- versionName "0.1.4"
+ versionName "0.1.5-SNAPSHOT"
}
lintOptions {
@@ -42,6 +42,18 @@ android {
}
}
+check {
+ abortOnError false
+
+ checkstyle {
+ config rootProject.file('config/checkstyle.xml')
+ }
+
+ pmd {
+ config rootProject.file('config/pmd.xml')
+ }
+}
+
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
@@ -49,8 +61,11 @@ dependencies {
androidTestCompile 'org.robolectric:robolectric:2.4'
//noinspection NewerVersionAvailable
- compile('org.onepf:opfpush:0.1.4@aar') {
- transitive = true
- }
+ compile 'org.onepf:opfpush:0.1.5-SNAPSHOT@aar'
compile 'com.google.android.gms:play-services-base:6.5.87'
+
+ provided 'com.android.support:support-annotations:19.1.0'
+ provided 'com.google.code.findbugs:annotations:3.0.0'
+ //noinspection NewerVersionAvailable
+ provided 'org.onepf:opfutils:0.1.15-SNAPSHOT'
}
diff --git a/opfpush-providers/gcm/gradle.properties b/opfpush-providers/gcm/gradle.properties
index d9cb49e4..2adbd65d 100644
--- a/opfpush-providers/gcm/gradle.properties
+++ b/opfpush-providers/gcm/gradle.properties
@@ -16,7 +16,7 @@
POM_NAME = Google Cloud Messaging OPFPush Provider
POM_ARTIFACT_ID = opfpush-gcm
-POM_DESCRIPTION = Google Cloud Messaging push provider implementation fo OPFPush library
+POM_DESCRIPTION = Google Cloud Messaging push provider implementation for OPFPush library
POM_PACKAGING = aar
POM_SCM_URL = https://github.com/onepf/OPFPush.git
POM_SCM_CONNECTION = scm:git@github.com:onepf/OPFPush.git
diff --git a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderGingerbreadTest.java b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderGingerbreadTest.java
index a64a39de..8697a4a8 100644
--- a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderGingerbreadTest.java
+++ b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderGingerbreadTest.java
@@ -39,6 +39,7 @@
reportSdk = Build.VERSION_CODES.GINGERBREAD_MR1
)
@RunWith(RobolectricTestRunner.class)
+@SuppressWarnings({"checkstyle:methodname", "PMD.MethodNamingConventions"})
public class GCMProviderGingerbreadTest extends GCMProviderTestBase {
@Test
diff --git a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTest.java b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTest.java
index 424670ed..35784228 100644
--- a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTest.java
+++ b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTest.java
@@ -41,6 +41,7 @@
reportSdk = Build.VERSION_CODES.JELLY_BEAN
)
@RunWith(RobolectricTestRunner.class)
+@SuppressWarnings({"checkstyle:methodname", "PMD.MethodNamingConventions"})
public class GCMProviderTest extends GCMProviderTestBase {
@Test
diff --git a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTestBase.java b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTestBase.java
index 1c016423..de543057 100644
--- a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTestBase.java
+++ b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMProviderTestBase.java
@@ -37,6 +37,8 @@ public abstract class GCMProviderTestBase {
private static final Account TEST_GOOGLE_ACCOUNT
= new Account("OnePF Test", GOOGLE_ACCOUNT_TYPE);
+ private static final int LATEST_GCM_SERVICE_VERSION_CODE = 4030000;
+
@Before
public void addGooglePlayApp() {
ShadowLog.stream = System.out;
@@ -67,7 +69,7 @@ protected static void addGMSServiceApp(String versionName, int versionCode) {
}
protected static void addLatestGMSServiceApp() {
- addGMSServiceApp("4.0.30", 4030000);
+ addGMSServiceApp("4.0.30", LATEST_GCM_SERVICE_VERSION_CODE);
}
@After
diff --git a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMReceiverTest.java b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMReceiverTest.java
index f35a79b5..4e57c02b 100644
--- a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMReceiverTest.java
+++ b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMReceiverTest.java
@@ -54,6 +54,7 @@
emulateSdk = Build.VERSION_CODES.JELLY_BEAN_MR2
)
@RunWith(RobolectricTestRunner.class)
+@SuppressWarnings({"checkstyle:methodname", "PMD.MethodNamingConventions"})
public class GCMReceiverTest {
private static final String ACTION_RECEIVE = "com.google.android.c2dm.intent.RECEIVE";
diff --git a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMServiceTest.java b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMServiceTest.java
index 10e1e480..bb5f5a9d 100644
--- a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMServiceTest.java
+++ b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/GCMServiceTest.java
@@ -66,6 +66,7 @@
)
@RunWith(RobolectricTestRunner.class)
//TODO look how messageType is stored in intent and implement tests for onMessage and onDeletedMessage functionality
+@SuppressWarnings({"checkstyle:methodname", "PMD.MethodNamingConventions"})
public class GCMServiceTest {
private static final String PREF_NAME = "org.onepf.openpush";
diff --git a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/shadow/ShadowGooglePlayServiceUtil.java b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/shadow/ShadowGooglePlayServiceUtil.java
index 34c3451a..6c35749d 100644
--- a/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/shadow/ShadowGooglePlayServiceUtil.java
+++ b/opfpush-providers/gcm/src/androidTest/java/org/onepf/opfpush/gcm/shadow/ShadowGooglePlayServiceUtil.java
@@ -32,7 +32,7 @@
* @since 10/9/14.
*/
@Implements(GooglePlayServicesUtil.class)
-public class ShadowGooglePlayServiceUtil {
+public final class ShadowGooglePlayServiceUtil {
@Implementation
public static int isGooglePlayServicesAvailable(Context context) {
@@ -43,4 +43,8 @@ public static int isGooglePlayServicesAvailable(Context context) {
return ConnectionResult.SERVICE_MISSING;
}
}
+
+ private ShadowGooglePlayServiceUtil() {
+ throw new UnsupportedOperationException();
+ }
}
diff --git a/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMConstants.java b/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMConstants.java
index d3a41816..e39e7235 100644
--- a/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMConstants.java
+++ b/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMConstants.java
@@ -35,6 +35,8 @@ public final class GCMConstants {
static final String ACTION_REGISTRATION_CALLBACK = BuildConfig.APPLICATION_ID + ".intent.REGISTRATION";
static final String ACTION_UNREGISTRATION_CALLBACK = BuildConfig.APPLICATION_ID + ".intent.UNREGISTRATION";
+ static final String C2DM_ACTION_RECEIVE = "com.google.android.c2dm.intent.RECEIVE";
+
static final String EXTRA_ERROR_ID = "error_id";
static final String EXTRA_REGISTRATION_ID = "registration_id";
static final String ERROR_AUTHENTICATION_FAILED = "AUTHENTICATION_FAILED";
@@ -45,12 +47,13 @@ public final class GCMConstants {
static final String GOOGLE_ACCOUNT_TYPE = "com.google";
static final String ANDROID_RELEASE_4_0_4 = "4.0.4";
+ static final String PERMISSION_SEND = "com.google.android.c2dm.permission.SEND";
static final String PERMISSION_RECEIVE = "com.google.android.c2dm.permission.RECEIVE";
static final String PERMISSION_C2D_MESSAGE_SUFFIX = ".permission.C2D_MESSAGE";
static final String GOOGLE_CLOUD_MESSAGING_CLASS_NAME = "com.google.android.gms.gcm.GoogleCloudMessaging";
static final String MESSAGES_TO_SUFFIX = "@gcm.googleapis.com";
- static final String GOOGLE_SERVICES_FRAMEWORK_PACKAGE = "com.google.android.gsf";
+ static final String GOOGLE_SERVICES_FRAMEWORK_PACKAGE = "com.google.android.gsf";
private GCMConstants() {
throw new UnsupportedOperationException();
diff --git a/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMProvider.java b/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMProvider.java
index 69698d55..a16ff9c6 100644
--- a/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMProvider.java
+++ b/opfpush-providers/gcm/src/main/java/org/onepf/opfpush/gcm/GCMProvider.java
@@ -16,9 +16,9 @@
package org.onepf.opfpush.gcm;
-import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
@@ -33,6 +33,8 @@
import org.onepf.opfpush.BasePushProvider;
import org.onepf.opfpush.SenderPushProvider;
import org.onepf.opfpush.model.Message;
+import org.onepf.opfpush.util.ManifestUtils;
+import org.onepf.opfpush.util.ReceiverUtils;
import org.onepf.opfutils.OPFLog;
import org.onepf.opfutils.OPFUtils;
import org.onepf.opfutils.exception.WrongThreadException;
@@ -42,7 +44,11 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import static android.Manifest.permission.GET_ACCOUNTS;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
+import static org.onepf.opfpush.gcm.GCMConstants.ACTION_REGISTRATION_CALLBACK;
+import static org.onepf.opfpush.gcm.GCMConstants.ACTION_UNREGISTRATION_CALLBACK;
+import static org.onepf.opfpush.gcm.GCMConstants.C2DM_ACTION_RECEIVE;
import static org.onepf.opfpush.gcm.GCMConstants.ANDROID_RELEASE_4_0_4;
import static org.onepf.opfpush.gcm.GCMConstants.GOOGLE_ACCOUNT_TYPE;
import static org.onepf.opfpush.gcm.GCMConstants.GOOGLE_CLOUD_MESSAGING_CLASS_NAME;
@@ -51,8 +57,8 @@
import static org.onepf.opfpush.gcm.GCMConstants.MESSAGES_TO_SUFFIX;
import static org.onepf.opfpush.gcm.GCMConstants.PERMISSION_C2D_MESSAGE_SUFFIX;
import static org.onepf.opfpush.gcm.GCMConstants.PERMISSION_RECEIVE;
+import static org.onepf.opfpush.gcm.GCMConstants.PERMISSION_SEND;
import static org.onepf.opfpush.gcm.GCMConstants.PROVIDER_NAME;
-import static org.onepf.opfutils.OPFUtils.hasRequestedPermission;
/**
* Google Cloud Messaging push provider implementation.
@@ -93,14 +99,33 @@ public synchronized void unregister() {
}
@Override
- public boolean checkManifest() {
+ public void checkManifest() {
OPFLog.methodD();
+ super.checkManifest();
final Context context = getContext();
- return super.checkManifest()
- && (!needGoogleAccounts() || hasRequestedPermission(getContext(), Manifest.permission.GET_ACCOUNTS))
- && hasRequestedPermission(context, Manifest.permission.WAKE_LOCK)
- && hasRequestedPermission(context, PERMISSION_RECEIVE)
- && hasRequestedPermission(context, context.getPackageName() + PERMISSION_C2D_MESSAGE_SUFFIX);
+ if (needGoogleAccounts()) {
+ context.enforceCallingOrSelfPermission(GET_ACCOUNTS,
+ ManifestUtils.getSecurityExceptionMessage(GET_ACCOUNTS));
+ }
+ context.enforceCallingOrSelfPermission(PERMISSION_RECEIVE,
+ ManifestUtils.getSecurityExceptionMessage(PERMISSION_RECEIVE));
+ final String c2dmPermission = context.getPackageName() + PERMISSION_C2D_MESSAGE_SUFFIX;
+ context.enforceCallingOrSelfPermission(c2dmPermission,
+ ManifestUtils.getSecurityExceptionMessage(c2dmPermission));
+
+ ManifestUtils.checkService(context, new ComponentName(context, GCMService.class));
+ ManifestUtils.checkService(context, new ComponentName(context, SendMessageService.class));
+
+ final Intent c2dmReceiveBroadcastIntent = new Intent(C2DM_ACTION_RECEIVE);
+ final Intent registrationBroadcastIntent = new Intent(ACTION_REGISTRATION_CALLBACK);
+ final Intent unregistrationBroadcastIntent = new Intent(ACTION_UNREGISTRATION_CALLBACK);
+
+ ReceiverUtils.checkReceiver(context, c2dmReceiveBroadcastIntent,
+ GCMReceiver.class.getName(), PERMISSION_SEND);
+ ReceiverUtils.checkReceiver(context, registrationBroadcastIntent,
+ GCMReceiver.class.getName(), PERMISSION_SEND);
+ ReceiverUtils.checkReceiver(context, unregistrationBroadcastIntent,
+ GCMReceiver.class.getName(), PERMISSION_SEND);
}
@Override
diff --git a/opfpush-providers/gcm/src/test/java/org/onepf/opfpush/gcm/PreferencesProviderTest.java b/opfpush-providers/gcm/src/test/java/org/onepf/opfpush/gcm/PreferencesProviderTest.java
index 69ab9727..2a96d455 100644
--- a/opfpush-providers/gcm/src/test/java/org/onepf/opfpush/gcm/PreferencesProviderTest.java
+++ b/opfpush-providers/gcm/src/test/java/org/onepf/opfpush/gcm/PreferencesProviderTest.java
@@ -33,10 +33,12 @@
import java.lang.reflect.Field;
import java.util.Random;
+import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
+
/**
* Created by antonpp on 02.03.15.
*/
-@Config(emulateSdk = 18, manifest = Config.NONE)
+@Config(emulateSdk = JELLY_BEAN_MR2, manifest = Config.NONE)
@RunWith(RobolectricTestRunner.class)
public class PreferencesProviderTest extends Assert {
diff --git a/opfpush-providers/nokia/build.gradle b/opfpush-providers/nokia/build.gradle
index 7cee9ea5..cea7e9a4 100644
--- a/opfpush-providers/nokia/build.gradle
+++ b/opfpush-providers/nokia/build.gradle
@@ -16,6 +16,7 @@
apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.library'
+apply plugin: 'com.noveogroup.android.check'
apply from: 'https://raw.githubusercontent.com/onepf/OPF-mvn-repo/master/OPF-mvn-push.gradle'
android {
@@ -25,8 +26,7 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
- versionCode 4
- versionName "0.1.4"
+ versionName "0.1.5-SNAPSHOT"
}
lintOptions {
@@ -37,10 +37,25 @@ android {
}
}
+check {
+ abortOnError false
+
+ checkstyle {
+ config rootProject.file('config/checkstyle.xml')
+ }
+
+ pmd {
+ config rootProject.file('config/pmd.xml')
+ }
+}
+
dependencies {
//noinspection NewerVersionAvailable
- compile('org.onepf:opfpush:0.1.4@aar') {
- transitive = true
- }
+ compile 'org.onepf:opfpush:0.1.5-SNAPSHOT@aar'
compile 'com.nokia:push:1.0'
+
+ provided 'com.android.support:support-annotations:19.1.0'
+ provided 'com.google.code.findbugs:annotations:3.0.0'
+ //noinspection NewerVersionAvailable
+ provided 'org.onepf:opfutils:0.1.15-SNAPSHOT'
}
diff --git a/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaNotificationsProvider.java b/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaNotificationsProvider.java
index dac6cde5..2fce4f0f 100644
--- a/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaNotificationsProvider.java
+++ b/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaNotificationsProvider.java
@@ -16,6 +16,7 @@
package org.onepf.opfpush.nokia;
+import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
@@ -24,10 +25,13 @@
import com.nokia.push.PushRegistrar;
import org.onepf.opfpush.BasePushProvider;
+import org.onepf.opfpush.util.ManifestUtils;
import org.onepf.opfutils.OPFLog;
import java.util.Locale;
+import static org.onepf.opfpush.nokia.NokiaPushConstants.PERMISSION_C2D_MESSAGE_SUFFIX;
+import static org.onepf.opfpush.nokia.NokiaPushConstants.PERMISSION_RECEIVE;
import static org.onepf.opfpush.nokia.NokiaPushConstants.PROVIDER_NAME;
import static org.onepf.opfpush.nokia.NokiaPushConstants.NOKIA_MANUFACTURER;
import static org.onepf.opfpush.nokia.NokiaPushConstants.NOKIA_STORE_APP_PACKAGE;
@@ -68,15 +72,24 @@ public boolean isAvailable() {
}
@Override
- public boolean checkManifest() {
+ public void checkManifest() {
OPFLog.methodD();
- try {
- PushRegistrar.checkManifest(getContext());
- return super.checkManifest();
- } catch (UnsupportedOperationException exception) {
- OPFLog.e(exception.toString());
- return false;
- }
+ super.checkManifest();
+ final Context context = getContext();
+ PushRegistrar.checkManifest(context);
+
+ context.enforceCallingOrSelfPermission(PERMISSION_RECEIVE,
+ ManifestUtils.getSecurityExceptionMessage(PERMISSION_RECEIVE));
+ final String c2dmPermission = context.getPackageName() + PERMISSION_C2D_MESSAGE_SUFFIX;
+ context.enforceCallingOrSelfPermission(c2dmPermission,
+ ManifestUtils.getSecurityExceptionMessage(c2dmPermission));
+
+ ManifestUtils.checkService(context, new ComponentName(context, NokiaNotificationService.class));
+ }
+
+ @Override
+ public void onRegistrationInvalid() {
+ //nothing
}
/**
diff --git a/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaPushConstants.java b/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaPushConstants.java
index b99d3482..907c38d8 100644
--- a/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaPushConstants.java
+++ b/opfpush-providers/nokia/src/main/java/org/onepf/opfpush/nokia/NokiaPushConstants.java
@@ -32,6 +32,9 @@ public final class NokiaPushConstants {
static final String NOKIA_MANUFACTURER = "Nokia";
static final String NOKIA_STORE_APP_PACKAGE = "com.nokia.store";
+ static final String PERMISSION_RECEIVE = "com.nokia.pushnotifications.permission.RECEIVE";
+ static final String PERMISSION_C2D_MESSAGE_SUFFIX = ".permission.C2D_MESSAGE";
+
private NokiaPushConstants() {
throw new UnsupportedOperationException();
}
diff --git a/opfpush/build.gradle b/opfpush/build.gradle
index ef01eccd..ca248712 100644
--- a/opfpush/build.gradle
+++ b/opfpush/build.gradle
@@ -17,6 +17,7 @@
apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.library'
+apply plugin: 'com.noveogroup.android.check'
apply from: 'https://raw.githubusercontent.com/onepf/OPF-mvn-repo/master/OPF-mvn-push.gradle'
android {
@@ -26,8 +27,7 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
- versionCode 4
- versionName "0.1.4"
+ versionName "0.1.5-SNAPSHOT"
}
lintOptions {
@@ -42,13 +42,26 @@ android {
}
}
+check {
+ abortOnError false
+
+ checkstyle {
+ config rootProject.file('config/checkstyle.xml')
+ }
+
+ pmd {
+ config rootProject.file('config/pmd.xml')
+ }
+}
+
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.robolectric:robolectric:2.4'
-
- compile 'com.android.support:support-annotations:19.1.0'
- compile 'com.google.code.findbugs:annotations:3.0.0'
- compile 'org.onepf:opfutils:0.1.12'
+
+ provided 'com.android.support:support-annotations:19.1.0'
+ provided 'com.google.code.findbugs:annotations:3.0.0'
+ //noinspection NewerVersionAvailable
+ provided 'org.onepf:opfutils:0.1.15-SNAPSHOT'
}
\ No newline at end of file
diff --git a/opfpush/gradle.properties b/opfpush/gradle.properties
index 8f249f60..07b3af33 100644
--- a/opfpush/gradle.properties
+++ b/opfpush/gradle.properties
@@ -16,7 +16,7 @@
POM_NAME = OPFPush library
POM_ARTIFACT_ID = opfpush
-POM_DESCRIPTION = OPRPush library wrap push providers to comfort work on different androdi version from different vendors
+POM_DESCRIPTION = OPFPush library wrap push providers to comfort work on different android versions from different vendors
POM_PACKAGING = aar
POM_SCM_URL = https://github.com/onepf/OPFPush.git
POM_SCM_CONNECTION = scm:git@github.com:onepf/OPFPush.git
diff --git a/opfpush/src/main/java/org/onepf/opfpush/BasePushProvider.java b/opfpush/src/main/java/org/onepf/opfpush/BasePushProvider.java
index 3f6f0914..3adf8e6f 100644
--- a/opfpush/src/main/java/org/onepf/opfpush/BasePushProvider.java
+++ b/opfpush/src/main/java/org/onepf/opfpush/BasePushProvider.java
@@ -17,14 +17,17 @@
package org.onepf.opfpush;
import android.content.Context;
+import android.content.Intent;
import android.support.annotation.NonNull;
-import org.onepf.opfutils.OPFLog;
+import org.onepf.opfpush.util.ManifestUtils;
+import org.onepf.opfpush.util.ReceiverUtils;
import org.onepf.opfutils.OPFUtils;
+import static android.Manifest.permission.ACCESS_NETWORK_STATE;
import static android.Manifest.permission.INTERNET;
import static android.Manifest.permission.RECEIVE_BOOT_COMPLETED;
-import static org.onepf.opfutils.OPFUtils.hasRequestedPermission;
+import static android.Manifest.permission.WAKE_LOCK;
/**
* Implements the common functionality of the {@link org.onepf.opfpush.PushProvider} interface.
@@ -80,10 +83,18 @@ public boolean equals(final Object o) {
}
@Override
- public boolean checkManifest() {
- OPFLog.methodD();
- return hasRequestedPermission(appContext, INTERNET)
- && hasRequestedPermission(appContext, RECEIVE_BOOT_COMPLETED);
+ public void checkManifest() {
+ appContext.enforceCallingOrSelfPermission(INTERNET, ManifestUtils.getSecurityExceptionMessage(INTERNET));
+ appContext.enforceCallingOrSelfPermission(RECEIVE_BOOT_COMPLETED,
+ ManifestUtils.getSecurityExceptionMessage(RECEIVE_BOOT_COMPLETED));
+ appContext.enforceCallingOrSelfPermission(WAKE_LOCK, ManifestUtils.getSecurityExceptionMessage(WAKE_LOCK));
+ appContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE,
+ ManifestUtils.getSecurityExceptionMessage(ACCESS_NETWORK_STATE));
+
+ ReceiverUtils.checkReceiver(appContext, new Intent(Intent.ACTION_BOOT_COMPLETED),
+ BootCompleteReceiver.class.getName(), null);
+ ReceiverUtils.checkReceiver(appContext, new Intent(appContext, RetryBroadcastReceiver.class),
+ RetryBroadcastReceiver.class.getName(), null);
}
@Override
@@ -97,21 +108,12 @@ public String getName() {
return name;
}
- @Override
- public void onUnavailable() {
- }
-
@NonNull
@Override
public String getHostAppPackage() {
return hostAppPackage;
}
- @Override
- public void onRegistrationInvalid() {
- }
-
-
@Override
public String toString() {
return name + "(hostAppPackage='" + hostAppPackage + ')';
diff --git a/opfpush/src/main/java/org/onepf/opfpush/EventListenerWrapperCreator.java b/opfpush/src/main/java/org/onepf/opfpush/EventListenerWrapperCreator.java
index 76fd95a5..5bcc087f 100644
--- a/opfpush/src/main/java/org/onepf/opfpush/EventListenerWrapperCreator.java
+++ b/opfpush/src/main/java/org/onepf/opfpush/EventListenerWrapperCreator.java
@@ -19,13 +19,14 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.onepf.opfpush.listener.EventListener;
import org.onepf.opfpush.model.OPFError;
import org.onepf.opfutils.OPFLog;
-import org.onepf.opfutils.OPFUtils;
import java.io.Serializable;
import java.util.Map;
@@ -46,6 +47,7 @@
* @author Roman Savin
* @since 26.12.14
*/
+@SuppressWarnings({"PMD.StdCyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity"})
final class EventListenerWrapperCreator {
private EventListenerWrapperCreator() {
@@ -70,13 +72,15 @@ private static EventListener createMainLooperWrapper(@NonNull final EventListene
OPFLog.methodD(eventListener);
return new EventListener() {
+ private final Handler handler = new Handler(Looper.getMainLooper());
+
@Override
public void onMessage(
@NonNull final Context context,
@NonNull final String providerName,
@Nullable final Bundle extras
) {
- OPFUtils.post(new Runnable() {
+ handler.post(new Runnable() {
@Override
public void run() {
OPFLog.d("Post onMessage(%1$s, %2$s)", providerName, extras);
@@ -91,7 +95,7 @@ public void onDeletedMessages(
@NonNull final String providerName,
final int messagesCount
) {
- OPFUtils.post(new Runnable() {
+ handler.post(new Runnable() {
@Override
public void run() {
OPFLog.d("Post onDeletedMessages(%1$s, %2$s)", providerName, messagesCount);
@@ -106,7 +110,7 @@ public void onRegistered(
@NonNull final String providerName,
@NonNull final String registrationId
) {
- OPFUtils.post(new Runnable() {
+ handler.post(new Runnable() {
@Override
public void run() {
OPFLog.d("Post onRegistered(%1$s, %2$s)", providerName, registrationId);
@@ -121,7 +125,7 @@ public void onUnregistered(
@NonNull final String providerName,
@Nullable final String registrationId
) {
- OPFUtils.post(new Runnable() {
+ handler.post(new Runnable() {
@Override
public void run() {
OPFLog.d("Post onUnregistered(%1$s, %2$s)", providerName, registrationId);
@@ -134,7 +138,7 @@ public void run() {
public void onNoAvailableProvider(
@NonNull final Context context,
@NonNull final Map registrationErrors) {
- OPFUtils.post(new Runnable() {
+ handler.post(new Runnable() {
@Override
public void run() {
OPFLog.d("Post onNoAvailableProvider()");
diff --git a/opfpush/src/main/java/org/onepf/opfpush/PushProvider.java b/opfpush/src/main/java/org/onepf/opfpush/PushProvider.java
index c1da0371..7f14ae64 100644
--- a/opfpush/src/main/java/org/onepf/opfpush/PushProvider.java
+++ b/opfpush/src/main/java/org/onepf/opfpush/PushProvider.java
@@ -90,9 +90,10 @@ public interface PushProvider {
/**
* Verify that application manifest contains all needed permissions.
*
- * @return {@code true} If all required permissions described in the manifest, else {@code false}.
+ * @throws java.lang.IllegalStateException If not all required permissions and components have been
+ * described in the AndroidManifest.xml file.
*/
- boolean checkManifest();
+ void checkManifest();
/**
* Callback method, that called when the application state change, like update to new version,
diff --git a/opfpush/src/main/java/org/onepf/opfpush/model/OPFError.java b/opfpush/src/main/java/org/onepf/opfpush/model/OPFError.java
index c88f9008..defedce7 100644
--- a/opfpush/src/main/java/org/onepf/opfpush/model/OPFError.java
+++ b/opfpush/src/main/java/org/onepf/opfpush/model/OPFError.java
@@ -16,8 +16,6 @@
package org.onepf.opfpush.model;
-import org.onepf.opfpush.listener.EventListener;
-
/**
* Registration or unregistering operation error.
* Divided in two categories: recoverable and unrecoverable.
@@ -29,7 +27,7 @@
* Unrecoverable error means that the error is fatal and the {@link org.onepf.opfpush.OPFPushHelper}
* can't register this provider. In this case the {@link org.onepf.opfpush.OPFPushHelper} chooses the next
* available provider. If there are no any other available providers,
- * the {@link EventListener#onNoAvailableProvider(android.content.Context, java.util.Map)} method will be called.
+ * the {@link org.onepf.opfpush.listener.EventListener#onNoAvailableProvider(android.content.Context, java.util.Map)} method will be called.
*
* @author Kirill Rozov
* @author Roman Savin
diff --git a/opfpush/src/main/java/org/onepf/opfpush/util/ManifestUtils.java b/opfpush/src/main/java/org/onepf/opfpush/util/ManifestUtils.java
new file mode 100644
index 00000000..ead99d9c
--- /dev/null
+++ b/opfpush/src/main/java/org/onepf/opfpush/util/ManifestUtils.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2012-2014 One Platform Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onepf.opfpush.util;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.support.annotation.NonNull;
+
+import java.util.Locale;
+
+/**
+ * @author Roman Savin
+ * @since 04.03.2015
+ */
+public final class ManifestUtils {
+
+ public static String getSecurityExceptionMessage(final String permission) {
+ return String.format(
+ Locale.US,
+ "You must add %s permission to the AndroidManifest.xml",
+ permission
+ );
+ }
+
+ public static void checkService(@NonNull final Context context,
+ @NonNull final ComponentName serviceName) {
+ final PackageManager packageManager = context.getPackageManager();
+ try {
+ packageManager.getServiceInfo(serviceName, 0);
+ } catch (PackageManager.NameNotFoundException e) {
+ throw new IllegalStateException("Service " + serviceName
+ + " hasn't been declared in AndroidManifest.xml");
+ }
+ }
+
+ private ManifestUtils() {
+ throw new UnsupportedOperationException();
+ }
+}
diff --git a/opfpush/src/main/java/org/onepf/opfpush/util/ReceiverUtils.java b/opfpush/src/main/java/org/onepf/opfpush/util/ReceiverUtils.java
index e7cb41fe..00b24f48 100644
--- a/opfpush/src/main/java/org/onepf/opfpush/util/ReceiverUtils.java
+++ b/opfpush/src/main/java/org/onepf/opfpush/util/ReceiverUtils.java
@@ -20,17 +20,19 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.PatternMatcher;
import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
import org.onepf.opfpush.PackageChangeReceiver;
import org.onepf.opfpush.PushProvider;
import org.onepf.opfutils.OPFLog;
+import org.onepf.opfutils.OPFUtils;
import java.util.List;
-import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
import static org.onepf.opfpush.OPFConstants.ACTION_NO_AVAILABLE_PROVIDER;
import static org.onepf.opfpush.OPFConstants.ACTION_RECEIVE;
import static org.onepf.opfpush.OPFConstants.ACTION_REGISTRATION;
@@ -77,21 +79,56 @@ public static BroadcastReceiver registerPackageChangeReceiver(
public static boolean isOPFReceiverRegistered(@NonNull final Context context) {
OPFLog.methodD(context);
+ final Intent intentBroadcastReceive = new Intent(ACTION_RECEIVE);
+ final Intent intentBroadcastRegistration = new Intent(ACTION_REGISTRATION);
+ final Intent intentBroadcastUnregistration = new Intent(ACTION_UNREGISTRATION);
+ final Intent intentBroadcastNoAvailableProvider = new Intent(ACTION_NO_AVAILABLE_PROVIDER);
+
+ try {
+ checkReceiver(context, intentBroadcastReceive, null, null);
+ checkReceiver(context, intentBroadcastRegistration, null, null);
+ checkReceiver(context, intentBroadcastUnregistration, null, null);
+ checkReceiver(context, intentBroadcastNoAvailableProvider, null, null);
+ } catch (RuntimeException e) {
+ return false;
+ }
+ return true;
+ }
+
+ public static void checkReceiver(@NonNull final Context context,
+ @NonNull final Intent broadcastIntent,
+ @Nullable final String receiverName,
+ @Nullable final String permission) {
+ final PackageManager packageManager = context.getPackageManager();
+ final String packageName = context.getPackageName();
+
+ final List receivers = packageManager
+ .queryBroadcastReceivers(broadcastIntent, PackageManager.GET_INTENT_FILTERS);
+ if (receivers.isEmpty()) {
+ throw new IllegalStateException("No receivers for intent "
+ + OPFUtils.toString(broadcastIntent));
+ }
- final Intent intent = new Intent(ACTION_RECEIVE);
- final List resolveInfos = context.getPackageManager()
- .queryBroadcastReceivers(intent, GET_RESOLVED_FILTER);
-
- for (ResolveInfo resolveInfo : resolveInfos) {
- final IntentFilter intentFilter = resolveInfo.filter;
- if (intentFilter != null && intentFilter.hasAction(ACTION_RECEIVE)
- && intentFilter.hasAction(ACTION_REGISTRATION)
- && intentFilter.hasAction(ACTION_UNREGISTRATION)
- && intentFilter.hasAction(ACTION_NO_AVAILABLE_PROVIDER)) {
- return true;
+ ResolveInfo neededReceiver = null;
+ for (ResolveInfo receiver : receivers) {
+ if ((receiverName == null || receiver.activityInfo.name.equals(receiverName))
+ && receiver.activityInfo.packageName.equals(packageName)) {
+ neededReceiver = receiver;
+ break;
}
}
- return false;
+ if (neededReceiver == null) {
+ throw new IllegalStateException("Receiver " + receiverName
+ + " hasn't been declared in AndroidManifest.xml");
+ }
+
+ if (permission != null
+ && !permission.equals(neededReceiver.activityInfo.permission)) {
+ throw new IllegalStateException("There is no permission "
+ + permission
+ + " for receiver "
+ + receiverName);
+ }
}
}
diff --git a/opfpush/src/test/java/org/onepf/opfpush/SettingsTest.java b/opfpush/src/test/java/org/onepf/opfpush/SettingsTest.java
index 9a005e41..4f5c6a61 100644
--- a/opfpush/src/test/java/org/onepf/opfpush/SettingsTest.java
+++ b/opfpush/src/test/java/org/onepf/opfpush/SettingsTest.java
@@ -2,6 +2,7 @@
import android.content.Context;
import android.content.SharedPreferences;
+import android.util.Log;
import junit.framework.Assert;
@@ -18,29 +19,34 @@
import java.lang.reflect.Field;
import java.util.Random;
+import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
+
/**
* Created by antonpp on 24.02.15.
*/
-@Config(emulateSdk = 18, manifest = Config.NONE)
+@Config(emulateSdk = JELLY_BEAN_MR2, manifest = Config.NONE)
@RunWith(RobolectricTestRunner.class)
+@SuppressWarnings({"checkstyle:methodname", "PMD.MethodNamingConventions"})
public class SettingsTest extends Assert {
+ private static final String LOG_TAG = SettingsTest.class.getSimpleName();
+
private static final String KEY_LAST_PROVIDER_NAME = "last_provider_name";
private static final String KEY_STATE = "state";
private static final String KEY_LAST_ANDROID_ID = "android_id";
private static final int NUM_TESTS = 100;
private static final int NUM_PROVIDERS = 100;
+ private static final int RANDOM_STRING_LENGTH = 16;
private static final Random RND = new Random();
private MockNamePushProvider[] pushProviders;
- private Context ctx;
private SharedPreferences sharedPreferences;
private Settings settings;
@Before
public void setup() {
- ctx = Robolectric.application.getApplicationContext();
+ Context ctx = Robolectric.application.getApplicationContext();
sharedPreferences = ctx.getSharedPreferences(ctx.getPackageName(), Context.MODE_MULTI_PROCESS);
settings = Settings.getInstance(ctx);
pushProviders = new MockNamePushProvider[NUM_PROVIDERS];
@@ -59,11 +65,11 @@ public void eraseSettingsInstance() {
instanceField.set(null, null);
} catch (NoSuchFieldException | IllegalAccessException e) {
- e.printStackTrace();
+ Log.e(LOG_TAG, e.getMessage());
}
}
}
-
+
@Test
public void getState_commonSituation() {
// first is always UNREGISTERED
@@ -71,14 +77,14 @@ public void getState_commonSituation() {
// check all states get correctly
for (State state : State.values()) {
- sharedPreferences.edit().putInt(KEY_STATE, state.getValue()).commit();
+ sharedPreferences.edit().putInt(KEY_STATE, state.getValue()).apply();
assertEquals(state, settings.getState());
}
}
@Test
public void getState_nullIsTreatedAsUnregistered() {
- sharedPreferences.edit().putInt(KEY_STATE, -1).commit();
+ sharedPreferences.edit().putInt(KEY_STATE, -1).apply();
assertEquals(State.UNREGISTERED, settings.getState());
}
@@ -121,7 +127,7 @@ public void getLastProviderName() {
String expected;
for (int i = 0; i < NUM_TESTS; ++i) {
expected = pushProviders[RND.nextInt(NUM_PROVIDERS)].getName();
- sharedPreferences.edit().putString(KEY_LAST_PROVIDER_NAME, expected).commit();
+ sharedPreferences.edit().putString(KEY_LAST_PROVIDER_NAME, expected).apply();
assertEquals(expected, settings.getLastProviderName());
}
}
@@ -152,7 +158,7 @@ private String[] getRandomStrings(int n, int len) {
@Test
public void saveLastAndroidId_commonSituation() {
- String[] randomStrings = getRandomStrings(NUM_TESTS, 16);
+ String[] randomStrings = getRandomStrings(NUM_TESTS, RANDOM_STRING_LENGTH);
for (int i = 0; i < NUM_TESTS; ++i) {
settings.saveLastAndroidId(randomStrings[i]);
assertEquals(randomStrings[i], sharedPreferences.getString(KEY_LAST_ANDROID_ID, null));
@@ -162,23 +168,23 @@ public void saveLastAndroidId_commonSituation() {
@Test
public void saveLastAndroidId_passingNullClearsPreferences() {
// test null case
- sharedPreferences.edit().putString(KEY_LAST_ANDROID_ID, "notNull").commit();
+ sharedPreferences.edit().putString(KEY_LAST_ANDROID_ID, "notNull").apply();
settings.saveLastAndroidId(null);
assertFalse(sharedPreferences.contains(KEY_LAST_ANDROID_ID));
}
@Test
public void getLastAndroidId() {
- String[] randomStrings = getRandomStrings(NUM_TESTS, 16);
+ String[] randomStrings = getRandomStrings(NUM_TESTS, RANDOM_STRING_LENGTH);
for (int i = 0; i < NUM_TESTS; ++i) {
- sharedPreferences.edit().putString(KEY_LAST_ANDROID_ID, randomStrings[i]).commit();
+ sharedPreferences.edit().putString(KEY_LAST_ANDROID_ID, randomStrings[i]).apply();
assertEquals(randomStrings[i], settings.getLastAndroidId());
}
}
@Test
public void getLastAndroidId_and_saveLastAndroidId() {
- String[] randomStrings = getRandomStrings(NUM_TESTS, 16);
+ String[] randomStrings = getRandomStrings(NUM_TESTS, RANDOM_STRING_LENGTH);
for (int i = 0; i < NUM_TESTS; ++i) {
settings.saveLastAndroidId(randomStrings[i]);
assertEquals(randomStrings[i], settings.getLastAndroidId());
diff --git a/opfpush/src/test/java/org/onepf/opfpush/mock/MockNamePushProvider.java b/opfpush/src/test/java/org/onepf/opfpush/mock/MockNamePushProvider.java
index cb9aaa48..aa9873f8 100644
--- a/opfpush/src/test/java/org/onepf/opfpush/mock/MockNamePushProvider.java
+++ b/opfpush/src/test/java/org/onepf/opfpush/mock/MockNamePushProvider.java
@@ -17,26 +17,30 @@ public class MockNamePushProvider extends BasePushProvider {
public MockNamePushProvider(@NonNull String name) {
this(name, DEFAULT_HOST_APP_PACKAGE);
}
-
+
public MockNamePushProvider(@NonNull String name,
- @NonNull String hotAppPackage) {
+ @NonNull String hotAppPackage) {
super(Robolectric.application, name, hotAppPackage);
}
@Override
public void register() {
+ //nothing
}
@Override
public void unregister() {
+ //nothing
}
@Override
public void onRegistrationInvalid() {
+ //nothing
}
@Override
public void onUnavailable() {
+ //nothing
}
@Override
diff --git a/samples/opfpush-sample/build.gradle b/samples/opfpush-sample/build.gradle
index 34b4128f..8dac69c7 100644
--- a/samples/opfpush-sample/build.gradle
+++ b/samples/opfpush-sample/build.gradle
@@ -32,15 +32,14 @@ android {
}
dependencies {
- compile 'org.onepf:opfpush-adm:0.1.4@aar'
- compile 'org.onepf:opfpush-gcm:0.1.4@aar'
- compile 'org.onepf:opfpush-nokia:0.1.4@aar'
- compile ('org.onepf:opfpush:0.1.4@aar') {
- transitive = true
- }
+ compile 'org.onepf:opfpush-adm:0.1.5-SNAPSHOT@aar'
+ compile 'org.onepf:opfpush-gcm:0.1.5-SNAPSHOT@aar'
+ compile 'org.onepf:opfpush-nokia:0.1.5-SNAPSHOT@aar'
+ compile 'org.onepf:opfpush:0.1.5-SNAPSHOT@aar'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.nokia:push:1.0'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'de.greenrobot:eventbus:2.4.0'
+ compile 'org.onepf:opfutils:0.1.15'
}
diff --git a/samples/opfpush-sample/src/main/AndroidManifest.xml b/samples/opfpush-sample/src/main/AndroidManifest.xml
index cb8ade48..2720e964 100644
--- a/samples/opfpush-sample/src/main/AndroidManifest.xml
+++ b/samples/opfpush-sample/src/main/AndroidManifest.xml
@@ -84,6 +84,7 @@
+
diff --git a/settings.gradle b/settings.gradle
index d916fc0c..6f2b9726 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -15,8 +15,14 @@
*/
include ':opfpush',
- ':opfpush-providers:gcm',
- ':opfpush-providers:nokia',
- ':opfpush-providers:adm',
+ ':gcm',
+ ':nokia',
+ ':adm',
/*':unity-plugin',*/
- 'samples:opfpush-sample'
\ No newline at end of file
+ ':opfpush-sample'
+
+project(':gcm').projectDir = new File('opfpush-providers/gcm')
+project(':nokia').projectDir = new File('opfpush-providers/nokia')
+project(':adm').projectDir = new File('opfpush-providers/adm')
+
+project(':opfpush-sample').projectDir = new File('samples/opfpush-sample')
\ No newline at end of file