Use reflection to detect if GCM is available, so it is an optional dependency

This commit is contained in:
Gautier Pelloux-Prayer 2014-12-03 09:50:37 +01:00
parent a7ad4001c2
commit 0e10ba8a88

View file

@ -1,5 +1,7 @@
package org.linphone.compatibility; package org.linphone.compatibility;
import java.lang.reflect.Method;
import org.linphone.LinphonePreferences; import org.linphone.LinphonePreferences;
import org.linphone.R; import org.linphone.R;
import org.linphone.mediastream.Log; import org.linphone.mediastream.Log;
@ -8,8 +10,6 @@ import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.media.AudioManager; import android.media.AudioManager;
import com.google.android.gcm.GCMRegistrar;
/* /*
ApiEightPlus.java ApiEightPlus.java
Copyright (C) 2012 Belledonne Communications, Grenoble, France Copyright (C) 2012 Belledonne Communications, Grenoble, France
@ -31,32 +31,37 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/** /**
* @author Sylvain Berfini * @author Sylvain Berfini
*/ */
@TargetApi(8) @TargetApi(8)
public class ApiEightPlus { public class ApiEightPlus {
public static void initPushNotificationService(Context context) { public static void initPushNotificationService(Context context) {
try { try {
Class<?> GCMRegistrar = Class.forName("com.google.android.gcm.GCMRegistrar");
// Starting the push notification service // Starting the push notification service
GCMRegistrar.checkDevice(context); GCMRegistrar.getMethod("checkDevice", Context.class).invoke(null, context);
try { try {
GCMRegistrar.checkManifest(context); GCMRegistrar.getMethod("checkManifest", Context.class).invoke(null, context);
} catch (IllegalStateException e){ } catch (IllegalStateException e){
Log.e("No receiver found",e); Log.e("Push notification: No receiver found",e);
} }
final String regId = GCMRegistrar.getRegistrationId(context); final String regId = (String)GCMRegistrar.getMethod("getRegistrationId", Context.class).invoke(null, context);
String newPushSenderID = context.getString(R.string.push_sender_id); String newPushSenderID = context.getString(R.string.push_sender_id);
String currentPushSenderID = LinphonePreferences.instance().getPushNotificationRegistrationID(); String currentPushSenderID = LinphonePreferences.instance().getPushNotificationRegistrationID();
if (regId.equals("") || currentPushSenderID == null || !currentPushSenderID.equals(newPushSenderID)) { if (regId.equals("") || currentPushSenderID == null || !currentPushSenderID.equals(newPushSenderID)) {
GCMRegistrar.register(context, newPushSenderID); GCMRegistrar.getMethod("register", Context.class, String[].class).invoke(null, context, new String[]{newPushSenderID});
Log.d("Push Notification : storing current sender id = " + newPushSenderID); Log.d("Push Notification: storing current sender id = " + newPushSenderID);
LinphonePreferences.instance().setPushNotificationRegistrationID(newPushSenderID); LinphonePreferences.instance().setPushNotificationRegistrationID(newPushSenderID);
} else { } else {
Log.d("Push Notification : already registered with id = " + regId); Log.d("Push Notification: already registered with id = " + regId);
LinphonePreferences.instance().setPushNotificationRegistrationID(regId); LinphonePreferences.instance().setPushNotificationRegistrationID(regId);
} }
} catch (java.lang.UnsupportedOperationException e) { } catch (java.lang.UnsupportedOperationException e) {
Log.i("Push Notification not activated"); Log.i("Push Notification: not activated");
} catch (Exception e1) {
//assume the jar is not provided
Log.i("Push Notification: assuming GCM jar is not provided.");
} }
} }