/*
Swisscom Safe Connect
Copyright (C) 2014 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.utils.gcm;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.swisscom.safeconnect.BuildConfig;
import com.swisscom.safeconnect.activity.DashboardActivity;
import com.swisscom.safeconnect.backend.BackendConnector;
import com.swisscom.safeconnect.model.PlumberResponse;
import com.swisscom.safeconnect.utils.Config;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by cianci on 9/10/14.
*/
public class GcmManager {
private GoogleCloudMessaging gcm;
private AtomicInteger msgId = new AtomicInteger();
private String regid;
private Context mContext;
public GcmManager(Context ctx) {
mContext = ctx;
}
public void registerPlayServices() {
// Check device for Play Services APK. If check succeeds, proceed with GCM registration.
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(mContext);
registerInBackground();
} else {
Log.i(Config.TAG, "No valid Google Play Services APK found.");
}
}
/**
* Check the device to make sure it has the Google Play Services APK. If
* it doesn't, display a dialog that allows users to download the APK from
* the Google Play Store or enable it in the device's system settings.
*/
public boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if (resultCode != ConnectionResult.SUCCESS && resultCode != ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, (DashboardActivity)mContext,
Config.PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
if (BuildConfig.DEBUG) Log.d(Config.TAG, "This device is not supported for push notifications.");
//finish();
}
return false;
}
return true;
}
private void sendRegistrationIdToBackend(String gcmId) {
new BackendConnector(mContext).saveGcmId(Config.getInstance().getPhoneNumber(), Config.getInstance().getDeviceId(), gcmId,
new BackendConnector.ResponseCallback<PlumberResponse>() {
@Override
public void onRequestComplete(int statusCode, PlumberResponse response) {
if (statusCode == 200) {
if (BuildConfig.DEBUG) Log.d(Config.TAG, "GCM id was sent to plumber server");
} else {
Log.d(Config.TAG, "Something went wrong when sending gcm id to plumber!");
}
}
});
}
/**
* Registers the application with GCM servers asynchronously.
* <p>
* Stores the registration ID and app versionCode in the application's
* shared preferences.
*/
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(mContext);
}
regid = gcm.register(Config.SENDER_ID);
msg = "Device registered, registration ID=" + regid;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
sendRegistrationIdToBackend(regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
if (BuildConfig.DEBUG) Log.d(Config.TAG, msg);
}
}.execute(null, null, null);
}
}