/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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 gcm.play.android.samples.com.gcmquickstart;

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

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

import java.io.IOException;

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";
    private static final String[] TOPICS = {"global"};

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        try {
            // In the (unlikely) event that multiple refresh operations occur simultaneously,
            // ensure that they are processed sequentially.
            synchronized (TAG) {
                // [START register_for_gcm]
                // Initially this call goes out to the network to retrieve the token, subsequent calls
                // are local.
                // [START get_token]
                InstanceID instanceID = InstanceID.getInstance(this);
                String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                // [END get_token]
                Log.i(TAG, "GCM Registration Token: " + token);

                // TODO: Implement this method to send any registration to your app's servers.
                sendRegistrationToServer(token);

                // Subscribe to topic channels
                subscribeTopics(token);

                // You should store a boolean that indicates whether the generated token has been
                // sent to your server. If the boolean is false, send the token to your server,
                // otherwise your server should have already received the token.
                sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
                // [END register_for_gcm]
            }
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
            // If an exception happens while fetching the new token or updating our registration data
            // on a third-party server, this ensures that we'll attempt the update at a later time.
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
        }
        // Notify UI that registration has completed, so the progress indicator can be hidden.
        Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

    /**
     * Persist registration to third-party servers.
     *
     * Modify this method to associate the user's GCM registration token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }

    /**
     * Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
     *
     * @param token GCM token
     * @throws IOException if unable to reach the GCM PubSub service
     */
    // [START subscribe_topics]
    private void subscribeTopics(String token) throws IOException {
        for (String topic : TOPICS) {
            GcmPubSub pubSub = GcmPubSub.getInstance(this);
            pubSub.subscribe(token, "/topics/" + topic, null);
        }
    }
    // [END subscribe_topics]

}

@@ -1515,199 +1515,8 @@
   //
- In the (unlikely) event that multiple refresh operations occur simultaneously,%0A            // ensure that they are processed sequentially.%0A            synchronized (TAG) %7B%0A                //
  %5BST
@@ -1529,36 +1529,32 @@
 gister_for_gcm%5D%0A
-    
             // I
@@ -1648,20 +1648,16 @@
         
-    
 // are l
@@ -1662,20 +1662,16 @@
  local.%0A
-    
         
@@ -1707,20 +1707,16 @@
         
-  
-  
 Instance
@@ -1753,36 +1753,32 @@
 Instance(this);%0A
-    
             Stri
@@ -1869,20 +1869,16 @@
         
-   
- 
 GoogleCl
@@ -1920,36 +1920,32 @@
 l);%0A            
-    
 // %5BEND get_toke
@@ -1955,28 +1955,24 @@
             
-   
- 
 Log.i(TAG, %22
@@ -2000,36 +2000,32 @@
 n: %22 + token);%0A%0A
-    
             // T
@@ -2099,20 +2099,16 @@
 ervers.%0A
-    
         
@@ -2157,20 +2157,16 @@
         
-    
 // Subsc
@@ -2188,20 +2188,16 @@
 hannels%0A
-    
         
@@ -2237,20 +2237,16 @@
         
-    
 // You s
@@ -2331,20 +2331,16 @@
         
-    
 // sent 
@@ -2419,28 +2419,24 @@
             
- 
-   
 // otherwise
@@ -2484,28 +2484,24 @@
  the token.%0A
-    
             
@@ -2599,36 +2599,32 @@
 ();%0A            
- 
-   
 // %5BEND register
@@ -2637,22 +2637,8 @@
 cm%5D%0A
-            %7D%0A
     

Remove unnecessary synchronized.