Proyectos de Subversion Android Microlearning - Inconcert

Rev

Rev 18 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 18 Rev 19
Línea 19... Línea 19...
19
import android.app.NotificationChannel;
19
import android.app.NotificationChannel;
20
import android.app.NotificationManager;
20
import android.app.NotificationManager;
21
import android.app.PendingIntent;
21
import android.app.PendingIntent;
22
import android.content.ContentResolver;
22
import android.content.ContentResolver;
23
import android.content.DialogInterface;
23
import android.content.DialogInterface;
-
 
24
import android.content.PeriodicSync;
24
import android.net.ConnectivityManager;
25
import android.net.ConnectivityManager;
25
import android.os.Build;
26
import android.os.Build;
26
import android.os.Handler;
27
import android.os.Handler;
27
import android.os.Looper;
28
import android.os.Looper;
28
import android.text.TextUtils;
29
import android.text.TextUtils;
Línea 90... Línea 91...
90
import com.cesams.twogetskills.inconcert.preference.Preference;
91
import com.cesams.twogetskills.inconcert.preference.Preference;
91
import com.cesams.twogetskills.inconcert.receiver.ConnectivityReceiver;
92
import com.cesams.twogetskills.inconcert.receiver.ConnectivityReceiver;
92
import com.cesams.twogetskills.inconcert.receiver.InternalReceiver;
93
import com.cesams.twogetskills.inconcert.receiver.InternalReceiver;
93
import com.cesams.twogetskills.inconcert.room.ResultCount;
94
import com.cesams.twogetskills.inconcert.room.ResultCount;
94
import com.cesams.twogetskills.inconcert.skeleton.IReloadData;
95
import com.cesams.twogetskills.inconcert.skeleton.IReloadData;
-
 
96
import com.cesams.twogetskills.inconcert.sync.WorkSync;
95
import com.google.android.gms.tasks.OnCompleteListener;
97
import com.google.android.gms.tasks.OnCompleteListener;
96
import com.google.android.gms.tasks.Task;
98
import com.google.android.gms.tasks.Task;
97
import com.google.android.material.bottomnavigation.BottomNavigationView;
99
import com.google.android.material.bottomnavigation.BottomNavigationView;
98
import com.google.android.material.navigation.NavigationBarView;
100
import com.google.android.material.navigation.NavigationBarView;
99
import com.google.android.material.snackbar.Snackbar;
101
import com.google.android.material.snackbar.Snackbar;
Línea 100... Línea 102...
100
 
102
 
-
 
103
import androidx.appcompat.widget.Toolbar;
-
 
104
import androidx.work.Constraints;
-
 
105
import androidx.work.ExistingPeriodicWorkPolicy;
-
 
106
import androidx.work.NetworkType;
-
 
107
import androidx.work.PeriodicWorkRequest;
-
 
108
import androidx.work.WorkManager;
Línea 101... Línea 109...
101
import androidx.appcompat.widget.Toolbar;
109
import androidx.work.WorkRequest;
102
 
110
 
Línea 103... Línea 111...
103
import com.cesams.twogetskills.inconcert.Constants;
111
import com.cesams.twogetskills.inconcert.Constants;
Línea 119... Línea 127...
119
import java.util.Date;
127
import java.util.Date;
120
import java.util.HashMap;
128
import java.util.HashMap;
121
import java.util.List;
129
import java.util.List;
122
import java.util.Random;
130
import java.util.Random;
123
import java.util.TimeZone;
131
import java.util.TimeZone;
-
 
132
import java.util.concurrent.TimeUnit;
Línea 124... Línea 133...
124
 
133
 
125
//import de.hdodenhof.circleimageview.CircleImageView;
134
//import de.hdodenhof.circleimageview.CircleImageView;
126
import okhttp3.Call;
135
import okhttp3.Call;
127
import okhttp3.Callback;
136
import okhttp3.Callback;
128
import okhttp3.FormBody;
137
import okhttp3.FormBody;
129
import okhttp3.OkHttpClient;
138
import okhttp3.OkHttpClient;
130
import okhttp3.Request;
139
import okhttp3.Request;
131
import okhttp3.RequestBody;
140
import okhttp3.RequestBody;
Línea -... Línea 141...
-
 
141
import okhttp3.Response;
132
import okhttp3.Response;
142
 
-
 
143
 
133
 
144
public class MainActivity extends  AppCompatActivity implements ITwoGetSkills {
134
public class MainActivity extends  AppCompatActivity implements ITwoGetSkills {
145
    /*
135
    private boolean isSyncDevice = false;
146
    private boolean isSyncDevice = false;
-
 
147
    private boolean isSyncToken = false;
136
    private boolean isSyncToken = false;
148
    private boolean isSyncBatch = false;
137
    private boolean isSyncBatch = false;
149
    */
138
    private boolean isForeground = false;
150
    private boolean isForeground = false;
Línea 139... Línea 151...
139
    private boolean isSmartLockLoginRun = false;
151
    private boolean isSmartLockLoginRun = false;
Línea 151... Línea 163...
151
    private boolean connected = false;
163
    private boolean connected = false;
152
    private ConnectivityReceiver mConnectivityReceiver;
164
    private ConnectivityReceiver mConnectivityReceiver;
153
    private InternalReceiver mInternalReceiver;
165
    private InternalReceiver mInternalReceiver;
154
    private ProgressBar mProgressBar;
166
    private ProgressBar mProgressBar;
155
    private Toolbar mToolbar;
167
    private Toolbar mToolbar;
156
    private Account mAccount;
168
    //private Account mAccount;
157
    private AppDatabase mAppDatabase;
169
    private AppDatabase mAppDatabase;
Línea -... Línea 170...
-
 
170
 
-
 
171
 
-
 
172
    private WorkManager mWorkManager;
-
 
173
    private boolean isActiveOperation;
Línea 158... Línea 174...
158
 
174
 
159
 
175
 
160
 
176
 
161
    ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
177
    ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
Línea 201... Línea 217...
201
                preference.save();
217
                preference.save();
Línea 202... Línea 218...
202
 
218
 
203
                Log.e("BUG Token", "SyncRecord - Device");
219
                Log.e("BUG Token", "SyncRecord - Device");
204
                Sync sync = new Sync(Constants.SYNC_ADAPTER_TYPE_DEVICE, uuid);
220
                Sync sync = new Sync(Constants.SYNC_ADAPTER_TYPE_DEVICE, uuid);
-
 
221
                mAppDatabase.getSyncDao().insert(sync);
Línea 205... Línea 222...
205
                mAppDatabase.getSyncDao().insert(sync);
222
                registerDevice(sync);
206
 
223
 
207
                if (!TextUtils.isEmpty(preference.getDeviceToken())) {
224
                if (!TextUtils.isEmpty(preference.getDeviceToken())) {
208
                    Log.e("BUG Token", "SyncRecord - FCM1");
225
                    Log.e("BUG Token", "SyncRecord - FCM1");
-
 
226
                    sync = new Sync(Constants.SYNC_ADAPTER_TYPE_FCM, preference.getDeviceToken());
-
 
227
                    mAppDatabase.getSyncDao().insert(sync);
209
                    sync = new Sync(Constants.SYNC_ADAPTER_TYPE_FCM, preference.getDeviceToken());
228
 
210
                    mAppDatabase.getSyncDao().insert(sync);
229
                    registerFcm(sync);
Línea 211... Línea 230...
211
                }
230
                }
212
            }
231
            }
213
 
232
 
Línea 214... Línea 233...
214
            /**** CANAL DE NOTIFICACIONES **/
233
            /**** CANAL DE NOTIFICACIONES **/
215
            createNotificationChannel();
234
            createNotificationChannel();
Línea 216... Línea 235...
216
           // badgenotification= findViewById(R.id.cart_badge);
235
           // badgenotification= findViewById(R.id.cart_badge);
Línea 217... Línea 236...
217
 
236
 
Línea 345... Línea 364...
345
 
364
 
Línea 346... Línea 365...
346
            if (getIntent().getExtras() != null) {
365
            if (getIntent().getExtras() != null) {
347
 
366
 
348
                for (String key : getIntent().getExtras().keySet()) {
367
                for (String key : getIntent().getExtras().keySet()) {
349
                    Object value = getIntent().getExtras().get(key);
-
 
Línea 350... Línea 368...
350
                    if(value.toString().equals("content-refresh") && !isShowingCartelRefresh)
368
                    Object value = getIntent().getExtras().get(key);
Línea 351... Línea 369...
351
                        {
369
                    if (value.toString().equals("content-refresh") && !isShowingCartelRefresh) {
352
 
370
 
353
                            Log.e("OnCreate","intent cartel");
371
                        Log.e("OnCreate", "intent cartel");
354
 
372
 
Línea 355... Línea 373...
355
                            //Vengo de notificacion, pero el app estaba cerrada, asi que muestro el cartel de content-refresh
373
                        //Vengo de notificacion, pero el app estaba cerrada, asi que muestro el cartel de content-refresh
Línea 356... Línea 374...
356
                           // preference.setRefreshContentRequired(true);
374
                        // preference.setRefreshContentRequired(true);
-
 
375
                        saveNotificationCenterNew("Nuevo contenido para descargar", "", "Tienes nuevas capsulas de aprendizaje para descargar");
-
 
376
                        CartelRefreshContent();
-
 
377
 
-
 
378
                    }
-
 
379
 
-
 
380
                }
-
 
381
            }
-
 
382
 
-
 
383
        Constraints constraints = new Constraints.Builder()
-
 
384
                .setRequiredNetworkType(NetworkType.CONNECTED)
Línea -... Línea 385...
-
 
385
                .build();
-
 
386
 
-
 
387
        PeriodicWorkRequest mWorkRequestSync =
-
 
388
                new PeriodicWorkRequest.Builder(WorkSync.class, 15, TimeUnit.MINUTES)
-
 
389
                        // Constraints
-
 
390
                        .build();
357
                            saveNotificationCenterNew("Nuevo contenido para descargar","","Tienes nuevas capsulas de aprendizaje para descargar");
391
 
-
 
392
 
358
                            CartelRefreshContent();
393
 
Línea 359... Línea 394...
359
 
394
        mWorkManager = WorkManager.getInstance(getApplication());
Línea 360... Línea 395...
360
                        }
395
        mWorkManager.enqueueUniquePeriodicWork(
Línea 1311... Línea 1346...
1311
    }
1346
    }
Línea 1312... Línea 1347...
1312
 
1347
 
1313
    @Override
1348
    @Override
1314
    public void changeTopicActive(String topicUuid)
1349
    public void changeTopicActive(String topicUuid)
1315
    {
1350
    {
1316
        preference.setSourceNavigation(Constants.SOURCE_NAVIGATION_MY_CAPSULES);
1351
        preference.setSourceNavigation(Constants.SOURCE_NAVIGATION_TOPICS);
-
 
1352
        preference.setTopicUuidActive(topicUuid);
1317
        preference.setTopicUuidActive(topicUuid);
1353
        preference.setCapsuleUuidActive("");
1318
        preference.setSlideUuidActive("");
1354
        preference.setSlideUuidActive("");
Línea 1319... Línea 1355...
1319
        preference.save();
1355
        preference.save();
1320
 
1356
 
Línea 2584... Línea 2620...
2584
    }
2620
    }
Línea 2585... Línea 2621...
2585
 
2621
 
2586
    @Override
2622
    @Override
2587
    public void requestExecuteSyncAdapter() {
2623
    public void requestExecuteSyncAdapter() {
2588
            // Pass the settings flags by inserting them in a bundle
2624
            // Pass the settings flags by inserting them in a bundle
2589
            Bundle settingsBundle = new Bundle();
2625
            /*Bundle settingsBundle = new Bundle();
2590
            settingsBundle.putBoolean(
2626
            settingsBundle.putBoolean(
2591
                    ContentResolver.SYNC_EXTRAS_MANUAL, true);
2627
                    ContentResolver.SYNC_EXTRAS_MANUAL, true);
2592
            settingsBundle.putBoolean(
2628
            settingsBundle.putBoolean(
Línea 2593... Línea 2629...
2593
                    ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
2629
                    ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
-
 
2630
 
-
 
2631
            ContentResolver.requestSync(mAccount, Constants.AUTHORITY, settingsBundle);*/
2594
 
2632
 
Línea 2595... Línea 2633...
2595
            ContentResolver.requestSync(mAccount, Constants.AUTHORITY, settingsBundle);
2633
        syncToServerOrCheckChanges();
2596
    }
2634
    }
Línea 2710... Línea 2748...
2710
    public AppDatabase getDatabase() {
2748
    public AppDatabase getDatabase() {
2711
        return mAppDatabase;
2749
        return mAppDatabase;
2712
    }
2750
    }
Línea 2713... Línea 2751...
2713
 
2751
 
2714
    @Override
2752
    @Override
2715
    public void syncToServerOrCheckChanges()
2753
    public void registerDevice(Sync record)
2716
    {
2754
    {
2717
        if(TextUtils.isEmpty(preference.getDeviceUuid())) {
2755
        if(isActiveOperation) {
2718
            return;
2756
            return;
Línea 2719... Línea -...
2719
        }
-
 
2720
 
2757
        }
Línea 2721... Línea -...
2721
        SyncDao syncDao = mAppDatabase.getSyncDao();
-
 
2722
        List<Sync> records = syncDao.selectBatch();
-
 
2723
 
-
 
2724
        if(records.size() > 0) {
-
 
2725
            syncToServer(records);
-
 
2726
        } else {
-
 
2727
 
-
 
2728
                long timeLast = 0;
-
 
2729
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
-
 
2730
                if (!TextUtils.isEmpty(preference.getLastDataRefresh())) {
-
 
2731
                    try {
-
 
2732
                        timeLast = simpleDateFormat.parse(preference.getLastDataRefresh()).getTime();
-
 
2733
                    } catch (Exception e) {
-
 
2734
 
-
 
2735
                    }
-
 
2736
 
-
 
2737
                }
-
 
2738
 
-
 
Línea -... Línea 2758...
-
 
2758
 
-
 
2759
        Log.d(TAG, "registerDevice");
2739
 
2760
 
Línea -... Línea 2761...
-
 
2761
 
-
 
2762
        try {
-
 
2763
            Http http = new Http(this.getCacheDir());
2740
                Calendar calendar = Calendar.getInstance();
2764
            OkHttpClient client = http.getHttpClient(false);
-
 
2765
 
-
 
2766
            RequestBody formBody = new FormBody.Builder()
-
 
2767
                    .add(Constants.POST_DEVICE_FIELD_APPLICATION_ID,  String.valueOf(Configuration.APPLICATION_ID))
-
 
2768
                    .add(Constants.POST_DEVICE_FIELD_VARIANT_ID,  String.valueOf(Configuration.VARIANT_ID))
-
 
2769
                    .add(Constants.POST_DEVICE_FIELD_DEVICE_UUID, preference.getDeviceUuid())
-
 
2770
                    .add(Constants.POST_DEVICE_FIELD_MANUFACTURER, Build.MANUFACTURER)
-
 
2771
                    .add(Constants.POST_DEVICE_FIELD_BRAND, Build.BRAND)
Línea 2741... Línea 2772...
2741
                long timeNow = calendar.getTime().getTime();
2772
                    .add(Constants.POST_DEVICE_FIELD_VERSION, Build.VERSION.RELEASE  + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName())
2742
 
-
 
2743
                if(timeNow > (timeLast + Constants.CHECK_CHANGES_TOKEN_INTERVAL)){
2773
                    .add(Constants.POST_DEVICE_FIELD_MODEL, Build.MODEL)
2744
 
-
 
2745
                    //Procesamiento en caso que el token no este, check cada 15 minutos.
2774
                    .add(Constants.POST_DEVICE_FIELD_PLATFORM, "android")
2746
 
-
 
2747
                    if(preference.isRefreshTokenIsRequired()) {
2775
                    .add(Constants.POST_DEVICE_FIELD_SYNC_ID, String.valueOf(record.getId()))
2748
                        FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
2776
                    .build();
Línea 2749... Línea -...
2749
                            @Override
-
 
2750
                            public void onComplete(@NonNull Task<String> task) {
2777
 
Línea -... Línea 2778...
-
 
2778
            Log.d(TAG, "URL = " + Configuration.URL_DEVICE);
-
 
2779
            Request request = new Request.Builder()
2751
                                if (!task.isSuccessful()) {
2780
                    .url(Configuration.URL_DEVICE)
-
 
2781
                    .post(formBody)
-
 
2782
                    .build();
-
 
2783
 
Línea 2752... Línea 2784...
2752
                                    Log.w(TAG, "Fetching FCM registration token failed", task.getException());
2784
            isActiveOperation = true;
-
 
2785
 
Línea -... Línea 2786...
-
 
2786
            Call call = client.newCall(request);
2753
                                    return;
2787
            call.enqueue(new okhttp3.Callback() {
2754
                                }
2788
                public void onResponse(Call call, Response response)
2755
 
2789
                        throws IOException {
2756
                                // Get new FCM registration token
2790
                    Log.d(TAG, "Response Device :  " +  response.body().toString());
-
 
2791
                    isActiveOperation = false;
-
 
2792
 
-
 
2793
                    processResponseSyncToServer(response.body().string(),"device");
Línea -... Línea 2794...
-
 
2794
                }
-
 
2795
 
-
 
2796
                public void onFailure(Call call, IOException e) {
-
 
2797
                    isActiveOperation = false;
-
 
2798
                    Log.d(TAG, "Error :  " +  e.getMessage());
-
 
2799
                }
Línea 2757... Línea 2800...
2757
                                String token = task.getResult();
2800
            });
-
 
2801
        } catch (Exception e) {
-
 
2802
        }
Línea 2758... Línea 2803...
2758
 
2803
    }
2759
                                Log.e("BUG Token", "Token 3 :  " + token);
2804
 
-
 
2805
    @Override
2760
 
2806
    public void registerFcm(Sync record)
2761
                                createSyncRecordNewToken(token);
2807
    {
2762
 
2808
        isActiveOperation = true;
2763
                                preference.setRefreshTokenIsRequired(false);
2809
        Log.d(TAG, "FCM");
Línea 2764... Línea 2810...
2764
                                preference.save();
2810
        Log.e("Token a Sync",""+record.getData());
-
 
2811
 
-
 
2812
        try {
-
 
2813
            Http http = new Http(this.getCacheDir());
-
 
2814
            OkHttpClient client = http.getHttpClient(false);
Línea 2765... Línea 2815...
2765
                            }
2815
 
-
 
2816
            RequestBody formBody = new FormBody.Builder()
-
 
2817
                    .add(Constants.POST_FCM_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID))
2766
                        });
2818
                    .add(Constants.POST_FCM_FIELD_VARIANT_ID, String.valueOf(Configuration.VARIANT_ID))
2767
 
2819
                    .add(Constants.POST_FCM_FIELD_DEVICE_UUID,preference.getDeviceUuid())
2768
 
2820
                    .add(Constants.POST_FCM_FIELD_TOKEN, record.getData())
-
 
2821
                    .add(Constants.POST_FCM_FIELD_SYNC_ID, String.valueOf(record.getId()))
2769
                    } else {
2822
                    .build();
Línea 2770... Línea 2823...
2770
 
2823
 
2771
                        if (preference.getDeviceToken().isEmpty()) {
2824
            Log.d(TAG, "URL = " + Configuration.URL_FCM);
2772
                            FirebaseMessaging.getInstance().deleteToken().addOnCompleteListener(new OnCompleteListener<Void>() {
2825
            Request request = new Request.Builder()
2773
                                @Override
2826
                    .url(Configuration.URL_FCM)
-
 
2827
                    .post(formBody)
-
 
2828
                    .build();
-
 
2829
 
-
 
2830
            Call call = client.newCall(request);
2774
                                public void onComplete(@NonNull Task<Void> task) {
2831
            call.enqueue(new okhttp3.Callback() {
2775
                                    preference.setRefreshTokenIsRequired(true);
2832
                public void onResponse(Call call, Response response)
Línea 2776... Línea 2833...
2776
                                    preference.save();
2833
                        throws IOException {
2777
 
2834
                    isActiveOperation = false;
2778
                                    Log.e("Esta vacio"," mando delete token");
2835
                    Log.e("Se envio", "Procesando respuesta");
Línea -... Línea 2836...
-
 
2836
                    processResponseSyncToServer(response.body().string(),"");
-
 
2837
                }
-
 
2838
 
-
 
2839
                public void onFailure(Call call, IOException e) {
-
 
2840
                    Log.d(TAG, "Error :  " +  e.getMessage());
-
 
2841
                    isActiveOperation = false;
-
 
2842
                }
-
 
2843
            });
-
 
2844
        } catch (Exception e) {
-
 
2845
 
-
 
2846
 
-
 
2847
        }
-
 
2848
    }
-
 
2849
 
Línea 2779... Línea 2850...
2779
 
2850
 
2780
                                }
-
 
2781
                            });
-
 
2782
                        }
-
 
2783
                    }
2851
    @Override
2784
                }
2852
    public void syncToServerOrCheckChanges()
2785
 
-
 
2786
                 if (timeNow > (timeLast + Constants.CHECK_CHANGES_INTERVAL)) {
-
 
Línea 2787... Línea -...
2787
                     //Solicitar revision de cambios cada 4 horas.
-
 
Línea -... Línea 2853...
-
 
2853
    {
-
 
2854
 
-
 
2855
        boolean otherOperationInProgress = false;
-
 
2856
        SyncDao syncDao = mAppDatabase.getSyncDao();
-
 
2857
        List<Sync> records = syncDao.selectBatch();
-
 
2858
        for(Sync record : records) {
-
 
2859
            if (record.getType() == Constants.SYNC_ADAPTER_TYPE_DEVICE) {
-
 
2860
                otherOperationInProgress = true;
-
 
2861
                registerDevice(record);
-
 
2862
                break;
-
 
2863
            } else if  (record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM) {
Línea 2788... Línea -...
2788
                    requestCheckChanges();
-
 
2789
                }
-
 
Línea -... Línea 2864...
-
 
2864
                otherOperationInProgress = true;
-
 
2865
                registerFcm(record);
Línea -... Línea 2866...
-
 
2866
                break;
-
 
2867
            }
-
 
2868
        }
2790
        }
2869
 
-
 
2870
        if(otherOperationInProgress) {
2791
    }
2871
            return;
-
 
2872
        }
2792
 
2873
 
-
 
2874
 
Línea 2793... Línea -...
2793
 
-
 
2794
 
-
 
2795
    public void syncToServer(List<Sync> records)
-
 
2796
    {
-
 
2797
 
-
 
2798
 
-
 
2799
        int maxRecordsSyncBatch = 0;
-
 
2800
        FormBody.Builder formBodyBatchBuilder = new FormBody.Builder();
-
 
2801
        formBodyBatchBuilder.add(Constants.POST_SYNC_BATCH_FIELD_DEVICE_UUID, preference.getDeviceUuid());
-
 
2802
 
-
 
2803
        for(Sync record : records)
-
 
2804
        {
-
 
2805
 
-
 
2806
           // Log.e("Ingreso a", "syncToServer"+record.getType()+" data"+record.getData());
2875
        if(records.size() > 0) {
2807
 
-
 
2808
          //  Log.d(TAG, "SyncRecord ID = " + record.getId() + " Data : "  + record.getData() + " Type= " + record.getType());
-
 
2809
 
-
 
2810
 
-
 
2811
            if(record.getType() == Constants.SYNC_ADAPTER_TYPE_DEVICE && !isSyncDevice) {
-
 
2812
                Log.d(TAG, "Device");
-
 
2813
 
-
 
2814
 
-
 
2815
                try {
2876
            syncToServer(records);
2816
                    Http http = new Http(this.getCacheDir());
2877
        } else {
2817
                    OkHttpClient client = http.getHttpClient(false);
-
 
Línea 2818... Línea 2878...
2818
 
2878
            long timeLast = 0;
2819
                    RequestBody formBody = new FormBody.Builder()
-
 
Línea 2820... Línea 2879...
2820
                            .add(Constants.POST_DEVICE_FIELD_APPLICATION_ID,  String.valueOf(Configuration.APPLICATION_ID))
2879
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
2821
                            .add(Constants.POST_DEVICE_FIELD_DEVICE_UUID, preference.getDeviceUuid())
2880
            if (!TextUtils.isEmpty(preference.getLastDataRefresh())) {
2822
                            .add(Constants.POST_DEVICE_FIELD_MANUFACTURER, Build.MANUFACTURER)
-
 
2823
                            .add(Constants.POST_DEVICE_FIELD_BRAND, Build.BRAND)
2881
                try {
2824
                            .add(Constants.POST_DEVICE_FIELD_VERSION, Build.VERSION.RELEASE  + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName())
2882
                    timeLast = simpleDateFormat.parse(preference.getLastDataRefresh()).getTime();
2825
                            .add(Constants.POST_DEVICE_FIELD_MODEL, Build.MODEL)
2883
                } catch (Exception e) {
-
 
2884
                }
-
 
2885
            }
-
 
2886
 
-
 
2887
 
-
 
2888
            Calendar calendar = Calendar.getInstance();
-
 
2889
            long timeNow = calendar.getTime().getTime();
-
 
2890
 
-
 
2891
            if(timeNow > (timeLast + Constants.CHECK_CHANGES_TOKEN_INTERVAL)){
-
 
2892
                if(preference.isRefreshTokenIsRequired()) {
-
 
2893
                    FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
-
 
2894
                        @Override
-
 
2895
                        public void onComplete(@NonNull Task<String> task) {
2826
                            .add(Constants.POST_DEVICE_FIELD_PLATFORM, "android")
2896
                            if (!task.isSuccessful()) {
2827
                            .add(Constants.POST_DEVICE_FIELD_SYNC_ID, String.valueOf(record.getId()))
2897
                                Log.w(TAG, "Fetching FCM registration token failed", task.getException());
Línea 2828... Línea 2898...
2828
                            .build();
2898
                                return;
2829
 
2899
                            }
2830
                    Log.d(TAG, "URL = " + Configuration.URL_DEVICE);
2900
 
2831
                    Request request = new Request.Builder()
2901
                            // Get new FCM registration token
-
 
2902
                            String token = task.getResult();
Línea 2832... Línea -...
2832
                            .url(Configuration.URL_DEVICE)
-
 
2833
                            .post(formBody)
-
 
2834
                            .build();
-
 
Línea 2835... Línea -...
2835
 
-
 
2836
                    isSyncDevice = true;
-
 
2837
 
-
 
2838
                    Call call = client.newCall(request);
-
 
2839
                    call.enqueue(new okhttp3.Callback() {
-
 
2840
                        public void onResponse(Call call, Response response)
-
 
2841
                                throws IOException {
-
 
2842
                            Log.d(TAG, "Response Device :  " +  response.body().toString());
-
 
2843
                            isSyncDevice = false;
-
 
2844
 
-
 
2845
                            processResponseSyncToServer(response.body().string(),"device");
-
 
2846
                        }
-
 
2847
 
-
 
2848
                        public void onFailure(Call call, IOException e) {
-
 
2849
                            isSyncDevice = false;
-
 
2850
                            Log.d(TAG, "Error :  " +  e.getMessage());
-
 
2851
                        }
-
 
2852
                    });
-
 
2853
                } catch (Exception e) {
-
 
2854
                }
-
 
2855
            }
-
 
2856
 
-
 
Línea 2857... Línea -...
2857
            if(record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM && !isSyncToken && !isSyncDevice) {
-
 
2858
                isSyncToken = true;
-
 
2859
                Log.d(TAG, "FCM");
2903
                            Log.e("BUG Token", "Token 3 :  " + token);
2860
                Log.e("Token a Sync",""+record.getData());
2904
 
2861
 
2905
                            createSyncRecordNewToken(token);
2862
                try {
2906
 
-
 
2907
                            preference.setRefreshTokenIsRequired(false);
Línea -... Línea 2908...
-
 
2908
                            preference.save();
-
 
2909
                        }
Línea 2863... Línea -...
2863
                    Http http = new Http(this.getCacheDir());
-
 
2864
                    OkHttpClient client = http.getHttpClient(false);
-
 
Línea 2865... Línea 2910...
2865
 
2910
                    });
2866
                    RequestBody formBody = new FormBody.Builder()
2911
                } else {
2867
                            .add(Constants.POST_FCM_FIELD_APPLICATION_ID,String.valueOf(Configuration.APPLICATION_ID))
2912
                    if (preference.getDeviceToken().isEmpty()) {
2868
                            .add(Constants.POST_FCM_FIELD_VARIANT_ID,String.valueOf(Configuration.VARIANT_ID))
2913
                        FirebaseMessaging.getInstance().deleteToken().addOnCompleteListener(new OnCompleteListener<Void>() {
2869
                            .add(Constants.POST_FCM_FIELD_DEVICE_UUID,preference.getDeviceUuid())
2914
                            @Override
2870
                            .add(Constants.POST_FCM_FIELD_TOKEN, record.getData())
2915
                            public void onComplete(@NonNull Task<Void> task) {
2871
                            .add(Constants.POST_FCM_FIELD_SYNC_ID, String.valueOf(record.getId()))
2916
                                preference.setRefreshTokenIsRequired(true);
Línea -... Línea 2917...
-
 
2917
                                preference.save();
-
 
2918
 
-
 
2919
                                Log.e("Esta vacio"," mando delete token");
Línea -... Línea 2920...
-
 
2920
 
2872
                            .build();
2921
                            }
2873
 
2922
                        });
2874
                    Log.d(TAG, "URL = " + Configuration.URL_FCM);
2923
                    }
Línea 2875... Línea 2924...
2875
                    Request request = new Request.Builder()
2924
                }
2876
                            .url(Configuration.URL_FCM)
2925
            }
2877
                            .post(formBody)
2926
 
Línea 2926... Línea 2975...
2926
                    .build();
2975
                    .build();
Línea 2927... Línea 2976...
2927
 
2976
 
2928
                Call call = client.newCall(request);
2977
                Call call = client.newCall(request);
2929
                call.enqueue(new okhttp3.Callback() {
2978
                call.enqueue(new okhttp3.Callback() {
2930
                    public void onResponse(Call call, Response response) throws IOException {
2979
                    public void onResponse(Call call, Response response) throws IOException {
2931
                        isSyncBatch = false;
2980
                        isActiveOperation = false;
2932
                        processResponseServerBatch(response.body().string());
2981
                        processResponseServerBatch(response.body().string());
Línea 2933... Línea 2982...
2933
                    }
2982
                    }
2934
 
2983
 
2935
                    public void onFailure(Call call, IOException e) {
2984
                    public void onFailure(Call call, IOException e) {
2936
                        Log.d(TAG, "Error :  " +  e.getMessage());
2985
                        Log.d(TAG, "Error :  " +  e.getMessage());
2937
                        isSyncBatch = false;
2986
                        isActiveOperation = false;
2938
                    }
2987
                    }
Línea 2976... Línea 3025...
2976
 
3025
 
2977
                /*
3026
                /*
2978
                if(jsonObjectData.has("message")) {
3027
                if(jsonObjectData.has("message")) {
2979
                    message = jsonObjectData.getString("message");
3028
                    message = jsonObjectData.getString("message");
-
 
3029
                }
2980
                }
3030
                */
2981
 
3031
/*
2982
                if(jsonObjectData.has("aes")) {
3032
                if(jsonObjectData.has("aes")) {
2983
                    preference.setAes(jsonObjectData.getString("aes"));
3033
                    preference.setAes(jsonObjectData.getString("aes"));
2984
                    preference.save(this);
3034
                    preference.save();
Línea 2985... Línea 3035...
2985
                }
3035
                }
2986
 
3036
 
2987
                if(jsonObjectData.has("password")) {
3037
                if(jsonObjectData.has("password")) {
2988
                    preference.setPassword(jsonObjectData.getString("password"));
-
 
2989
                    preference.save(this);
3038
                    preference.setPassword(jsonObjectData.getString("password"));
-
 
3039
                    preference.save();
2990
                }
3040
                }*/
2991
                */
3041
 
2992
            }
3042
            }
2993
            if(success && sync_id > 0) {
3043
            if(success && sync_id > 0) {
Línea 3019... Línea 3069...
3019
 
3069
 
3020
                /*
3070
                /*
3021
                if(jsonObjectData.has("message")) {
3071
                if(jsonObjectData.has("message")) {
3022
                    message = jsonObjectData.getString("message");
3072
                    message = jsonObjectData.getString("message");
-
 
3073
                }
Línea 3023... Línea 3074...
3023
                }
3074
                */
3024
 
3075
 
3025
                if(jsonObjectData.has("aes")) {
3076
                if(jsonObjectData.has("aes")) {
3026
                    preference.setAes(jsonObjectData.getString("aes"));
3077
                    preference.setAes(jsonObjectData.getString("aes"));
Línea 3027... Línea 3078...
3027
                    preference.save(this);
3078
                    preference.save();
3028
                }
3079
                }
3029
 
3080
 
3030
                if(jsonObjectData.has("password")) {
3081
                if(jsonObjectData.has("password")) {
3031
                    preference.setPassword(jsonObjectData.getString("password"));
-
 
-
 
3082
                    preference.setPassword(jsonObjectData.getString("password"));
3032
                    preference.save(this);
3083
                    preference.save();
3033
                }
3084
                }
3034
                */
3085
 
3035
            }
3086
            }
3036
            Log.d(TAG, "SyncID = " + sync_id);
3087
            Log.d(TAG, "SyncID = " + sync_id);