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 allRecordings = new ArrayList(); private ArrayList playingPositions = new ArrayList(); private ArrayList allGWsIP = new ArrayList(); 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 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)); Toast.makeText(context, getString(R.string.loadingRecordings), Toast.LENGTH_SHORT).show(); GetRecordings(parentTab.allRadios.get(which).GW_ID, parentTab.allRadios.get(which).ID); } }); AlertDialog alert = builder.create(); alert.show(); } }); textViewGateway.setVisibility(View.INVISIBLE); parentTab.recordingsActivity = 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 = dialog.findViewById(R.id.text); ImageView image = dialog.findViewById(R.id.image); image.setImageResource(R.mipmap.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.crtRadio != null) textViewGateway.setText(parentTab.crtRadio.IP); /* if(parentTab.allRadios == null) GetGWRadios(); */ if(playingPosition < 0 && parentTab.crtRadio != null) { SM.Debug("GetRecordings resume + crtRadio:"+parentTab.crtRadio.toString()); GetRecordings(parentTab.crtRadio.GW_ID, parentTab.crtRadio.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 list) { //SM.Debug("## UpdateRecordings: " + list.size()); allRecordings = new ArrayList(); 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(); for(int i=0; i