safedispatch-mobile/safeDispatch/src/main/java/com/safemobile/safedispatch/RadioActivity.java
Bogdan c07986d604 Merge branch 'develop' into features/SD-188-Create_Permission_module_and_request_audio
# Conflicts:
#	safeDispatch/src/main/java/com/safemobile/dispatch/HistoryActivity.java
#	safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java
2022-03-24 12:29:12 +02:00

1255 lines
38 KiB
Java

package com.safemobile.safedispatch;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import com.safemobile.lib.AppParams;
import com.safemobile.lib.OperationCodes;
import com.safemobile.lib.SM;
import com.safemobile.lib.Vehicle;
import com.safemobile.lib.radio.Channel;
import com.safemobile.lib.radio.RadioGW;
import com.safemobile.lib.radio.Zone;
import com.safemobile.lib.radio.Zone_and_channel;
import com.safemobile.lib.sound.AudioHandle;
import java.util.ArrayList;
import java.util.Locale;
/**
* fix import
*/
public class RadioActivity extends Activity {
/* Visual Elements */
private Button buttonPTT, buttonDKey;
private RadioGroup radioGroupCallType;
private RadioButton radioAll, radioGroup, radioPrivate;
private LinearLayout layoutGateway, layoutSpinnerChannel, layoutSpinnerZone, layoutSpinnerCallType;
private TextView textViewMessageChannel, textViewMessageStatus, textViewGateway, textViewChannel, textViewZone, textViewIP, textViewCallType;
private ImageView imageViewStatus;
/* Misc */
private TabLayoutActivity parentTab;
private Context context;
private Activity activity;
private int radioID, GWID, zoneNR, chNR;
private int rStatus = 0;
private long imei = 0;
private int calltype = 0;
private int groupid = 0;
private int callstatus = 0;
/* Lists */
private ArrayList<String> allVehicleNames = new ArrayList<String>();
public ArrayList<Vehicle> allVehicle = new ArrayList<Vehicle>();
private ArrayList<Integer> allContactsIDs = new ArrayList<Integer>();
private ArrayList<Integer> allGroupsIDs = new ArrayList<Integer>();
private ArrayList<String> allGroupsNames = new ArrayList<String>();
private ArrayList<Zone> crtZones = new ArrayList<Zone>();
private ArrayList<Channel> crtChannels = new ArrayList<Channel>();
private ArrayList<String> allGWsIP = new ArrayList<String>();
private ArrayAdapter<String> adapter;
// Need handler for callbacks to the UI thread
private final Handler myHandler = new Handler();
public Thread audioThread;
//PTT
private AudioHandle audioH = null;
private final int ALLCall = 101, PRIVATECall = 102, GROUPCall = 103;
private int selectedID=0, selectedCallType = 101;
private Boolean pttONoff = false, FIRST = true;
public Bundle savedInstanceState;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
// get parentTab
parentTab = (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());
setContentView(R.layout.tabradio);
// get context
context = this;
activity = this;
allGroupsNames.add("1");
allGroupsNames.add("2");
allGroupsIDs.add(1);
allGroupsIDs.add(2);
if(AppParams.DEMO)
{
allGWsIP.add("192.168.1.40");
allGWsIP.add("192.168.1.60");
}
// get visual elements
buttonPTT = (Button) findViewById(R.id.buttonPTT);
buttonDKey = (Button) findViewById(R.id.buttonDKey);
// get CallType Spinner
layoutSpinnerCallType = (LinearLayout) findViewById(R.id.layoutSpinnerCallType);
layoutSpinnerCallType.setOnClickListener(CallTypeListener);
textViewCallType = (TextView) findViewById(R.id.textViewCallType);
// get status dot
imageViewStatus = (ImageView) findViewById(R.id.imageViewStatus);
layoutSpinnerChannel = (LinearLayout) findViewById(R.id.layoutSpinnerChannel);
layoutSpinnerChannel.setOnClickListener(ChannelChangeListener);
//spinnerChannel.setOnItemClickListener(ChannelChangeListener);
layoutSpinnerZone = (LinearLayout) findViewById(R.id.layoutSpinnerZone);
layoutSpinnerZone.setOnClickListener(ZoneChangeListener);
// get TextView from Spinner
textViewChannel = (TextView) findViewById(R.id.textViewChannel);
textViewZone = (TextView) findViewById(R.id.textViewZone);
textViewIP = (TextView) findViewById(R.id.textViewIP);
textViewIP.setText(AppParams.IP);
// get CallType
radioGroupCallType = (RadioGroup) findViewById(R.id.radioGroupCallType);
radioGroupCallType.setOnCheckedChangeListener(CallTypeChanged);
radioAll = (RadioButton) findViewById(R.id.radioAll);
radioGroup = (RadioButton) findViewById(R.id.radioGroup);
radioPrivate = (RadioButton) findViewById(R.id.radioPrivate);
textViewMessageChannel = (TextView) findViewById(R.id.textViewMessageChannel);
textViewGateway = (TextView) findViewById(R.id.textViewGateway);
textViewGateway.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf"));
textViewGateway.setTextSize(24);
textViewMessageStatus = (TextView) findViewById(R.id.textViewMessageStatus);
layoutGateway = (LinearLayout) findViewById(R.id.layoutGateway);
layoutGateway.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter = new ArrayAdapter<String>(
context, R.layout.template_simple_list_item,
allGWsIP);
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("Select RadioGW");
builder.setAdapter(adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// change gateway
//textViewGateway.setText(allGWsIP.get(which));
// change visual if selected another radioGW
if(!AppParams.DEMO)
radioGWChanged(allGWsIP.get(which));
else
{
textViewGateway.setText(allGWsIP.get(which));
textViewZone.setText("Zone 1");
textViewChannel.setText("Channel 1");
textViewMessageChannel.setText("Channel 1");
if(which == 0)
UpdateEnableDisableButtons("online");
else
UpdateEnableDisableButtons("offline");
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
// set button listener
buttonPTT.setOnClickListener(PTTClickListener);
buttonDKey.setOnClickListener(DKeyClickListener);
//disable DKey button
//buttonDKey.setEnabled(false);
/*
//hide PTT button
buttonPTT.setVisibility(View.GONE);
*/
audioThread = new Thread(new Runnable() {
@Override
public void run() {
//start audio
try{
if(audioH == null && !AppParams.IP.equalsIgnoreCase("n/a"))
audioH = new AudioHandle(AppParams.IP,0);
}
catch(Exception ex)
{
SM.Exception("#### audioH exception! ####");
SM.Exception(ex.toString());
}
}
});
audioThread.start();
if(!AppParams.DEMO)
UpdateEnableDisableButtons("offline");
else
{
textViewZone.setText("Zone 1");
textViewChannel.setText("Channel 1");
textViewMessageChannel.setText("Channel 1");
textViewIP.setText("89.33.56.51");
textViewGateway.setText("192.168.1.40");
imageViewStatus.setImageResource(R.drawable.status_online);
}
// send current activity to parrent
parentTab.setRadioActivity(this);
// register to be notified when an event is triggered
registerBroadcastIntents();
}
@Override
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.exit))
.setCancelable(false)
.setNeutralButton(getString(R.string.logout), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
parentTab.whenBackPressed(AppParams.ActivityResult.logout);
}
})
.setPositiveButton(getString(R.string.ext), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
parentTab.whenBackPressed(AppParams.ActivityResult.exit);
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public void onPause()
{
super.onPause();
SM.Debug("onPause");
}
@Override
public void onResume()
{
super.onResume();
// get all vehicles from TabLayoutActivity
if(!allVehicle.equals(parentTab.getAllVehicle()))
{
allVehicle = parentTab.getAllVehicle();
UpdateVehicle();
}
/*
// first request made from TabLiveActivity -> tabHost.setCurrentTab(3); //tabHost.setCurrentTab(0); AND tcpConnection change on first init
if(FIRST)
{
FIRST = false;
}
else
*/
if(AppParams.listRadios.size() == 0)
GetGWRadios();
SM.Debug("onResume");
}
private OnClickListener PTTClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
SM.Debug("Make call calltype:"+selectedCallType + " id:"+selectedID);
// get selectedID
if(selectedCallType == GROUPCall)
selectedID = getGroupID4Name(textViewCallType.getText().toString());
else if (selectedCallType == PRIVATECall)
selectedID = getPrivateImei4Name(textViewCallType.getText().toString());
else
selectedID = -1;
//showDialog("PTT Click");
if(!pttONoff)
{
String chanMsg = "", statMsg = "";
if(parentTab.getCrtRadio() != null)
SendPTT(selectedCallType, selectedID, parentTab.getCrtRadio().GW_ID, parentTab.getCrtRadio().ID,AppParams.USERID);
// flag in Call
parentTab.setInCall(true);
pttONoff= true;
if(audioH!=null)
audioH.soundNeeded = true;
buttonPTT.setText(getString(R.string.PTToff));
buttonPTT.setBackgroundResource(R.drawable.style_buttonptt_green);
// change text in Screen according to callType
switch(selectedCallType)
{
case PRIVATECall : chanMsg = getString(R.string.PrivateCall); statMsg = textViewCallType.getText().toString(); break;
case GROUPCall : chanMsg = getString(R.string.GroupCall); statMsg = textViewCallType.getText().toString(); break;
case ALLCall : chanMsg = getString(R.string.AllCall); statMsg = ""; break;
}
textViewMessageChannel.setText(chanMsg);
textViewMessageStatus.setText(statMsg);
// change status image
imageViewStatus.setImageResource(R.drawable.status_idle);
// disable buttons
UpdateEnableDisableButtons("disablePTT");
parentTab.enableMenuButtons(false);
}
else
{
//if(selectedCallType != PRIVATECall)
{
pttOffClick();
}
/*else
{
buttonPTT.setEnabled(false);
buttonPTT.setText("PTT");
buttonPTT.setBackgroundResource(R.layout.style_buttonptt);
textViewMessageStatus.setText("Hang Time");
// if private call was made, wait 2 seconds before enable call
new Thread(new Runnable() {
public void run()
{
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myHandler.post(UpdatepttOffClickRUN);
}
}).start();
}*/
}
}
};
final Runnable UpdatepttOffClickRUN = new Runnable() {
public void run() {
pttOffClick();
}
};
private void pttOffClick()
{
String chanMsg = "", statMsg = "";
if(!AppParams.DEMO)
SendPTT(selectedCallType+10, 1, parentTab.getCrtRadio().GW_ID, parentTab.getCrtRadio().ID,AppParams.USERID);
pttONoff= false;
if(audioH!=null)
audioH.soundNeeded = false;
buttonPTT.setText(getString(R.string.PTT));
buttonPTT.setBackgroundResource(R.drawable.style_buttonptt);
// change text in Screen
/*
if(rStatus == 1)
statMsg = "Online!";
else
statMsg = "Offline!";
*/
chanMsg = textViewChannel.getText().toString();
textViewMessageChannel.setText(chanMsg);
textViewMessageStatus.setText(statMsg);
// change status image
imageViewStatus.setImageResource(R.drawable.status_online);
// enable buttons
UpdateEnableDisableButtons("enable");
parentTab.enableMenuButtons(true);
}
private OnClickListener DKeyClickListener = new OnClickListener() {
@Override
public void onClick(View v)
{
SendDekey();
//showDialog("DKey Click");
}
};
private OnClickListener ChannelChangeListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(parentTab.getCrtRadio() !=null)
{
// display dialog with adapter
ArrayList<String> tmp = new ArrayList<String>();
for(Channel ch: crtChannels)
tmp.add(ch.chName);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, tmp);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.selChannel));
builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// change Channel in Display
//textViewMessageChannel.setText(crtChannels.get(which).chName);
// set channel name
//textViewChannel.setText(crtChannels.get(which).chName);
// send change to App
onZoneCHChange(parentTab.getCrtRadio().ID, parentTab.getCrtRadio().GW_ID, getNR4Zone(textViewZone.getText().toString()), getNR4CH(crtChannels.get(which).chName));
}
});
AlertDialog alert = builder.create();
alert.show();
}
if(AppParams.DEMO)
{
// display dialog with adapter
final ArrayList<String> tmp = new ArrayList<String>();
if(textViewZone.getText().equals("Zone 1"))
{
tmp.add("Channel 1");
tmp.add("Channel 2");
tmp.add("Channel 3");
tmp.add("Channel 4");
}
else
{
tmp.add("Ch1 ");
tmp.add("Ch2");
tmp.add("Ch3");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, tmp);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.selChannel));
builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// change Channel in Display
textViewMessageChannel.setText(tmp.get(which));
textViewChannel.setText(tmp.get(which));
// set channel name
//textViewChannel.setText(crtChannels.get(which).chName);
// send change to App
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
};
private OnClickListener ZoneChangeListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(parentTab.getCrtRadio() !=null)
{
// display dialog with adapter
ArrayList<String> tmp = new ArrayList<String>();
for(Zone zone: crtZones)
tmp.add(zone.ZoneName);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, tmp);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.selZone));
builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// set zone name
//textViewZone.setText(crtZones.get(which).ZoneName);
// send change to App
//onZoneCHChange(parentTab.crtRadio.ID, parentTab.crtRadio.GW_ID, getNR4Zone(crtZones.get(which).ZoneName), getNR4CH(textViewChannel.getText().toString()));
onZoneCHChange(parentTab.getCrtRadio().ID, parentTab.getCrtRadio().GW_ID, getNR4Zone(crtZones.get(which).ZoneName), 1);
}
});
AlertDialog alert = builder.create();
alert.show();
}
if(AppParams.DEMO)
{
// display dialog with adapter
final ArrayList<String> tmp = new ArrayList<String>();
tmp.add("Zone 1");
tmp.add("Zone 2");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, tmp);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.selZone));
builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// set zone name
textViewZone.setText(tmp.get(which));
if(textViewZone.getText().equals("Zone 1"))
{
textViewChannel.setText("Channel 1");
textViewMessageChannel.setText("Channel 1");
}
else
{
textViewChannel.setText("Ch1");
textViewMessageChannel.setText("Ch1");
}
// send change to App
//onZoneCHChange(parentTab.crtRadio.ID, parentTab.crtRadio.GW_ID, getNR4Zone(crtZones.get(which).ZoneName), getNR4CH(textViewChannel.getText().toString()));
//onZoneCHChange(parentTab.crtRadio.ID, parentTab.crtRadio.GW_ID, getNR4Zone(crtZones.get(which).ZoneName), 1);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
};
private OnClickListener CallTypeListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(parentTab.getCrtRadio() !=null)
{
// create spinner selected
AlertDialog.Builder builder = new AlertDialog.Builder(context);
switch(radioGroupCallType.getCheckedRadioButtonId())
{
case R.id.radioGroup :
// set adapter and title
adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, allGroupsNames);
builder.setTitle(getString(R.string.selectGroup));
break;
case R.id.radioPrivate :
// set adapter and title
adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, allVehicleNames);
builder.setTitle(getString(R.string.selectVehicle));
break;
}
builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(radioGroupCallType.getCheckedRadioButtonId())
{
case R.id.radioGroup :
// set adapter and title
textViewCallType.setText(allGroupsNames.get(which));
break;
case R.id.radioPrivate :
textViewCallType.setText(allVehicleNames.get(which));
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
else if (AppParams.DEMO)
{
// create spinner selected
AlertDialog.Builder builder = new AlertDialog.Builder(context);
switch(radioGroupCallType.getCheckedRadioButtonId())
{
case R.id.radioGroup :
// set adapter and title
adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, allGroupsNames);
builder.setTitle(getString(R.string.selectGroup));
break;
case R.id.radioPrivate :
// set adapter and title
adapter = new ArrayAdapter<String>(activity, R.layout.template_simple_list_item, allVehicleNames);
builder.setTitle(getString(R.string.selectVehicle));
break;
}
builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(radioGroupCallType.getCheckedRadioButtonId())
{
case R.id.radioGroup :
// set adapter and title
textViewCallType.setText(allGroupsNames.get(which));
break;
case R.id.radioPrivate :
textViewCallType.setText(allVehicleNames.get(which));
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
};
private OnCheckedChangeListener CallTypeChanged = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(radioGroupCallType.getCheckedRadioButtonId())
{
case R.id.radioAll :
layoutSpinnerCallType.setVisibility(View.INVISIBLE);
selectedCallType = ALLCall;
break;
case R.id.radioGroup :
layoutSpinnerCallType.setVisibility(View.VISIBLE);
textViewCallType.setText(allGroupsNames.get(0));
selectedCallType = GROUPCall;
break;
case R.id.radioPrivate :
layoutSpinnerCallType.setVisibility(View.VISIBLE);
if(allVehicleNames.size() > 0)
textViewCallType.setText(allVehicleNames.get(0));
else
textViewCallType.setText("");
selectedCallType = PRIVATECall;
break;
}
}
};
public void showDialog(String errorMsg)
{
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
dialog.setTitle(getString(R.string.message));
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
TextView text = dialog.findViewById(R.id.text);
ImageView image = dialog.findViewById(R.id.image);
image.setImageResource(R.mipmap.ic_launcher);
text.setText(errorMsg);
dialog.show();
}
// called from ParentTabActivity
public void UpdateRadios(ArrayList<RadioGW> radios)
{
// save radios
AppParams.listRadios = radios;
if(parentTab.getCrtRadio() == null)
parentTab.setCrtRadio(AppParams.listRadios.get(0));
// if crtRadio not exists anymore
if(!AppParams.listRadios.contains(parentTab.getCrtRadio()))
parentTab.setCrtRadio(AppParams.listRadios.get(0));
SM.Debug(parentTab.getCrtRadio().toString());
// get status for selected Radio
ReqRadioStatus(parentTab.getCrtRadio().ID, parentTab.getCrtRadio().GW_ID);
// get selected Zone and CH
onZoneCHChange(parentTab.getCrtRadio().ID, parentTab.getCrtRadio().GW_ID, 0,0);
myHandler.post(updateGatewaysRUN);
}
// Create runnable for posting
final Runnable updateGatewaysRUN = new Runnable() {
public void run() {
updateGateways();
}
};
public void updateGateways()
{
// get all radio IP
allGWsIP.clear();
for(RadioGW radio: AppParams.listRadios)
allGWsIP.add(radio.IP);
textViewGateway.setText(parentTab.getCrtRadio().IP);
}
// modify privateCall Adapter after resume if Vehicle List Modified
public void UpdateVehicle()
{
allVehicleNames = new ArrayList<String>();
if(allVehicle!=null)
for(Vehicle vehicle: allVehicle)
allVehicleNames.add(vehicle.name);
}
// called from ParentTabActivity to set radioID, GWID, zoneNR and chNR
public void UpdateZoneCH(int _radioID, int _GWID, int _zoneNR, int _chNR)
{
boolean cmdForMe =false;
// save received id's zoneNr and CHNR
this.radioID = _radioID;
this.GWID = _GWID;
this.zoneNR = _zoneNR;
this.chNR = _chNR;
SM.Debug("am primit: " + _radioID + "," + _GWID + "," + _zoneNR + "," + _chNR);
if ((parentTab.getCrtRadio() == null)||((parentTab.getCrtRadio().GW_ID == _GWID)&&(parentTab.getCrtRadio().ID == _radioID)))
{
// update crtRadios, crtZones, and crtChannel
for(RadioGW radio: AppParams.listRadios)
if(radio.GW_ID == _GWID && radio.ID == _radioID)
{
parentTab.setCrtRadio(radio);
crtZones = radio.zoneList;
cmdForMe = true;
}
}
if (cmdForMe)
{
for(Zone zone: parentTab.getCrtRadio().zoneList)
if(zone.id == _zoneNR)
crtChannels = zone.channelList;
// update UI
myHandler.post(UpdateResultsZoneChannelRUN);
}
}
// Create runnable for posting
final Runnable UpdateResultsZoneChannelRUN = new Runnable() {
public void run() {
updateResultsInUi();
}
};
private void updateResultsInUi()
{
if(AppParams.listRadios.size()>0)
{
textViewGateway.setText(parentTab.getCrtRadio().IP);
// get all radio IP
allGWsIP.clear();
for(RadioGW radio: AppParams.listRadios)
allGWsIP.add(radio.IP);
// set spinners and text
if(parentTab.getCrtRadio() == null)
{
parentTab.setCrtRadio(AppParams.listRadios.get(0));
textViewGateway.setText(allGWsIP.get(0));
// set zone
ArrayList<String> zones = new ArrayList<String>();
crtZones = parentTab.getCrtRadio().zoneList;
for(Zone zone: crtZones)
zones.add(zone.ZoneName);
textViewZone.setText(zones.get(0).toString());
//spinnerZone.setAdapter(new ArrayAdapter<String>(context, R.layout.template_simple_list_item, zones));
//spinnerZone.setSelection(0);
// set channel
ArrayList<String> channel = new ArrayList<String>();
crtChannels = parentTab.getCrtRadio().zoneList.get(0).channelList;
for(Channel ch: crtChannels)
channel.add(ch.chName);
textViewChannel.setText(channel.get(0).toString());
//spinnerChannel.setAdapter(new ArrayAdapter<String>(context, R.layout.template_simple_list_item, channel));
//if(spinnerChannel.getSelectedItemPosition() !=0)
// spinnerChannel.setSelection(0);
}
else
{
for(RadioGW radio: AppParams.listRadios)
if(radio.ID == radioID && radio.GW_ID == GWID)
parentTab.setCrtRadio(radio);
// get zones for adapter
ArrayList<String> zones = new ArrayList<String>();
crtZones = parentTab.getCrtRadio().zoneList;
int position = 0;
// get selected Zone
for(int i=0; i< crtZones.size(); i++)
{
Zone zone = crtZones.get(i);
zones.add(zone.ZoneName);
if(zone.id == zoneNR)
position = i; // save crt position in array
}
textViewZone.setText(zones.get(position).toString());
// set channel
ArrayList<String> channel = new ArrayList<String>();
crtChannels = parentTab.getCrtRadio().zoneList.get(position).channelList;
position = 0;
// get current channel
for(int i=0; i< crtChannels.size(); i++)
{
Channel ch = crtChannels.get(i);
channel.add(ch.chName);
if(ch.id == chNR)
position = i;
}
textViewChannel.setText(channel.get(position).toString());
textViewMessageChannel.setText(channel.get(position).toString());
}
UpdateEnableDisableButtons("online");
imageViewStatus.setImageResource(R.drawable.status_online);
}
}
// Radio Gateway changed from UI
private void radioGWChanged(String newIP)
{
// get gateway id
for(RadioGW radio:AppParams.listRadios)
if(radio.IP.equals(newIP))
{
UpdateEnableDisableButtons("offline");
onZoneCHChange(radio.ID, radio.GW_ID,0,0); // get zone and channel for crt radio
parentTab.setCrtRadio(radio);
}
textViewGateway.setText(newIP);
imageViewStatus.setImageResource(R.drawable.status_offline);
// clear Spinners
textViewZone.setText("");
textViewChannel.setText("");
textViewMessageChannel.setText("");
// reset adapter for spinners
crtChannels = new ArrayList<Channel>();
crtZones = new ArrayList<Zone>();
}
// broadcastCall from ParentTabActivity to set radio status
public void UpdateBroadcastCall(long Imei,int Calltype,int GroupID,int CallStatus)
{
// save received id's zoneNr and CHNR
SM.Debug("Status: Imei: " + Imei+ " Calltype: "+Calltype+ " GroupID: "+GroupID+" CallStatus:"+CallStatus);
this.imei = Imei;
this.calltype = Calltype;
this.groupid = GroupID;
this.callstatus =CallStatus;
myHandler.post(UpdateB);
}
final Runnable UpdateB = new Runnable() {
public void run() {
UpdateResultsBroadcastCall();
}
};
private void UpdateResultsBroadcastCall()
{
SM.Debug("UpdateResultsBroadcastCall with Imei: " +imei+ " CallType:"+calltype+ " GroupID:"+groupid+" CallStatus:"+callstatus);
if ((callstatus==1)||(callstatus==2))
{
// modify UI only when call is received, not in hangtime
if(callstatus == 1)
{
layoutGateway.setEnabled(false);
String CallType = getString(R.string.AllCall);
if (calltype==102)
{
CallType = getString(R.string.PrivateCall);
radioPrivate.setChecked(true);
radioAll.setChecked(false);
radioGroup.setChecked(false);
selectedCallType = PRIVATECall;
textViewCallType.setText(parentTab.getSuperVehHash().get((long)imei).name);
layoutSpinnerCallType.setVisibility(View.VISIBLE);
}
else if (calltype==103)
{
CallType = getString(R.string.GroupCall) + "("+groupid+")";
radioPrivate.setChecked(false);
radioAll.setChecked(false);
radioGroup.setChecked(true);
selectedCallType = GROUPCall;
textViewCallType.setText(groupid+"");
layoutSpinnerCallType.setVisibility(View.VISIBLE);
}
if(CallType.equals(getString(R.string.AllCall)))
{
radioPrivate.setChecked(false);
radioAll.setChecked(true);
radioGroup.setChecked(false);
selectedCallType = ALLCall;
layoutSpinnerCallType.setVisibility(View.INVISIBLE);
}
textViewMessageChannel.setText(CallType);
}
if (callstatus==1)
{
if (parentTab.getSuperVehHash().get((long)imei)!=null)
textViewMessageStatus.setText(parentTab.getSuperVehHash().get((long)imei).name);
else
textViewMessageStatus.setText("Imei: "+Long.toString(imei));
}
else if(selectedCallType == calltype)
textViewMessageStatus.setText(getString(R.string.hangTime));
// change UI only if call types equals
if(selectedCallType == calltype)
{
imageViewStatus.setImageResource(R.drawable.status_idle);
// disable buttons
UpdateEnableDisableButtons("disableIncCall");
}
// enable PTT button and disable DKey when HangTime
if(callstatus==2 && selectedCallType == calltype)
{
buttonPTT.setEnabled(true);
buttonDKey.setEnabled(false);
}
if(parentTab.getCrtActivity() == parentTab.RADIO_TAB_ID && selectedCallType == calltype)
parentTab.enableMenuButtons(false);
if(selectedCallType == calltype)
parentTab.setInCall(true);
}
else if (callstatus==3)
{
if((calltype == 0 && pttONoff == true) || selectedCallType == calltype)
{
layoutGateway.setEnabled(true);
textViewMessageChannel.setText(textViewChannel.getText());
textViewMessageStatus.setText(" ");
imageViewStatus.setImageResource(R.drawable.status_online);
if(audioH!=null)
audioH.StopSound();
// disable buttons
UpdateEnableDisableButtons("enable");
if(pttONoff)
{
pttONoff= false;
if(audioH!=null)
audioH.soundNeeded = false;
buttonPTT.setText(getString(R.string.PTT));
buttonPTT.setBackgroundResource(R.drawable.style_buttonptt);
}
parentTab.enableMenuButtons(true);
parentTab.setInCall(false);
}
}
}
// called from ParentTabActivity to set radio status
public void UpdateRadioStatus (int status)
{
// save received id's zoneNr and CHNR
this.rStatus = status;
SM.Debug("rStatus: " + rStatus );
myHandler.post(UpdateRadioS);
}
final Runnable UpdateRadioS = new Runnable() {
public void run() {
updateResultsStatus();
}
};
private void updateResultsStatus()
{
SM.Debug("updateResultsStatusUI with Status: " + (rStatus == 1? "ONLINE" : "OFFLINE"));
//textViewGateway.setText(crtRadio.IP);
if(rStatus == 1)
{
imageViewStatus.setImageResource(R.drawable.status_online);
//textViewMessageStatus.setText("Online!!!");
UpdateEnableDisableButtons("online");
}
else
{
imageViewStatus.setImageResource(R.drawable.status_offline);
//textViewMessageStatus.setText("Offline!!!");
UpdateEnableDisableButtons("offline");
}
}
public void UpdateEnableDisableButtons(String type)
{
if(type.equals("disableIncCall"))
{
radioGroupCallType.setEnabled(false);
radioAll.setEnabled(false);
radioGroup.setEnabled(false);
radioPrivate.setEnabled(false);
// disable spinners
layoutSpinnerZone.setEnabled(false);
layoutSpinnerChannel.setEnabled(false);
layoutSpinnerCallType.setEnabled(false);
// enable Dkey button and disable PTT
buttonDKey.setEnabled(true);
buttonPTT.setEnabled(false);
}
else if (type.equals("disablePTT"))
{
radioGroupCallType.setEnabled(false);
radioAll.setEnabled(false);
radioGroup.setEnabled(false);
radioPrivate.setEnabled(false);
// disable spinners
layoutSpinnerZone.setEnabled(false);
layoutSpinnerChannel.setEnabled(false);
layoutSpinnerCallType.setEnabled(false);
// disable Dkey button
buttonDKey.setEnabled(false);
}
else if (type.equals("enable"))
{
radioGroupCallType.setEnabled(true);
radioAll.setEnabled(true);
radioGroup.setEnabled(true);
radioPrivate.setEnabled(true);
// enable spinners
layoutSpinnerZone.setEnabled(true);
layoutSpinnerChannel.setEnabled(true);
layoutSpinnerCallType.setEnabled(true);
// disable Dkey button and enable PTT
buttonDKey.setEnabled(false);
buttonPTT.setEnabled(true);
}
else if(type.equals("online"))
{
radioGroupCallType.setEnabled(true);
radioAll.setEnabled(true);
radioGroup.setEnabled(true);
radioPrivate.setEnabled(true);
// enable spinners
layoutSpinnerZone.setEnabled(true);
layoutSpinnerChannel.setEnabled(true);
layoutSpinnerCallType.setEnabled(true);
// disable Dkey button
buttonDKey.setEnabled(false);
buttonPTT.setEnabled(true);
textViewMessageStatus.setText("");
parentTab.enableMenuButtons(true);
}
else if (type.equals("offline"))
{
radioGroupCallType.setEnabled(false);
radioAll.setEnabled(false);
radioGroup.setEnabled(false);
radioPrivate.setEnabled(false);
// disable spinners
layoutSpinnerZone.setEnabled(false);
layoutSpinnerChannel.setEnabled(false);
layoutSpinnerCallType.setEnabled(false);
// disable Dkey button
buttonDKey.setEnabled(false);
buttonPTT.setEnabled(false);
// reset zone and channel text
textViewZone.setText("");
textViewChannel.setText("");
parentTab.enableMenuButtons(true); //enable menu buttons
// reset status icon
imageViewStatus.setImageResource(R.drawable.status_offline);
// reset screen messages
textViewMessageChannel.setText("");
textViewMessageStatus.setText(getString(R.string.offline));
}
}
// get Group Contact ID from Name
private int getGroupID4Name(String name)
{
int i = 0;
for (String groupName: allGroupsNames)
{
// if searched name - return corresponding id
if(groupName.equals(name))
return allGroupsIDs.get(i);
i++;
}
return -1;
}
// get Private Contact ID from Name
private int getPrivateImei4Name(String name)
{
int i = 0;
for (String vehicleName:allVehicleNames)
{
// if searched name - return corresponding id
if(vehicleName.equals(name))
return Integer.parseInt(allVehicle.get(i).imei);
i++;
}
return -1;
}
// get zone number from spinner zoneName
private int getNR4Zone(String zoneName)
{
for(Zone zone: crtZones)
if(zone.ZoneName.equals(zoneName))
return zone.id;
return -1;
}
// get channel number from spinner chName
private int getNR4CH(String chName)
{
for(Channel ch: crtChannels)
if(ch.chName.equals(chName))
return ch.id;
return -1;
}
// send GetGWRadios to AppServer
private void GetGWRadios()
{
parentTab.getRadiosList();
}
// send Dekey to AppServer
private void SendDekey()
{
if(parentTab.getCrtRadio() != null)
parentTab.sendDekey(parentTab.getCrtRadio().GW_ID, parentTab.getCrtRadio().ID);
}
// send change Channel and Zone message to AppServer
private void onZoneCHChange(int RadioID, int GWID, int zone, int ch)
{
SM.Debug("Changed Z: " + zone + " | Channel: " + ch);
parentTab.getSetZoneAndChannel(GWID,RadioID,zone,ch);
}
// request radio status
private void ReqRadioStatus(int RadioID, int GWID)
{
SM.Debug("Request radio status 4 radioId: " + RadioID + " | GW_ID:" + GWID);
parentTab.getRadioStatus(GWID,RadioID);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//sendPTT
//callType:
//101 -allcall init
//111 -allcall stop
//102 -prvcall init
//112 -prvcall stop
//103 -grpcall init
//113 -grpcall stop
public void SendPTT(int callType, int id,int gwid, int rgwid,long userID)
{
SM.Debug("sendPTT callType:"+callType);
parentTab.sendPTT(callType, id,gwid,rgwid,userID);
}
/** Register for broadcasts */
private void registerBroadcastIntents() {
IntentFilter intentFilter = new IntentFilter(OperationCodes.CHANNEL_BRDCST+"");
this.registerReceiver(mReceiver, intentFilter);
intentFilter = new IntentFilter(OperationCodes.RADIOID_CHANGED+"");
this.registerReceiver(mReceiver, intentFilter);
}
//The BroadcastReceiver that listens for Notification broadcasts
public final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//SM.Debug("### NOTIFICATION ###", "Radio: " + intent.getAction());
final String action = intent.getAction();
if (action.equals(OperationCodes.CHANNEL_BRDCST+"")) {
Zone_and_channel zc = AppParams.crtZoneAndChannel;
UpdateZoneCH(zc.rgwID, zc.gwID, zc.zoneNr, zc.channelNr);
}
else if (action.equals(OperationCodes.RADIOID_CHANGED+"")) {
textViewGateway.setText(parentTab.getCrtRadio().IP);
textViewChannel.setText(parentTab.getCrtRadio().getChannelName());
textViewZone.setText(parentTab.getCrtRadio().getZoneName());
// update UI
myHandler.post(UpdateResultsZoneChannelRUN);
radioGWChanged(parentTab.getCrtRadio().IP);
}
}
};
}