package com.home.shelly;
import android.content.Context;
import android.util.Log;
import com.digaus.capbackgroundfcm.BackgroundFCMData;
import com.digaus.capbackgroundfcm.BackgroundFCMRemoteMessage;
import com.digaus.capbackgroundfcm.BackgroundHandlerInterface;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class BackgroundFCMHandler implements BackgroundHandlerInterface {
private static String TAG = "BackgroundFCMHandler";
private Context context;
private JSONObject additionalData;
public void setContext(Context context){
this.context = context;
}
public void setAdditionalData(JSONObject additionalData){
this.additionalData = additionalData;
}
public BackgroundFCMData handleNotification(BackgroundFCMRemoteMessage remoteMessage) {
if (remoteMessage.getData() != null && remoteMessage.getData().getString("type") != null) {
if (remoteMessage.getData().getString("type").equals("device-update2")) {
return this.handleDeviceUpdate(remoteMessage, this.additionalData);
} else if (remoteMessage.getData().getString("type").equals("app-update2")) {
return this.handleAppUpdate(remoteMessage, this.additionalData);
}
}
return null;
}
private BackgroundFCMData handleAppUpdate(BackgroundFCMRemoteMessage remoteMessage, JSONObject obj) {
try {
JSONObject translations = obj.getJSONObject("translations");
String title = translations.getString("app.shelly-home.app-update.update.label");
String body = translations.getString("app.shelly-home.app-update.update-installed.label");
return new BackgroundFCMData(title, body);
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
return null;
}
private BackgroundFCMData handleDeviceUpdate(BackgroundFCMRemoteMessage remoteMessage, JSONObject obj) {
try {
JSONObject translations = obj.getJSONObject("translations");
List<String> filteredDevices = this.checkFirmware(obj);
if (filteredDevices.size() > 0) {
String title = translations.getString("app.shelly-home.device-update.update.label");
String body = translations.getString("app.shelly-home.device-update.available.label").replace("{{count}}", filteredDevices.size() + "");
return new BackgroundFCMData(title, body);
}
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
return null;
}
private List<String> checkFirmware(JSONObject obj) {
return filteredDevices;
}
private JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException {
HttpURLConnection urlConnection;
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000 );
urlConnection.setConnectTimeout(5000 );
urlConnection.setDoOutput(true);
urlConnection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
String jsonString = sb.toString();
System.out.println("JSON: " + jsonString);
return new JSONObject(jsonString);
}
}