Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.saholic.profittill.Services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.saholic.profittill.Constants.ProfitTillConstants;
import com.saholic.profittill.Network.NotificationPoll;
import com.saholic.profittill.Utils.UtilityFunctions;

import org.apache.http.HttpEntity;
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.util.ArrayList;

public class PollingService extends IntentService {
    SharedPreferences apiData, userData;
    SharedPreferences.Editor apiEditor,userEditor,userDataEditor;
    String regId;
    String msg = "";
    ArrayList<NameValuePair> nameValuePairsGcm;
    public PollingService() {

        super("PollingService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);

        userEditor = userData.edit();
        userDataEditor = userData.edit();
        apiEditor = apiData.edit();
        userEditor.putString("alarm_set", "true").commit();
        if (!userData.getString("id", "").equals("")) {
            if (isInternetOn()) {
                String url = apiData.getString("pollnotification.url", "http://45.33.50.227:3001/pollNotifications") + "?user_id=" + userData.getString("id", "") + "&android_id=" + UtilityFunctions.androidId(getApplicationContext());
                new NotificationPoll().fetchNotfications(getApplicationContext(), null, url);
            }

            if (userData.getString("fcm_token_sent", "false").equals("false") && (!userData.getString("id", "").equals(""))) {
                regId = FirebaseInstanceId.getInstance().getToken();
                Log.d("RegisterActivity", "registerInBackground - regId: "
                        + regId);
                msg = "Device registered";
                new gcmPushData().execute(msg);
            }

        }
    }

    public final boolean isInternetOn() {
        ConnectivityManager connec = (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
        if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
            return true;

        } else if (
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
            return false;
        }
        return false;
    }
    class gcmPushData extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... arg0) {
            String id = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(apiData.getString("gcm.push.url", "http://api.profittill.com/gcm_users/add"));
                nameValuePairsGcm = new ArrayList<>();
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid", regId));
                nameValuePairsGcm.add(new BasicNameValuePair("imeinumber", telephonyManager.getDeviceId()));
                nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", null)));
                nameValuePairsGcm.add(new BasicNameValuePair("notification_type","fcm"));
                nameValuePairsGcm.add(new BasicNameValuePair("device_message", arg0[0]));
                nameValuePairsGcm.add(new BasicNameValuePair("androidid", UtilityFunctions.androidId(PollingService.this)));
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    Log.d("ResponseCode GCM ", status + "");
                    userDataEditor.putString("fcm_token_sent", "true");
                    userDataEditor.commit();
                } else {
                    Log.d("ResponseCode GCM ", status + "");
                }

                nameValuePairsGcm.clear();
                Log.e("pass 1", "connection success ");
            } catch (Exception e) {
                Log.e("Fail 1", e.toString());

            }
            return id;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
}}}