2022-03-24 09:18:49 +00:00
|
|
|
package com.safemobile.safedispatch;
|
2022-03-10 14:31:03 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import com.safemobile.adapters.AlertGridViewAdapter;
|
|
|
|
import com.safemobile.adapters.AlertGridViewAdapter.ViewHolder;
|
|
|
|
|
|
|
|
import com.safemobile.lib.Alarm;
|
|
|
|
import com.safemobile.lib.AppParams;
|
|
|
|
import com.safemobile.lib.OperationCodes;
|
|
|
|
import com.safemobile.lib.SM;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
2022-03-18 16:43:48 +00:00
|
|
|
import android.os.Looper;
|
2022-03-10 14:31:03 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
|
|
|
import android.widget.GridView;
|
|
|
|
|
|
|
|
public class AlarmActivity extends Activity {
|
|
|
|
private Context context;
|
2022-03-18 16:43:48 +00:00
|
|
|
private TabLayoutActivity parentTab;
|
|
|
|
private ArrayList<Alarm> allAlarms = new ArrayList<>();
|
|
|
|
private ArrayList<Boolean> acknowledged = new ArrayList<>();
|
2022-03-10 14:31:03 +00:00
|
|
|
private AlertGridViewAdapter adapter;
|
2022-03-18 16:43:48 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
/* Visual resources */
|
|
|
|
private GridView gridView;
|
2022-03-18 16:43:48 +00:00
|
|
|
private View convertViewAlarm;
|
|
|
|
|
|
|
|
private int ackPosition;
|
|
|
|
private Bundle savedInstanceState;
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// Need handler for callbacks to the UI thread
|
2022-03-18 16:43:48 +00:00
|
|
|
private final Handler myHandler = new Handler(Looper.getMainLooper());
|
|
|
|
|
|
|
|
private static final String ALARM = "alarm";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the activity is first created.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
this.setSavedInstanceState(savedInstanceState);
|
|
|
|
// get parentTab
|
|
|
|
setParentTab((TabLayoutActivity) getParent());
|
|
|
|
|
|
|
|
Locale locale = new Locale(AppParams.LANGUAGETMP);
|
|
|
|
Locale.setDefault(locale);
|
|
|
|
Configuration config = new Configuration();
|
|
|
|
config.locale = locale;
|
|
|
|
getBaseContext().getResources().updateConfiguration(config,
|
|
|
|
getBaseContext().getResources().getDisplayMetrics());
|
|
|
|
|
|
|
|
context = this;
|
|
|
|
setContentView(R.layout.tabalarm);
|
|
|
|
|
|
|
|
gridView = findViewById(R.id.gridViewAlarms);
|
|
|
|
adapter = new AlertGridViewAdapter(this, getAllAlarms(), context, acknowledged);
|
|
|
|
gridView.setAdapter(adapter);
|
|
|
|
|
|
|
|
gridView.setOnItemClickListener(onItemClickListener);
|
|
|
|
|
|
|
|
// change tab header fontFace
|
|
|
|
TextView textView1 = findViewById(R.id.textView1);
|
|
|
|
textView1.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf"));
|
|
|
|
textView1.setTextSize(24);
|
|
|
|
|
2022-03-23 11:07:40 +00:00
|
|
|
getParentTab().setAlarmActivity(this);
|
2022-03-18 16:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setMessage(getString(R.string.exit))
|
|
|
|
.setCancelable(false)
|
|
|
|
.setNeutralButton(getString(R.string.logout), (dialog, id) -> getParentTab().whenBackPressed(AppParams.ActivityResult.logout))
|
|
|
|
.setPositiveButton(getString(R.string.ext), (dialog, id) -> getParentTab().whenBackPressed(AppParams.ActivityResult.exit))
|
|
|
|
.setNegativeButton(getString(R.string.cancel), (dialog, id) -> dialog.cancel());
|
|
|
|
AlertDialog alert = builder.create();
|
|
|
|
alert.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
SM.Debug("onPause");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (!AppParams.DEMO)
|
|
|
|
getAlarms();
|
|
|
|
else {
|
|
|
|
for (int i = getAllAlarms().size() - 1; i >= 0; i--) {
|
|
|
|
Alarm alarm = getAllAlarms().get(i);
|
|
|
|
if (alarm.ack == 1)
|
|
|
|
getAllAlarms().remove(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
acknowledged = new ArrayList<>();
|
|
|
|
|
|
|
|
for (int i = 0; i < getAllAlarms().size(); i++)
|
|
|
|
acknowledged.add(false);
|
|
|
|
updateResultsInUi(ALARM);
|
|
|
|
}
|
|
|
|
SM.Debug("onResume");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private final OnItemClickListener onItemClickListener = new OnItemClickListener() {
|
2022-03-10 14:31:03 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
|
2022-03-18 16:43:48 +00:00
|
|
|
long arg3) {
|
2022-03-10 14:31:03 +00:00
|
|
|
SM.Debug(" ### CLICK Alarm : " + position);
|
2022-03-18 16:43:48 +00:00
|
|
|
setConvertViewAlarm(arg1);
|
2022-03-10 14:31:03 +00:00
|
|
|
// save position
|
2022-03-18 16:43:48 +00:00
|
|
|
ackPosition = position;
|
|
|
|
if (!AppParams.DEMO)
|
|
|
|
setACK(getAllAlarms().get(position).idx, getAllAlarms().get(position).type, getAllAlarms().get(position).unitName);
|
|
|
|
else {
|
|
|
|
getAllAlarms().get(position).ack = 1;
|
2022-03-10 14:31:03 +00:00
|
|
|
updateResultsInUi("else");
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 16:43:48 +00:00
|
|
|
|
|
|
|
// send ACK to AppServer
|
|
|
|
private void setACK(int idx, int type, String unitName) {
|
|
|
|
getParentTab().executeNetworkStuff(new String[]{OperationCodes.SendAlarmAcknoledge + "", idx + "", type + "", unitName});
|
|
|
|
}
|
2022-03-10 14:31:03 +00:00
|
|
|
};
|
2022-03-18 16:43:48 +00:00
|
|
|
|
|
|
|
// Update Alarms received from AppServer
|
|
|
|
public void updateAlarms(ArrayList<Alarm> list) {
|
|
|
|
SM.Debug("## updateAlarms: " + list.size());
|
|
|
|
setAllAlarms(list);
|
|
|
|
acknowledged = new ArrayList<>();
|
2022-03-10 14:31:03 +00:00
|
|
|
// set acknowledged based on alarm.ack value
|
2022-03-18 16:43:48 +00:00
|
|
|
|
|
|
|
for (Alarm alarm : getAllAlarms()) {
|
|
|
|
acknowledged.add(alarm.ack == 1);
|
|
|
|
SM.Debug("ALARM", "SC_ID> " + alarm.sc_id + " | "
|
|
|
|
+ (getParentTab().getVehHashByScId().get(alarm.sc_id) == null ? "null" : getParentTab().getVehHashByScId().get(alarm.sc_id).name));
|
|
|
|
|
|
|
|
|
|
|
|
if (getParentTab().getVehHashByScId().get(alarm.sc_id) != null)
|
|
|
|
alarm.unitName = getParentTab().getVehHashByScId().get(alarm.sc_id).name;
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 16:43:48 +00:00
|
|
|
myHandler.post(updateResultsRUN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create runnable for posting
|
|
|
|
final Runnable updateResultsRUN = () -> updateResultsInUi(ALARM);
|
|
|
|
|
|
|
|
private void updateResultsInUi(String param) {
|
|
|
|
if (param.equals(ALARM)) {
|
|
|
|
// set adapter
|
|
|
|
adapter = new AlertGridViewAdapter(this, getAllAlarms(), context, acknowledged);
|
|
|
|
gridView.setAdapter(adapter);
|
|
|
|
} else {
|
|
|
|
adapter.changeACK(ackPosition);
|
|
|
|
SM.Debug("Set ACK: " + ackPosition + " | " + (Boolean.TRUE.equals(acknowledged.get(ackPosition)) ? "true" : "false"));
|
|
|
|
|
|
|
|
ViewHolder viewAlarm;
|
|
|
|
viewAlarm = (ViewHolder) getConvertViewAlarm().getTag();
|
|
|
|
|
|
|
|
switch (Boolean.TRUE.equals(acknowledged.get(ackPosition)) ? 1 : 0) {
|
2022-03-10 14:31:03 +00:00
|
|
|
case 1:
|
|
|
|
viewAlarm.imageViewAlert.setImageResource(R.drawable.alert_off);
|
|
|
|
break;
|
|
|
|
case 0:
|
2022-03-18 16:43:48 +00:00
|
|
|
viewAlarm.imageViewAlert.setImageResource(R.drawable.siren);
|
2022-03-10 14:31:03 +00:00
|
|
|
break;
|
2022-03-18 16:43:48 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalStateException("Unexpected value: " + (Boolean.TRUE.equals(acknowledged.get(ackPosition)) ? 1 : 0));
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 16:43:48 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// update ACK received from AppServer
|
2022-03-18 16:43:48 +00:00
|
|
|
public void updateACK() {
|
|
|
|
myHandler.post(updateAckRUN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create runnable for posting
|
|
|
|
final Runnable updateAckRUN = () -> {
|
|
|
|
SM.Debug(" updateACK: ");
|
|
|
|
updateResultsInUi("adapter");
|
|
|
|
};
|
|
|
|
|
|
|
|
// send to AppServer
|
|
|
|
private void getAlarms() {
|
|
|
|
getParentTab().executeNetworkStuff(new String[]{OperationCodes.GetAlarms + "", AppParams.USERID + ""});
|
|
|
|
}
|
|
|
|
|
|
|
|
public TabLayoutActivity getParentTab() {
|
|
|
|
return parentTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setParentTab(TabLayoutActivity parentTab) {
|
|
|
|
this.parentTab = parentTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<Alarm> getAllAlarms() {
|
|
|
|
return allAlarms;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAllAlarms(ArrayList<Alarm> allAlarms) {
|
|
|
|
this.allAlarms = allAlarms;
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 16:43:48 +00:00
|
|
|
|
|
|
|
public View getConvertViewAlarm() {
|
|
|
|
return convertViewAlarm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConvertViewAlarm(View convertViewAlarm) {
|
|
|
|
this.convertViewAlarm = convertViewAlarm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bundle getSavedInstanceState() {
|
|
|
|
return savedInstanceState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSavedInstanceState(Bundle savedInstanceState) {
|
|
|
|
this.savedInstanceState = savedInstanceState;
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
|
|
|
}
|