package com.safemobile.dispatch; import java.util.ArrayList; import java.util.Locale; /** fix import*/ import com.safemobile.adapters.AlertGridViewAdapter; import com.safemobile.adapters.AlertGridViewAdapter.ViewHolder; import com.safemobile.lib.Alarm; import com.safemobile.lib.AppParams; import com.safemobile.lib.OperationCodes; import com.safemobile.lib.SM; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.res.Configuration; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.AdapterView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; public class AlarmActivity extends Activity { private Context context; public TabLayoutActivity parentTab; public ArrayList allAlarms = new ArrayList(); private ArrayList acknowledged = new ArrayList(); private AlertGridViewAdapter adapter; /* Visual resources */ private GridView gridView; public View convertViewAlarm; private int ack_position; public Bundle savedInstanceState; // Need handler for callbacks to the UI thread private final Handler myHandler = new Handler(); /** Called when the activity is first created. */ @Override 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()); context = this; setContentView(R.layout.tabalarm); gridView = (GridView) findViewById(R.id.gridViewAlarms); adapter = new AlertGridViewAdapter(this, allAlarms, context, acknowledged); gridView.setAdapter(adapter); gridView.setOnItemClickListener(onItemClickListener); // change tab header fontface TextView textView1 = (TextView) findViewById(R.id.textView1); textView1.setTypeface(Typeface.createFromAsset(getAssets(), "Sketch_Block.ttf")); textView1.setTextSize(24); parentTab.alarmActivity = this; } @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(); if(!AppParams.DEMO) GetAlarms(); else if (AppParams.DEMO) { for(int i=allAlarms.size()-1; i>=0; i--) { Alarm alarm = allAlarms.get(i); if(alarm.ack==1) allAlarms.remove(i); } acknowledged = new ArrayList(); for(int i=0;i arg0, View arg1, int position, long arg3) { SM.Debug(" ### CLICK Alarm : " + position); convertViewAlarm = arg1; // save position ack_position = position; //Toast.makeText(context, "SETACK " + idx, 1000).show(); if(!AppParams.DEMO) setACK(allAlarms.get(position).idx,allAlarms.get(position).type); else { allAlarms.get(position).ack = 1; updateResultsInUi("else"); } } }; // Update Alarms received from AppServer public void UpdateAlarms(ArrayList list) { SM.Debug("## UpdateAlarms: " + list.size()); allAlarms = list; acknowledged = new ArrayList(); // set acknowledged based on alarm.ack value for(Alarm alarm: allAlarms) { acknowledged.add((alarm.ack == 1)? true: false); SM.Debug("ALARM", "SC_ID> " + alarm.sc_id + " | " + (parentTab.VehHashbySc_id.get(alarm.sc_id) == null ? "null" : parentTab.VehHashbySc_id.get(alarm.sc_id).name )); if (parentTab.VehHashbySc_id.get(alarm.sc_id)!=null) alarm.unitName = parentTab.VehHashbySc_id.get(alarm.sc_id).name; /*Enumeration keylist = parentTab.SuperVehHash.keys(); while(keylist.hasMoreElements()) { SuperVehicle tmp = (SuperVehicle)((parentTab.SuperVehHash.get((long)keylist.nextElement()))); if (tmp.sc_id==alarm.sc_id) { alarm.unitName = tmp.name; break; } }*/ } myHandler.post(UpdateResultsRUN); } // Create runnable for posting final Runnable UpdateResultsRUN = new Runnable() { public void run() { updateResultsInUi("alarm"); } }; private void updateResultsInUi(String param) { if(param.equals("alarm")) { // set adapter adapter = new AlertGridViewAdapter(this, allAlarms, context, acknowledged); gridView.setAdapter(adapter); } else { adapter.changeACK(ack_position); SM.Debug("Set ACK: " + ack_position + " | " + (acknowledged.get(ack_position) ? "true": "false")); ViewHolder viewAlarm = new ViewHolder(); viewAlarm = (ViewHolder) convertViewAlarm.getTag(); switch(acknowledged.get(ack_position) ? 1 : 0) { case 1: viewAlarm.imageViewAlert.setImageResource(R.drawable.alert_off); //view.imgViewIcon.setImageDrawable(adapter.convertToGrayscale(activity.getResources().getDrawable(liveVehicle.get(position).getSmallIcon()))); break; case 0: //view.imgViewAlarm.setImageResource(R.drawable.siren); //view.imgViewIcon.setImageResource(liveVehicle.get(position).getSmallIcon()); break; } } } // update ACK received from AppServer public void UpdateACK() { myHandler.post(UpdateAckRUN); } // Create runnable for posting final Runnable UpdateAckRUN = new Runnable() { public void run() { SM.Debug(" UpdateACK: "); updateResultsInUi("adapter"); } }; // send to AppServer private void GetAlarms() { parentTab.executeNetworkStuff(new String[]{OperationCodes.GetAlarms +"", AppParams.USERID + ""}); //parentTab.getAlarms(AppParams.USERID); } // send ACK to AppServer private void setACK(int idx, int type) { parentTab.executeNetworkStuff(new String[]{OperationCodes.SendAlarmAcknoledge +"", idx + "", type + ""}); //parentTab.sendAlarmAcknoledge(idx, type); } }