package com.safemobile.dispatch; 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 allVehicleNames = new ArrayList(); public ArrayList allVehicle = new ArrayList(); private ArrayList allContactsIDs = new ArrayList(); private ArrayList allGroupsIDs = new ArrayList(); private ArrayList allGroupsNames = new ArrayList(); private ArrayList crtZones = new ArrayList(); private ArrayList crtChannels = new ArrayList(); private ArrayList allGWsIP = new ArrayList(); private ArrayAdapter 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( 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.radioActivity = 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.crtRadio!= null) SendPTT(selectedCallType, selectedID,parentTab.crtRadio.GW_ID, parentTab.crtRadio.ID,AppParams.USERID); // flag in Call parentTab.inCall = 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.crtRadio.GW_ID, parentTab.crtRadio.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.crtRadio!=null) { // display dialog with adapter ArrayList tmp = new ArrayList(); for(Channel ch: crtChannels) tmp.add(ch.chName); ArrayAdapter adapter = new ArrayAdapter(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.crtRadio.ID, parentTab.crtRadio.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 tmp = new ArrayList(); 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 adapter = new ArrayAdapter(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.crtRadio!=null) { // display dialog with adapter ArrayList tmp = new ArrayList(); for(Zone zone: crtZones) tmp.add(zone.ZoneName); ArrayAdapter adapter = new ArrayAdapter(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.crtRadio.ID, parentTab.crtRadio.GW_ID, getNR4Zone(crtZones.get(which).ZoneName), 1); } }); AlertDialog alert = builder.create(); alert.show(); } if(AppParams.DEMO) { // display dialog with adapter final ArrayList tmp = new ArrayList(); tmp.add("Zone 1"); tmp.add("Zone 2"); ArrayAdapter adapter = new ArrayAdapter(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.crtRadio!=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(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(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(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(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 = (TextView) dialog.findViewById(R.id.text); ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.drawable.ic_launcher); text.setText(errorMsg); dialog.show(); } // called from ParentTabActivity public void UpdateRadios(ArrayList radios) { // save radios AppParams.listRadios = radios; if(parentTab.crtRadio == null) parentTab.crtRadio = AppParams.listRadios.get(0); // if crtRadio not exists anymore if(!AppParams.listRadios.contains(parentTab.crtRadio)) parentTab.crtRadio = AppParams.listRadios.get(0); SM.Debug(parentTab.crtRadio.toString()); // get status for selected Radio ReqRadioStatus(parentTab.crtRadio.ID, parentTab.crtRadio.GW_ID); // get selected Zone and CH onZoneCHChange(parentTab.crtRadio.ID, parentTab.crtRadio.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.crtRadio.IP); } // modify privateCall Adapter after resume if Vehicle List Modified public void UpdateVehicle() { allVehicleNames = new ArrayList(); 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.crtRadio == null)||((parentTab.crtRadio.GW_ID == _GWID)&&(parentTab.crtRadio.ID == _radioID))) { // update crtRadios, crtZones, and crtChannel for(RadioGW radio: AppParams.listRadios) if(radio.GW_ID == _GWID && radio.ID == _radioID) { parentTab.crtRadio = radio; crtZones = radio.zoneList; cmdForMe = true; } } if (cmdForMe) { for(Zone zone: parentTab.crtRadio.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.crtRadio.IP); // get all radio IP allGWsIP.clear(); for(RadioGW radio: AppParams.listRadios) allGWsIP.add(radio.IP); // set spinners and text if(parentTab.crtRadio == null) { parentTab.crtRadio = AppParams.listRadios.get(0); textViewGateway.setText(allGWsIP.get(0)); // set zone ArrayList zones = new ArrayList(); crtZones = parentTab.crtRadio.zoneList; for(Zone zone: crtZones) zones.add(zone.ZoneName); textViewZone.setText(zones.get(0).toString()); //spinnerZone.setAdapter(new ArrayAdapter(context, R.layout.template_simple_list_item, zones)); //spinnerZone.setSelection(0); // set channel ArrayList channel = new ArrayList(); crtChannels = parentTab.crtRadio.zoneList.get(0).channelList; for(Channel ch: crtChannels) channel.add(ch.chName); textViewChannel.setText(channel.get(0).toString()); //spinnerChannel.setAdapter(new ArrayAdapter(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.crtRadio = radio; // get zones for adapter ArrayList zones = new ArrayList(); crtZones = parentTab.crtRadio.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 channel = new ArrayList(); crtChannels = parentTab.crtRadio.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.crtRadio = 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(); crtZones = new ArrayList(); } // 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.crtActivity == parentTab.RADIO && selectedCallType == calltype) parentTab.enableMenuButtons(false); if(selectedCallType == calltype) parentTab.inCall = 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.inCall = 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.crtRadio != null) parentTab.sendDekey(parentTab.crtRadio.GW_ID, parentTab.crtRadio.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.crtRadio.IP); textViewChannel.setText(parentTab.crtRadio.getChannelName()); textViewZone.setText(parentTab.crtRadio.getZoneName()); // update UI myHandler.post(UpdateResultsZoneChannelRUN); radioGWChanged(parentTab.crtRadio.IP); } } }; }