package co.babil.braavos.modules.gcm;

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;

import co.babil.braavos.R;

public class GCMRegistrationIntentService extends IntentService {
    private static final String TAG = "IntentService";

    public GCMRegistrationIntentService() {
        super(GCMRegistrationIntentService.class.getName());
        Log.d(TAG, "gcm service started");

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = GCMPreferences.get(this);
        Log.d(TAG, "gcm service");

        try {
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            Log.i(TAG, "GCM Registration Token: " + token);
            GCMMessageHelpers.sendUpstreamMessageWithToken(getApplicationContext(), token);

            sharedPreferences.edit().putString(GCMPreferences.TOKEN, token).apply();
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
        }

        Intent registrationComplete = new Intent(GCMPreferences.SAVED_TO_SERVER);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

//    private void subscribeTopics(String token) throws IOException {
//        GcmPubSub pubSub = GcmPubSub.getInstance(this);
//
//        for (String topic : TOPICS) {
//            pubSub.subscribe(token, "/topics/" + topic, null);
//        }
//    }
}

@@ -419,16 +419,17 @@
 rvice %7B%0A
+%0A
     priv
@@ -459,16 +459,19 @@
  TAG = %22
+GCM
 IntentSe
@@ -588,52 +588,8 @@
 ));%0A
-        Log.d(TAG, %22gcm service started%22);%0A%0A
     
@@ -731,43 +731,8 @@
 is);
-%0A        Log.d(TAG, %22gcm service%22);
 %0A%0A  
@@ -985,21 +985,28 @@
 Log.
-i
+d
 (TAG, %22
-GCM R
+Refreshing r
 egis
@@ -1013,17 +1013,17 @@
 tration 
-T
+t
 oken: %22 
@@ -1032,16 +1032,17 @@
 token);%0A
+%0A
         
@@ -1117,33 +1117,32 @@
 text(), token);%0A
-%0A
             shar
@@ -1254,25 +1254,25 @@
         Log.
-d
+e
 (TAG, %22Faile
@@ -1492,264 +1492,7 @@
    %7D
-%0A%0A//    private void subscribeTopics(String token) throws IOException %7B%0A//        GcmPubSub pubSub = GcmPubSub.getInstance(this);%0A//%0A//        for (String topic : TOPICS) %7B%0A//            pubSub.subscribe(token, %22/topics/%22 + topic, null);%0A//        %7D%0A//    %7D
 %0A%7D%0A

Tweak GCM logs