407 lines
12 KiB
Java
407 lines
12 KiB
Java
package com.safemobile.safedispatch;
|
|
|
|
import android.app.Activity;
|
|
import android.app.AlertDialog;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.content.res.AssetFileDescriptor;
|
|
import android.content.res.Configuration;
|
|
import android.graphics.Typeface;
|
|
import android.media.MediaPlayer;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.view.View;
|
|
import android.widget.AdapterView;
|
|
import android.widget.AdapterView.OnItemClickListener;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.GridView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.safemobile.adapters.RecordingsGridViewAdapter;
|
|
import com.safemobile.lib.AppParams;
|
|
import com.safemobile.lib.OperationCodes;
|
|
import com.safemobile.lib.Recording;
|
|
import com.safemobile.lib.SM;
|
|
import com.safemobile.lib.Vehicle;
|
|
import com.safemobile.lib.radio.RadioGW;
|
|
import com.safemobile.lib.sound.RecordingHandle;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Locale;
|
|
|
|
public class RecordingsActivity extends Activity {
|
|
private Context context;
|
|
private Activity activity;
|
|
private TabLayoutActivity parentTab;
|
|
private ArrayList<Recording> allRecordings = new ArrayList<>();
|
|
private ArrayList<Boolean> playingPositions = new ArrayList<>();
|
|
private GridView gridView;
|
|
private RecordingsGridViewAdapter adapter;
|
|
private View convertViewRecording;
|
|
private boolean isRecordingPlaying = false;
|
|
|
|
private int playingPosition = -1;
|
|
|
|
// Need handler for callbacks to the UI thread
|
|
private final Handler myHandler = new Handler(Looper.getMainLooper());
|
|
private final ArrayList<String> allGWsIP = new ArrayList<>();
|
|
|
|
//recoding TCP and audio
|
|
private RecordingHandle recHandle = null;
|
|
|
|
/* Dialog */
|
|
private TextView textViewCount;
|
|
private TextView textViewGateway;
|
|
|
|
private Bundle savedInstanceState;
|
|
|
|
/**
|
|
* Called when the activity is first created.
|
|
*/
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
this.setSavedInstanceState(savedInstanceState);
|
|
setParentTab((TabLayoutActivity) getParent());
|
|
|
|
Locale locale = new Locale(AppParams.LANGUAGETMP);
|
|
Locale.setDefault(locale);
|
|
Configuration config = new Configuration();
|
|
config.locale = locale;
|
|
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
|
|
|
|
setContentView(R.layout.tabrecordings);
|
|
|
|
// get context
|
|
context = this;
|
|
activity = this;
|
|
|
|
// change tab header fontFace
|
|
TextView textView1 = findViewById(R.id.textViewTitle);
|
|
textView1.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf"));
|
|
textView1.setTextSize(24);
|
|
|
|
gridView = findViewById(R.id.gridViewRecordings);
|
|
adapter = new RecordingsGridViewAdapter(activity, getAllRecordings(), playingPositions);
|
|
gridView.setAdapter(adapter);
|
|
|
|
gridView.setOnItemClickListener(onItemClickListener);
|
|
gridView.setOnTouchListener((v, event) -> isRecordingPlaying);
|
|
|
|
if (recHandle == null && !AppParams.DEMO)
|
|
recHandle = new RecordingHandle(AppParams.IP);
|
|
|
|
textViewCount = findViewById(R.id.textViewCount);
|
|
updateNumberOfRecordings();
|
|
|
|
textViewGateway = findViewById(R.id.textViewGateway);
|
|
textViewGateway.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf"));
|
|
textViewGateway.setTextSize(24);
|
|
textViewGateway.setOnClickListener(v -> {
|
|
allGWsIP.clear();
|
|
for (RadioGW radio : AppParams.listRadios)
|
|
allGWsIP.add(radio.IP);
|
|
|
|
ArrayAdapter<String> 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,
|
|
(dialog, which) -> {
|
|
// change gateway
|
|
textViewGateway.setText(allGWsIP.get(which));
|
|
Toast.makeText(context, getString(R.string.loadingRecordings), Toast.LENGTH_SHORT).show();
|
|
getRecordings(getParentTab().getAllRadios().get(which).GW_ID, getParentTab().getAllRadios().get(which).ID);
|
|
});
|
|
AlertDialog alert = builder.create();
|
|
alert.show();
|
|
});
|
|
textViewGateway.setVisibility(View.INVISIBLE);
|
|
|
|
getParentTab().setRecordingsActivity(this);
|
|
|
|
// register to receive broadcasts
|
|
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), (dialog, id) -> getParentTab().whenBackPressed(AppParams.ActivityResult.logout))
|
|
.setPositiveButton(getString(R.string.ext), (dialog, id) -> getParentTab().whenBackPressed(AppParams.ActivityResult.exit))
|
|
.setNegativeButton(getString(R.string.cancel), (dialog, id) -> dialog.cancel());
|
|
AlertDialog alert = builder.create();
|
|
alert.show();
|
|
}
|
|
|
|
/**
|
|
* Update the number of recordings displayed on the top layout
|
|
*/
|
|
private void updateNumberOfRecordings() {
|
|
textViewCount.setText("[" + AppParams.recordings.size() + "]");
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
SM.Debug("onPause");
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (getParentTab().getTCPState() != null && !AppParams.DEMO) {
|
|
|
|
Toast.makeText(context, getString(R.string.moreRecordings), Toast.LENGTH_SHORT).show();
|
|
|
|
if (getParentTab().getCrtRadio() != null)
|
|
textViewGateway.setText(getParentTab().getCrtRadio().IP);
|
|
|
|
if (getPlayingPosition() < 0 && getParentTab().getCrtRadio() != null) {
|
|
SM.Debug("getRecordings resume + crtRadio:" + getParentTab().getCrtRadio().toString());
|
|
getRecordings(getParentTab().getCrtRadio().GW_ID, getParentTab().getCrtRadio().ID);
|
|
}
|
|
}
|
|
SM.Debug("onResume");
|
|
}
|
|
|
|
|
|
|
|
private final OnItemClickListener onItemClickListener = new OnItemClickListener() {
|
|
@Override
|
|
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
|
|
|
|
// disable grid scrolling and item click
|
|
if (isRecordingPlaying)
|
|
return;
|
|
|
|
isRecordingPlaying = true;
|
|
setConvertViewRecording(view);
|
|
|
|
// change background to playing
|
|
adapter.changePlaying(position, true);
|
|
|
|
// if no recording is playing and not DEMO
|
|
if (getPlayingPosition() < 0 && !AppParams.DEMO) {
|
|
// send recording request to App Server
|
|
sendPlayRequest(getAllRecordings().get(position).id);
|
|
// flag that sound is needed
|
|
recHandle.startSound();
|
|
recHandle.setSoundNeeded(true);
|
|
}
|
|
// no recording is playing and DEMO
|
|
else if (getPlayingPosition() < 0 && AppParams.DEMO) {
|
|
// create player which will play demo recordings
|
|
MediaPlayer player = new MediaPlayer();
|
|
|
|
AssetFileDescriptor assetFileDescriptor = null;
|
|
try {
|
|
switch (position) {
|
|
case 1:
|
|
assetFileDescriptor = getAssets().openFd("startwindows.mp3");
|
|
break;
|
|
case 2:
|
|
assetFileDescriptor = getAssets().openFd("exitwindows.mp3");
|
|
break;
|
|
default:
|
|
assetFileDescriptor = getAssets().openFd("mike.mp3");
|
|
break;
|
|
}
|
|
player.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
|
|
player.prepare();
|
|
player.start();
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if(assetFileDescriptor != null) {
|
|
try {
|
|
assetFileDescriptor.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// save playing position
|
|
setPlayingPosition(position);
|
|
|
|
// start the timer which will reset the UI to 'no recording playing'
|
|
startRecordingStopperTimer(getAllRecordings().get(getPlayingPosition()).endGMT - getAllRecordings().get(getPlayingPosition()).startGMT);
|
|
}
|
|
|
|
/**
|
|
* create Timer which will stop the recording after a specific time
|
|
*/
|
|
private void startRecordingStopperTimer(final int seconds) {
|
|
new Thread(() -> {
|
|
// sleep for at least 1 second
|
|
final int ms = (seconds > 1 ? seconds * 1000 : 1000);
|
|
|
|
// sleep for amount of time
|
|
try {
|
|
Thread.sleep(ms);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
Thread.currentThread().interrupt();
|
|
}
|
|
|
|
// modify the UI after the recording is done playing
|
|
myHandler.post(stopRecordingRUN);
|
|
}).start();
|
|
}
|
|
};
|
|
|
|
// Create runnable for posting
|
|
final Runnable stopRecordingRUN = this::updateStopRecording;
|
|
|
|
private void updateStopRecording() {
|
|
// change playing icon
|
|
adapter.changePlaying(getPlayingPosition(), false);
|
|
|
|
// enable grid
|
|
isRecordingPlaying = false;
|
|
|
|
// set playing Recording position to -1
|
|
setPlayingPosition(-1);
|
|
}
|
|
|
|
// Update Recordings received from AppServer
|
|
public void updateRecordings(ArrayList<Recording> list) {
|
|
setAllRecordings(new ArrayList<>());
|
|
for (Recording rec : list) {
|
|
getAllRecordings().add(rec);
|
|
}
|
|
myHandler.post(updateResultsRUN);
|
|
}
|
|
|
|
// playRecording received from AppServer
|
|
public void playRecording(long id) {
|
|
// change adapter image
|
|
}
|
|
|
|
// Create runnable for posting
|
|
final Runnable updateResultsRUN = this::updateResultsInUi;
|
|
|
|
private void updateResultsInUi() {
|
|
// clear played items
|
|
playingPositions = new ArrayList<>();
|
|
|
|
ArrayList<Recording> recordings = getAllRecordings();
|
|
ArrayList<Recording> filteredList = new ArrayList<>();
|
|
ArrayList<Vehicle> vehicles = getParentTab().getAllVehicle();
|
|
for (Recording recording : recordings) {
|
|
if (recording.callType == 0 || recording.callType == 4) {
|
|
for (Vehicle vehicle : vehicles)
|
|
if (vehicle.imei.equals(String.valueOf(recording.subID))) {
|
|
filteredList.add(recording);
|
|
break;
|
|
}
|
|
} else {
|
|
filteredList.add(recording);
|
|
}
|
|
}
|
|
|
|
setAllRecordings(filteredList);
|
|
|
|
for (int i = 0; i < getAllRecordings().size(); i++)
|
|
playingPositions.add(true);
|
|
|
|
adapter = new RecordingsGridViewAdapter(activity, getAllRecordings(), playingPositions);
|
|
|
|
// playing positions need to be false because no recording is played
|
|
playingPositions.clear();
|
|
for (int i = 0; i < getAllRecordings().size(); i++)
|
|
playingPositions.add(false);
|
|
|
|
updateNumberOfRecordings();
|
|
|
|
gridView.setAdapter(adapter);
|
|
}
|
|
|
|
// send
|
|
private void getRecordings(int radioGWID, int radioID) {
|
|
getParentTab().getRecordings(radioGWID, radioID);
|
|
}
|
|
|
|
public void sendPlayRequest(long recordId) {
|
|
getParentTab().sendPlayRecordingRequest(recordId);
|
|
}
|
|
|
|
/**
|
|
* Register for broadcasts
|
|
*/
|
|
private void registerBroadcastIntents() {
|
|
IntentFilter intentFilter = new IntentFilter(OperationCodes.RECORDINGS_LIST_REP + "");
|
|
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) {
|
|
final String action = intent.getAction();
|
|
// zone and/or channel changed
|
|
if (action.equals(OperationCodes.RECORDINGS_LIST_REP + "")) {
|
|
updateRecordings(AppParams.recordings);
|
|
updateNumberOfRecordings();
|
|
} else if (action.equals(OperationCodes.RADIOID_CHANGED + "")) {
|
|
textViewGateway.setText(getParentTab().getCrtRadio().IP);
|
|
getRecordings(getParentTab().getCrtRadio().GW_ID, getParentTab().getCrtRadio().ID);
|
|
}
|
|
}
|
|
};
|
|
|
|
public TabLayoutActivity getParentTab() {
|
|
return parentTab;
|
|
}
|
|
|
|
public void setParentTab(TabLayoutActivity parentTab) {
|
|
this.parentTab = parentTab;
|
|
}
|
|
|
|
public ArrayList<Recording> getAllRecordings() {
|
|
return allRecordings;
|
|
}
|
|
|
|
public void setAllRecordings(ArrayList<Recording> allRecordings) {
|
|
this.allRecordings = allRecordings;
|
|
}
|
|
|
|
public View getConvertViewRecording() {
|
|
return convertViewRecording;
|
|
}
|
|
|
|
public void setConvertViewRecording(View convertViewRecording) {
|
|
this.convertViewRecording = convertViewRecording;
|
|
}
|
|
|
|
public int getPlayingPosition() {
|
|
return playingPosition;
|
|
}
|
|
|
|
public void setPlayingPosition(int playingPosition) {
|
|
this.playingPosition = playingPosition;
|
|
}
|
|
|
|
public Bundle getSavedInstanceState() {
|
|
return savedInstanceState;
|
|
}
|
|
|
|
public void setSavedInstanceState(Bundle savedInstanceState) {
|
|
this.savedInstanceState = savedInstanceState;
|
|
}
|
|
} |