org.roman.findme.RegistrationIntentService.java Source code

Java tutorial

Introduction

Here is the source code for org.roman.findme.RegistrationIntentService.java

Source

/**
 * 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 org.roman.findme;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.TelephonyManager;
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 org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static org.roman.findme.location_service.Constants.TAG;

public class RegistrationIntentService extends IntentService {
    SharedPreferences sPref;
    public static final String APP_PREFERENCES = "find_me_base";
    private static final String[] TOPICS = { "global" };

    public RegistrationIntentService() {
        super(TAG);
    }

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

        try {

            InstanceID instanceID = InstanceID.getInstance(this);
            final String[] token = new String[] { null };

            token[0] = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            Log.i(TAG, "GCM Registration Token: " + token);

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    sendRegistrationToServer(token[0]);
                }
            });
            thread.start();

            subscribeTopics(token[0]);
            sPref = getSharedPreferences(RegistrationIntentService.APP_PREFERENCES, Context.MODE_PRIVATE);
            SharedPreferences.Editor ed = sPref.edit();
            ed.putString("my_token", token[0]);
            ed.commit();

            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();

        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
            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);
    }

    @SuppressWarnings("deprecation")
    private void sendRegistrationToServer(String token) {
        String device = Build.MANUFACTURER.toUpperCase() + "-" + Build.MODEL + "_" + " " + Build.VERSION.SDK_INT;
        String uid = Build.SERIAL;
        Log.i("TAG", "android.os.Build.SERIAL: " + Build.SERIAL);
        Log.d(TAG, "uid " + uid);

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("token", token));
        nameValuePairs.add(new BasicNameValuePair("action", "register"));
        nameValuePairs.add(new BasicNameValuePair("device", device));
        nameValuePairs.add(new BasicNameValuePair("uid", uid));

        makeResponse(nameValuePairs);

    }

    public static void makeResponse(List<NameValuePair> nameValuePairs) {
        String url = "http://task-master.zzz.com.ua/server.php";
        Log.d(TAG, "sendRequest");
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            InputStream in = response.getEntity().getContent();

            StringBuilder stringbuilder = new StringBuilder();
            BufferedReader bfrd = new BufferedReader(new InputStreamReader(in), 1024);
            String line;
            while ((line = bfrd.readLine()) != null)
                stringbuilder.append(line);

            String downloadedString = stringbuilder.toString();
            Log.d(TAG, downloadedString);

        } catch (Exception e) {
            Log.d(TAG, e.toString());
        }
    }

    /**
     * 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 {
        GcmPubSub pubSub = GcmPubSub.getInstance(this);
        for (String topic : TOPICS) {
            pubSub.subscribe(token, "/topics/" + topic, null);
        }
    }
    // [END subscribe_topics]

}