Rev 18 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
package com.cesams.twogetskills.inconcert.activity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.core.splashscreen.SplashScreen;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.PeriodicSync;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnticipateInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupMenu;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.cesams.twogetskills.inconcert.Configuration;
import com.cesams.twogetskills.inconcert.dao.AnswerDao;
import com.cesams.twogetskills.inconcert.dao.AppDatabase;
import com.cesams.twogetskills.inconcert.dao.CapsuleDao;
import com.cesams.twogetskills.inconcert.dao.CompanyDao;
import com.cesams.twogetskills.inconcert.dao.DatabaseHelper;
import com.cesams.twogetskills.inconcert.dao.NotificationCenterDao;
import com.cesams.twogetskills.inconcert.dao.ConfigDao;
import com.cesams.twogetskills.inconcert.dao.ProgressDao;
import com.cesams.twogetskills.inconcert.dao.QuestionDao;
import com.cesams.twogetskills.inconcert.dao.QuizDao;
import com.cesams.twogetskills.inconcert.dao.SlideDao;
import com.cesams.twogetskills.inconcert.dao.SyncDao;
import com.cesams.twogetskills.inconcert.dao.TopicDao;
import com.cesams.twogetskills.inconcert.dao.UserExtendedDao;
import com.cesams.twogetskills.inconcert.dao.UserLogDao;
import com.cesams.twogetskills.inconcert.entity.Config;
import com.cesams.twogetskills.inconcert.entity.UserExtended;
import com.cesams.twogetskills.inconcert.fragment.BiometricOptions;
import com.cesams.twogetskills.inconcert.fragment.CapsuleDetail;
import com.cesams.twogetskills.inconcert.fragment.CapsuleFragment;
import com.cesams.twogetskills.inconcert.fragment.CompanyFragment;
import com.cesams.twogetskills.inconcert.fragment.FinishCapsuleFragment;
import com.cesams.twogetskills.inconcert.fragment.FinishTopicFragment;
import com.cesams.twogetskills.inconcert.fragment.GalleryFragment;
import com.cesams.twogetskills.inconcert.fragment.NotificationCenter;
import com.cesams.twogetskills.inconcert.fragment.ProgressFragment;
import com.cesams.twogetskills.inconcert.fragment.SlideFragment;
import com.cesams.twogetskills.inconcert.fragment.TimelineFragment;
import com.cesams.twogetskills.inconcert.fragment.TopicFragment;
import com.cesams.twogetskills.inconcert.fragment.UserProfileFragment;
import com.cesams.twogetskills.inconcert.fragment.MyCapsulesFragment;
import com.cesams.twogetskills.inconcert.library.Http;
import com.cesams.twogetskills.inconcert.library.MD5;
import com.cesams.twogetskills.inconcert.library.UniqueID;
import com.cesams.twogetskills.inconcert.entity.Answer;
import com.cesams.twogetskills.inconcert.entity.Capsule;
import com.cesams.twogetskills.inconcert.entity.Company;
import com.cesams.twogetskills.inconcert.entity.Progress;
import com.cesams.twogetskills.inconcert.entity.Question;
import com.cesams.twogetskills.inconcert.entity.Quiz;
import com.cesams.twogetskills.inconcert.entity.Slide;
import com.cesams.twogetskills.inconcert.entity.Sync;
import com.cesams.twogetskills.inconcert.entity.Topic;
import com.cesams.twogetskills.inconcert.entity.UserLog;
import com.cesams.twogetskills.inconcert.preference.Preference;
import com.cesams.twogetskills.inconcert.receiver.ConnectivityReceiver;
import com.cesams.twogetskills.inconcert.receiver.InternalReceiver;
import com.cesams.twogetskills.inconcert.room.ResultCount;
import com.cesams.twogetskills.inconcert.skeleton.IReloadData;
import com.cesams.twogetskills.inconcert.sync.WorkSync;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.widget.Toolbar;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;
import com.cesams.twogetskills.inconcert.Constants;
import com.cesams.twogetskills.inconcert.R;
import com.cesams.twogetskills.inconcert.fragment.IntroFragment;
import com.cesams.twogetskills.inconcert.fragment.SigninFragment;
import com.cesams.twogetskills.inconcert.skeleton.ITwoGetSkills;
import com.google.firebase.messaging.FirebaseMessaging;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
//import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements ITwoGetSkills {
/*
private boolean isSyncDevice = false;
private boolean isSyncToken = false;
private boolean isSyncBatch = false;
*/
private boolean isForeground = false;
private boolean isSmartLockLoginRun = false;
private boolean isShowingCartelRefresh = false;
private final static String PREFIX_FRAG = "FRAG";
private final static String TAG = "C2GS - MainActivity";
private HashMap<String, Fragment> fragmentHashMap;
private TextView textViewMessageNotConnection;
BottomNavigationView mNavigationView;
private Preference preference;
private boolean connected = false;
private ConnectivityReceiver mConnectivityReceiver;
private InternalReceiver mInternalReceiver;
private ProgressBar mProgressBar;
private Toolbar mToolbar;
//private Account mAccount;
private AppDatabase mAppDatabase;
private WorkManager mWorkManager;
private boolean isActiveOperation;
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if(result.getResultCode() == RESULT_OK) {
boolean completed = result.getData().hasExtra("completed") && result.getData().getBooleanExtra("completed", false);
int requestCode = result.getData().hasExtra("requestCode") ? result.getData().getIntExtra("requestCode", 0) : 0;
if(requestCode == Constants.REQUEST_CODE_QUIZ) {
createProgressAndSyncRecord(preference.getSlideUuidActive(), completed, true, false);
} else if(requestCode == Constants.REQUEST_CODE_AUDIO_VIDEO) {
createProgressAndSyncRecord(preference.getSlideUuidActive(), completed, false, true);
} else {
createProgressAndSyncRecord(preference.getSlideUuidActive(), completed, false, false);
}
}
}
);
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "MainActivity onCreate");
mAppDatabase = DatabaseHelper.getInstance(getApplicationContext()).getAppDatabase();
setNoCapsuleActive();
preference = Preference.getInstance(getApplicationContext());
preference.load();
if(TextUtils.isEmpty(preference.getDeviceUuid())) {
Log.e("BUG Token", "Preference - Set Device");
String uuid = UniqueID.id(getApplicationContext());
preference.setDeviceUuid(uuid);
preference.save();
Log.e("BUG Token", "SyncRecord - Device");
Sync sync = new Sync(Constants.SYNC_ADAPTER_TYPE_DEVICE, uuid);
mAppDatabase.getSyncDao().insert(sync);
registerDevice(sync);
if (!TextUtils.isEmpty(preference.getDeviceToken())) {
Log.e("BUG Token", "SyncRecord - FCM1");
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_FCM, preference.getDeviceToken());
mAppDatabase.getSyncDao().insert(sync);
registerFcm(sync);
}
}
/**** CANAL DE NOTIFICACIONES **/
createNotificationChannel();
// badgenotification= findViewById(R.id.cart_badge);
/*** CUENTAS DE SINCRONIZACION **/
//mAccount = CreateSyncAccount(getApplicationContext());
textViewMessageNotConnection = findViewById(R.id.main_activity_text_view_message_not_connection);
mConnectivityReceiver = new ConnectivityReceiver();
registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
IntentFilter intentFilterInternal = new IntentFilter();
intentFilterInternal.addAction(Constants.BROADCAST_TYPE_NOTIFICATION);
intentFilterInternal.addAction(Constants.BROADCAST_TYPE_TOKEN);
intentFilterInternal.addAction(Constants.BROADCAST_TYPE_COMMAND);
intentFilterInternal.addAction(Constants.BROADCAST_TYPE_SYNC_TO_SERVER_OR_CHECK_CHANGES);
mInternalReceiver = new InternalReceiver();
this.registerReceiver(mInternalReceiver, intentFilterInternal);
fragmentHashMap = new HashMap<>();
mProgressBar = findViewById(R.id.progressBar);
mToolbar = findViewById(R.id.toolbar);
mToolbar.setOverflowIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.toolbarnotifi));
setSupportActionBar(findViewById(R.id.toolbar));
getSupportActionBar().setIcon(R.drawable.inconcertmargin);
mNavigationView = findViewById(R.id.bottomNavigationView);
mNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//Ocultamos el teclado para mejor experiencia
hideKeyboard(mNavigationView);
if(item.getItemId()==R.id.action_home){
setTitleActionBar(getString(R.string.app_name));
invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
mNavigationView.getMenu().getItem(0).setChecked(true);
}
if(item.getItemId()==R.id.action_topicos){
setTitleActionBar(getString(R.string.menu_topics));
invokeFragment(Constants.IDX_FRAGMENT_TOPICS);
mNavigationView.getMenu().getItem(1).setChecked(true);
}
if(item.getItemId()==R.id.action_progreso){
setTitleActionBar(getString(R.string.progress_title));
invokeFragment(Constants.IDX_FRAGMENT_COMPANIES);
mNavigationView.getMenu().getItem(2).setChecked(true);
}
if(item.getItemId()==R.id.action_more){
PopupMenu popupMenu = new PopupMenu(MainActivity.this,MainActivity.this.findViewById(R.id.action_more));
// Inflating popup menu from popup_menu.xml file
popupMenu.getMenuInflater().inflate(R.menu.more_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Toast message on menu item clicked
//Toast.makeText(MainActivity.this, "You Clicked " + menuItem.getTitle(), Toast.LENGTH_SHORT).show();
if(menuItem.getItemId()==R.id.miperfil){
setTitleActionBar(getString(R.string.menu_user_profile));
invokeFragment(Constants.IDX_FRAGMENT_USER_PROFILE);
}
if(menuItem.getItemId()==R.id.lineadeltiempo){
setTitleActionBar(getString(R.string.menu_timeline));
invokeFragment(Constants.IDX_FRAGMENT_TIMELINE);
}
return true;
}
});
// Showing the popup menu
popupMenu.show();
mNavigationView.getMenu().getItem(3).setChecked(true);
}
return false;
}
});
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
String token = task.getResult();
if(preference.getDeviceToken().isEmpty() || !preference.getDeviceToken().equals(token)) {
createSyncRecordNewToken(token);
Log.e("BUG Token", "Token 1 : " + token);
}
}
});
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
FirebaseMessaging.getInstance().setAutoInitEnabled(true);
}
}, 10000);
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
if (value.toString().equals("content-refresh") && !isShowingCartelRefresh) {
Log.e("OnCreate", "intent cartel");
//Vengo de notificacion, pero el app estaba cerrada, asi que muestro el cartel de content-refresh
// preference.setRefreshContentRequired(true);
saveNotificationCenterNew("Nuevo contenido para descargar", "", "Tienes nuevas capsulas de aprendizaje para descargar");
CartelRefreshContent();
}
}
}
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequest mWorkRequestSync =
new PeriodicWorkRequest.Builder(WorkSync.class, 15, TimeUnit.MINUTES)
// Constraints
.build();
mWorkManager = WorkManager.getInstance(getApplication());
mWorkManager.enqueueUniquePeriodicWork(
WorkSync.class.getSimpleName(),
ExistingPeriodicWorkPolicy.KEEP, //Existing Periodic Work policy
mWorkRequestSync //work request
);
}
public void hideKeyboard(View view) {
try {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setNoCapsuleActive()
{
ConfigDao configuracion = mAppDatabase.getConfigDao();
if(configuracion.selectConfigByTitle("act_capsule") == null)
{
Log.e("La capsula activa"," es nula");
com.cesams.twogetskills.inconcert.entity.Config confignueva;
confignueva = new com.cesams.twogetskills.inconcert.entity.Config();
confignueva.setTitle("act_capsule");
confignueva.setNow("");
configuracion.insert(confignueva);
}
else{
Log.e("La capsula activa"," es vacia o tiene algo");
com.cesams.twogetskills.inconcert.entity.Config confignueva;
confignueva = new com.cesams.twogetskills.inconcert.entity.Config();
confignueva.setTitle("act_capsule");
confignueva.setNow("");
configuracion.update(confignueva);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
unregisterReceiver(mConnectivityReceiver);
unregisterReceiver(mInternalReceiver);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.action_notifications) {// Toast.makeText(this, "Abriendo centro de notificaciones", Toast.LENGTH_LONG).show();
invokeFragment(Constants.IDX_FRAGMENT_NOTIFICATION);
//Ocultamos el teclado para mejor experiencia
hideKeyboard(mNavigationView);
}
return true;
}
@Override
protected void onResume() {
super.onResume();
isForeground = true;
preference = Preference.getInstance(getApplicationContext());
preference.load();
if(preference.isRefreshContentRequired() && !isShowingCartelRefresh)
{
//Hubo una notificacion de actualización de contenido antes
// pero no se pudo procesar por alguna interrupcion
CartelRefreshContent();
}
if(TextUtils.isEmpty(preference.getUserUuid())) {
//Verifico que el teclado no vaya a relanzar el fragment de intro al usar Google Smartlock para traer
//Informacion de inicio de sesion almacenada en su cuenta personal de Google
if (!isSmartLockLoginRun) {
invokeFragment(Constants.IDX_FRAGMENT_INTRO);
isSmartLockLoginRun= true;
}
} else {
if(preference.getFragmentIdxActive() == 0) {
invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES); //La pantalla principal ahora es Welcome, no Topic
} else {
switch (preference.getFragmentIdxActive())
{
case Constants.IDX_FRAGMENT_GALLERY:
case Constants.IDX_FRAGMENT_SLIDES:
if (preference.getCapsuleUuidActive().isEmpty() || preference.getSlideUuidActive().isEmpty()) {
invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
} else {
invokeFragment(preference.getFragmentIdxActive());
}
break;
case Constants.IDX_FRAGMENT_CAPSULES:
if (preference.getTopicUuidActive().isEmpty()) {
invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
} else {
invokeFragment(preference.getFragmentIdxActive());
}
break;
default :
invokeFragment(preference.getFragmentIdxActive());
break;
}
}
}
}
@Override
protected void onPause() {
super.onPause();
isForeground = false;
preference.save();
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
@Override
public void showFcmNotification(String title, String body, int new_capsules, String url, Boolean content_refresh)
{
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this, Constants.NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notificacion) //set icon for notification
.setContentTitle(title) //set title of notification
.setContentText(body)//this is notification message
.setAutoCancel(true) // makes auto cancel of notification
.setPriority(NotificationCompat.PRIORITY_DEFAULT); //set priority of notification
if(new_capsules > 0) {
builder.setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE);
builder.setNumber(new_capsules);
}
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//notification message will get at NotificationView
notificationIntent.putExtra(title, body);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_IMMUTABLE);
builder.setContentIntent(pendingIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
if(new_capsules > 0) {
String message = new_capsules == 1
? "Hay 1 cápsula nueva disponible"
: "Hay " + new_capsules + " cápsulas disponible";
showMessageSnackBarWithClose(message);
saveNotificationCenterNew("Nueva cápsula","",message);
} else {
showMessageSnackBarWithClose(body);
saveNotificationCenterNew(title,"",body);
}
if(content_refresh)
{
if(!isShowingCartelRefresh)
{
Log.e("On fcm mensaje boolean","intent cartel");
CartelRefreshContent();
}
}
}
public void saveNotificationCenterNew(String title,String url,String message){
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATE_SERVICE);
String dateOn = simpleDateFormat.format(date);
NotificationCenterDao notificacionDao = mAppDatabase.getNotificationCenterDao();
com.cesams.twogetskills.inconcert.entity.NotificationCenter notificacionueva;
notificacionueva = new com.cesams.twogetskills.inconcert.entity.NotificationCenter();
notificacionueva.setTitle(title);
notificacionueva.setDate(dateOn);
notificacionueva.setUrl(url);
List<com.cesams.twogetskills.inconcert.entity.NotificationCenter> lista;
lista=notificacionDao.selectAllNotification();
boolean registrada=false;
notificacionueva.setDescription(message);
for(com.cesams.twogetskills.inconcert.entity.NotificationCenter notificacion: lista)
{
if(notificacion.getDescription().equals(notificacionueva.getDescription()))
{
registrada=true;
//Ya existia, como llego de nuevo la pongo en no vista nuevamente.
notificacionueva.setId(notificacion.getId());
notificacionueva.setViewed("No");
notificacionDao.update(notificacionueva);
Log.e("Ya existia","la notificacion: "+notificacion.getDescription());
}
}
if(!registrada)
{
notificacionDao.insert(notificacionueva);
Log.e("No existe", "agregando.. "+ notificacionueva.getDescription());
}
}
@Override
public void saveNotificationCenter(String title, String url, String mensaje) {
saveNotificationCenterNew(title,url,mensaje);
}
@Override
public void signout()
{
if(isConnectedInternet()) {
try {
Http http = new Http(getCacheDir());
OkHttpClient client = http.getHttpClient(false);
Request request = new Request.Builder()
.url(Configuration.URL_SIGNOUT)
//Que parametros se necesitan para un logout?
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
public void onResponse(Call call, Response response)
throws IOException {
Log.e("Respuesta logout",""+response);
}
public void onFailure(Call call, IOException e) {
Log.e(TAG, "Error : " + e.getMessage());
}
});
} catch (Exception e) {
showMessageSnackBar(e.getMessage());
}
}
mAppDatabase.getAnswerDao().removeAll();
mAppDatabase.getQuestionDao().removeAll();
mAppDatabase.getQuizDao().removeAll();
mAppDatabase.getSlideDao().removeAll();
mAppDatabase.getCapsuleDao().removeAll();
mAppDatabase.getTopicDao().removeAll();
mAppDatabase.getUserExtendedDao().removeAll();
mAppDatabase.getUserLogDao().removeAll();
mAppDatabase.getProgressDao().removeAll();
mAppDatabase.getNotificationCenterDao().removeAllnotifications();
preference.setUserUuid("");
preference.setFirstName("");
preference.setLastName("");
preference.setEmail("");
preference.setImage("");
preference.setMaxDateChanges("");
preference.setCompanyCount(0);
preference.setCompanyUuidActive("");
preference.setTopicUuidActive("");
// preference.setCapsuleUuidActive("");
setNoCapsuleActive();
preference.setSlideUuidActive("");
preference.save();
hideNavigationAndtoolbar();
invokeFragment(Constants.IDX_FRAGMENT_INTRO);
}
@Override
public void onBackPressed() {
// Log.e("Origen navegacion",""+preference.getOrigenNavigation());
//super.onBackPressed();
switch (preference.getFragmentIdxActive())
{
/*
case Constants.IDX_FRAGMENT_INTRO :
case Constants.IDX_FRAGMENT_SIGNIN :
case Constants.IDX_FRAGMENT_TOPICS :
finish();
return;*/
case Constants.IDX_FRAGMENT_PROGRESS :
if(preference.getCompanyCount() > 1) {
invokeFragment(Constants.IDX_FRAGMENT_COMPANIES);
} else {
invokeFragment(Constants.IDX_FRAGMENT_TOPICS);
}
break;
case Constants.IDX_FRAGMENT_COMPANIES:
case Constants.IDX_FRAGMENT_TIMELINE :
break;
case Constants.IDX_FRAGMENT_CAPSULES :
invokeFragment(Constants.IDX_FRAGMENT_TOPICS);
break;
case Constants.IDX_FRAGMENT_FINISH_TOPIC :
preference.setTopicUuidActive("");
preference.setCapsuleUuidActive("");
preference.setSlideUuidActive("");
preference.save();
if(preference.getSourceNavigation() == Constants.SOURCE_NAVIGATION_MY_CAPSULES) {
setTitleActionBar(getString(R.string.app_name));
invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
} else {
invokeFragment(Constants.IDX_FRAGMENT_TOPICS);
}
return;
case Constants.IDX_FRAGMENT_SLIDES :
preference.setSlideUuidActive("");
preference.save();
if(preference.getSourceNavigation() == Constants.SOURCE_NAVIGATION_MY_CAPSULES) {
setTitleActionBar(getString(R.string.app_name));
invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
} else {
invokeFragment(Constants.IDX_FRAGMENT_CAPSULES);
}
return;
case Constants.IDX_FRAGMENT_FINISH_CAPSULE :
preference.setSlideUuidActive("");
preference.save();
if(preference.getSourceNavigation() == Constants.SOURCE_NAVIGATION_MY_CAPSULES) {
setTitleActionBar(getString(R.string.app_name));
invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
} else {
invokeFragment(Constants.IDX_FRAGMENT_CAPSULES);
}
return;
case Constants.IDX_FRAGMENT_GALLERY :
invokeFragment(Constants.IDX_FRAGMENT_SLIDES);
}
}
@Override
public void hideProgressBar() {
//accessing it from ui-thread
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.INVISIBLE);
}
});
}
@Override
public void showProgressBar() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.VISIBLE);
}
});
}
@Override
public void hideNavigationAndtoolbar() {
// Log.e("Ocultar"," Navigation and Toolbar");
//accessing it from ui-thread
runOnUiThread(() -> {
mNavigationView.setVisibility(View.GONE);
mToolbar.setVisibility(View.GONE);
});
}
@Override
public void showNavigationAndToolbar() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mNavigationView.setVisibility(View.VISIBLE);
mToolbar.setVisibility(View.VISIBLE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}
/**
* Create a new dummy account for the sync adapter
*
* @param context The application context
*/
public static Account CreateSyncAccount(Context context) {
// Create the account type and default account
Account newAccount = new Account(
Constants.ACCOUNT, Constants.ACCOUNT_TYPE);
// Get an instance of the Android account manager
AccountManager accountManager =
(AccountManager) context.getSystemService(
ACCOUNT_SERVICE);
/*
* Add the account and account type, no password or user data
* If successful, return the Account object, otherwise report an error.
*/
if (accountManager.addAccountExplicitly(newAccount, null, null)) {
/*
* If you don't set android:syncable="true" in
* in your <provider> element in the manifest,
* then call context.setIsSyncable(account, AUTHORITY, 1)
* here.
*/
ContentResolver.setIsSyncable(newAccount, Constants.AUTHORITY, 1);
ContentResolver.setSyncAutomatically(newAccount, Constants.AUTHORITY, true);
ContentResolver.addPeriodicSync(newAccount,
Constants.AUTHORITY, Bundle.EMPTY, Constants.SYNC_INTERVAL);
} else {
/*
* The account exists or some other error occurred. Log this, report it,
* or handle it internally.
*/
Account[] accounts = accountManager.getAccounts();
if(accounts != null && accounts.length > 0) {
for(Account account : accounts)
{
if(account.type.equals(Constants.ACCOUNT_TYPE)) {
return account;
}
}
return accounts[0];
} else {
return null;
}
}
return newAccount;
}
@Override
public void createSyncRecordNewToken(String token)
{
Log.e("BUG Token", "Preference - Set Token");
//Prueba de regeneracion de token
preference.setDeviceToken (token);
preference.save();
if(!TextUtils.isEmpty(preference.getDeviceUuid())) {
Log.e("BUG Token", "SyncRecord - FCM1");
Sync sync = new Sync(Constants.SYNC_ADAPTER_TYPE_FCM, token);
mAppDatabase.getSyncDao().insert(sync);
}
}
@Override
public void executeFcmCommand(String command)
{
if (command.equals("signout")) {
signout();
}
if (command.equals("content-refresh")){
if (!isShowingCartelRefresh) {
Log.e("On Execute fcm command","intent cartel");
saveNotificationCenterNew("Nuevo contenido para descargar", "", "Tienes nuevas capsulas de aprendizaje para descargar");
// preference.setRefreshContentRequired(true);
CartelRefreshContent();
}
}
}
public void CartelRefreshContent (){
isShowingCartelRefresh= true;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setMessage("Tienes nuevas capsulas para descargar");
alertDialogBuilder.setPositiveButton("Si, actualizar ahora",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
preference.setRefreshContentRequired(true);
if(preference.getFragmentIdxActive() == Constants.IDX_FRAGMENT_TOPICS) {
String fragmentKeyActual = PREFIX_FRAG + preference.getFragmentIdxActive();
if (fragmentHashMap.containsKey(fragmentKeyActual)) {
Fragment fragment = fragmentHashMap.get(fragmentKeyActual);
if (fragment instanceof IReloadData) {
((IReloadData) fragment).onReloadData();
}
}
}
else{
invokeFragment(Constants.IDX_FRAGMENT_TOPICS);
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.teal_700, null));
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
isShowingCartelRefresh=false;
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Object value = intent.getExtras().get(key);
if (value.toString().equals("content-refresh")) {
if (!isShowingCartelRefresh) {
// preference.setRefreshContentRequired(true);
saveNotificationCenterNew("Nuevo contenido para descargar", "", "Tienes nuevas capsulas de aprendizaje para descargar");
Log.e("On new","intent cartel");
CartelRefreshContent();
}
}
}
}
}
@Override
public void setConnectedInternet(Boolean isConnected)
{
connected = isConnected;
textViewMessageNotConnection.setVisibility(isConnected ? View.INVISIBLE : View.VISIBLE);
}
@Override
public boolean isConnectedInternet() {
return connected;
}
@Override
public void showMessageSnackBar(String message) {
Snackbar.make(this.findViewById(R.id.fragment_container), message, Snackbar.LENGTH_LONG).show();
}
@Override
public void showMessageSnackBarWithClose(String message) {
final Snackbar snackBar = Snackbar.make(this.findViewById(R.id.fragment_container), message, Snackbar.LENGTH_INDEFINITE);
snackBar.setAction(R.string.snackbar_close, new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call your action method here
snackBar.dismiss();
}
});
snackBar.show();
}
@Override
public void onErrorFatal() {
invokeFragment(Constants.IDX_FRAGMENT_SIGNIN);
}
@Override
public void invokeFragment(int fragmentIdxActiveNuevo)
{
// Log.e("Invoco fragmento"," "+fragmentIdxActiveNuevo);
String fragmentKeyActual = PREFIX_FRAG + preference.getFragmentIdxActive();
String fragmentKeyNuevo = PREFIX_FRAG + fragmentIdxActiveNuevo;
preference.setFragmentIdxActive(fragmentIdxActiveNuevo);
preference.save();
Fragment fragment;
if(!fragmentKeyActual.equalsIgnoreCase(fragmentKeyNuevo)) {
if(fragmentHashMap.containsKey(fragmentKeyActual)) {
fragment = fragmentHashMap.get(fragmentKeyActual);
if(fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.hide(fragment);
fragmentTransaction.commitAllowingStateLoss();
}
}
}
boolean add = false;
fragment = null;
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
// mDrawerToggle.setDrawerIndicatorEnabled(false);
switch(fragmentIdxActiveNuevo) {
case Constants.IDX_FRAGMENT_SIGNIN :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().hide();
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new SigninFragment();
}
break;
case Constants.IDX_FRAGMENT_BIOMETRIC:
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().hide();
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new BiometricOptions();
}
break;
case Constants.IDX_FRAGMENT_TOPICS :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new TopicFragment();
}
break;
case Constants.IDX_FRAGMENT_NOTIFICATION:
// badgenotification.setVisibility(View.VISIBLE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new NotificationCenter();
}
break;
case Constants.IDX_FRAGMENT_CAPSULES :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new CapsuleFragment();
}
break;
case Constants.IDX_FRAGMENT_SLIDES :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new SlideFragment();
}
break;
case Constants.IDX_FRAGMENT_GALLERY :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new GalleryFragment();
}
break;
case Constants.IDX_FRAGMENT_FINISH_CAPSULE :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().hide();
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new FinishCapsuleFragment();
}
break;
case Constants.IDX_FRAGMENT_FINISH_TOPIC :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().hide();
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new FinishTopicFragment();
}
break;
case Constants.IDX_FRAGMENT_TIMELINE :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new TimelineFragment();
}
break;
case Constants.IDX_FRAGMENT_COMPANIES:
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new CompanyFragment();
}
break;
case Constants.IDX_FRAGMENT_PROGRESS :
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new ProgressFragment();
}
break;
case Constants.IDX_FRAGMENT_USER_PROFILE:
// badgenotification.setVisibility(View.GONE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new UserProfileFragment();
}
break;
case Constants.IDX_FRAGMENT_MY_CAPSULES:
// badgenotification.setVisibility(View.VISIBLE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new MyCapsulesFragment();
// Log.e("Vete"," a welcome");
}
break;
case Constants.IDX_FRAGMENT_DETAIL_CAPSULE:
// badgenotification.setVisibility(View.VISIBLE);
// getSupportActionBar().show();
// mDrawerToggle.setDrawerIndicatorEnabled(true);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new CapsuleDetail();
// Log.e("Vete"," a welcome");
}
break;
default :
// getSupportActionBar().hide();
// badgenotification.setVisibility(View.GONE);
if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
fragment = fragmentHashMap.get(fragmentKeyNuevo);
} else {
add = true;
fragment = new IntroFragment();
}
break;
}
if(add) {
fragmentHashMap.put(fragmentKeyNuevo, fragment);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment, fragmentKeyNuevo);
fragmentTransaction.commitAllowingStateLoss();
}
if(fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.show(fragment);
fragmentTransaction.commitAllowingStateLoss();
}
}
@Override
public void changeTopicActive(String topicUuid)
{
preference.setSourceNavigation(Constants.SOURCE_NAVIGATION_TOPICS);
preference.setTopicUuidActive(topicUuid);
preference.setCapsuleUuidActive("");
preference.setSlideUuidActive("");
preference.save();
invokeFragment(Constants.IDX_FRAGMENT_CAPSULES);
}
@Override
public void changeCapsuleActiveSourceNavigationMyCapsules(String topicUuid, String capsuleUuid)
{
preference.setTopicUuidActive(topicUuid);
preference.setCapsuleUuidActive(capsuleUuid);
preference.setSlideUuidActive("");
preference.setSourceNavigation(Constants.SOURCE_NAVIGATION_MY_CAPSULES);
preference.save();
invokeFragment(Constants.IDX_FRAGMENT_SLIDES);
ProgressDao progressDao = getDatabase().getProgressDao();
Progress progress = progressDao.selectByCapsuleUuid(capsuleUuid);
if (progress != null && progress.getCompleted() == 1) {
invokeFragment(Constants.IDX_FRAGMENT_DETAIL_CAPSULE);
} else {
invokeFragment(Constants.IDX_FRAGMENT_SLIDES);
}
}
@Override
public void changeCapsuleActiveSourceNavigationTopics(String capsuleUuid)
{
Log.d(TAG, "changeCapsuleActiveSourceNavigationTopics capsuleUuid : " + capsuleUuid );
preference.setCapsuleUuidActive(capsuleUuid);
preference.setSlideUuidActive("");
preference.setSourceNavigation(Constants.SOURCE_NAVIGATION_TOPICS);
preference.save();
ProgressDao progressDao = getDatabase().getProgressDao();
Progress progress = progressDao.selectByCapsuleUuid(capsuleUuid);
if (progress != null && progress.getCompleted() == 1) {
invokeFragment(Constants.IDX_FRAGMENT_DETAIL_CAPSULE);
} else {
invokeFragment(Constants.IDX_FRAGMENT_SLIDES);
}
}
@Override
public void changeCapsuleActive(String capsuleUuid)
{
Log.d(TAG, "changeCapsuleActive capsuleUuid : " + capsuleUuid );
preference.setCapsuleUuidActive(capsuleUuid);
preference.setSlideUuidActive("");
preference.save();
ProgressDao progressDao = getDatabase().getProgressDao();
Progress progress = progressDao.selectByCapsuleUuid(capsuleUuid);
if (progress != null && progress.getCompleted() == 1) {
invokeFragment(Constants.IDX_FRAGMENT_DETAIL_CAPSULE);
} else {
invokeFragment(Constants.IDX_FRAGMENT_SLIDES);
}
}
/*
public void setCapsulaActiva(String capsuleUuid) {
ConfigDao configuracion = mAppDatabase.getConfigDao();
Config actual = configuracion.selectConfigByTitle("act_capsule");
com.cesams.twogetskills.entity.Config confignueva;
confignueva = new com.cesams.twogetskills.entity.Config();
confignueva.setId(actual.getId());
confignueva.setTitle("act_capsule");
confignueva.setNow(capsuleUuid);
configuracion.update(confignueva);
}
*/
@Override
public void changeCompanyActive(String companyUuid)
{
Log.d(TAG, "changeCompanyActive : " + companyUuid);
preference.setCompanyUuidActive(companyUuid);
preference.save();
}
@Override
public synchronized void changeSlideActive(String slideUuid, String type, boolean showGallery)
{
Log.d(TAG, "changeSlideActive : " + slideUuid);
if(!preference.getSlideUuidActive().equals(slideUuid) && type.equals(Constants.SLIDE_TYPE_IMAGE)) {
Log.d(TAG, "registerOnPageChangeCallback - createProgressAndSyncRecord");
createProgressAndSyncRecord(slideUuid, true, false, false);
}
preference.setSlideUuidActive(slideUuid);
preference.save();
if(showGallery) {
invokeFragment(Constants.IDX_FRAGMENT_GALLERY);
}
}
@Override
public int getSourceNavigation() {
return preference.getSourceNavigation();
}
@Override
public
void setSourceNavigation(int sourceNavigation) {
preference.setSourceNavigation(sourceNavigation);
}
@Override
public void setTopicUuidActive(String topicUuid) {
preference.setTopicUuidActive(topicUuid);
}
@Override
public void setCapsuleUuidActive(String capsuleUuid) {
preference.setCapsuleUuidActive(capsuleUuid);
}
@Override
public void setSlideUuidActive(String slideUuid) {
preference.setSlideUuidActive(slideUuid);
}
@Override
public String getTopicUuidActive()
{
return preference.getTopicUuidActive();
}
@Override
public String getCapsuleUuidActive()
{
return preference.getCapsuleUuidActive();
}
@Override
public String getSlideUuidActive()
{
return preference.getSlideUuidActive();
}
@Override
public String getCompanyUuidActive()
{
return preference.getCompanyUuidActive();
}
@Override
public void setTitleActionBar(String title)
{
getSupportActionBar().setTitle(title);
}
@Override
public synchronized void createProgressAndSyncRecord(String slideUuid, boolean completed, boolean isQuiz, boolean isAudioOrVideo)
{
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
String dateOn = simpleDateFormat.format(date);
SlideDao slideDao = mAppDatabase.getSlideDao();
Slide slide = slideDao.selectByUuid(slideUuid);
CapsuleDao capsuleDao = mAppDatabase.getCapsuleDao();
Capsule capsule = capsuleDao.selectByUuid(slide.getCapsuleUuid());
TopicDao topicDao = mAppDatabase.getTopicDao();
Topic topic = topicDao.selectByUuid(capsule.getTopicUuid());
UserLog userLog;
UserLogDao userLogDao = mAppDatabase.getUserLogDao();
SyncDao syncDao = mAppDatabase.getSyncDao();
Sync sync;
String userUuid = preference.getUserUuid();
// Progreso y UserLog del Slide
ProgressDao progressDao = mAppDatabase.getProgressDao();
Progress progressSlide = progressDao.selectBySlideUuidAndUserUuid(slide.getUuid(), userUuid);
Progress progressCapsule = progressDao.selectByCapsuleUuidAndUserUuid(slide.getCapsuleUuid(),userUuid);
Progress progressTopic = progressDao.selectByTopicUuidAndUserUuid(slide.getTopicUuid(),userUuid);
int totalSlides = 0;
int viewSlides = 0;
double percentaje = 0;
boolean newRecord = false;
boolean retakeQuiz = false;
/*** INICIO PROCESO DIAPOSITIVA ***/
if(progressSlide == null) {
retakeQuiz = false;
progressSlide = new Progress();
progressSlide.setUserUuid(preference.getUserUuid());
progressSlide.setCompanyUuid(topic.getCompanyUuid());
progressSlide.setTopicUuid(topic.getUuid());
progressSlide.setCapsuleUuid(capsule.getUuid());
progressSlide.setSlideUuid(slide.getUuid());
progressSlide.setType(Constants.PROGERSS_TYPE_SLIDE);
progressSlide.setCompleted(completed ? 1 : 0);
progressSlide.setAddedOn(dateOn);
progressSlide.setUpdatedOn(dateOn);
progressDao.insert(progressSlide);
} else {
retakeQuiz = true;
if (progressSlide.getCompleted() == 1) {
progressSlide.setReturningAfterCompleted(progressSlide.getReturningAfterCompleted() + 1);
} else {
if (completed) {
//0 y 1 para Boolean
progressSlide.setCompleted(completed ? 1 : 0);
} else {
progressSlide.setReturning(progressSlide.getReturning() + 1);
}
}
progressSlide.setUpdatedOn(dateOn);
progressDao.update(progressSlide);
}
/*** FIN PROCESO DIAPOSITIVA ***/
/*** INICIO PROCESO TOPICO ***/
ResultCount resultCount;
resultCount = slideDao.getCountByTopicUuid(slide.getTopicUuid());
totalSlides = resultCount.getCount();
resultCount = progressDao.getCountSlidesCompletedByTopicUuidAndUserUuid(slide.getTopicUuid(), userUuid);
viewSlides = resultCount.getCount();
if(totalSlides == 0) {
percentaje = 0;
} else {
percentaje = (viewSlides * 100) / totalSlides;
}
if(progressTopic == null) {
newRecord = true;
progressTopic = new Progress();
progressTopic.setUserUuid(preference.getUserUuid());
progressTopic.setCompanyUuid( topic.getCompanyUuid());
progressTopic.setTopicUuid (topic.getUuid());
progressTopic.setViewSlides(viewSlides);
progressTopic.setTotalSlides(totalSlides);
progressTopic.setProgress (percentaje);
progressTopic.setType(Constants.PROGERSS_TYPE_TOPIC);
progressTopic.setAddedOn(dateOn);
progressTopic.setUpdatedOn(dateOn);
progressDao.insert(progressTopic);
} else {
newRecord = false;
progressTopic.setViewSlides(viewSlides);
progressTopic.setTotalSlides(totalSlides);
progressTopic.setProgress (percentaje);
progressDao.update(progressTopic);
}
try {
JSONObject json = progressTopic.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
if(newRecord) {
userLog = new UserLog();
userLog.setUserUuid(preference.getUserUuid());
userLog.setActivity(Constants.USER_LOG_ACTIVITY_START_TOPIC);
userLog.setCompanyUuid (topic.getCompanyUuid());
userLog.setTopicUuid (topic.getUuid());
userLog.setAddedOn(dateOn);
userLogDao.insert(userLog);
try {
JSONObject json = userLog.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
}
/*** FIN PROCESO TOPICO ***/
/*** INICIO PROCESO CAPSULA ***/
resultCount = slideDao.getCountByCapsuleUuid(slide.getCapsuleUuid());
totalSlides = resultCount.getCount();
resultCount = progressDao.getCountSlidesCompletedByCapsuleUuidAndUserUuid(slide.getCapsuleUuid(), userUuid);
viewSlides = resultCount.getCount();
if(totalSlides == 0) {
percentaje = 0;
} else {
percentaje = (viewSlides * 100) / totalSlides;
}
if(progressCapsule == null) {
newRecord = true;
progressCapsule = new Progress();
progressCapsule.setUserUuid(preference.getUserUuid());
progressCapsule.setCompanyUuid(topic.getCompanyUuid());
progressCapsule.setTopicUuid(topic.getUuid());
progressCapsule.setCapsuleUuid (capsule.getUuid());
progressCapsule.setViewSlides(viewSlides);
progressCapsule.setTotalSlides(totalSlides);
progressCapsule.setProgress(percentaje);
progressCapsule.setType(Constants.PROGERSS_TYPE_CAPSULE);
progressCapsule.setAddedOn(dateOn);
progressCapsule.setUpdatedOn(dateOn);
progressDao.insert(progressCapsule);
Log.d(TAG, "Progress Capsule Nueva : setLastCapsuleActive : " + preference.getLastCapsuleUuidActive());
preference.setLastCapsuleUuidActive(capsule.getUuid());
} else {
newRecord = false;
progressCapsule.setViewSlides(viewSlides);
progressCapsule.setTotalSlides(totalSlides);
progressCapsule.setProgress(percentaje);
progressCapsule.setUpdatedOn(dateOn);
Log.d(TAG, "Progress Capsule : getLastCapsuleActive : " + preference.getLastCapsuleUuidActive());
// Log.d(TAG, "Progress Capsule : CapsuleActive : " + preference.getCapsuleUuidActive());
if (!preference.getLastCapsuleUuidActive().equals(getCapsuleUuidActive())) {
// Log.d(TAG, "Capsule Progress : " + progressCapsule.getProgress());
// Log.d(TAG, "Capsule Completed : " + progressCapsule.getCompleted());
// Log.d(TAG, "Capsule Total slides : " + progressCapsule.getTotalSlides());
// Log.d(TAG, "Capsule View slides : " + progressCapsule.getViewSlides());
if(progressCapsule.getCompleted() == 1) {
Log.d(TAG, "Capsule OLD returningAfterCompleted = " + progressCapsule.getReturningAfterCompleted());
int returningAfterCompleted = progressCapsule.getReturningAfterCompleted() + 1;
Log.d(TAG, "Capsule NEW returningAfterCompleted = " + returningAfterCompleted);
progressCapsule.setReturningAfterCompleted(returningAfterCompleted);
}
preference.setLastCapsuleUuidActive(capsule.getUuid());
preference.save();
Log.d(TAG, "Progress Capsule : setLastCapsuleActive : " + preference.getLastCapsuleUuidActive());
}
progressDao.update(progressCapsule);
}
try {
JSONObject json = progressCapsule.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
if(newRecord) {
userLog = new UserLog();
userLog.setUserUuid(preference.getUserUuid());
userLog.setActivity(Constants.USER_LOG_ACTIVITY_START_CAPSULE);
userLog.setCompanyUuid(topic.getCompanyUuid());
userLog.setTopicUuid(topic.getUuid());
userLog.setCapsuleUuid(capsule.getUuid());
userLog.setAddedOn(dateOn);
userLogDao.insert(userLog);
try {
JSONObject json = userLog.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
}
/*** FIN PROCESO CAPSULA ***/
/*** INICIO USERLOG SLIDE ***/
userLog = new UserLog();
userLog.setUserUuid(preference.getUserUuid());
userLog.setActivity(Constants.USER_LOG_ACTIVITY_VIEW_SLIDE);
userLog.setCompanyUuid(topic.getCompanyUuid());
userLog.setTopicUuid(topic.getUuid());
userLog.setCapsuleUuid(capsule.getUuid());
userLog.setSlideUuid(slide.getUuid());
userLog.setAddedOn(dateOn);
userLogDao.insert(userLog);
try {
JSONObject json = userLog.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
/*** FIN ***/
/*** PROGRESS SLIDE***/
try {
JSONObject json = progressSlide.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
/** IS SLIDE = QUIZ **/
if (isQuiz) {
userLog = new UserLog();
userLog.setUserUuid(preference.getUserUuid());
userLog.setActivity(retakeQuiz ? Constants.USER_LOG_ACTIVITY_RETAKE_A_TEST : Constants.USER_LOG_ACTIVITY_TAKE_A_TEST);
userLog.setCompanyUuid (topic.getCompanyUuid());
userLog.setTopicUuid(slide.getTopicUuid());
userLog.setCapsuleUuid(slide.getCapsuleUuid());
userLog.setSlideUuid(slide.getUuid());
userLog.setAddedOn(dateOn);
userLogDao.insert(userLog);
try {
JSONObject json = userLog.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
if(progressSlide.getCompleted() == 1) {
userLog = new UserLog();
userLog.setUserUuid(preference.getUserUuid());
userLog.setActivity(Constants.USER_LOG_ACTIVITY_APPROVED_TEST);
userLog.setCompanyUuid(topic.getCompanyUuid());
userLog.setTopicUuid(slide.getTopicUuid());
userLog.setCapsuleUuid(slide.getCapsuleUuid());
userLog.setSlideUuid(slide.getUuid());
userLog.setAddedOn(dateOn);
userLogDao.insert(userLog);
try {
JSONObject json = userLog.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
} else {
if(progressSlide.getCompleted() == 1) {
userLog = new UserLog();
userLog.setUserUuid(preference.getUserUuid());
userLog.setActivity( Constants.USER_LOG_ACTIVITY_VIEW_SLIDE);
userLog.setCompanyUuid(topic.getCompanyUuid());
userLog.setTopicUuid(slide.getTopicUuid());
userLog.setCapsuleUuid(slide.getCapsuleUuid());
userLog.setSlideUuid(slide.getUuid());
userLog.setAddedOn(dateOn);
userLogDao.insert(userLog);
try {
JSONObject json = userLog.toJson();
json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
syncDao.insert(sync);
} catch (JSONException e) {
}
}
}
}
requestExecuteSyncAdapter();
}
@Override
public void launchVideoViewer(String videoAudioUrl)
{
Intent intent = new Intent(getApplicationContext(), VideoAudioActivity.class);
intent.putExtra("videoAudioUrl",videoAudioUrl);
intent.putExtra("deviceId", preference.getDeviceUuid());
intent.putExtra("password", preference.getPassword());
//startActivityForResult(intent, Constants.REQUEST_CODE_VIDEO);
mLauncher.launch(intent);
}
@Override
public void launchAudioViewer(String videoAudioUrl)
{
Intent intent = new Intent(getApplicationContext(), VideoAudioActivity.class);
intent.putExtra("videoAudioUrl",videoAudioUrl);
intent.putExtra("deviceId", preference.getDeviceUuid());
intent.putExtra("password", preference.getPassword());
//startActivityForResult(intent, Constants.REQUEST_CODE_AUDIO);
mLauncher.launch(intent);
}
@Override
public void launchTextViewer(String description)
{
Intent intent = new Intent(getApplicationContext(),TextActivity.class);
intent.putExtra("description",description);
//startActivityForResult(intent, Constants.REQUEST_CODE_TEXT);
mLauncher.launch(intent);
}
@Override
public void launchDocumentViewer(String file)
{
Intent intent = new Intent(getApplicationContext(),PdfActivity.class);
intent.putExtra("documentUrl",file);
intent.putExtra("deviceId", preference.getDeviceUuid());
intent.putExtra("password", preference.getPassword());
//startActivityForResult(intent, Constants.REQUEST_CODE_PDF);
mLauncher.launch(intent);
}
@Override
public void launchQuizViewer(String quizUuid)
{
ProgressDao progressDao = mAppDatabase.getProgressDao();
Progress progress = progressDao.selectBySlideUuidAndUserUuid(preference.getSlideUuidActive(), preference.getUserUuid());
boolean launch = false;
if(progress != null && progress.getCompleted() == 0) {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
try {
Long updateOn = simpleDateFormat.parse(progress.getUpdatedOn()).getTime();
//30 minutes
long mintime = 30 * 60 * 1000;
if(date.getTime() - updateOn > mintime ) {
launch = true;
}
launch = true;
} catch(Exception e) {
}
} else {
launch = true;
}
if(launch) {
QuizDao quizDao = mAppDatabase.getQuizDao();
Quiz quiz = quizDao.selectByUuid(quizUuid);
TopicDao topicDao = mAppDatabase.getTopicDao();
Topic topic = topicDao.selectByUuid(preference.getTopicUuidActive());
CapsuleDao capsuleDao = mAppDatabase.getCapsuleDao();
Capsule capsule = capsuleDao.selectByUuid(getCapsuleUuidActive());
SlideDao slideDao = mAppDatabase.getSlideDao();
Slide slide = slideDao.selectByUuid(preference.getSlideUuidActive());
QuestionDao questionDao = mAppDatabase.getQuestionDao();
List<Question> questions = questionDao.selectAllByQuizUuid(quiz.getUuid());
List<Answer> answers;
AnswerDao answerDao = mAppDatabase.getAnswerDao();
Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
// intent.putExtra("companyUuid", topic.getCompanyUuid());
// intent.putExtra("topicUuid", topic.getUuid());
//intent.putExtra("capsuleUuid", capsule.getUuid());
//intent.putExtra("slideUuid", slide.getUuid());
//intent.putExtra("userUuid", preference.getUserUuid());
//intent.putExtra("quizUuid", quizUuid);
intent.putExtra("quiz_uuid", quiz.getUuid());
intent.putExtra("quiz_company_uuid", quiz.getCompanyUuid());
intent.putExtra("quiz_name", quiz.getName());
intent.putExtra("quiz_points", quiz.getPoints());
intent.putExtra("quiz_max_time", quiz.getMaxTime());
intent.putExtra("quiz_minimum_points_required", quiz.getMinimumPointsRequired());
intent.putExtra("quiz_failed", quiz.getFailed());
intent.putExtra("quiz_text", quiz.getText());
intent.putExtra("questions", questions.size());
int i = 1;
int j = 1;
for(Question question : questions) {
intent.putExtra("question" + i + "_uuid", question.getUuid());
intent.putExtra("question" + i + "_text", question.getText());
intent.putExtra("question" + i + "_max_length", question.getMaxlength());
intent.putExtra("question" + i + "_position", question.getPosition());
intent.putExtra("question" + i + "_points", question.getPoints());
intent.putExtra("question" + i + "_type", question.getType());
answers = answerDao.selectAllByQuestionUuid(question.getUuid());
intent.putExtra("question" + i + "_answers", answers.size());
j = 1;
for(Answer answer : answers) {
intent.putExtra("question" + i + "_answer_uuid" + j, answer.getUuid());
intent.putExtra("question" + i + "_answer_text" + j, answer.getText());
intent.putExtra("question" + i + "_answer_points" + j, answer.getPoints());
intent.putExtra("question" + i + "_answer_correct" + j, answer.getCorrect());
j++;
}
i++;
}
//startActivityForResult(intent, Constants.REQUEST_CODE_QUIZ);
mLauncher.launch(intent);
} else {
showMessageSnackBar(getString(R.string.error_retry_quiz_min_time));
}
}
@Override
public Preference getPreference() {
return preference;
}
public void requestCheckChanges()
{
Log.d(TAG, "requestCheckChanges");
try {
CapsuleDao capsuleDao = mAppDatabase.getCapsuleDao();
List<Capsule> capsules = capsuleDao.selectAll();
if(capsules.size() > 0) {
//ArrayList<String> ids = new ArrayList<>();
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
TimeZone tz = calendar.getTimeZone();
int created = (int) (calendar.getTimeInMillis() / 1000);
Random random = new Random(created);
int rand = 1000 + random.nextInt(8999);
//Log.d("requestCheckChanges", "token = " + preference.getDeviceUuid());
//Log.d("requestCheckChanges", "created = " + created);
// Log.d("requestCheckChanges", "rand = " + rand);
//Log.d("requestCheckChanges", "calc = " + preference.password + ':' + created + ':' + rand);
String secret = MD5.generar(preference.getPassword() + ':' + created + ':' + rand);
//Log.d("requestCheckChanges", "secret = " + secret);
FormBody.Builder formBodyCheckChangeBuilder = new FormBody.Builder();
formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID));
formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_FIELD_VARIANT_ID, String.valueOf(Configuration.VARIANT_ID));
formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_FIELD_DEVICE_UUID, preference.getDeviceUuid());
formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_FIELD_MAX_DATE_CHANGES, preference.getMaxDateChanges());
formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_FIELD_IS_FOREGROUND, String.valueOf(isForeground ? 1 : 0));
Http http = new Http(this.getCacheDir(), preference.getDeviceUuid(), secret, created, rand);
OkHttpClient client = http.getHttpClient(false);
formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_FIELD_MAX_IDS, String.valueOf(capsules.size()));
//formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_MAX_IDS, String.valueOf(0));
int i = 1;
for(Capsule capsule : capsules)
{
Log.d("requestCheckChanges", "id" + i + " = " + capsule.getTopicUuid() + "|" + capsule.getUuid());
formBodyCheckChangeBuilder.add(Constants.POST_MICROLEARNING_CHECK_CHANGES_FIELD_ID + i, capsule.getTopicUuid() + "|" + capsule.getUuid());
i++;
}
RequestBody formBody = formBodyCheckChangeBuilder.build();
Log.d(TAG, "URL = " + Configuration.URL_CHECK_CHANGES);
Request request = new Request.Builder()
.url(Configuration.URL_CHECK_CHANGES)
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
public void onResponse(Call call, Response response)
throws IOException {
processResponseServerChanges(response.body().string());
}
public void onFailure(Call call, IOException e) {
Log.d(TAG, "Error : " + e.getMessage());
}
});
}
} catch(Exception e) {
}
}
public void syncFromServer(JSONObject data)
{
try {
JSONObject objUser = data.getJSONObject("user");
String userUuid = objUser.getString("uuid");
AnswerDao answerDao = mAppDatabase.getAnswerDao();
QuestionDao questionDao = mAppDatabase.getQuestionDao();
QuizDao quizDao = mAppDatabase.getQuizDao();
SlideDao slideDao = mAppDatabase.getSlideDao();
CapsuleDao capsuleDao = mAppDatabase.getCapsuleDao();
TopicDao topicDao = mAppDatabase.getTopicDao();
CompanyDao companyDao = mAppDatabase.getCompanyDao();
UserExtendedDao userExtendedDao = mAppDatabase.getUserExtendedDao();
ProgressDao progressDao = mAppDatabase.getProgressDao();
progressDao.removeAllUserUuidNotEqual(userUuid);
UserLogDao userLogDao = mAppDatabase.getUserLogDao();
userLogDao.removeAllUserUuidNotEqual(userUuid);
JSONArray arrayCapsules;
JSONArray arraySlides;
JSONArray arrayAnswers;
JSONArray arrayQuestions;
JSONArray arrayProgress;
JSONArray arrayQuizzes;
JSONArray arrayUserLog;
JSONObject objTopic;
JSONObject objCapsule;
JSONObject objSlide;
JSONObject objAnswer;
JSONObject objQuestion;
JSONObject objQuiz;
JSONObject objProgress;
JSONObject objUserLog;
int i,j,x;
if(data.has("progress")) {
arrayProgress = data.getJSONArray("progress");
for (i = 0; i < arrayProgress.length(); i++) {
objProgress = arrayProgress.getJSONObject(i);
Progress progress = null;
userUuid = objProgress.getString("user_uuid");
String type = objProgress.getString("type");
String topicUuid = objProgress.getString("topic_uuid");
String capsuleUuid = objProgress.getString("capsule_uuid");
String slideUuid = objProgress.getString("slide_uuid");
if(type == Constants.PROGERSS_TYPE_SLIDE) {
progress = progressDao.selectBySlideUuidAndUserUuid(slideUuid, userUuid);
} else if(type == Constants.PROGERSS_TYPE_CAPSULE) {
progress = progressDao.selectByCapsuleUuidAndUserUuid(capsuleUuid, userUuid);
} else if(type == Constants.PROGERSS_TYPE_TOPIC) {
progress = progressDao.selectByTopicUuidAndUserUuid(topicUuid, userUuid);
}
if(progress == null) {
progress = new Progress();
progress.setUserUuid(userUuid);
progress.setCompanyUuid(objProgress.getString("company_uuid"));
progress.setTopicUuid(topicUuid);
progress.setCapsuleUuid(capsuleUuid);
progress.setSlideUuid(slideUuid);
progress.setProgress(objProgress.getDouble("progress"));
progress.setTotalSlides(objProgress.getInt("total_slides"));
progress.setViewSlides(objProgress.getInt("view_slides"));
progress.setType(type);
progress.setReturning(objProgress.getInt("returning"));
progress.setReturningAfterCompleted(objProgress.getInt("returning_after_completed"));
progress.setCompleted(objProgress.getInt("completed"));
progress.setAddedOn(objProgress.getString("added_on"));
progress.setUpdatedOn(objProgress.getString("updated_on"));
progressDao.insert(progress);
}
}
}
if(data.has("userlog")) {
arrayUserLog = data.getJSONArray("userlog");
for (i = 0; i < arrayUserLog.length(); i++) {
objUserLog = arrayUserLog.getJSONObject(i);
userUuid = objUserLog.getString("user_uuid");
String activity = objUserLog.getString("activity");
String added_on = objUserLog.getString("added_on");
UserLog userLog = userLogDao.selectOneByUserUuidAndActivityAndAddedOn(userUuid, activity, added_on);
if(userLog == null) {
userLog = new UserLog();
userLog.setUserUuid(objUserLog.getString("user_uuid"));
userLog.setCompanyUuid(objUserLog.getString("company_uuid"));
userLog.setTopicUuid(objUserLog.getString("topic_uuid"));
userLog.setCapsuleUuid(objUserLog.getString("capsule_uuid"));
userLog.setSlideUuid(objUserLog.getString("slide_uuid"));
userLog.setActivity(objUserLog.getString("activity"));
userLog.setAddedOn(objUserLog.getString("added_on"));
userLogDao.insert(userLog);
}
}
}
String uuid;
Company company;
if(data.has("quizzes")) {
Quiz quiz;
Question question;
Answer answer;
arrayQuizzes = data.getJSONArray("quizzes");
for (i = 0; i < arrayQuizzes.length(); i++) {
objQuiz = arrayQuizzes.getJSONObject(i);
uuid = objQuiz.getString("company_uuid");
company = companyDao.selectByUuid(uuid);
if (company == null) {
company = new Company();
company.setUuid(objQuiz.getString("company_uuid"));
company.setName(objQuiz.getString("company_name"));
company.setImage(objQuiz.getString("company_image"));
companyDao.insert(company);
} else {
company.setName(objQuiz.getString("company_name"));
company.setImage(objQuiz.getString("company_image"));
companyDao.update(company);
}
uuid = objQuiz.getString("uuid");
quiz = quizDao.selectByUuid(uuid);
if(quiz == null) {
quiz = new Quiz();
quiz.setUuid( objQuiz.getString("uuid"));
quiz.setCompanyUuid( company.getUuid() );
quiz.setFailed( objQuiz.getString("failed"));
quiz.setName( objQuiz.getString("name"));
quiz.setText( objQuiz.getString("text"));
quiz.setPoints( objQuiz.getInt("points"));
quiz.setMinimumPointsRequired(objQuiz.getInt("minimum_points_required"));
quiz.setMaxTime( objQuiz.getInt("max_time"));
quizDao.insert(quiz);
} else {
quiz.setCompanyUuid( company.getUuid() );
quiz.setFailed( objQuiz.getString("failed"));
quiz.setName( objQuiz.getString("name"));
quiz.setText( objQuiz.getString("text"));
quiz.setPoints( objQuiz.getInt("points"));
quiz.setMinimumPointsRequired(objQuiz.getInt("minimum_points_required"));
quiz.setMaxTime( objQuiz.getInt("max_time"));
quizDao.update(quiz);
}
arrayQuestions = objQuiz.getJSONArray("questions");
for (j = 0; j < arrayQuestions.length(); j++) {
objQuestion = arrayQuestions.getJSONObject(j);
uuid = objQuestion.getString("uuid");
question = questionDao.selectByUuid(uuid);
if(question == null) {
question = new Question();
question.setQuizUuid( quiz.getUuid());
question.setUuid(uuid) ;
question.setText( objQuestion.getString("text"));
question.setType( objQuestion.getString("type"));
question.setPoints( objQuestion.getInt("points"));
question.setMaxlength( objQuestion.getInt("maxlength"));
questionDao.insert(question);
} else {
question.setQuizUuid( quiz.getUuid());
question.setText( objQuestion.getString("text"));
question.setType( objQuestion.getString("type"));
question.setPoints( objQuestion.getInt("points"));
question.setMaxlength( objQuestion.getInt("maxlength"));
questionDao.update(question);
}
arrayAnswers = objQuestion.getJSONArray("answers");
for (x = 0; x < arrayAnswers.length(); x++) {
objAnswer = arrayAnswers.getJSONObject(x);
uuid = objAnswer.getString("uuid");
answer = answerDao.selectByUuid(uuid);
if(answer == null) {
answer = new Answer();
answer.setQuestionUuid(question.getUuid());
answer.setUuid(uuid);
answer.setText(objAnswer.getString("text"));
answer.setPoints(objAnswer.getInt("points"));
answer.setCorrect(objAnswer.getString("correct"));
answerDao.insert(answer);
} else {
answer.setQuestionUuid(question.getUuid());
answer.setText(objAnswer.getString("text"));
answer.setPoints(objAnswer.getInt("points"));
answer.setCorrect(objAnswer.getString("correct"));
answerDao.update(answer);
}
}
}
}
}
if(data.has("topics")) {
Topic topic;
Capsule capsule;
Slide slide;
JSONArray arrayTopics = data.getJSONArray("topics");
for (i = 0; i < arrayTopics.length(); i++) {
objTopic = arrayTopics.getJSONObject(i);
uuid = objTopic.getString("company_uuid");
company = companyDao.selectByUuid(uuid);
if (company == null) {
company = new Company();
company.setUuid(objTopic.getString("company_uuid"));
company.setName(objTopic.getString("company_name"));
company.setImage(objTopic.getString("company_image"));
companyDao.insert(company);
} else {
company.setName(objTopic.getString("company_name"));
company.setImage(objTopic.getString("company_image"));
companyDao.update(company);
}
uuid = objTopic.getString("uuid");
topic = topicDao.selectByUuid(uuid);
if (topic == null) {
topic = new Topic();
topic.setUuid(uuid);
topic.setCompanyUuid(company.getUuid());
topic.setName(objTopic.getString("name"));
topic.setDescription(objTopic.getString("description"));
topic.setImage(objTopic.getString("image"));
topic.setPosition(objTopic.getInt("position"));
topicDao.insert(topic);
} else {
topic.setCompanyUuid(company.getUuid());
topic.setName(objTopic.getString("name"));
topic.setDescription(objTopic.getString("description"));
topic.setImage(objTopic.getString("image"));
topic.setPosition(objTopic.getInt("position"));
topicDao.update(topic);
}
arrayCapsules = objTopic.getJSONArray("capsules");
// Log.e("Objeto:",""+objTopic.getJSONArray("capsules"));
for (j = 0; j < arrayCapsules.length(); j++) {
objCapsule = arrayCapsules.getJSONObject(j);
Log.e("Capsula:",""+objCapsule.toString());
uuid = objCapsule.getString("uuid");
capsule = capsuleDao.selectByUuid(uuid);
if(capsule == null) {
capsule = new Capsule();
capsule.setTopicUuid(topic.getUuid());
capsule.setUuid(uuid);
capsule.setName(objCapsule.getString("name"));
capsule.setDescription(objCapsule.getString("description"));
capsule.setImage(objCapsule.getString("image"));
capsule.setPosition(objCapsule.getInt("position"));
capsule.setAddedOn(objCapsule.getString("added_on"));
capsule.setUpdatedOn(objCapsule.getString("updated_on"));
capsule.setLinkComments(objCapsule.getString("link_comments"));
capsule.setLinkCommentAdd(objCapsule.getString("link_comment_add"));
capsule.setTotalComments(objCapsule.getInt("total_comments"));
capsule.setTotalRating(objCapsule.getInt("total_rating"));
// Log.e("Capsula ","added on"+objCapsule.getString("added_on"));
capsuleDao.insert(capsule);
} else {
capsule = new Capsule();
capsule.setTopicUuid(topic.getUuid());
capsule.setName(objCapsule.getString("name"));
capsule.setDescription(objCapsule.getString("description"));
capsule.setImage(objCapsule.getString("image"));
capsule.setPosition(objCapsule.getInt("position"));
capsule.setAddedOn(objCapsule.getString("added_on"));
capsule.setUpdatedOn(objCapsule.getString("updated_on"));
capsule.setLinkComments(objCapsule.getString("link_comments"));
capsule.setLinkCommentAdd(objCapsule.getString("link_comment_add"));
capsule.setTotalComments(objCapsule.getInt("total_comments"));
capsule.setTotalRating(objCapsule.getInt("total_rating"));
// Log.e("Capsula ","added on"+objCapsule.getString("added_on"));
capsuleDao.update(capsule);
}
arraySlides = objCapsule.getJSONArray("slides");
for (x = 0; x < arraySlides.length(); x++) {
objSlide = arraySlides.getJSONObject(x);
uuid = objSlide.getString("uuid");
slide = slideDao.selectByUuid(uuid);
if(slide == null) {
slide = new Slide();
slide.setUuid(uuid);
slide.setTopicUuid(capsule.getTopicUuid());
slide.setCapsuleUuid(capsule.getUuid());
slide.setQuizUuid(objSlide.getString("quiz_uuid"));
slide.setName(objSlide.getString("name"));
slide.setDescription(objSlide.getString("description"));
slide.setPosition(objSlide.getInt("position"));
slide.setType(objSlide.getString("type"));
slide.setFile(objSlide.getString("file"));
slide.setBackground(objSlide.getString("background"));
slideDao.insert(slide);
} else {
slide.setTopicUuid(capsule.getTopicUuid());
slide.setCapsuleUuid(capsule.getUuid());
slide.setQuizUuid(objSlide.getString("quiz_uuid"));
slide.setName(objSlide.getString("name"));
slide.setDescription(objSlide.getString("description"));
slide.setPosition(objSlide.getInt("position"));
slide.setType(objSlide.getString("type"));
slide.setFile(objSlide.getString("file"));
slide.setBackground(objSlide.getString("background"));
slideDao.update(slide);
}
}
}
}
}
if(data.has( "extended")) {
JSONObject objExtended;
JSONObject objItem;
JSONArray objItems;
UserExtended userExtended;
JSONArray extendedCompanies = data.getJSONArray("extended");
for(i = 0 ; i < extendedCompanies.length(); i++)
{
objExtended = extendedCompanies.getJSONObject(i);
if(objExtended.has("details")) {
uuid = objExtended.getString("company_uuid");
company = companyDao.selectByUuid(uuid);
if (company == null) {
company = new Company();
company.setUuid(objExtended.getString("company_uuid"));
company.setName(objExtended.getString("company_name"));
company.setImage(objExtended.getString("company_image"));
companyDao.insert(company);
} else {
company.setName(objExtended.getString("company_name"));
company.setImage(objExtended.getString("company_image"));
companyDao.update(company);
}
objItems = objExtended.getJSONArray("details");
for(j = 0 ; j < objItems.length(); j++) {
objItem = objItems.getJSONObject(j);
uuid = objItem.getString("uuid");
userExtended = userExtendedDao.selectByUuid(uuid);
if(userExtended == null) {
userExtended = new UserExtended();
userExtended.setCompanyUuid(company.getUuid());
userExtended.setUuid(uuid);
userExtended.setLabel(objItem.getString("label"));
userExtended.setValue(objItem.getString("value"));
userExtendedDao.insert(userExtended);
} else {
userExtended.setCompanyUuid(company.getUuid());
userExtended.setLabel(objItem.getString("label"));
userExtended.setValue(objItem.getString("value"));
userExtendedDao.update(userExtended);
}
}
}
}
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
if(data.has("max_date_changes")) {
String max_date_changes = data.getString("max_date_changes");
Log.d("syncFromServer", "max_date_changes : " + max_date_changes);
if(!TextUtils.isEmpty(max_date_changes)) {
preference.setMaxDateChanges(max_date_changes);
}
} else {
Log.d("syncFromServer", "No max_date_changes");
}
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
String addedOn = simpleDateFormat.format(date);
List<Company> companies = mAppDatabase.getCompanyDao().selectAll();
int companySize = companies.size();
if(companySize > 0) {
if(TextUtils.isEmpty(preference.getCompanyUuidActive())) {
preference.setCompanyUuidActive(companies.get(0).getUuid());
} else {
boolean companyExist = false;
for(i = 0; i < companies.size(); i++) {
if (companies.get(i).getUuid().equals(preference.getCompanyUuidActive())) {
companyExist = true;
break;
}
}
if(!companyExist && companies.size() > 0) {
preference.setCompanyUuidActive(companies.get(0).getUuid());
}
}
} else {
preference.setCompanyUuidActive("");
}
preference.setLastDataRefresh(addedOn);
preference.setCompanyCount(companySize);
preference.save();
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
}
}
@Override
public void requestExecuteSyncAdapter() {
// Pass the settings flags by inserting them in a bundle
/*Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(mAccount, Constants.AUTHORITY, settingsBundle);*/
syncToServerOrCheckChanges();
}
private void processResponseServerChanges(String dataString)
{
Log.d(TAG, "processResponseServerChanges = " + dataString);
try {
JSONObject objJSON = new JSONObject(dataString);
boolean success = objJSON.has("success") ? objJSON.getBoolean("success") : false;
if(success) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
preference.setLastDataRefresh(simpleDateFormat.format(calendar.getTime()));
preference.save();
String max_date_changes = "";
boolean processChanges = false;
JSONObject data = objJSON.getJSONObject("data");
if(data.has("max_date_changes") && data.has("new_capsules")) {
int new_capsules = data.getInt("new_capsules");
max_date_changes = data.getString("max_date_changes");
processChanges = new_capsules > 0 && !max_date_changes.equals(preference.getMaxDateChanges());
} else {
processChanges = false;
}
if(processChanges && !max_date_changes.isEmpty()) {
int new_capsules = data.getInt("new_capsules");
String message = new_capsules == 1
? "Hay 1 cápsula disponible"
: "Hay " + new_capsules + " cápsulas disponible";
showMessageSnackBarWithClose(message);
preference.setMaxDateChanges(max_date_changes);
if(!isForeground) {
String body = new_capsules == 1
? "Hay 1 cápsula disponible"
: "Hay " + new_capsules + " cápsulas disponible";
showFcmNotification("Nuevo contenido", body, new_capsules, "", false);
}
/*
if(!isForeground) {
String body = new_capsules == 1
? "Hay 1 cápsula nueva disponible"
: "Hay " + new_capsules + " cápsulas disponible";
showFcmNotification("Nuevo contenido", body, new_capsules);
AnswerDao answerDao = mAppDatabase.getAnswerDao();
answerDao.removeAll();
QuestionDao questionDao = mAppDatabase.getQuestionDao();
questionDao.removeAll();
QuizDao quizDao = mAppDatabase.getQuizDao();
quizDao.removeAll();
SlideDao slideDao = mAppDatabase.getSlideDao();
slideDao.removeAll();
CapsuleDao capsuleDao = mAppDatabase.getCapsuleDao();
capsuleDao.removeAll();
TopicDao topicDao = mAppDatabase.getTopicDao();
topicDao.removeAll();
CompanyDao companyDao = mAppDatabase.getCompanyDao();
companyDao.removeAll();
UserExtendedDao userExtendedDao = mAppDatabase.getUserExtendedDao();
userExtendedDao.removeAll();
this.syncFromServer(data);
if(!TextUtils.isEmpty(preference.getSlideUuidActive())) {
Slide slide = mAppDatabase.getSlideDao().selectByUuid(preference.getSlideUuidActive());
if(slide == null) {
preference.setFragmentIdxActive(Constants.IDX_FRAGMENT_TOPICS);
preference.save(this);
}
}
}*/
}
}
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
}
}
@Override
public AppDatabase getDatabase() {
return mAppDatabase;
}
@Override
public void registerDevice(Sync record)
{
if(isActiveOperation) {
return;
}
Log.d(TAG, "registerDevice");
try {
Http http = new Http(this.getCacheDir());
OkHttpClient client = http.getHttpClient(false);
RequestBody formBody = new FormBody.Builder()
.add(Constants.POST_DEVICE_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID))
.add(Constants.POST_DEVICE_FIELD_VARIANT_ID, String.valueOf(Configuration.VARIANT_ID))
.add(Constants.POST_DEVICE_FIELD_DEVICE_UUID, preference.getDeviceUuid())
.add(Constants.POST_DEVICE_FIELD_MANUFACTURER, Build.MANUFACTURER)
.add(Constants.POST_DEVICE_FIELD_BRAND, Build.BRAND)
.add(Constants.POST_DEVICE_FIELD_VERSION, Build.VERSION.RELEASE + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName())
.add(Constants.POST_DEVICE_FIELD_MODEL, Build.MODEL)
.add(Constants.POST_DEVICE_FIELD_PLATFORM, "android")
.add(Constants.POST_DEVICE_FIELD_SYNC_ID, String.valueOf(record.getId()))
.build();
Log.d(TAG, "URL = " + Configuration.URL_DEVICE);
Request request = new Request.Builder()
.url(Configuration.URL_DEVICE)
.post(formBody)
.build();
isActiveOperation = true;
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
public void onResponse(Call call, Response response)
throws IOException {
Log.d(TAG, "Response Device : " + response.body().toString());
isActiveOperation = false;
processResponseSyncToServer(response.body().string(),"device");
}
public void onFailure(Call call, IOException e) {
isActiveOperation = false;
Log.d(TAG, "Error : " + e.getMessage());
}
});
} catch (Exception e) {
}
}
@Override
public void registerFcm(Sync record)
{
isActiveOperation = true;
Log.d(TAG, "FCM");
Log.e("Token a Sync",""+record.getData());
try {
Http http = new Http(this.getCacheDir());
OkHttpClient client = http.getHttpClient(false);
RequestBody formBody = new FormBody.Builder()
.add(Constants.POST_FCM_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID))
.add(Constants.POST_FCM_FIELD_VARIANT_ID, String.valueOf(Configuration.VARIANT_ID))
.add(Constants.POST_FCM_FIELD_DEVICE_UUID,preference.getDeviceUuid())
.add(Constants.POST_FCM_FIELD_TOKEN, record.getData())
.add(Constants.POST_FCM_FIELD_SYNC_ID, String.valueOf(record.getId()))
.build();
Log.d(TAG, "URL = " + Configuration.URL_FCM);
Request request = new Request.Builder()
.url(Configuration.URL_FCM)
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
public void onResponse(Call call, Response response)
throws IOException {
isActiveOperation = false;
Log.e("Se envio", "Procesando respuesta");
processResponseSyncToServer(response.body().string(),"");
}
public void onFailure(Call call, IOException e) {
Log.d(TAG, "Error : " + e.getMessage());
isActiveOperation = false;
}
});
} catch (Exception e) {
}
}
@Override
public void syncToServerOrCheckChanges()
{
boolean otherOperationInProgress = false;
SyncDao syncDao = mAppDatabase.getSyncDao();
List<Sync> records = syncDao.selectBatch();
for(Sync record : records) {
if (record.getType() == Constants.SYNC_ADAPTER_TYPE_DEVICE) {
otherOperationInProgress = true;
registerDevice(record);
break;
} else if (record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM) {
otherOperationInProgress = true;
registerFcm(record);
break;
}
}
if(otherOperationInProgress) {
return;
}
if(records.size() > 0) {
syncToServer(records);
} else {
long timeLast = 0;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
if (!TextUtils.isEmpty(preference.getLastDataRefresh())) {
try {
timeLast = simpleDateFormat.parse(preference.getLastDataRefresh()).getTime();
} catch (Exception e) {
}
}
Calendar calendar = Calendar.getInstance();
long timeNow = calendar.getTime().getTime();
if(timeNow > (timeLast + Constants.CHECK_CHANGES_TOKEN_INTERVAL)){
if(preference.isRefreshTokenIsRequired()) {
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
String token = task.getResult();
Log.e("BUG Token", "Token 3 : " + token);
createSyncRecordNewToken(token);
preference.setRefreshTokenIsRequired(false);
preference.save();
}
});
} else {
if (preference.getDeviceToken().isEmpty()) {
FirebaseMessaging.getInstance().deleteToken().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
preference.setRefreshTokenIsRequired(true);
preference.save();
Log.e("Esta vacio"," mando delete token");
}
});
}
}
}
if (timeNow > (timeLast + Constants.CHECK_CHANGES_INTERVAL)) {
requestCheckChanges();
}
}
}
public void syncToServer(List<Sync> records)
{
int maxRecordsSyncBatch = 0;
FormBody.Builder formBodyBatchBuilder = new FormBody.Builder();
formBodyBatchBuilder.add(Constants.POST_SYNC_BATCH_FIELD_DEVICE_UUID, preference.getDeviceUuid());
for(Sync record : records)
{
if(record.getType() == Constants.SYNC_ADAPTER_TYPE_SYNC ) {
Log.d(TAG, "SYNC BATCH");
maxRecordsSyncBatch++;
formBodyBatchBuilder.add(Constants.POST_SYNC_BATCH_FIELD_RECORD_DATA + maxRecordsSyncBatch, record.getData());
formBodyBatchBuilder.add(Constants.POST_SYNC_BATCH_FIELD_RECORD_SYNC_ID + maxRecordsSyncBatch, String.valueOf(record.getId()));
}
}
if(isActiveOperation) {
return;
}
if(maxRecordsSyncBatch > 0) {
Log.d(TAG, "Sync Batch");
isActiveOperation = true;
try {
Http http = new Http(this.getCacheDir());
OkHttpClient client = http.getHttpClient(false);
formBodyBatchBuilder.add(Constants.POST_SYNC_BATCH_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID));
formBodyBatchBuilder.add(Constants.POST_SYNC_BATCH_FIELD_VARIANT_ID, String.valueOf(Configuration.VARIANT_ID));
formBodyBatchBuilder.add(Constants.POST_SYNC_BATCH_FIELD_MAX_RECORDS, String.valueOf(maxRecordsSyncBatch));
RequestBody formBody = formBodyBatchBuilder.build();
Log.d(TAG, "URL = " + Configuration.URL_SYNC_BATCH);
Request request = new Request.Builder()
.url(Configuration.URL_SYNC_BATCH)
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
public void onResponse(Call call, Response response) throws IOException {
isActiveOperation = false;
processResponseServerBatch(response.body().string());
}
public void onFailure(Call call, IOException e) {
Log.d(TAG, "Error : " + e.getMessage());
isActiveOperation = false;
}
});
} catch (Exception e) {
}
}
}
private void processResponseServerBatch(String dataString)
{
boolean success = false;
long sync_id = 0;
Log.d(TAG, "processResponseServer = " + dataString);
try {
JSONObject objJSON = new JSONObject(dataString);
success = objJSON.has("success") ? objJSON.getBoolean("success") : false;
if(success && objJSON.has("data")) {
JSONArray jsonArrayData = objJSON.getJSONArray("data");
JSONObject jsonObjectData;
int max = jsonArrayData.length();
for(int i = 0; i < max; i++) {
jsonObjectData = jsonArrayData.getJSONObject(i);
if(jsonObjectData.has("success") && jsonObjectData.getBoolean("success")) {
sync_id = jsonObjectData.getLong("sync_id");
mAppDatabase.getSyncDao().remove(sync_id);
}
}
/*
if(jsonObjectData.has("message")) {
message = jsonObjectData.getString("message");
}
*/
/*
if(jsonObjectData.has("aes")) {
preference.setAes(jsonObjectData.getString("aes"));
preference.save();
}
if(jsonObjectData.has("password")) {
preference.setPassword(jsonObjectData.getString("password"));
preference.save();
}*/
}
if(success && sync_id > 0) {
Log.d(TAG, "DELETE SYNC RECORD : " + sync_id);
mAppDatabase.getSyncDao().remove(sync_id);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void processResponseSyncToServer(String dataString, String origen)
{
boolean success = false;
long sync_id = 0;
Log.d(TAG, "processResponseServer = " + dataString);
try {
JSONObject objJSON = new JSONObject(dataString);
success = objJSON.has("success") ? objJSON.getBoolean("success") : false;
if(success && objJSON.has("data")) {
JSONObject jsonObjectData = objJSON.getJSONObject("data");
if(jsonObjectData.has("sync_id")) {
sync_id = jsonObjectData.getLong("sync_id");
}
/*
if(jsonObjectData.has("message")) {
message = jsonObjectData.getString("message");
}
*/
if(jsonObjectData.has("aes")) {
preference.setAes(jsonObjectData.getString("aes"));
preference.save();
}
if(jsonObjectData.has("password")) {
preference.setPassword(jsonObjectData.getString("password"));
preference.save();
}
}
Log.d(TAG, "SyncID = " + sync_id);
if(success && sync_id > 0) {
Log.d(TAG, "DELETE SYNC RECORD : " + sync_id);
mAppDatabase.getSyncDao().remove(sync_id);
if(origen.equals("device"))
{
syncToServerOrCheckChanges();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/*
private void processResponseSyncTokenFCMtoServer(String dataString)
{
Log.e("Respuesta",""+dataString);
boolean success = false;
try {
JSONObject objJSON = new JSONObject(dataString);
success = objJSON.has("success") && objJSON.getBoolean("success");
if(success) {
Log.e("Token almacenado"," en server de manera exitosa");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
*/
/*
private void processResponseServerCheckChanges(String dataString) {
Log.d(TAG, "processResponseServerCheckChanges = " + dataString);
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject objJSON = new JSONObject(dataString);
boolean success = objJSON.has("success") ? objJSON.getBoolean("success") : false;
String message = "";
if (objJSON.has("data")) {
Object item = objJSON.get("data");
if (item instanceof String) {
message = item.toString();
}
}
if (success) {
mAppDatabase.getAnswerDao().removeAll();
mAppDatabase.getQuestionDao().removeAll();
mAppDatabase.getQuizDao().removeAll();
mAppDatabase.getSlideDao().removeAll();
mAppDatabase.getCapsuleDao().removeAll();
mAppDatabase.getTopicDao().removeAll();
JSONObject data = objJSON.getJSONObject("data");
syncFromServer(data);
}
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
}
reloadNavHeader();
}
});
}
*/
}