Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

Rev 27 | Rev 29 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 27 Rev 28
Línea 1... Línea 1...
1
package com.cesams.twogetskills.service;
1
package com.cesams.twogetskills.service;
2
import android.content.Intent;
2
import android.content.Intent;
-
 
3
import android.graphics.drawable.Drawable;
-
 
4
import android.os.Build;
-
 
5
import android.text.TextUtils;
3
import android.util.Log;
6
import android.util.Log;
Línea 4... Línea 7...
4
 
7
 
5
import com.cesams.twogetskills.Configuration;
8
import com.cesams.twogetskills.Configuration;
-
 
9
import com.cesams.twogetskills.Constants;
-
 
10
import com.cesams.twogetskills.dao.AppDatabase;
-
 
11
import com.cesams.twogetskills.dao.DatabaseHelper;
-
 
12
import com.cesams.twogetskills.dao.SyncDao;
6
import com.cesams.twogetskills.Constants;
13
import com.cesams.twogetskills.entity.Sync;
7
import com.cesams.twogetskills.library.Functions;
14
import com.cesams.twogetskills.library.Functions;
-
 
15
import com.cesams.twogetskills.library.Http;
8
import com.cesams.twogetskills.library.Http;
16
import com.cesams.twogetskills.library.UniqueID;
9
import com.cesams.twogetskills.preference.Preference;
17
import com.cesams.twogetskills.preference.Preference;
10
import com.google.firebase.messaging.FirebaseMessagingService;
18
import com.google.firebase.messaging.FirebaseMessagingService;
Línea -... Línea 19...
-
 
19
import com.google.firebase.messaging.RemoteMessage;
-
 
20
 
-
 
21
import org.json.JSONException;
11
import com.google.firebase.messaging.RemoteMessage;
22
import org.json.JSONObject;
-
 
23
 
Línea 12... Línea 24...
12
 
24
import java.io.IOException;
13
import java.io.IOException;
25
import java.util.List;
14
 
26
 
15
import okhttp3.Call;
27
import okhttp3.Call;
Línea 22... Línea 34...
22
 
34
 
Línea 23... Línea 35...
23
public class FcmMessagingService extends FirebaseMessagingService {
35
public class FcmMessagingService extends FirebaseMessagingService {
-
 
36
 
-
 
37
 
-
 
38
    private static final String TAG = "C2GS - FcmMessageServ";
-
 
39
    private AppDatabase mAppDatabase;
Línea 24... Línea 40...
24
 
40
    boolean isSyncDevice = false;
25
 
41
    boolean isSyncToken = false;
-
 
42
    Preference preference;
-
 
43
 
-
 
44
 
-
 
45
    @Override
26
    private static final String TAG = "C2GS - FcmMessageServ";
46
    public void onCreate() {
27
 
47
 
Línea 28... Línea 48...
28
 
48
        preference = Preference.getInstance(getApplicationContext());
Línea -... Línea 49...
-
 
49
        preference.load();
29
    @Override
50
        mAppDatabase = DatabaseHelper.getInstance(getApplicationContext()).getAppDatabase();
Línea 30... Línea 51...
30
    public void onCreate() {
51
        super.onCreate();
31
        super.onCreate();
52
    }
Línea 32... Línea 53...
32
    }
53
 
33
 
54
    public void onMessageReceived(RemoteMessage remoteMessage) {
Línea 34... Línea 55...
34
    public void onMessageReceived(RemoteMessage remoteMessage) {
55
 
35
 
56
 
36
        Log.d("FcmMessagingService", "onMessageReceived");
57
        Log.d("FcmMessagingService", "onMessageReceived");
37
 
58
 
38
        if (remoteMessage.getData().size() > 0) {
59
        if (remoteMessage.getData().size() > 0) {
39
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
60
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
40
 
61
 
41
            if(remoteMessage.getData().containsKey("command")) {
62
            if(remoteMessage.getData().containsKey("command")) {
Línea 76... Línea 97...
76
            intent.putExtra("command", command);
97
            intent.putExtra("command", command);
77
            intent.putExtra("new_capsules", new_capsules);
98
            intent.putExtra("new_capsules", new_capsules);
Línea 78... Línea 99...
78
 
99
 
79
 
100
 
80
 
101
 
Línea -... Línea 102...
-
 
102
            sendBroadcast(intent);
81
            this.sendBroadcast(intent);
103
        }
82
        }
104
    }
-
 
105
 
83
    }
106
 
-
 
107
 
84
 
108
 
85
 
-
 
-
 
109
    public void onNewToken(String fcm_token)
86
 
110
    {
-
 
111
 
87
    public void onNewToken(String fcm_token)
112
        Log.e("FcmMessagingService", "onNewToken token = " + fcm_token);
Línea -... Línea 113...
-
 
113
 
88
    {
114
        String uuid = UniqueID.id(getApplicationContext());
Línea -... Línea 115...
-
 
115
 
-
 
116
        preference.setDeviceToken (fcm_token);
-
 
117
        preference.setDeviceUuid(uuid);
89
        Log.e("FcmMessagingService", "onNewToken token = " + fcm_token);
118
        preference.save();
-
 
119
 
90
        Preference preference = Preference.getInstance(getApplicationContext());
120
        Sync sync = new Sync(Constants.SYNC_ADAPTER_TYPE_DEVICE, uuid);
91
        preference.load();
121
        mAppDatabase.getSyncDao().insert(sync);
-
 
122
 
-
 
123
        sync = new Sync(Constants.SYNC_ADAPTER_TYPE_FCM, fcm_token);
-
 
124
        mAppDatabase.getSyncDao().insert(sync);
-
 
125
 
-
 
126
        syncTokenFMCTo2getSkillServer();
-
 
127
/*
-
 
128
            Intent intent = new Intent(Constants.BROADCAST_TYPE_TOKEN);
-
 
129
            intent.putExtra("token", Functions.trimNull(fcm_token));
-
 
130
            sendBroadcast(intent);
-
 
131
 */
-
 
132
 
-
 
133
    }
-
 
134
 
-
 
135
    private void syncTokenFMCTo2getSkillServer(){
-
 
136
 
-
 
137
 
-
 
138
        SyncDao syncDao = mAppDatabase.getSyncDao();
-
 
139
        List<Sync> records = syncDao.selectBatch();
-
 
140
 
-
 
141
        if(records.size() > 0) {
-
 
142
            syncToServer(records);
-
 
143
        }
-
 
144
}
-
 
145
 
-
 
146
    public void syncToServer(List<Sync> records) {
-
 
147
 
-
 
148
        for (Sync record : records) {
-
 
149
            if (record.getType() == Constants.SYNC_ADAPTER_TYPE_DEVICE && !isSyncDevice) {
-
 
150
 
-
 
151
 
-
 
152
                try {
-
 
153
                    Http http = new Http(this.getCacheDir());
-
 
154
                    OkHttpClient client = http.getHttpClient(false);
-
 
155
 
-
 
156
                    RequestBody formBody = new FormBody.Builder()
-
 
157
                            .add(Constants.POST_DEVICE_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID))
-
 
158
                            .add(Constants.POST_DEVICE_FIELD_DEVICE_UUID, preference.getDeviceUuid())
-
 
159
                            .add(Constants.POST_DEVICE_FIELD_MANUFACTURER, Build.MANUFACTURER)
-
 
160
                            .add(Constants.POST_DEVICE_FIELD_BRAND, Build.BRAND)
-
 
161
                            .add(Constants.POST_DEVICE_FIELD_VERSION, Build.VERSION.RELEASE + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName())
-
 
162
                            .add(Constants.POST_DEVICE_FIELD_MODEL, Build.MODEL)
-
 
163
                            .add(Constants.POST_DEVICE_FIELD_PLATFORM, "android")
-
 
164
                            .add(Constants.POST_DEVICE_FIELD_SYNC_ID, String.valueOf(record.getId()))
-
 
165
                            .build();
-
 
166
 
-
 
167
                    Log.d(TAG, "URL = " + Configuration.URL_DEVICE);
-
 
168
                    Request request = new Request.Builder()
-
 
169
                            .url(Configuration.URL_DEVICE)
-
 
170
                            .post(formBody)
-
 
171
                            .build();
-
 
172
 
-
 
173
                    Call call = client.newCall(request);
-
 
174
                    call.enqueue(new okhttp3.Callback() {
-
 
175
                        public void onResponse(Call call, Response response)
-
 
176
                                throws IOException {
-
 
177
                           // isSyncDevice = false;
-
 
178
                            Log.d(TAG, "Response Device :  " + response.body().toString());
-
 
179
 
-
 
180
                            processResponseSyncToServer(response.body().string(), "device");
-
 
181
                        }
-
 
182
 
-
 
183
                        public void onFailure(Call call, IOException e) {
-
 
184
                            isSyncDevice = false;
-
 
185
                            Log.d(TAG, "Error :  " + e.getMessage());
-
 
186
                        }
-
 
187
                    });
-
 
188
                } catch (Exception e) {
-
 
189
                }
-
 
190
            }
-
 
191
            if(record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM && !isSyncDevice){
-
 
192
 
-
 
193
                //Sucede que intenta sincronizar primero el Token antes que el device.
-
 
194
                Log.e("Esta intentando","sync el token antes que device");
-
 
195
            }
-
 
196
 
-
 
197
            if (record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM && !isSyncToken && isSyncDevice) {
-
 
198
                isSyncToken = true;
-
 
199
                Log.d(TAG, "FCM");
-
 
200
                Log.e("Token a Sync", "" + record.getData());
-
 
201
 
-
 
202
                try {
-
 
203
                    Http http = new Http(this.getCacheDir());
-
 
204
                    OkHttpClient client = http.getHttpClient(false);
-
 
205
 
-
 
206
                    RequestBody formBody = new FormBody.Builder()
-
 
207
                            .add(Constants.POST_FCM_FIELD_DEVICE_UUID, preference.getDeviceUuid())
-
 
208
                            .add(Constants.POST_FCM_FIELD_TOKEN, record.getData())
-
 
209
                            .add(Constants.POST_FCM_FIELD_SYNC_ID, String.valueOf(record.getId()))
-
 
210
                            .build();
-
 
211
 
-
 
212
                    Log.d(TAG, "URL = " + Configuration.URL_FCM);
-
 
213
                    Request request = new Request.Builder()
-
 
214
                            .url(Configuration.URL_FCM)
-
 
215
                            .post(formBody)
-
 
216
                            .build();
-
 
217
 
-
 
218
                    Call call = client.newCall(request);
-
 
219
                    call.enqueue(new okhttp3.Callback() {
-
 
220
                        public void onResponse(Call call, Response response)
-
 
221
                                throws IOException {
-
 
222
                            isSyncToken = false;
-
 
223
                            Log.e("Send FCM Token", "Procesando respuesta..");
-
 
224
                            processResponseSyncToServer(response.body().string(), "fcmtoken");
-
 
225
                        }
-
 
226
 
-
 
227
                        public void onFailure(Call call, IOException e) {
-
 
228
                            Log.d(TAG, "Error :  " + e.getMessage());
-
 
229
                            isSyncToken = false;
-
 
230
                        }
-
 
231
                    });
-
 
232
                } catch (Exception e) {
-
 
233
                    e.printStackTrace();
-
 
234
 
-
 
235
                }
-
 
236
            }
-
 
237
        }
-
 
238
    }
-
 
239
 
-
 
240
    private void processResponseSyncToServer(String dataString, String origen)
-
 
241
    {
-
 
242
        boolean success = false;
-
 
243
        long sync_id = 0;
-
 
244
 
-
 
245
        Log.d(TAG, "processResponseServer = " + dataString);
-
 
246
        try {
-
 
247
            JSONObject objJSON = new JSONObject(dataString);
-
 
248
            success = objJSON.has("success") ? objJSON.getBoolean("success")  : false;
-
 
249
            if(success  && objJSON.has("data")) {
-
 
250
                JSONObject jsonObjectData = objJSON.getJSONObject("data");
-
 
251
 
-
 
252
 
-
 
253
                if(jsonObjectData.has("sync_id")) {
-
 
254
                    sync_id = jsonObjectData.getLong("sync_id");
-
 
255
                }
-
 
256
 
-
 
257
            }
-
 
258
            Log.d(TAG, "SyncID = " + sync_id);
-
 
259
            if(success && sync_id > 0) {
-
 
260
                Log.d(TAG, "DELETE SYNC RECORD : " + sync_id);
92
        preference.setDeviceToken (fcm_token);
261
                mAppDatabase.getSyncDao().remove(sync_id);
93
        preference.save();
262
 
-
 
263
                if(origen.equals("device"))
-
 
264
                {
94
 
265
                    Log.e("Sync","device exitoso, ahora sigo con TokenFCM");
95
        /*
266
                    isSyncDevice=true;
96
 
267
                    syncTokenFMCTo2getSkillServer();
97
        try{
-
 
98
            Intent intent = new Intent(Constants.BROADCAST_TYPE_TOKEN);
268
                }
Línea 99... Línea 269...
99
            intent.putExtra("token", Functions.trimNull(fcm_token));
269
                if(origen.equals("fcmtoken")){
100
            //intent.putExtra("token", fcm_token);
270
                    Log.e("Token", "termino de sync con exito");