Skip to content

Instantly share code, notes, and snippets.

@wangerekaharun
Created December 22, 2017 18:17
Show Gist options
  • Save wangerekaharun/0c0af715640fbbe8269461fb7ff88c01 to your computer and use it in GitHub Desktop.
Save wangerekaharun/0c0af715640fbbe8269461fb7ff88c01 to your computer and use it in GitHub Desktop.
ServiceClass
package ke.co.appslab.callsledger.Services;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import ke.co.appslab.callsledger.API.ApIService;
import ke.co.appslab.callsledger.API.ApiEndpoints;
import ke.co.appslab.callsledger.Models.CallLogs;
import ke.co.appslab.callsledger.SQLite.DBOperations;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by harun on 11/3/17.
*/
public class CallLogService extends Service {
DBOperations dbOperations;
List<CallLogs> callLogsList=new ArrayList<>();
SharedPreferences sharedPreferences;
private static final String PREF_NAME="callsledger_pref";
public static final String ACCESS_TOKEN="access_token";
int clientId;
String agentId,duration,description,date,accessToken;
public CallLogService(Context context){
super();
}
public CallLogService(){
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
dbOperations=new DBOperations(getApplicationContext());
sharedPreferences=getApplicationContext().getSharedPreferences(PREF_NAME,getApplicationContext().MODE_PRIVATE);
callLogsList=dbOperations.getCallLogs();
try {
for (int i=0;i<callLogsList.size();i++){
clientId=callLogsList.get(i).getClientId();
agentId=callLogsList.get(i).getAgentId();
description=callLogsList.get(i).getCallType();
duration=callLogsList.get(i).getCallDuration();
date=callLogsList.get(i).getCallDate();
dbOperations.updateCallSyncStatus(callLogsList.get(i).getCallLogId(),1);
//post the data to db
accessToken=sharedPreferences.getString(ACCESS_TOKEN,null);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
//.addHeader("Accept","application/json")
.addHeader("Authorization", "Bearer " + accessToken)
.build();
return chain.proceed(newRequest);
}
}).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiEndpoints.SEND_CALL_LOGS)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
ApIService service = retrofit.create(ApIService.class);
Call<CallLogs> call=service.postCallLogs(agentId,clientId,description,duration,date);
call.enqueue(new Callback<CallLogs>() {
@Override
public void onResponse(Call<CallLogs> call, Response<CallLogs> response) {
if (response.isSuccessful()){
Toast.makeText(getApplicationContext(),"Record added successfully",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(),response.message(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<CallLogs> call, Throwable t) {
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}catch (Exception a){
a.printStackTrace();
Toast.makeText(getApplicationContext(),"No new records to sync",Toast.LENGTH_SHORT).show();
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent broadCastIntent=new Intent("android.intent.action.BOOT_COMPLETED");
sendBroadcast(broadCastIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment