eu.codeplumbers.cosi.services.CosiCallService.java Source code

Java tutorial

Introduction

Here is the source code for eu.codeplumbers.cosi.services.CosiCallService.java

Source

/*
 *     Cos android client for Cozy Cloud
 *
 *     Copyright (C)  2016 Hamza Abdelkebir
 *
 *     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 eu.codeplumbers.cosi.services;

import android.Manifest;
import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.CallLog;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.support.v7.app.NotificationCompat;
import android.util.Base64;
import android.util.Log;

import org.apache.commons.io.IOUtils;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import eu.codeplumbers.cosi.R;
import eu.codeplumbers.cosi.db.models.Call;
import eu.codeplumbers.cosi.db.models.Device;
import eu.codeplumbers.cosi.services.events.CallSyncEvent;
import eu.codeplumbers.cosi.utils.DateUtils;

import static eu.codeplumbers.cosi.utils.Constants.REFRESH;
import static eu.codeplumbers.cosi.utils.Constants.SERVICE_ERROR;
import static eu.codeplumbers.cosi.utils.Constants.SYNC_MESSAGE;

/**
 * Created by thor on 10/29/16.
 */

public class CosiCallService extends IntentService {

    private List<Call> allCalls;
    private String errorMessage;
    private String authHeader;
    private String designUrl;
    private String syncUrl;

    private int notification_id = 1338;
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private boolean mSyncRunning;
    private boolean stopSync;

    public CosiCallService() {
        super("CosiCallService");

        stopSync = false;
        mSyncRunning = false;
    }

    public CosiCallService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // Do the task here
        Log.i(CosiCallService.class.getName(), "Cosi Service running");

        // Release the wake lock provided by the WakefulBroadcastReceiver.
        WakefulBroadcastReceiver.completeWakefulIntent(intent);

        allCalls = new ArrayList<>();

        Device device = Device.registeredDevice();

        // cozy register device url
        designUrl = device.getUrl() + "/ds-api/request/call/all/";
        syncUrl = device.getUrl() + "/ds-api/data/";

        // concatenate username and password with colon for authentication
        final String credentials = device.getLogin() + ":" + device.getPassword();
        authHeader = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

        showNotification();

        if (isNetworkAvailable()) {
            try {
                // read local sms log first
                readCallLog();
                EventBus.getDefault().post(new CallSyncEvent(SYNC_MESSAGE, "Getting remote calls from Cozy..."));
                getRemoteCalls();

                mBuilder.setContentText(getString(R.string.lbl_sms_sync_done));
                mBuilder.setProgress(0, 0, false);
                mNotifyManager.notify(notification_id, mBuilder.build());

                if (!allCalls.isEmpty()) {
                    mNotifyManager.cancel(notification_id);
                    sendChangesToCozy();
                    EventBus.getDefault().post(new CallSyncEvent(REFRESH, "Sync OK"));
                } else {
                    EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, "Sync failed"));
                }

            } catch (Exception e) {
                e.printStackTrace();
                mSyncRunning = false;
                EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
            }
        } else {
            mSyncRunning = false;
            EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, "No Internet connection"));
            mBuilder.setContentText("Sync failed because no Internet connection was available");
            mNotifyManager.notify(notification_id, mBuilder.build());
        }
    }

    /**
     * Read phone call log and update app database
     */
    private void readCallLog() {
        //Fetches the complete call log in descending order. i.e recent calls appears first.
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
            Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,
                    CallLog.Calls.DATE + " DESC");

            int totalCalls = c.getCount();

            EventBus.getDefault()
                    .post(new CallSyncEvent(SYNC_MESSAGE, "Reading phone log: " + c.getCount() + " calls..."));

            if (c.moveToFirst()) {
                for (int i = 0; i < totalCalls; i++) {
                    EventBus.getDefault()
                            .post(new CallSyncEvent(SYNC_MESSAGE, "Local call " + i + "/" + totalCalls + "..."));
                    String callerID = c.getString(c.getColumnIndex(CallLog.Calls._ID));
                    String callerNumber = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
                    long callDateandTime = c.getLong(c.getColumnIndex(CallLog.Calls.DATE));
                    long callDuration = c.getLong(c.getColumnIndex(CallLog.Calls.DURATION));
                    int callType = c.getInt(c.getColumnIndex(CallLog.Calls.TYPE));

                    Call call = Call.getByIdNumberDate(callerID, callerNumber, callDateandTime);

                    if (call == null) {
                        call = new Call();
                    }

                    call.setRemoteId(
                            (call.getRemoteId() != "" && call.getRemoteId() != null) ? call.getRemoteId() : "");
                    call.setCallerId(callerID);
                    call.setCallerNumber(callerNumber);
                    call.setDateAndTime(DateUtils.formatDate(callDateandTime));
                    call.setType(callType);
                    call.setDuration(callDuration);

                    boolean hasDevice = call.getDeviceId() != "" && call.getDeviceId() != null;

                    call.setDeviceId(hasDevice ? call.getDeviceId() : Device.registeredDevice().getLogin());
                    call.save();

                    allCalls.add(call);

                    c.moveToNext();
                }
            }
            c.close();
        } else {
            EventBus.getDefault()
                    .post(new CallSyncEvent(SERVICE_ERROR, getString(R.string.permission_denied_call_log)));
        }
    }

    /**
     * Make remote request to get all calls stored in Cozy
     */
    public String getRemoteCalls() {
        URL urlO = null;
        try {
            urlO = new URL(designUrl);
            HttpURLConnection conn = (HttpURLConnection) urlO.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            conn.setRequestProperty("Authorization", authHeader);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");

            // read the response
            int status = conn.getResponseCode();
            InputStream in = null;

            if (status >= HttpURLConnection.HTTP_BAD_REQUEST) {
                in = conn.getErrorStream();
            } else {
                in = conn.getInputStream();
            }

            StringWriter writer = new StringWriter();
            IOUtils.copy(in, writer, "UTF-8");
            String result = writer.toString();

            JSONArray jsonArray = new JSONArray(result);

            if (jsonArray != null) {
                if (jsonArray.length() == 0) {
                    EventBus.getDefault().post(new CallSyncEvent(SYNC_MESSAGE, "Your Cozy has no calls stored."));
                    Call.setAllUnsynced();
                } else {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        EventBus.getDefault().post(new CallSyncEvent(SYNC_MESSAGE,
                                "Reading calls on Cozy " + i + "/" + jsonArray.length() + "..."));
                        JSONObject callJson = jsonArray.getJSONObject(i).getJSONObject("value");
                        Call call = Call.getByRemoteId(callJson.get("_id").toString());
                        if (call == null) {
                            call = new Call(callJson);
                        } else {
                            call.setRemoteId(callJson.getString("_id"));
                            call.setCallerId(callJson.getString("callerId"));
                            call.setCallerNumber(callJson.getString("callerNumber"));
                            call.setDuration(callJson.getLong("duration"));
                            call.setDateAndTime(callJson.getString("dateAndTime"));
                            call.setType(callJson.getInt("type"));
                        }

                        call.save();

                        allCalls.add(call);
                    }
                }
            } else {
                errorMessage = new JSONObject(result).getString("error");
                EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, errorMessage));
            }

            in.close();
            conn.disconnect();

        } catch (MalformedURLException e) {
            EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
            stopSelf();
        } catch (ProtocolException e) {
            EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
            stopSelf();
        } catch (IOException e) {
            EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
            stopSelf();
        } catch (JSONException e) {
            EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
            stopSelf();
        }
        return errorMessage;
    }

    public void sendChangesToCozy() {
        List<Call> unSyncedCalls = Call.getAllUnsynced();
        int i = 0;
        for (Call call : unSyncedCalls) {
            URL urlO = null;
            try {
                JSONObject jsonObject = call.toJsonObject();
                mBuilder.setProgress(unSyncedCalls.size(), i, false);
                mBuilder.setContentText("Syncing " + jsonObject.getString("docType") + ":");
                mNotifyManager.notify(notification_id, mBuilder.build());
                EventBus.getDefault()
                        .post(new CallSyncEvent(SYNC_MESSAGE, getString(R.string.lbl_calls_send_changes)));
                String remoteId = jsonObject.getString("remoteId");
                String requestMethod = "";

                if (remoteId.isEmpty()) {
                    urlO = new URL(syncUrl);
                    requestMethod = "POST";
                } else {
                    urlO = new URL(syncUrl + remoteId + "/");
                    requestMethod = "PUT";
                }

                HttpURLConnection conn = (HttpURLConnection) urlO.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                conn.setRequestProperty("Authorization", authHeader);
                conn.setDoOutput(true);
                conn.setDoInput(true);

                conn.setRequestMethod(requestMethod);

                // set request body
                jsonObject.remove("remoteId");
                long objectId = jsonObject.getLong("id");
                jsonObject.remove("id");
                OutputStream os = conn.getOutputStream();
                os.write(jsonObject.toString().getBytes("UTF-8"));
                os.flush();

                // read the response
                InputStream in = new BufferedInputStream(conn.getInputStream());

                StringWriter writer = new StringWriter();
                IOUtils.copy(in, writer, "UTF-8");
                String result = writer.toString();

                JSONObject jsonObjectResult = new JSONObject(result);

                if (jsonObjectResult != null && jsonObjectResult.has("_id")) {
                    result = jsonObjectResult.getString("_id");
                    call.setRemoteId(result);
                    call.save();
                }

                in.close();
                conn.disconnect();

            } catch (MalformedURLException e) {
                EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
                stopSelf();
            } catch (ProtocolException e) {
                EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
                stopSelf();
            } catch (IOException e) {
                EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
                stopSelf();
            } catch (JSONException e) {
                EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
                stopSelf();
            }
            i++;
        }
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
                Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public void showNotification() {
        mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle(getString(R.string.lbl_calls))
                .setContentText(getString(R.string.lbl_notes_ongoing_sync))
                .setSmallIcon(R.drawable.ic_call_black_24dp).setOngoing(true);

    }
}