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;
import java.lang.reflect.Method;
import org.linphone.LinphonePreferences;
import org.linphone.R;
import org.linphone.mediastream.Log;
@ -8,8 +10,6 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.media.AudioManager;
import com.google.android.gcm.GCMRegistrar;
/*
ApiEightPlus.java
Copyright (C) 2012 Belledonne Communications, Grenoble, France
@ -26,42 +26,47 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* @author Sylvain Berfini
*/
@TargetApi(8)
public class ApiEightPlus {
public static void initPushNotificationService(Context context) {
try {
// Starting the push notification service
GCMRegistrar.checkDevice(context);
try {
GCMRegistrar.checkManifest(context);
} catch (IllegalStateException e){
Log.e("No receiver found",e);
}
final String regId = GCMRegistrar.getRegistrationId(context);
String newPushSenderID = context.getString(R.string.push_sender_id);
String currentPushSenderID = LinphonePreferences.instance().getPushNotificationRegistrationID();
if (regId.equals("") || currentPushSenderID == null || !currentPushSenderID.equals(newPushSenderID)) {
GCMRegistrar.register(context, newPushSenderID);
public static void initPushNotificationService(Context context) {
try {
Class<?> GCMRegistrar = Class.forName("com.google.android.gcm.GCMRegistrar");
// Starting the push notification service
GCMRegistrar.getMethod("checkDevice", Context.class).invoke(null, context);
try {
GCMRegistrar.getMethod("checkManifest", Context.class).invoke(null, context);
} catch (IllegalStateException e){
Log.e("Push notification: No receiver found",e);
}
final String regId = (String)GCMRegistrar.getMethod("getRegistrationId", Context.class).invoke(null, context);
String newPushSenderID = context.getString(R.string.push_sender_id);
String currentPushSenderID = LinphonePreferences.instance().getPushNotificationRegistrationID();
if (regId.equals("") || currentPushSenderID == null || !currentPushSenderID.equals(newPushSenderID)) {
GCMRegistrar.getMethod("register", Context.class, String[].class).invoke(null, context, new String[]{newPushSenderID});
Log.d("Push Notification : storing current sender id = " + newPushSenderID);
LinphonePreferences.instance().setPushNotificationRegistrationID(newPushSenderID);
} else {
Log.d("Push Notification : already registered with id = " + regId);
LinphonePreferences.instance().setPushNotificationRegistrationID(regId);
}
} catch (java.lang.UnsupportedOperationException e) {
Log.i("Push Notification not activated");
}
}
Log.d("Push Notification: storing current sender id = " + newPushSenderID);
LinphonePreferences.instance().setPushNotificationRegistrationID(newPushSenderID);
} else {
Log.d("Push Notification: already registered with id = " + regId);
LinphonePreferences.instance().setPushNotificationRegistrationID(regId);
}
} catch (java.lang.UnsupportedOperationException e) {
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.");
}
}
@SuppressWarnings("deprecation")
public static String getAudioManagerEventForBluetoothConnectionStateChangedEvent() {
return AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED;
}
}
@SuppressWarnings("deprecation")
public static String getAudioManagerEventForBluetoothConnectionStateChangedEvent() {
return AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED;
}
}