safedispatch-mobile/safeDispatch/src/main/java/com/safemobile/dispatch/RecordingsActivity.java

412 lines
13 KiB
Java

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.AssetFileDescriptor;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
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.radio.RadioGW;
import com.safemobile.lib.sound.RecordingHandle;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
/**
* fix import
*/
public class RecordingsActivity extends Activity {
private Context context;
private Activity activity;
public TabLayoutActivity parentTab;
public ArrayList<Recording> allRecordings = new ArrayList<Recording>();
private ArrayList<Boolean> playingPositions = new ArrayList<Boolean>();
private ArrayList<String> allGWsIP = new ArrayList<String>();
private GridView gridView;
private RecordingsGridViewAdapter adapter;
public View convertViewRecording;
public int playingPosition = -1;
// Need handler for callbacks to the UI thread
private final Handler myHandler = new Handler();
//recoding TCP and audio
private RecordingHandle recHandle = null;
/* Dialog */
private TextView textViewCount, textViewGateway;
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.tabrecordings);
// get context
context = this;
activity = this;
// change tab header fontface
TextView textView1 = (TextView) findViewById(R.id.textViewTitle);
textView1.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf"));
textView1.setTextSize(24);
gridView = (GridView) findViewById(R.id.gridViewRecordings);
adapter = new RecordingsGridViewAdapter(activity, context, allRecordings, playingPositions);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(onItemClickListener);
if(recHandle == null && !AppParams.DEMO)
recHandle = new RecordingHandle(AppParams.IP);
textViewCount = (TextView) findViewById(R.id.textViewCount);
updateNumberOfRecordings();
textViewGateway = (TextView) findViewById(R.id.textViewGateway);
textViewGateway.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf"));
textViewGateway.setTextSize(24);
textViewGateway.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
allGWsIP.clear();
for(RadioGW radio: AppParams.listRadios)
allGWsIP.add(radio.IP);
ArrayAdapter<String> 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));
Toast.makeText(context, getString(R.string.loadingRecordings), Toast.LENGTH_SHORT).show();
GetRecordings(parentTab.getAllRadios().get(which).GW_ID, parentTab.getAllRadios().get(which).ID);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
textViewGateway.setVisibility(View.INVISIBLE);
parentTab.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), 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();
}
/** Update the number of recordings displayed on the top layout */
private void updateNumberOfRecordings() {
textViewCount.setText("[" + AppParams.recordings.size() + "]");
}
public void showDialog(String errorMsg)
{
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("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();
}
@Override
public void onPause()
{
super.onPause();
SM.Debug("onPause");
}
@Override
public void onResume()
{
super.onResume();
if(parentTab.getTCPState() != null && !AppParams.DEMO){
Toast.makeText(context, getString(R.string.moreRecordings), Toast.LENGTH_SHORT).show();
if(parentTab.getCrtRadio() != null)
textViewGateway.setText(parentTab.getCrtRadio().IP);
/*
if(parentTab.allRadios == null)
GetGWRadios();
*/
if(playingPosition < 0 && parentTab.getCrtRadio() != null)
{
SM.Debug("GetRecordings resume + crtRadio:"+ parentTab.getCrtRadio().toString());
GetRecordings(parentTab.getCrtRadio().GW_ID, parentTab.getCrtRadio().ID);
}
}
SM.Debug("onResume");
}
private OnItemClickListener onItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
// disable grid scrolling and item click
gridView.setEnabled(false);
convertViewRecording = view;
// change background to playing
adapter.changePlaying(position, true);
// if no recording is playing and not DEMO
if(playingPosition<0 && !AppParams.DEMO)
{
// send recording request to App Server
SendPlayRequest(allRecordings.get(position).ID);
// flag that sound is needed
recHandle.StartSound();
recHandle.soundNeeded = true;
}
// no recording is playing and DEMO
else if(playingPosition<0 && AppParams.DEMO)
{
// create player which will play demo recordings
MediaPlayer player = new MediaPlayer();
AssetFileDescriptor afd;
try {
switch(position)
{
case 1: afd = getAssets().openFd("startwindows.mp3"); break;
case 2: afd = getAssets().openFd("exitwindows.mp3"); break;
default : afd = getAssets().openFd("mike.mp3"); break;
};
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
} catch (IOException e) { }
}
// save playing position
playingPosition = position;
// start the timer which will reset the UI to 'no recording playing'
startRecordingStoperTimer(allRecordings.get(playingPosition).endGMT - allRecordings.get(playingPosition).startGMT);
}
};
/** create Timer which will stop the recording after a specific time */
private void startRecordingStoperTimer(final int seconds) {
new Thread(new Runnable() {
@Override
public void run() {
// 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();
}
// modify the UI after the recording is done playing
myHandler.post(stopRecordingRUN);
}
}).start();
}
// Create runnable for posting
final Runnable stopRecordingRUN = new Runnable() {
@Override
public void run() {
updateStopRecording();
}
};
private void updateStopRecording()
{
// change playing icon
adapter.changePlaying(playingPosition, false);
// enable grid
gridView.setEnabled(true);
// set playing Recording position to -1
playingPosition = -1;
}
// Update Recordings received from AppServer
public void UpdateRecordings(ArrayList<Recording> list)
{
//SM.Debug("## UpdateRecordings: " + list.size());
allRecordings = new ArrayList<Recording>();
for(Recording rec : list)
{
if(rec.typeID == 1 && rec.subID == AppParams.USERID)
allRecordings.add(rec);
else if (rec.typeID != 1)
allRecordings.add(rec);
}
myHandler.post(UpdateResultsRUN);
}
// PlayRecording received from AppServer
public void PlayRecording(long id)
{
// change adapter image
}
// Create runnable for posting
final Runnable UpdateResultsRUN = new Runnable() {
public void run() {
updateResultsInUi();
}
};
private void updateResultsInUi()
{
// clear played items
playingPositions = new ArrayList<Boolean>();
for(int i=0; i<allRecordings.size(); i++)
playingPositions.add(true);
// set adapter - where playingPositions stores exists values
adapter = new RecordingsGridViewAdapter(activity, context, allRecordings, playingPositions);
// playing positions need to be false because no recording is played
playingPositions.clear();
for(int i=0; i<allRecordings.size(); i++)
playingPositions.add(false);
updateNumberOfRecordings();
gridView.setAdapter(adapter);
}
// send
private void GetRecordings(int radioGWID, int radioID)
{
parentTab.getRecordings(radioGWID, radioID);
}
public void SendPlayRequest(long record_id)
{
parentTab.sendPlayRecordingRequest(record_id);
}
/** 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) {
//SM.Debug("### NOTIFICATION ###", "Radio: " + intent.getAction());
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(parentTab.getCrtRadio().IP);
GetRecordings(parentTab.getCrtRadio().GW_ID, parentTab.getCrtRadio().ID);
}
}
};
}