2022-03-24 09:18:49 +00:00
|
|
|
package com.safemobile.safedispatch;
|
2022-03-10 14:31:03 +00:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
2022-03-18 06:40:05 +00:00
|
|
|
import android.os.Looper;
|
2022-03-10 14:31:03 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
2022-03-14 09:53:00 +00:00
|
|
|
import android.widget.GridView;
|
2022-03-10 14:31:03 +00:00
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
import com.safemobile.adapters.ConversationGridViewAdapter;
|
|
|
|
import com.safemobile.adapters.MessagesGridViewAdapter;
|
|
|
|
import com.safemobile.lib.AppParams;
|
|
|
|
import com.safemobile.lib.Msg;
|
|
|
|
import com.safemobile.lib.OperationCodes;
|
|
|
|
import com.safemobile.lib.SM;
|
|
|
|
import com.safemobile.lib.SMS;
|
|
|
|
import com.safemobile.lib.Vehicle;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.Locale;
|
2022-03-18 06:40:05 +00:00
|
|
|
import java.util.Objects;
|
2022-03-14 09:53:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* fix import
|
|
|
|
*/
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
public class MessagesActivity extends Activity {
|
2022-03-18 06:40:05 +00:00
|
|
|
/**
|
|
|
|
* Called when the activity is first created.
|
|
|
|
*/
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
private MessagesGridViewAdapter mAdapter;
|
|
|
|
private ConversationGridViewAdapter convAdapter;
|
2022-03-18 06:40:05 +00:00
|
|
|
private ArrayList<Msg> listLastMessages = new ArrayList<>();
|
|
|
|
private ArrayList<Msg> listSpecificConversation = new ArrayList<>();
|
|
|
|
private ArrayList<Boolean> dispatcherPositions = new ArrayList<>();
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
private GridView gridView;
|
|
|
|
private Context context;
|
|
|
|
private Activity activity;
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
private final Hashtable<Long, ArrayList<SMS>> tableSMS = new Hashtable<>();
|
|
|
|
private final Hashtable<String, Long> seqIDSMSHash = new Hashtable<>();
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// Need handler for callbacks to the UI thread
|
2022-03-18 06:40:05 +00:00
|
|
|
private final Handler myHandler = new Handler(Looper.getMainLooper());
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
/* Visual resources */
|
2022-03-18 06:40:05 +00:00
|
|
|
private LinearLayout layoutSend;
|
|
|
|
private LinearLayout layoutHeader;
|
|
|
|
private LinearLayout layoutHeaderConversation;
|
2022-03-10 14:31:03 +00:00
|
|
|
private TextView textViewSelectedContact;
|
2022-03-18 06:40:05 +00:00
|
|
|
private ImageView imageViewSelectedContact;
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
private Button imageButtonSend;
|
|
|
|
private EditText editTextMsg;
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
private TabLayoutActivity parentTab;
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
/* Message args */
|
2022-03-18 06:40:05 +00:00
|
|
|
private int ACTION;
|
|
|
|
private static final int MSG_UPDATE = 0;
|
|
|
|
|
|
|
|
private ArrayList<String> allVehicleNames = new ArrayList<>();
|
|
|
|
private ArrayList<Vehicle> allVehicle = new ArrayList<>();
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// tip of Messages and flag first load
|
|
|
|
public boolean LASTMESSAGES = true;
|
2022-03-18 06:40:05 +00:00
|
|
|
private boolean FIRST = true;
|
|
|
|
|
|
|
|
// store selected scId and selected unitType
|
|
|
|
private long scId = 0;
|
|
|
|
private int unitType = 0;
|
|
|
|
|
|
|
|
private Bundle savedInstanceState;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
this.setSavedInstanceState(savedInstanceState);
|
|
|
|
|
|
|
|
// get parentTab
|
|
|
|
parentTab = (TabLayoutActivity) getParent();
|
|
|
|
|
|
|
|
context = this;
|
|
|
|
activity = this;
|
|
|
|
|
|
|
|
Locale locale = new Locale(AppParams.LANGUAGETMP);
|
|
|
|
Locale.setDefault(locale);
|
|
|
|
Configuration config = new Configuration();
|
|
|
|
config.locale = locale;
|
|
|
|
getBaseContext().getResources().updateConfiguration(config,
|
|
|
|
getBaseContext().getResources().getDisplayMetrics());
|
|
|
|
|
|
|
|
setContentView(R.layout.tabtext);
|
|
|
|
|
|
|
|
// get LayoutSend and hide
|
|
|
|
layoutSend = findViewById(R.id.layoutSendMsg);
|
|
|
|
layoutSend.setVisibility(View.GONE);
|
|
|
|
// get header layout
|
|
|
|
layoutHeader = findViewById(R.id.layoutHeader);
|
|
|
|
layoutHeaderConversation = findViewById(R.id.layoutHeaderConversation);
|
|
|
|
// show only header layout
|
|
|
|
layoutHeaderConversation.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
// get SelectedContact TextView and ImageView
|
|
|
|
textViewSelectedContact = findViewById(R.id.textViewSelectedContact);
|
|
|
|
imageViewSelectedContact = findViewById(R.id.imageViewSelectedContact);
|
|
|
|
|
|
|
|
ImageView imageBarcode = findViewById(R.id.imageBarcode);
|
|
|
|
imageBarcode.setOnClickListener(v -> {
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
|
|
|
|
startActivityForResult(intent, 0);
|
|
|
|
} catch (Exception e) {
|
|
|
|
showErrorDialog(getResources().getString(R.string.barcodeError));
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
|
|
|
});
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
// change tab header fontFace
|
|
|
|
TextView textView1 = findViewById(R.id.textView1);
|
|
|
|
textView1.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf"));
|
|
|
|
textView1.setTextSize(24);
|
|
|
|
|
|
|
|
// prepared arraylist and passed it to the Adapter class
|
|
|
|
mAdapter = new MessagesGridViewAdapter(this, listLastMessages);
|
|
|
|
convAdapter = new ConversationGridViewAdapter(this, listSpecificConversation, dispatcherPositions, new ArrayList<>());
|
|
|
|
// Set custom adapter to gridview
|
|
|
|
gridView = findViewById(R.id.gridView1);
|
|
|
|
gridView.setAdapter(mAdapter);
|
|
|
|
|
|
|
|
// Implement On Item click listener
|
|
|
|
gridView.setOnItemClickListener((arg0, arg1, position, arg3) -> {
|
|
|
|
if (LASTMESSAGES) {
|
|
|
|
// get position
|
|
|
|
Msg item = mAdapter.getItem(position);
|
|
|
|
// get Specific Conversation
|
|
|
|
setScId(item.from.sc_id);
|
|
|
|
SM.Debug("Selected scId: " + getScId());
|
|
|
|
// set Conversation type
|
|
|
|
LASTMESSAGES = false;
|
|
|
|
// call get SMS
|
|
|
|
getSMS4unit(getScId());
|
|
|
|
// create crt_Vehicle
|
|
|
|
setUnitType((int) item.from.driver_id);
|
|
|
|
// save selected vehicle
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
if (AppParams.DEMO) {
|
|
|
|
// modify UI
|
|
|
|
ACTION = MSG_UPDATE;
|
|
|
|
updateResultsInUi();
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
|
|
|
});
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
/* Buttons and EditBoxes */
|
|
|
|
ImageButton imageButtonAdd = findViewById(R.id.imageButtonAdd);
|
|
|
|
imageButtonAdd.setOnClickListener(v -> showDialog());
|
|
|
|
|
|
|
|
// button send message
|
|
|
|
imageButtonSend = findViewById(R.id.imageButtonSend);
|
|
|
|
imageButtonSend.setOnClickListener(v -> {
|
|
|
|
sendSMS(getScId(), editTextMsg.getText().toString());
|
|
|
|
Toast.makeText(context, "Sending message...", Toast.LENGTH_SHORT).show();
|
|
|
|
// disable send button and editBox
|
|
|
|
editTextMsg.setEnabled(false);
|
|
|
|
imageButtonSend.setEnabled(false);
|
|
|
|
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
myHandler.post(enableButtonRUN);
|
|
|
|
|
|
|
|
if (AppParams.DEMO) {
|
|
|
|
Thread.sleep(5500);
|
|
|
|
myHandler.post(demoReceivedSMSRUN);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
|
|
|
}).start();
|
2022-03-10 14:31:03 +00:00
|
|
|
});
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
// button back to conversations
|
|
|
|
ImageButton imageButtonBack = findViewById(R.id.imageButtonBack);
|
|
|
|
imageButtonBack.setOnClickListener(v -> {
|
|
|
|
// set LastMessage conversation type
|
|
|
|
LASTMESSAGES = true;
|
|
|
|
// refresh Grid
|
|
|
|
getLastSMS();
|
|
|
|
if (AppParams.DEMO)
|
|
|
|
updateResultsInUi();
|
2022-03-10 14:31:03 +00:00
|
|
|
});
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
// get Message editText
|
|
|
|
editTextMsg = findViewById(R.id.editTextMsg);
|
|
|
|
|
|
|
|
gridView.setId(1); // id needed for IconContextMenu
|
|
|
|
registerForContextMenu(gridView);
|
2022-03-23 11:07:40 +00:00
|
|
|
parentTab.setMessageActivity(this);
|
2022-03-18 06:40:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// Create runnable for posting
|
2022-03-18 06:40:05 +00:00
|
|
|
final Runnable demoReceivedSMSRUN = new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
parentTab.setImei(getScId() + "");
|
2022-03-10 14:31:03 +00:00
|
|
|
parentTab.updateResultsInUi("realpha");
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
int timeGMT = (int) (System.currentTimeMillis() / 1000L);
|
|
|
|
String sendSMSSeqID = "1." + timeGMT;
|
|
|
|
// add mess to not ack list
|
|
|
|
seqIDSMSHash.put(sendSMSSeqID, getScId());
|
|
|
|
// get scId conversation and add message
|
|
|
|
ArrayList<SMS> crtSmsList = tableSMS.get(getScId());
|
|
|
|
SMS sms = new SMS(0, 0, timeGMT, "i got your sms", 0, getScId());
|
|
|
|
sms.seq_idx = sendSMSSeqID;
|
|
|
|
if (crtSmsList != null)
|
|
|
|
crtSmsList.add(sms);
|
|
|
|
|
|
|
|
// add message to listLast
|
|
|
|
boolean exists = false;
|
|
|
|
for (Msg msg : listLastMessages)
|
|
|
|
// if conversation exists in lastMessages
|
|
|
|
if (msg.from.sc_id == getScId()) {
|
|
|
|
exists = true;
|
|
|
|
msg.message = "i got your sms";
|
|
|
|
msg.received = Calendar.getInstance().getTime();
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
// if last messages doesn't contain this conversation
|
|
|
|
if (!exists) {
|
|
|
|
Vehicle sentVehicle = null;
|
|
|
|
for (Vehicle veh : getAllVehicle())
|
|
|
|
if (veh.sc_id == getScId())
|
|
|
|
sentVehicle = veh;
|
|
|
|
|
|
|
|
listLastMessages.add(new Msg(sentVehicle, "i got your sms", Calendar.getInstance().getTime(), sendSMSSeqID));
|
|
|
|
}
|
|
|
|
SM.Debug("time: " + timeGMT + " | " + Calendar.getInstance().getTime());
|
|
|
|
|
|
|
|
updateResultsInUi();
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +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) -> parentTab.whenBackPressed(AppParams.ActivityResult.logout))
|
|
|
|
.setPositiveButton(getString(R.string.ext), (dialog, id) -> parentTab.whenBackPressed(AppParams.ActivityResult.exit))
|
|
|
|
.setNegativeButton(getString(R.string.cancel), (dialog, id) -> dialog.cancel());
|
|
|
|
AlertDialog alert = builder.create();
|
|
|
|
alert.show();
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
SM.Debug("onPause");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (parentTab.getTCPState() != null && FIRST) {
|
|
|
|
updateVehicles(parentTab.getAllVehicle());
|
|
|
|
FIRST = false;
|
|
|
|
if (LASTMESSAGES)
|
|
|
|
parentTab.executeNetworkStuff(new String[]{OperationCodes.GetLastSMS + "", AppParams.USERID + ""});
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
SM.Debug("onResume");
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
if (requestCode == 0 && resultCode == RESULT_OK) {
|
|
|
|
String contents = intent.getStringExtra("SCAN_RESULT");
|
|
|
|
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
|
|
|
|
editTextMsg.setText(String.format("%s%s", editTextMsg.getText().toString(), contents));
|
|
|
|
// Handle successful scan
|
|
|
|
Toast.makeText(this, getResources().getString(R.string.barcodeContent) + ":" + contents + " with format:" + format, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request SMS 4 Unit from AppServer
|
|
|
|
private void getSMS4UnitWithTime(long scId, long time) {
|
|
|
|
if (!AppParams.DEMO) {
|
|
|
|
parentTab.executeNetworkStuff(new String[]{OperationCodes.GetRecentSMSs + "", scId + "", time + ""});
|
|
|
|
|
|
|
|
SM.Debug(" #### GetSMS4uni:" + scId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getSMS4unit(long scId) {
|
|
|
|
if (!AppParams.DEMO) {
|
|
|
|
SM.Debug("getSMS4unit : " + scId);
|
|
|
|
ArrayList<SMS> listSMS = tableSMS.get(scId);
|
|
|
|
long timeGMT = 0;
|
|
|
|
if (listSMS != null && !listSMS.isEmpty())
|
|
|
|
timeGMT = listSMS.get(listSMS.size() - 1).timeGMT;
|
|
|
|
|
|
|
|
// error in DB
|
|
|
|
if (String.valueOf(timeGMT).contains(".")) {
|
|
|
|
try {
|
|
|
|
// remove the dot and the parse it to String
|
|
|
|
timeGMT = Long.parseLong(String.valueOf(timeGMT).split("\\.")[1]);
|
|
|
|
} catch (Exception e) {
|
|
|
|
timeGMT = Long.parseLong(String.valueOf(timeGMT).replace(".", ""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parentTab.executeNetworkStuff(new String[]{OperationCodes.GetRecentSMSs + "", scId + "", timeGMT + ""});
|
|
|
|
|
|
|
|
SM.Debug(" #### GetSMS4uni:" + scId);
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request Last SMS from AppServer
|
|
|
|
public void getLastSMS() {
|
|
|
|
if (!AppParams.DEMO) {
|
|
|
|
if (LASTMESSAGES)
|
|
|
|
parentTab.executeNetworkStuff(new String[]{OperationCodes.GetLastSMS + "", AppParams.USERID + ""});
|
|
|
|
else {
|
|
|
|
ArrayList<SMS> listSMS = tableSMS.get(getScId());
|
|
|
|
long timeGMT = 0;
|
|
|
|
if (listSMS != null && !listSMS.isEmpty())
|
|
|
|
timeGMT = listSMS.get(listSMS.size() - 1).timeGMT;
|
|
|
|
parentTab.executeNetworkStuff(new String[]{OperationCodes.GetRecentSMSs + "", getScId() + "", timeGMT + ""});
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
updateResultsInUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void sendSMS(long scIdd, String txt) {
|
|
|
|
if (!AppParams.DEMO) {
|
|
|
|
int timeGMT = (int) (System.currentTimeMillis() / 1000L);
|
|
|
|
String sendSMSSeqID = "1." + timeGMT;
|
|
|
|
// add mess to not ack list
|
|
|
|
seqIDSMSHash.put(sendSMSSeqID, scIdd);
|
|
|
|
// get scId conversation and add message
|
|
|
|
SMS sms = new SMS(0, 0, timeGMT, txt, scIdd, 0);
|
|
|
|
sms.seq_idx = sendSMSSeqID;
|
|
|
|
tableSMS.get(scIdd).add(sms);
|
|
|
|
parentTab.executeNetworkStuff(new String[]{OperationCodes.SEND_TM + "", sendSMSSeqID, scIdd + "", txt});
|
|
|
|
|
|
|
|
updateResultsInUi();
|
|
|
|
} else {
|
2022-03-10 14:31:03 +00:00
|
|
|
int timeGMT = (int) (System.currentTimeMillis() / 1000L);
|
2022-03-18 06:40:05 +00:00
|
|
|
String sendSMSSeqID = "1." + timeGMT;
|
|
|
|
// add mess to not ack list
|
|
|
|
seqIDSMSHash.put(sendSMSSeqID, scIdd);
|
|
|
|
// get scId conversation and add message
|
|
|
|
ArrayList<SMS> crtSmsList = tableSMS.get(scIdd);
|
|
|
|
SMS sms = new SMS(0, 0, timeGMT, txt, scIdd, 0);
|
|
|
|
sms.seq_idx = sendSMSSeqID;
|
|
|
|
crtSmsList.add(sms);
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// add message to listLast
|
|
|
|
boolean exists = false;
|
|
|
|
for (Msg msg : listLastMessages)
|
|
|
|
// if conversation exists in lastMessages
|
2022-03-18 06:40:05 +00:00
|
|
|
if (msg.from.sc_id == scIdd) {
|
2022-03-10 14:31:03 +00:00
|
|
|
exists = true;
|
|
|
|
msg.message = txt;
|
|
|
|
msg.received = Calendar.getInstance().getTime();
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// if last messages doesn't contain this conversation
|
2022-03-18 06:40:05 +00:00
|
|
|
if (!exists) {
|
2022-03-10 14:31:03 +00:00
|
|
|
Vehicle sentVehicle = null;
|
2022-03-18 06:40:05 +00:00
|
|
|
for (Vehicle veh : getAllVehicle())
|
|
|
|
if (veh.sc_id == scIdd)
|
2022-03-10 14:31:03 +00:00
|
|
|
sentVehicle = veh;
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
listLastMessages.add(new Msg(sentVehicle, txt, Calendar.getInstance().getTime(), sendSMSSeqID));
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
SM.Debug("time: " + timeGMT + " | " + Calendar.getInstance().getTime());
|
2022-03-10 14:31:03 +00:00
|
|
|
LASTMESSAGES = false;
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
updateResultsInUi();
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// Update Vehicles received from AppServer
|
2022-03-18 06:40:05 +00:00
|
|
|
public void updateVehicles(ArrayList<Vehicle> list) {
|
|
|
|
SM.Debug("## updateVehicles: " + list.size());
|
|
|
|
setAllVehicle(list);
|
|
|
|
allVehicleNames = new ArrayList<>();
|
|
|
|
for (Vehicle v : getAllVehicle())
|
|
|
|
allVehicleNames.add(v.name);
|
|
|
|
|
|
|
|
if (tableSMS.size() == 0) {
|
|
|
|
for (Vehicle veh : getAllVehicle()) {
|
|
|
|
ArrayList<SMS> smsList = new ArrayList<>();
|
2022-03-10 14:31:03 +00:00
|
|
|
// populate conversation list if demo
|
2022-03-18 06:40:05 +00:00
|
|
|
if (AppParams.DEMO)
|
|
|
|
smsList = getDemoConversation(veh.sc_id);
|
|
|
|
|
|
|
|
tableSMS.put(veh.sc_id, smsList);
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// gets DEMO conversations
|
2022-03-18 06:40:05 +00:00
|
|
|
private ArrayList<SMS> getDemoConversation(long scId) {
|
|
|
|
ArrayList<SMS> smsList = new ArrayList<>();
|
|
|
|
if (scId == 101)
|
|
|
|
smsList.add(new SMS(1, 2, 1324016412, "Only one left", 101, 0));
|
|
|
|
else if (scId == 102)
|
|
|
|
smsList.add(new SMS(1, 2, 1328060100, "i'm at the train station", 0, 102));
|
|
|
|
else if (scId == 103) {
|
|
|
|
smsList.add(new SMS(1, 2, 1121016637, "Where are you now?", 103, 0));
|
|
|
|
smsList.add(new SMS(2, 2, 1121016693, "Near Elementary School 81", 0, 103));
|
|
|
|
smsList.add(new SMS(3, 2, 1121016693, "We have a client on Belmont Ave", 103, 0));
|
|
|
|
smsList.add(new SMS(4, 2, 1121016724, "It's only 4 blocks away", 103, 0));
|
|
|
|
smsList.add(new SMS(5, 2, 1121016693, "Can you take him?", 103, 0));
|
|
|
|
smsList.add(new SMS(6, 2, 1121016818, "I'll be right there", 0, 103));
|
|
|
|
} else if (scId == 105)
|
|
|
|
smsList.add(new SMS(1, 2, 1328061660, "Thanks", 0, 105));
|
|
|
|
return smsList;
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
public void updateSMS(ArrayList<SMS> list) {
|
|
|
|
SM.Debug("## updateSMS: " + list.size() + " [LASTMESSAGES:" + LASTMESSAGES + "]");
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// populate listLastMessages
|
2022-03-18 06:40:05 +00:00
|
|
|
if (LASTMESSAGES) {
|
|
|
|
listLastMessages = new ArrayList<>();
|
|
|
|
for (SMS sms : list) {
|
2022-03-10 14:31:03 +00:00
|
|
|
// get for sender
|
2022-03-18 06:40:05 +00:00
|
|
|
long senderId = sms.sc_id_dest;
|
|
|
|
if (senderId == 0)
|
|
|
|
senderId = sms.sc_id_sour;
|
2022-03-10 14:31:03 +00:00
|
|
|
// get vehicle with sender id
|
2022-03-18 06:40:05 +00:00
|
|
|
Vehicle messageVeh;
|
|
|
|
messageVeh = getVehicleById(senderId);
|
|
|
|
listLastMessages.add(0, new Msg(messageVeh, sms.mess, new Date((long) sms.timeGMT * 1000), sms.seq_idx));
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// update GridView
|
2022-03-18 06:40:05 +00:00
|
|
|
mAdapter = new MessagesGridViewAdapter(activity, listLastMessages);
|
|
|
|
} else {
|
|
|
|
// add new values to hashTable for key = scId
|
|
|
|
ArrayList<SMS> listScId = tableSMS.get(getScId());
|
|
|
|
if (!list.isEmpty())
|
|
|
|
for (SMS sms : list) {
|
2022-03-10 14:31:03 +00:00
|
|
|
// compare last item in hashList with first elem in arrived list
|
|
|
|
// protect is listSc_Id size = 0
|
2022-03-18 06:40:05 +00:00
|
|
|
if (listScId != null && (listScId.isEmpty()
|
|
|
|
|| (sms.sc_id_dest != listScId.get(listScId.size() - 1).sc_id_dest
|
|
|
|
|| sms.sc_id_sour != listScId.get(listScId.size() - 1).sc_id_sour
|
|
|
|
|| !Objects.equals(sms.mess, listScId.get(listScId.size() - 1).mess))))
|
2022-03-10 14:31:03 +00:00
|
|
|
listScId.add(sms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// modify UI
|
2022-03-18 06:40:05 +00:00
|
|
|
ACTION = MSG_UPDATE;
|
|
|
|
myHandler.post(this::updateResultsInUi);
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// Confirmation for sending message
|
2022-03-18 06:40:05 +00:00
|
|
|
public void confirmSMS(String data, final String seqID) {
|
|
|
|
SM.Debug("confirmSMS");
|
|
|
|
if (data.equals("0")) {
|
2022-03-10 14:31:03 +00:00
|
|
|
SM.Debug("Error on sending SMS");
|
|
|
|
showErrorDialog("Error on sending message.");
|
2022-03-18 06:40:05 +00:00
|
|
|
} else {
|
2022-03-10 14:31:03 +00:00
|
|
|
SM.Debug("ACK received for text message");
|
2022-03-18 06:40:05 +00:00
|
|
|
//get scId for crt message
|
|
|
|
long scId = seqIDSMSHash.get(seqID);
|
|
|
|
// get SMS list for crt scId
|
|
|
|
ArrayList<SMS> crtSMSList = tableSMS.get(scId);
|
|
|
|
if (crtSMSList != null) {
|
|
|
|
for (SMS sms : crtSMSList) {
|
|
|
|
|
|
|
|
if (sms.seq_idx.equals(seqID)) {
|
|
|
|
SM.Debug("######### AM SCHIMBAT STATUSUL pt: " + sms.mess + " | " + scId + " | " + seqID + " || " + sms.seq_idx);
|
|
|
|
// set ACK
|
|
|
|
sms.status = 2;
|
|
|
|
}
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove message from seqIDSMS Hash = don't wait confirmation for it
|
|
|
|
seqIDSMSHash.remove(seqID);
|
|
|
|
|
|
|
|
// store sms seqId to set to ACK in adapter
|
2022-03-18 06:40:05 +00:00
|
|
|
myHandler.post(() -> {
|
|
|
|
convAdapter.setACK(seqID);
|
|
|
|
convAdapter.changeView(seqID);
|
|
|
|
convAdapter.notifyDataSetChanged();
|
2022-03-10 14:31:03 +00:00
|
|
|
});
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
myHandler.post(() -> editTextMsg.setText(""));
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void newSMS(String imei, String message, final long time) {
|
|
|
|
SM.Debug(" ## newSMS: " + imei + " | " + message);
|
|
|
|
if (!LASTMESSAGES) {
|
2022-03-10 14:31:03 +00:00
|
|
|
// if received message from current conversation
|
2022-03-18 06:40:05 +00:00
|
|
|
if (getVehicleByImei(imei).sc_id == getScId())
|
|
|
|
getSMS4UnitWithTime(getScId(), time); // refresh MSG List
|
|
|
|
} else
|
|
|
|
getLastSMS(); // get last SMS
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// Create runnable for posting
|
2022-03-18 06:40:05 +00:00
|
|
|
final Runnable enableButtonRUN = this::updateEnableButtonsUI;
|
|
|
|
|
|
|
|
// enable buttons in UI after 200ms from send
|
|
|
|
private void updateEnableButtonsUI() {
|
2022-03-10 14:31:03 +00:00
|
|
|
// enable buttons
|
|
|
|
imageButtonSend.setEnabled(true);
|
|
|
|
editTextMsg.setEnabled(true);
|
|
|
|
editTextMsg.setText("");
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
private void updateResultsInUi() {
|
|
|
|
if (getAllVehicle() == null)
|
|
|
|
updateVehicles(parentTab.getAllVehicle());
|
|
|
|
|
|
|
|
if (ACTION == MSG_UPDATE) {
|
|
|
|
if (LASTMESSAGES) {
|
|
|
|
// show Header Layout
|
|
|
|
layoutHeader.setVisibility(View.VISIBLE);
|
|
|
|
// hide Conversation Header Layout
|
|
|
|
layoutHeaderConversation.setVisibility(View.GONE);
|
|
|
|
// hide Send Layout
|
|
|
|
layoutSend.setVisibility(View.GONE);
|
|
|
|
// change GridView adapter
|
|
|
|
gridView.setAdapter(mAdapter);
|
|
|
|
gridView.invalidate();
|
|
|
|
} else {
|
|
|
|
SM.Debug(" #### Modify adapter for SMS4unit");
|
|
|
|
// populate specific conversation
|
|
|
|
listSpecificConversation = new ArrayList<>();
|
|
|
|
dispatcherPositions = new ArrayList<>();
|
|
|
|
ArrayList<Boolean> ackPosition = new ArrayList<>();
|
|
|
|
ArrayList<SMS> list;
|
|
|
|
list = tableSMS.get(getScId());
|
|
|
|
if (list != null) {
|
|
|
|
for (SMS sms : list) {
|
|
|
|
// get for sender
|
|
|
|
long senderId = sms.sc_id_dest;
|
|
|
|
if (senderId == 0)
|
|
|
|
senderId = sms.sc_id_sour;
|
|
|
|
// get vehicle with sender id
|
|
|
|
Vehicle messageVeh;
|
|
|
|
messageVeh = getVehicleById(senderId);
|
|
|
|
// flag dispatcher message when source is 0
|
|
|
|
dispatcherPositions.add(sms.sc_id_sour == 0);
|
|
|
|
ackPosition.add(sms.status == 2);
|
|
|
|
|
|
|
|
listSpecificConversation.add(new Msg(messageVeh, sms.mess, new Date((long) sms.timeGMT * 1000), sms.seq_idx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
convAdapter = new ConversationGridViewAdapter(activity, listSpecificConversation, dispatcherPositions, ackPosition);
|
|
|
|
gridView.setAdapter(convAdapter);
|
|
|
|
if (!listSpecificConversation.isEmpty())
|
|
|
|
gridView.setSelection(listSpecificConversation.size() - 1);
|
|
|
|
|
|
|
|
// set unit name and image in Header Conversation
|
|
|
|
textViewSelectedContact.setText(getVehicleByScId(getScId()).name);
|
|
|
|
imageViewSelectedContact.setImageResource(getVehicleByScId(getScId()).getLargeIcon());
|
|
|
|
|
|
|
|
// clear editText
|
|
|
|
editTextMsg.setText("");
|
|
|
|
|
|
|
|
// change layouts visibility
|
|
|
|
layoutSend.setVisibility(View.VISIBLE);
|
|
|
|
layoutHeader.setVisibility(View.GONE);
|
|
|
|
layoutHeaderConversation.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
}
|
|
|
|
gridView.invalidate();
|
|
|
|
}
|
2022-03-10 14:31:03 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// gets Vehicle from id
|
2022-03-18 06:40:05 +00:00
|
|
|
public Vehicle getVehicleById(long vehicleId) {
|
|
|
|
for (Vehicle vehicle : getAllVehicle())
|
|
|
|
if (vehicle.sc_id == vehicleId)
|
|
|
|
return vehicle;
|
2022-03-10 14:31:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
// get Vehicle from imei
|
2022-03-18 06:40:05 +00:00
|
|
|
public Vehicle getVehicleByImei(String imei) {
|
|
|
|
for (Vehicle vehicle : getAllVehicle())
|
|
|
|
if (vehicle.imei.equalsIgnoreCase(imei))
|
2022-03-10 14:31:03 +00:00
|
|
|
return vehicle;
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
public Vehicle getVehicleByScId(long scId) {
|
|
|
|
for (Vehicle vehicle : getAllVehicle())
|
|
|
|
if (vehicle.sc_id == scId)
|
2022-03-10 14:31:03 +00:00
|
|
|
return vehicle;
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
// show a dialog
|
|
|
|
public void showDialog() {
|
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.template_simple_list_item, allVehicleNames);
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
|
|
builder.setTitle(getString(R.string.selectVehicle));
|
2022-03-18 06:40:05 +00:00
|
|
|
builder.setAdapter(adapter, (dialog, which) -> {
|
|
|
|
// get position
|
|
|
|
Vehicle item = getAllVehicle().get(which);
|
|
|
|
// get Specific Conversation
|
|
|
|
setScId(item.sc_id);
|
|
|
|
SM.Debug("Selected scId: " + getScId());
|
|
|
|
// set Conversation type
|
|
|
|
LASTMESSAGES = false;
|
|
|
|
// call get SMS
|
|
|
|
getSMS4unit(getScId());
|
|
|
|
// create crt_Vehicle
|
|
|
|
setUnitType((int) item.driver_id);
|
|
|
|
|
|
|
|
SM.Debug("AM SELECTAT: " + getScId() + " | " + item.name + " | " + getUnitType() + "\n\t " + item);
|
|
|
|
|
|
|
|
if (AppParams.DEMO)
|
|
|
|
updateResultsInUi();
|
|
|
|
});
|
2022-03-10 14:31:03 +00:00
|
|
|
AlertDialog alert = builder.create();
|
|
|
|
alert.show();
|
|
|
|
}
|
|
|
|
|
2022-03-18 06:40:05 +00:00
|
|
|
public void showErrorDialog(String errorMsg) {
|
2022-03-10 14:31:03 +00:00
|
|
|
Dialog dialog = new Dialog(context);
|
|
|
|
dialog.setTitle(getString(R.string.sendingError));
|
|
|
|
dialog.setContentView(R.layout.dialog);
|
|
|
|
dialog.setCancelable(true);
|
|
|
|
dialog.setCanceledOnTouchOutside(true);
|
|
|
|
|
2022-03-18 06:40:05 +00:00
|
|
|
TextView text = dialog.findViewById(R.id.text);
|
|
|
|
ImageView image = dialog.findViewById(R.id.image);
|
|
|
|
|
2022-03-10 14:31:03 +00:00
|
|
|
image.setImageResource(R.drawable.error);
|
|
|
|
text.setText(errorMsg);
|
|
|
|
dialog.show();
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
public ArrayList<Vehicle> getAllVehicle() {
|
|
|
|
return allVehicle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAllVehicle(ArrayList<Vehicle> allVehicle) {
|
|
|
|
this.allVehicle = allVehicle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getScId() {
|
|
|
|
return scId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setScId(long scId) {
|
|
|
|
this.scId = scId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getUnitType() {
|
|
|
|
return unitType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUnitType(int unitType) {
|
|
|
|
this.unitType = unitType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bundle getSavedInstanceState() {
|
|
|
|
return savedInstanceState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSavedInstanceState(Bundle savedInstanceState) {
|
|
|
|
this.savedInstanceState = savedInstanceState;
|
|
|
|
}
|
|
|
|
}
|