diff --git a/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java b/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java index eb83182..d879570 100644 --- a/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java +++ b/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java @@ -102,10 +102,8 @@ public class TCPService extends Service { /** Stop TCP Connection */ - public void stopTCPConnection() - { - if(tcp != null) - { + public void stopTCPConnection() { + if(tcp != null) { tcp.Stop(); tcp = null; } diff --git a/safeDispatch/src/main/java/com/safemobile/safedispatch/RadioActivity.java b/safeDispatch/src/main/java/com/safemobile/safedispatch/RadioActivity.java index 8dcf236..e30077c 100644 --- a/safeDispatch/src/main/java/com/safemobile/safedispatch/RadioActivity.java +++ b/safeDispatch/src/main/java/com/safemobile/safedispatch/RadioActivity.java @@ -2,10 +2,8 @@ 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; @@ -34,7 +32,6 @@ import com.safemobile.lib.radio.Zone_and_channel; import com.safemobile.lib.sound.AudioHandle; import java.util.ArrayList; -import java.util.Hashtable; import java.util.Locale; /** @@ -64,16 +61,15 @@ public class RadioActivity extends Activity { private int callstatus = 0; /* Lists */ - private ArrayList allVehicleNames = new ArrayList(); - public ArrayList allVehicle = new ArrayList(); + 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 final ArrayList allGroupsIDs = new ArrayList<>(); + private final ArrayList allGroupsNames = new ArrayList<>(); - private ArrayList crtZones = new ArrayList(); - private ArrayList crtChannels = new ArrayList(); - private ArrayList allGWsIP = new ArrayList(); + private ArrayList crtZones = new ArrayList<>(); + private ArrayList crtChannels = new ArrayList<>(); + private final ArrayList allGWsIP = new ArrayList<>(); private ArrayAdapter adapter; // Need handler for callbacks to the UI thread @@ -85,10 +81,10 @@ public class RadioActivity extends Activity { 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; + private Boolean pttONoff = false; private final int inCall = 1, hangTime = 2, callEnd = 3; - public Bundle savedInstanceState; + private Bundle savedInstanceState; /** Called when the activity is first created. */ @@ -116,56 +112,55 @@ public class RadioActivity extends Activity { allGroupsIDs.add(1); allGroupsIDs.add(2); - //==================== // get visual elements //==================== // get buttons - buttonPTT = (Button) findViewById(R.id.buttonPTT); - buttonDKey = (Button) findViewById(R.id.buttonDKey); + buttonPTT = findViewById(R.id.buttonPTT); + buttonDKey = findViewById(R.id.buttonDKey); // get CallType Spinner - layoutSpinnerCallType = (LinearLayout) findViewById(R.id.layoutSpinnerCallType); + layoutSpinnerCallType = findViewById(R.id.layoutSpinnerCallType); layoutSpinnerCallType.setOnClickListener(CallTypeListener); - textViewCallType = (TextView) findViewById(R.id.textViewCallType); + textViewCallType = findViewById(R.id.textViewCallType); // get status dot - imageViewStatus = (ImageView) findViewById(R.id.imageViewStatus); + imageViewStatus = findViewById(R.id.imageViewStatus); - layoutSpinnerChannel = (LinearLayout) findViewById(R.id.layoutSpinnerChannel); + layoutSpinnerChannel = findViewById(R.id.layoutSpinnerChannel); layoutSpinnerChannel.setOnClickListener(ChannelChangeListener); - layoutSpinnerZone = (LinearLayout) findViewById(R.id.layoutSpinnerZone); + layoutSpinnerZone = findViewById(R.id.layoutSpinnerZone); layoutSpinnerZone.setOnClickListener(ZoneChangeListener); // get TextView from Spinner - textViewChannel = (TextView) findViewById(R.id.textViewChannel); - textViewZone = (TextView) findViewById(R.id.textViewZone); + textViewChannel = findViewById(R.id.textViewChannel); + textViewZone = findViewById(R.id.textViewZone); // get IP - textViewIP = (TextView) findViewById(R.id.textViewIP); + textViewIP = findViewById(R.id.textViewIP); textViewIP.setText(AppParams.IP); // get CallType - radioGroupCallType = (RadioGroup) findViewById(R.id.radioGroupCallType); + radioGroupCallType = findViewById(R.id.radioGroupCallType); radioGroupCallType.setOnCheckedChangeListener(CallTypeChanged); // get radio buttons - radioAll = (RadioButton) findViewById(R.id.radioAll); - radioGroup = (RadioButton) findViewById(R.id.radioGroup); - radioPrivate = (RadioButton) findViewById(R.id.radioPrivate); + radioAll = findViewById(R.id.radioAll); + radioGroup = findViewById(R.id.radioGroup); + radioPrivate = findViewById(R.id.radioPrivate); // get channel - textViewMessageChannel = (TextView) findViewById(R.id.textViewMessageChannel); + textViewMessageChannel = findViewById(R.id.textViewMessageChannel); // get gateway - textViewGateway = (TextView) findViewById(R.id.textViewGateway); + textViewGateway = findViewById(R.id.textViewGateway); textViewGateway.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf")); textViewGateway.setTextSize(24); // get status - textViewMessageStatus = (TextView) findViewById(R.id.textViewMessageStatus); - layoutGateway = (LinearLayout) findViewById(R.id.layoutGateway); + textViewMessageStatus = findViewById(R.id.textViewMessageStatus); + layoutGateway = findViewById(R.id.layoutGateway); layoutGateway.setOnClickListener(GatwayListener); // set button ptt listener @@ -173,7 +168,6 @@ public class RadioActivity extends Activity { // set button DKey listener buttonDKey.setOnClickListener(DKeyClickListener); - startAudioThread(); if(!AppParams.DEMO) @@ -187,128 +181,87 @@ public class RadioActivity extends Activity { registerBroadcastIntents(); } - private void startAudioThread() - { - audioThread = new Thread(new Runnable() { + public Bundle getSavedInstanceState() { + return savedInstanceState; + } - @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()); - } + private void startAudioThread() { + audioThread = new Thread(() -> { + //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(); - } @Override - public void onBackPressed() - { + 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(); - } - }); + .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(); } - @Override - public void onPause() - { + public void onPause() { super.onPause(); SM.Debug("onPause"); } @Override - public void onResume() - { + public void onResume() { super.onResume(); // get all vehicles from TabLayoutActivity - if(!allVehicle.equals(parentTab.getAllVehicle())) - { + if (!allVehicle.equals(parentTab.getAllVehicle())) { allVehicle = parentTab.getAllVehicle(); UpdateVehicle(); - } - if(AppParams.listRadios.size() == 0) + if (AppParams.listRadios.size() == 0) GetGWRadios(); SM.Debug("onResume"); } - private OnClickListener GatwayListener = new OnClickListener() { - + private final OnClickListener GatwayListener = new OnClickListener() { @Override public void onClick(View v) { - adapter = new ArrayAdapter( + 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 + (dialog, which) -> { + // change gateway + //textViewGateway.setText(allGWsIP.get(which)); + // change visual if selected another radioGW - radioGWChanged(allGWsIP.get(which)); - - } + radioGWChanged(allGWsIP.get(which)); }); AlertDialog alert = builder.create(); alert.show(); } }; - - private OnClickListener PTTClickListener = new OnClickListener() { - @Override - - public void onClick(View v) { - - if (pttONoff) - pttOffClick(); - else - pttOnClick(); - } + private final OnClickListener PTTClickListener = v -> { + if (pttONoff) + pttOffClick(); + else + pttOnClick(); }; - final Runnable UpdatepttOffClickRUN = new Runnable() { - public void run() { - pttOffClick(); - } - }; - - - private void pttOnClick() - { + private void pttOnClick() { //================ // get selectedID //================ @@ -352,12 +305,10 @@ public class RadioActivity extends Activity { //=================== UpdateEnableDisableButtons("disablePTT"); parentTab.enableMenuButtons(false); - } - private void pttOffClick() - { - String chanMsg = "", statMsg = ""; + private void pttOffClick() { + String chanMsg, statMsg = ""; // send stop the call if(parentTab.getCrtRadio() != null) @@ -368,11 +319,9 @@ public class RadioActivity extends Activity { if(audioH != null) audioH.soundNeeded = false; - buttonPTT.setText(getString(R.string.PTT)); buttonPTT.setBackgroundResource(R.drawable.style_buttonptt); - chanMsg = textViewChannel.getText().toString(); textViewMessageChannel.setText(chanMsg); textViewMessageStatus.setText(statMsg); @@ -383,45 +332,32 @@ public class RadioActivity extends Activity { // enable buttons UpdateEnableDisableButtons("enable"); parentTab.enableMenuButtons(true); - } - private OnClickListener DKeyClickListener = new OnClickListener() { - @Override - public void onClick(View v) - { - SendDekey(); - } - }; - - private OnClickListener ChannelChangeListener = new OnClickListener() { + private final OnClickListener DKeyClickListener = v -> SendDekey(); + private final OnClickListener ChannelChangeListener = new OnClickListener() { @Override public void onClick(View v) { - - if(parentTab.getCrtRadio() != null) - { + if (parentTab.getCrtRadio() != null) { // display dialog with adapter - ArrayList tmp = new ArrayList(); + ArrayList tmp = new ArrayList<>(); for(Channel ch: crtChannels) tmp.add(ch.chName); - ArrayAdapter adapter = new ArrayAdapter(activity, R.layout.template_simple_list_item, tmp); + 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); + builder.setAdapter(adapter , (dialog, which) -> { + // change Channel in Display + //textViewMessageChannel.setText(crtChannels.get(which).chName); - // set channel name - //textViewChannel.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)); - } + // 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(); @@ -430,70 +366,51 @@ public class RadioActivity extends Activity { } }; - private OnClickListener ZoneChangeListener = new OnClickListener() { - + private final OnClickListener ZoneChangeListener = new OnClickListener() { @Override public void onClick(View v) { - - if(parentTab.getCrtRadio() !=null) - { + if (parentTab.getCrtRadio() !=null) { // display dialog with adapter - ArrayList tmp = new ArrayList(); + ArrayList tmp = new ArrayList<>(); for(Zone zone: crtZones) tmp.add(zone.ZoneName); - ArrayAdapter adapter = new ArrayAdapter(activity, R.layout.template_simple_list_item, tmp); + 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.getCrtRadio().ID, parentTab.getCrtRadio().GW_ID, getNR4Zone(crtZones.get(which).ZoneName), 1); - } + builder.setAdapter(adapter , (dialog, which) -> { + // send change to App + onZoneCHChange(parentTab.getCrtRadio().ID, parentTab.getCrtRadio().GW_ID, getNR4Zone(crtZones.get(which).ZoneName), 1); }); AlertDialog alert = builder.create(); alert.show(); } - } }; - private OnClickListener CallTypeListener = new OnClickListener() { - + private final OnClickListener CallTypeListener = new OnClickListener() { @Override public void onClick(View v) { - - - if(parentTab.getCrtRadio() != null) - { + 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(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; + 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()) - { + builder.setAdapter(adapter , (dialog, which) -> { + switch(radioGroupCallType.getCheckedRadioButtonId()) { case R.id.radioGroup : // set adapter and title textViewCallType.setText(allGroupsNames.get(which)); @@ -501,66 +418,43 @@ public class RadioActivity extends Activity { case R.id.radioPrivate : textViewCallType.setText(allVehicleNames.get(which)); break; - } } }); AlertDialog alert = builder.create(); alert.show(); } - } }; - private OnCheckedChangeListener CallTypeChanged = new OnCheckedChangeListener() { - + private final 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 : + 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(""); - layoutSpinnerCallType.setVisibility(View.VISIBLE); - if(allVehicleNames.size() > 0) - textViewCallType.setText(allVehicleNames.get(0)); - else - textViewCallType.setText(""); - - selectedCallType = PRIVATECall; - break; + 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 radios) - { + public void UpdateRadios(ArrayList radios) { // save radios AppParams.listRadios = radios; if(parentTab.getCrtRadio() == null) @@ -583,18 +477,13 @@ public class RadioActivity extends Activity { // Create runnable for posting - final Runnable updateGatewaysRUN = new Runnable() { - public void run() { - updateGateways(); - } - }; + final Runnable updateGatewaysRUN = this::updateGateways; - public void updateGateways() - { + public void updateGateways() { // get all radio IP allGWsIP.clear(); - for(RadioGW radio: AppParams.listRadios) + for (RadioGW radio: AppParams.listRadios) allGWsIP.add(radio.IP); textViewGateway.setText(parentTab.getCrtRadio().IP); @@ -602,19 +491,15 @@ public class RadioActivity extends Activity { // modify privateCall Adapter after resume if Vehicle List Modified - public void UpdateVehicle() - { - - allVehicleNames = new ArrayList(); - if( allVehicle!=null ) + public void UpdateVehicle() { + allVehicleNames = new ArrayList<>(); + if (allVehicle!=null) for(Vehicle vehicle: allVehicle) allVehicleNames.add(vehicle.name); - } - private RadioGW getRadioGW(int _radioID, int _gwID) - { - for(RadioGW radio: AppParams.listRadios) { + private RadioGW getRadioGW(int _radioID, int _gwID) { + for (RadioGW radio: AppParams.listRadios) { if (radio.GW_ID == _gwID && radio.ID == _radioID) return radio; } @@ -622,20 +507,17 @@ public class RadioActivity extends Activity { return null; } - private ArrayList getChannels(int zoneNR) - { + private ArrayList getChannels(int zoneNR) { for(Zone zone : parentTab.getCrtRadio().zoneList) { if (zone.id == zoneNR) return zone.channelList; } - return new ArrayList(); + return new ArrayList<>(); } // called from ParentTabActivity to set radioID, GWID, zoneNR and chNR - public void UpdateZoneCH(int _radioID, int _gwID, int _zoneNR, int _chNR) - { - boolean cmdForMe =false; + public void UpdateZoneCH(int _radioID, int _gwID, int _zoneNR, int _chNR) { // save received id's zoneNr and CHNR this.radioID = _radioID; this.GWID = _gwID; @@ -646,9 +528,8 @@ public class RadioActivity extends Activity { // get RadioGw RadioGW radioGW = getRadioGW(_radioID, _gwID); - if( radioGW == null) { + if (radioGW == null) return; - } // set current radio parentTab.setCrtRadio(radioGW); @@ -658,93 +539,74 @@ public class RadioActivity extends Activity { crtChannels = getChannels(_zoneNR); myHandler.post(UpdateResultsZoneChannelRUN); - } // Create runnable for posting - final Runnable UpdateResultsZoneChannelRUN = new Runnable() { - public void run() { - updateResultsInUi(); - } - }; + final Runnable UpdateResultsZoneChannelRUN = this::updateResultsInUi; - private void updateResultsInUi() - { - - if(AppParams.listRadios.size() > 0) - { + private void updateResultsInUi() { + if (AppParams.listRadios.size() > 0) { textViewGateway.setText(parentTab.getCrtRadio().IP); // get all radio IP allGWsIP.clear(); - for(RadioGW radio: AppParams.listRadios) + for (RadioGW radio: AppParams.listRadios) allGWsIP.add(radio.IP); // set spinners and text - if(parentTab.getCrtRadio() == null) - { + if (parentTab.getCrtRadio() == null) { parentTab.setCrtRadio(AppParams.listRadios.get(0)); textViewGateway.setText(allGWsIP.get(0)); // set zone - ArrayList zones = new ArrayList(); + ArrayList zones = new ArrayList<>(); crtZones = parentTab.getCrtRadio().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); + textViewZone.setText(zones.get(0)); // set channel - ArrayList channel = new ArrayList(); + ArrayList channel = new ArrayList<>(); 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(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) + textViewChannel.setText(channel.get(0)); + } else { + for (RadioGW radio: AppParams.listRadios) + if (radio.ID == radioID && radio.GW_ID == GWID) parentTab.setCrtRadio(radio); // get zones for adapter - ArrayList zones = new ArrayList(); + ArrayList zones = new ArrayList<>(); crtZones = parentTab.getCrtRadio().zoneList; int position = 0; // get selected Zone - for(int i=0; i< crtZones.size(); i++) - { + for (int i=0; i< crtZones.size(); i++) { Zone zone = crtZones.get(i); zones.add(zone.ZoneName); - if(zone.id == zoneNR) + if (zone.id == zoneNR) position = i; // save crt position in array } - textViewZone.setText(zones.get(position).toString()); + textViewZone.setText(zones.get(position)); // set channel - ArrayList channel = new ArrayList(); + ArrayList channel = new ArrayList<>(); crtChannels = parentTab.getCrtRadio().zoneList.get(position).channelList; // get current channel position = 0; - for(int i=0; i< crtChannels.size(); i++) - { + 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()); + textViewChannel.setText(channel.get(position)); + textViewMessageChannel.setText(channel.get(position)); } UpdateEnableDisableButtons("online"); imageViewStatus.setImageResource(R.drawable.status_online); @@ -752,13 +614,10 @@ public class RadioActivity extends Activity { } // Radio Gateway changed from UI - private void radioGWChanged(String newIP) - { - + private void radioGWChanged(String newIP) { // get gateway id - for(RadioGW radio:AppParams.listRadios) - if(radio.IP.equals(newIP)) - { + 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); @@ -771,90 +630,68 @@ public class RadioActivity extends Activity { ClearSpinners(); // reset adapter for spinners ResetAdaptersForSpinners(); - } - private void ClearSpinners() - { + private void ClearSpinners() { textViewZone.setText(""); textViewChannel.setText(""); textViewMessageChannel.setText(""); } - private void ResetAdaptersForSpinners() - { - crtChannels = new ArrayList(); - crtZones = new ArrayList(); + private void ResetAdaptersForSpinners() { + crtChannels = new ArrayList<>(); + crtZones = new ArrayList<>(); } - private void SetCheck(RadioButton radioButton) - { + private void SetCheck(RadioButton radioButton) { radioPrivate.setChecked(radioPrivate == radioButton ); radioGroup.setChecked(radioGroup == radioButton ); radioAll.setChecked(radioAll == radioButton ); - } // 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; + 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); - } + myHandler.post(UpdateB); + } - final Runnable UpdateB = new Runnable() { - public void run() { - UpdateResultsBroadcastCall(); - } - }; + final Runnable UpdateB = this::UpdateResultsBroadcastCall; - private void UpdateResultsBroadcastCall() - { + private void UpdateResultsBroadcastCall() { SM.Debug("UpdateResultsBroadcastCall with Imei: " +imei+ " CallType:"+calltype+ " GroupID:"+groupid+" CallStatus:"+callstatus); - if ((callstatus == inCall) || (callstatus== hangTime)) - { + if ((callstatus == inCall) || (callstatus== hangTime)) { // modify UI only when call is received, not in hangtime - if(callstatus == inCall) - { + if (callstatus == inCall) { layoutGateway.setEnabled(false); String CallType = getString(R.string.AllCall); - if (calltype == PRIVATECall ) - { + if (calltype == PRIVATECall) { CallType = getString(R.string.PrivateCall); SetCheck(radioPrivate); selectedCallType = PRIVATECall; - - } - else if (calltype == GROUPCall) - { + } else if (calltype == GROUPCall) { CallType = getString(R.string.GroupCall) + "("+groupid+")"; SetCheck(radioGroup); selectedCallType = GROUPCall; textViewCallType.setText(groupid + ""); - - } - else - { + } else { SetCheck(radioAll); selectedCallType = ALLCall; - } layoutSpinnerCallType.setVisibility( calltype != ALLCall ? View.VISIBLE : View.INVISIBLE); textViewMessageChannel.setText(CallType); - - if ( parentTab.getSuperVehHash() != null) { - if (parentTab.getSuperVehHash().containsKey((long) imei)) - textViewMessageStatus.setText(parentTab.getSuperVehHash().get((long) imei).name); + if (parentTab.getSuperVehHash() != null) { + if (parentTab.getSuperVehHash().containsKey(imei)) + textViewMessageStatus.setText(parentTab.getSuperVehHash().get(imei).name); else - textViewMessageStatus.setText("Imei: " + Long.toString(imei)); + textViewMessageStatus.setText("Imei: " + imei); } } @@ -865,52 +702,31 @@ public class RadioActivity extends Activity { buttonDKey.setEnabled(false); } - // 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 == hangTime && selectedCallType == calltype) -// { -// buttonPTT.setEnabled(true); -// buttonDKey.setEnabled(false); -// } - - if(parentTab.getCrtActivity() == parentTab.RADIO_TAB_ID && selectedCallType == calltype) + if (parentTab.getCrtActivity() == parentTab.RADIO_TAB_ID && selectedCallType == calltype) parentTab.enableMenuButtons(false); - if(selectedCallType == calltype) + if (selectedCallType == calltype) parentTab.setInCall(true); - - } - else if (callstatus == callEnd ) - { - if((calltype == 0 && pttONoff == true) || selectedCallType == calltype) - { + } else if (callstatus == callEnd ) { + if ((calltype == 0 && pttONoff) || selectedCallType == calltype) { layoutGateway.setEnabled(true); textViewMessageChannel.setText(textViewChannel.getText()); textViewMessageStatus.setText(" "); imageViewStatus.setImageResource(R.drawable.status_online); // stop sound - if(audioH!=null) + if (audioH!=null) audioH.StopSound(); // disable buttons UpdateEnableDisableButtons("enable"); - if(pttONoff) - { + if (pttONoff) { pttONoff= false; - if(audioH!=null) + if (audioH!=null) audioH.soundNeeded = false; - buttonPTT.setText(getString(R.string.PTT)); buttonPTT.setBackgroundResource(R.drawable.style_buttonptt); } @@ -921,8 +737,7 @@ public class RadioActivity extends Activity { } // called from ParentTabActivity to set radio status - public void UpdateRadioStatus (int status) - { + public void UpdateRadioStatus (int status) { // save received id's zoneNr and CHNR this.rStatus = status; @@ -930,14 +745,9 @@ public class RadioActivity extends Activity { myHandler.post(UpdateRadioS); } - final Runnable UpdateRadioS = new Runnable() { - public void run() { - updateResultsStatus(); - } - }; + final Runnable UpdateRadioS = this::updateResultsStatus; - private void updateResultsStatus() - { + private void updateResultsStatus() { SM.Debug("updateResultsStatusUI with Status: " + (rStatus == 1? "ONLINE" : "OFFLINE")); imageViewStatus.setImageResource( (rStatus == 1 ) ? R.drawable.status_online : R.drawable.status_offline); @@ -945,25 +755,21 @@ public class RadioActivity extends Activity { } - private void enableDisableRadioCallType(boolean enabled) - { + private void enableDisableRadioCallType(boolean enabled) { radioGroupCallType.setEnabled(enabled); radioAll.setEnabled(enabled); radioGroup.setEnabled(enabled); radioPrivate.setEnabled(enabled); } - private void enableDisableSpinners(boolean enabled) - { + private void enableDisableSpinners(boolean enabled) { layoutSpinnerZone.setEnabled(enabled); layoutSpinnerChannel.setEnabled(enabled); layoutSpinnerCallType.setEnabled(enabled); } - public void UpdateEnableDisableButtons(String type) - { - if(type.equals("disableIncCall")) - { + public void UpdateEnableDisableButtons(String type) { + if (type.equals("disableIncCall")) { enableDisableRadioCallType(false); enableDisableSpinners(false); @@ -971,27 +777,20 @@ public class RadioActivity extends Activity { buttonDKey.setEnabled(true); // disable PTT buttonPTT.setEnabled(false); - } - else if (type.equals("disablePTT")) - { - + } else if (type.equals("disablePTT")) { enableDisableRadioCallType(false); enableDisableSpinners(false); // disable Dkey button buttonDKey.setEnabled(false); - } - else if (type.equals("enable")) - { + } else if (type.equals("enable")) { enableDisableRadioCallType(true); enableDisableSpinners(true); // disable Dkey button and enable PTT buttonDKey.setEnabled(false); buttonPTT.setEnabled(true); - } - else if(type.equals("online")) - { + } else if(type.equals("online")) { enableDisableRadioCallType(true); enableDisableSpinners(true); @@ -1000,13 +799,10 @@ public class RadioActivity extends Activity { buttonPTT.setEnabled(true); textViewMessageStatus.setText(""); parentTab.enableMenuButtons(true); - } - else if (type.equals("offline")) - { + } else if (type.equals("offline")) { enableDisableRadioCallType(false); enableDisableSpinners(false); - // disable Dkey button buttonDKey.setEnabled(false); // disable PTT button @@ -1027,16 +823,12 @@ public class RadioActivity extends Activity { } } - // get Group Contact ID from Name - private int getGroupID4Name(String name) - { + private int getGroupID4Name(String name) { int i = 0; - - for (String groupName: allGroupsNames) - { + for (String groupName: allGroupsNames) { // if searched name - return corresponding id - if(groupName.equals(name)) + if (groupName.equals(name)) return allGroupsIDs.get(i); i++; } @@ -1045,11 +837,9 @@ public class RadioActivity extends Activity { } // get Private Contact ID from Name - private int getPrivateImei4Name(String name) - { - for(Vehicle vehicle: allVehicle) - { - if(vehicle.name.equals(name)) + private int getPrivateImei4Name(String name) { + for (Vehicle vehicle: allVehicle) { + if (vehicle.name.equals(name)) return Integer.parseInt(vehicle.imei); } @@ -1057,46 +847,40 @@ public class RadioActivity extends Activity { } // get zone number from spinner zoneName - private int getNR4Zone(String zoneName) - { - for(Zone zone: crtZones) - if(zone.ZoneName.equals(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)) + 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() - { + private void GetGWRadios() { parentTab.getRadiosList(); } // send Dekey to AppServer - private void SendDekey() - { - if(parentTab.getCrtRadio() != null) + 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) - { + 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) - { + private void ReqRadioStatus(int RadioID, int GWID) { SM.Debug("Request radio status 4 radioId: " + RadioID + " | GW_ID:" + GWID); parentTab.getRadioStatus(GWID,RadioID); try { @@ -1114,14 +898,11 @@ public class RadioActivity extends Activity { //112 -prvcall stop //103 -grpcall init //113 -grpcall stop - public void SendPTT(int callType, int id,int gwid, int rgwid,long userID) - { + 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 + ""); @@ -1135,20 +916,17 @@ public class RadioActivity extends Activity { 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(); // CHANNEL_BRDCST if (action.equals(OperationCodes.CHANNEL_BRDCST + "")) { - Zone_and_channel zc = AppParams.crtZoneAndChannel; UpdateZoneCH(zc.rgwID, zc.gwID, zc.zoneNr, zc.channelNr); } // RADIOID_CHANGED if (action.equals(OperationCodes.RADIOID_CHANGED + "")) { - textViewGateway.setText(parentTab.getCrtRadio().IP); textViewChannel.setText(parentTab.getCrtRadio().getChannelName()); textViewZone.setText(parentTab.getCrtRadio().getZoneName()); @@ -1159,5 +937,4 @@ public class RadioActivity extends Activity { } } }; - } \ No newline at end of file diff --git a/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java b/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java index 0737249..2acf71b 100644 --- a/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java +++ b/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java @@ -74,6 +74,9 @@ import android.content.res.AssetManager; import android.content.res.Configuration; import android.content.res.Resources; import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; @@ -571,8 +574,59 @@ public class TabLayoutActivity extends AbstractSDParentActivity { /* Create Service and bind to it */ getApplicationContext().bindService(new Intent(this, TCPService.class), serviceConnection, Context.BIND_AUTO_CREATE); + + + ConnectivityManager connectivityManager = + (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + connectivityManager.registerDefaultNetworkCallback(networkCallback); + else { + NetworkRequest request = new NetworkRequest.Builder() + .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build(); + connectivityManager.registerNetworkCallback(request, networkCallback); + } } + private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { + @Override + public void onAvailable(Network network) { + // network available + Log.v(TAG, "network available"); + Log.v(TAG, "tcp: " + (tcp != null)); + + if (tcp != null) { + if (tcp.isConnectionUP) + tcp.isConnectionUP = false; + + uiHandler.post(() -> { + Log.v(TAG, "myService: " + (myService != null)); + if (myService != null) { + myService.stopTCPConnection(); + myService.recreateTCPConnection(); + + getApplicationContext().bindService(new Intent(context, TCPService.class), serviceConnection, Context.BIND_AUTO_CREATE); + if (getRadioActivity() != null) + getRadioActivity().onCreate(getRadioActivity().getSavedInstanceState()); + if (getMessageActivity() != null) + getMessageActivity().onCreate(getMessageActivity().getSavedInstanceState()); + if (getRecordingsActivity() != null) + getRecordingsActivity().onCreate(getRecordingsActivity().getSavedInstanceState()); + if (getAlarmActivity() != null) + getAlarmActivity().onCreate(getAlarmActivity().getSavedInstanceState()); + + uiHandler.post(initTCPRUN); + } + }); + } + } + + @Override + public void onLost(Network network) { + // network unavailable + Log.v(TAG, "network unavailable"); + } + }; + /** * Broadcast Received for notifications */ @@ -755,7 +809,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity { if (getHistoryActivity() != null) getHistoryActivity().setLanguage(); if (getRadioActivity() != null) - getRadioActivity().onCreate(getRadioActivity().savedInstanceState); + getRadioActivity().onCreate(getRadioActivity().getSavedInstanceState()); if (getMessageActivity() != null) getMessageActivity().onCreate(getMessageActivity().getSavedInstanceState()); if (getRecordingsActivity() != null) @@ -2074,7 +2128,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity { getApplicationContext().bindService(new Intent(context, TCPService.class), serviceConnection, Context.BIND_AUTO_CREATE); if (getRadioActivity() != null) - getRadioActivity().onCreate(getRadioActivity().savedInstanceState); + getRadioActivity().onCreate(getRadioActivity().getSavedInstanceState()); if (getMessageActivity() != null) getMessageActivity().onCreate(getMessageActivity().getSavedInstanceState()); if (getRecordingsActivity() != null) @@ -2098,7 +2152,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity { // whenBackPressed(AppParams.ActivityResult.tcpDown); // send a broadcast - notifyBroadcast(OperationCodes.TCP_CONNECTION_DOWN + ""); +// notifyBroadcast(OperationCodes.TCP_CONNECTION_DOWN + ""); } @Override