Advertisement
moonlightcheese

Untitled

Jul 18th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.99 KB | None | 0 0
  1. package com.conceptualsystems.kitmobile;
  2.  
  3. import java.util.*;
  4.  
  5. import android.app.*;
  6. import android.os.*;
  7. import android.content.*;
  8. import android.database.*;
  9. import android.database.sqlite.*;
  10. import android.widget.*;
  11. import android.view.*;
  12. import android.util.Log;
  13.  
  14. import com.moonlightcheese.btsrv.*;
  15.  
  16. public class ShipActivity extends Activity
  17. {
  18.     //UI elements n stuff
  19.     private ListView mShipKitListView;
  20.     private LayoutInflater mInflater;
  21.     protected SharedPreferences mPrefs;
  22.     private ShipKitAdapter mShipKitAdapter;
  23.     private DbOpenHelper mDbHelper;
  24.     private SQLiteDatabase mDb;
  25.     private Cursor mShipCursor;
  26.     private TextView mTotalGross;
  27.     private TextView mTotalNet;
  28.     private TextView mTargetWeight;
  29.     private TextView mRemainingWeight;
  30.    
  31.     private String mDrvIdTX;
  32.     private String mCustIdTX;
  33.    
  34.     //constants
  35.     private String LOG_TAG = "KitMobile - ShipActivity.java";
  36.     private final static int DIALOG_BT_RECONNECT = 1;
  37.     private final static int DIALOG_TARGET_WEIGHT = 2;
  38.     private final static int DIALOG_ETX = 3;
  39.     private final static int DIALOG_FAIL = 4;
  40.     private final static int DIALOG_TX_PROGRESS = 5;
  41.     private final static int DIALOG_TRANSMIT = 6;
  42.     private final static int DIALOG_INVALID_KIT = 7;
  43.     private static String TX_FAIL_TEXT = "error";
  44.    
  45.     /** Called when the activity is first created. */
  46.     @Override
  47.     public void onCreate(Bundle savedInstanceState)
  48.     {
  49.         super.onCreate(savedInstanceState);
  50.        
  51.         mPrefs = getSharedPreferences("kitmobileprefs", Context.MODE_PRIVATE);
  52.         mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  53.         mDbHelper = new DbOpenHelper(getApplicationContext());
  54.         mDb = mDbHelper.getReadableDatabase();
  55.        
  56.         //data init
  57.         //mShipKitList = new ArrayList<Map<String, String>>();
  58.        
  59.         initScreen();
  60.        
  61.         if(!mPrefs.contains("targetweight")) {
  62.             showDialog(DIALOG_TARGET_WEIGHT);
  63.         } else {
  64.             new SummaryTask().execute();
  65.         }
  66.     }
  67.    
  68.     @Override
  69.     public void onDestroy() {
  70.         super.onDestroy();
  71.         if(mShipCursor!=null)
  72.             mShipCursor.close();
  73.         if(mDb!=null)
  74.             mDb.close();
  75.     }
  76.    
  77.     @Override
  78.     public void onResume() {
  79.         super.onResume();
  80.         IntentFilter filter = new IntentFilter();
  81.         filter.addAction(ScannerService.ACTION_READ_SCANNER);
  82.         filter.addAction(ScannerService.ACTION_REQUEST_RECONNECT);
  83.         registerReceiver(mReceiver, filter);
  84.         mShipCursor.requery();
  85.     }
  86.    
  87.     @Override
  88.     public void onPause() {
  89.         super.onPause();
  90.         unregisterReceiver(mReceiver);
  91.     }
  92.    
  93.     private final Runnable txRunnable = new Runnable() {
  94.         public void run() {
  95.             Bundle b = new Bundle();
  96.             String tm = prepareData();
  97.             boolean sent = false;
  98.             sent = sendData(tm);
  99.             if(sent)
  100.                 b.putBoolean("success", true);
  101.             else
  102.                 b.putBoolean("success", false);
  103.             Message m = new Message();
  104.             m.setData(b);
  105.             txHandle.sendMessage(m);
  106.         }
  107.     };
  108.    
  109.     private final Handler txHandle = new Handler() {
  110.         @Override
  111.         public void handleMessage(Message msg) {
  112.             boolean success = msg.getData().getBoolean("success");
  113.             dismissDialog(DIALOG_TX_PROGRESS);
  114.             if(success) {
  115.                 clearFields();
  116.                 finish();
  117.             } else {
  118.                 removeDialog(DIALOG_FAIL);
  119.                 showDialog(DIALOG_FAIL);
  120.             }
  121.            
  122.         }
  123.     };
  124.    
  125.     public void clearFields() {
  126.         mDbHelper.resetShipment(mDb);
  127.         SharedPreferences.Editor edit = mPrefs.edit();
  128.         edit.remove("targetweight");
  129.         edit.commit();
  130.     }
  131.    
  132.     /** inner classes **/
  133.     public class ShipKitAdapter extends CursorAdapter {
  134.        
  135.         public ShipKitAdapter(Context context, Cursor cur) {
  136.             super(context, cur, true);
  137.         }
  138.        
  139.         public void bindView(View view, Context context, Cursor cursor) {
  140.             try {
  141.                 ((TextView)view.findViewById(R.id.kit_id)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID)));
  142.                 ((TextView)view.findViewById(R.id.net_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET)));
  143.                 ((TextView)view.findViewById(R.id.net_uom)).setText("lbs");
  144.                 ((TextView)view.findViewById(R.id.product_name)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME)));
  145.                 ((TextView)view.findViewById(R.id.gross_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS)));
  146.                 ((TextView)view.findViewById(R.id.gross_uom)).setText("lbs");
  147.             } catch(Exception e) {
  148.                 Log.i(LOG_TAG, "error getting a field: "+e.getMessage());
  149.             }
  150.         }
  151.        
  152.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  153.             return mInflater.inflate(R.layout.ship_list_item, parent, false);
  154.         }
  155.     };
  156.    
  157.     private class DbOpenHelper extends SQLiteOpenHelper {
  158.        
  159.         DbOpenHelper(Context context) {
  160.             //
  161.             super(context,DbSchema.DATABASE_NAME, null, DbSchema.DATABASE_VERSION);
  162.         }
  163.        
  164.         @Override
  165.         public void onCreate(SQLiteDatabase db) {
  166.         }
  167.        
  168.         @Override
  169.         public void onOpen(SQLiteDatabase db) {
  170.             //open db
  171.         }
  172.        
  173.         @Override
  174.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  175.             //upgrade db
  176.         }
  177.        
  178.         public void resetShipment(SQLiteDatabase db) {
  179.             db.execSQL(DbSchema.ShipmentSchema.DROP_TABLE);
  180.             db.execSQL(DbSchema.ShipmentSchema.CREATE_TABLE);
  181.         }
  182.        
  183.         public void resetCustomers(SQLiteDatabase db) {
  184.             db.execSQL(DbSchema.CustomerSchema.DROP_TABLE);
  185.             db.execSQL(DbSchema.CustomerSchema.CREATE_TABLE);
  186.         }
  187.        
  188.         public void resetKits(SQLiteDatabase db) {
  189.             db.execSQL(DbSchema.KitSchema.DROP_TABLE);
  190.             db.execSQL(DbSchema.KitSchema.CREATE_TABLE);
  191.         }
  192.        
  193.         public void resetProducts(SQLiteDatabase db) {
  194.             db.execSQL(DbSchema.ProductSchema.DROP_TABLE);
  195.             db.execSQL(DbSchema.ProductSchema.CREATE_TABLE);
  196.         }
  197.     }
  198.    
  199.     // Create a BroadcastReceiver for ACTION_FOUND, ACTION_STATE_CHANGED, ACTION_DISCOVERY_FINISHED
  200.     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  201.         public void onReceive(Context context, Intent intent) {
  202.             String action = intent.getAction();
  203.             if (ScannerService.ACTION_READ_SCANNER.equals(action)) {
  204.                 String extra = intent.getStringExtra("scannerRead");
  205.                 Log.i(LOG_TAG, "read message: " + extra);
  206.                 //if this kit id exists, add it to the list.
  207.                 new ScanTask().execute(extra);
  208.             }
  209.             if(ScannerService.ACTION_REQUEST_RECONNECT.equals(action)) {
  210.                 //ask the user real nice if they want the bluetooth scanner reconnected after a disconnect
  211.                 Log.i(LOG_TAG, "requested reconnect!");
  212.                 //removeDialog(DIALOG_BT_RECONNECT);
  213.                 showDialog(DIALOG_BT_RECONNECT);
  214.             }
  215.         }
  216.     };
  217.    
  218.     private class ScanTask extends AsyncTask<String, Void, Boolean> {
  219.         String kitId;
  220.         int fail_code = 0;
  221.         //valid failure codes for this AsyncTask
  222.         final static int FAILURE_NO_KIT = 1;
  223.         final static int FAILURE_DUPLICATE = 2;
  224.        
  225.         protected Boolean doInBackground(String... kitIds) {
  226.             if(kitIds != null && !kitIds[0].equals("") && mDb!=null) {
  227.                 kitId = kitIds[0];
  228.                
  229.                 //check for the existence of the kit in the kit list
  230.                 Cursor kitCursor;
  231.                 String kitquery = "SELECT * FROM " + DbSchema.KitSchema.TABLE_NAME + " WHERE " + DbSchema.KitSchema.COLUMN_ID + "='" + kitId + "'";
  232.                 kitCursor = mDb.rawQuery(kitquery, null);
  233.                 if(kitCursor.getCount()>0) {
  234.                     //we're cool, keep on truckin'
  235.                 } else {
  236.                     Log.e(LOG_TAG, "kit was not added.  no such kit exists.  try using 'resync kits'");
  237.                     fail_code = FAILURE_NO_KIT;
  238.                     return false;
  239.                 }
  240.                
  241.                 //check if the kit is already in the shipment list.  we don't want it in there twice.
  242.                 String shipquery = "SELECT * FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
  243.                         " WHERE " +DbSchema.ShipmentSchema.COLUMN_ID + "='" + kitId + "'";
  244.                 Cursor shipmentCursor = mDb.rawQuery(shipquery, null);
  245.                 if(shipmentCursor.getCount()<=0) {  //if does not exist
  246.                     //we're cool, keep on truckin'
  247.                 } else {        //record already exists, don't add it to the shipment list twice.
  248.                     Log.e(LOG_TAG, "kit was not added.  record already exists in shipment.");
  249.                     fail_code = FAILURE_DUPLICATE;
  250.                     return false;
  251.                 }
  252.                
  253.                 //insert the record into the database
  254.                 ContentValues cv = new ContentValues();
  255.                 cv.put(DbSchema.ShipmentSchema.COLUMN_ID, kitId);
  256.                 mDb.insert(DbSchema.ShipmentSchema.TABLE_NAME, null, cv);
  257.             } else {
  258.                 //null kit id
  259.                 Log.e(LOG_TAG, "kitId was null in ScanTask");
  260.                 return false;
  261.             }
  262.            
  263.             return true;
  264.         }
  265.        
  266.         protected void onPostExecute(Boolean exists) {
  267.             if(exists) {
  268.                 Log.i(LOG_TAG, "adding kit to ship list...");
  269.                 refreshShipKitListView();
  270.                 Log.i(LOG_TAG, "added.");
  271.             } else {
  272.                 processFailCode(fail_code);
  273.             }
  274.         }
  275.     };
  276.    
  277.     public void processFailCode(int fail_code) {
  278.         switch(fail_code) {
  279.             case ScanTask.FAILURE_NO_KIT:
  280.                 //do things
  281.                 Toast.makeText(this, "Not a valid kit", Toast.LENGTH_SHORT).show();
  282.                 showDialog(DIALOG_INVALID_KIT);
  283.                 break;
  284.             case ScanTask.FAILURE_DUPLICATE:
  285.                 //do things
  286.                 Toast.makeText(this, "Kit is already in the list", Toast.LENGTH_SHORT).show();
  287.                 break;
  288.             default:
  289.                 //
  290.                 break;
  291.         }
  292.     }
  293.    
  294.     private class SummaryTask extends AsyncTask<Void, Void, Boolean> {
  295.         Integer totalGross = 0;
  296.         Integer totalNet = 0;
  297.         Integer maxWeight = 0;
  298.        
  299.         protected Boolean doInBackground(Void... unused) {
  300.             //get the information from the kits
  301.             mShipCursor.moveToFirst();
  302.             do {
  303.                 try {
  304.                     Log.i(LOG_TAG, "Kit: "+mShipCursor.getInt(mShipCursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID)));
  305.                     totalGross += mShipCursor.getInt(mShipCursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS));
  306.                     totalNet += mShipCursor.getInt(mShipCursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET));
  307.                     Log.i(LOG_TAG, ""+totalGross);
  308.                     Log.i(LOG_TAG, ""+totalNet);
  309.                 } catch(Exception e) {
  310.                     //Log.i(LOG_TAG, "problem fetching Cursor row: "+mShipCursor.getPosition());
  311.                     Log.i(LOG_TAG, "problem fetching Cursor row");
  312.                 }
  313.             } while(mShipCursor.moveToNext());
  314.            
  315.             maxWeight = mPrefs.getInt("targetweight", 0);
  316.            
  317.             return true;
  318.         }
  319.        
  320.         protected void onPostExecute(Boolean exists) {
  321.             if(totalGross>0)
  322.                 mTotalGross.setText(totalGross.toString());
  323.             else
  324.                 mTotalGross.setText("0");
  325.             if(totalNet>0)
  326.                 mTotalNet.setText(totalNet.toString());
  327.             else
  328.                 mTotalNet.setText("0");
  329.             Integer remWeight = maxWeight;
  330.             remWeight -= totalGross;
  331.             mRemainingWeight.setText(remWeight.toString());
  332.             mTargetWeight.setText(maxWeight.toString());
  333.         }
  334.     };
  335.    
  336.     public void refreshShipKitListView() {
  337.         mShipCursor.requery();
  338.         new SummaryTask().execute();
  339.     }
  340.    
  341.     private void initScreen() {
  342.         setContentView(R.layout.ship);
  343.         mTargetWeight = (TextView)findViewById(R.id.target_weight);
  344.         mRemainingWeight = (TextView)findViewById(R.id.remaining_weight);
  345.         mTotalGross = (TextView)findViewById(R.id.gross_weight);
  346.         mTotalNet = (TextView)findViewById(R.id.net_weight);
  347.         mShipKitListView = (ListView)findViewById(R.id.kit_list);
  348.         String screenquery = "SELECT " +
  349.             DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema._ID+" AS " + DbSchema.ShipmentSchema._ID + ", " +
  350.             DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID+" AS " + DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID + ", " +
  351.             DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_FIN_ID + ", " +
  352.             DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_GROSS+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS + ", " +
  353.             DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_NET+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET + ", " +
  354.             DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_NAME+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME + ", " +
  355.             DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_ID +
  356.             " FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
  357.             " LEFT OUTER JOIN " + DbSchema.KitSchema.TABLE_NAME +
  358.             " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_ID+"="+DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID +
  359.             " LEFT OUTER JOIN " + DbSchema.ProductSchema.TABLE_NAME +
  360.             " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+"="+DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID +
  361.             " ORDER BY " +DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema._ID + " DESC";
  362.         mShipCursor = mDb.rawQuery(screenquery, null);
  363.         mShipKitAdapter = new ShipKitAdapter(
  364.             ShipActivity.this,
  365.             mShipCursor
  366.         );
  367.         mShipKitListView.setAdapter(mShipKitAdapter);
  368.         final SQLiteDatabase mDbFinal = mDb;
  369.         final Cursor kitCursor = mShipKitAdapter.getCursor();
  370.         mShipKitListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  371.             public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  372.                 //long press deletes a shipment record.
  373.                 kitCursor.moveToPosition(position);
  374.                 String kit_id = kitCursor.getString(kitCursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID));
  375.                 mDb.delete(DbSchema.ShipmentSchema.TABLE_NAME, DbSchema.ShipmentSchema.COLUMN_ID+"=?", new String[] {kit_id} );
  376.                 refreshShipKitListView();
  377.                 return true;
  378.             }
  379.         });
  380.     }
  381.    
  382.     private void addFakeListItem(String id) {
  383.         int position = mShipKitAdapter.getCount();
  384.         Map<String, String> m = new HashMap<String, String>();
  385.         m.put("kit_id", id);
  386.         m.put("net_units", "5027");
  387.         m.put("net_uom", "lbs");
  388.         m.put("gross_units", "5155");
  389.         m.put("gross_uom", "lbs");
  390.         m.put("product_name", "COPPER #1");
  391.         //mShipKitAdapter.add(m);
  392.        
  393.         mShipKitAdapter.notifyDataSetChanged();
  394.     }
  395.    
  396.     // OPTIONS MENU
  397.     ///////////////
  398.     public boolean onCreateOptionsMenu(Menu menu) {
  399.         MenuInflater inflater = getMenuInflater();
  400.         inflater.inflate(R.menu.ship_options, menu);
  401.         return true;
  402.     }
  403.    
  404.     public boolean onOptionsItemSelected(MenuItem item) {
  405.         switch(item.getItemId()) {
  406.             case R.id.opt_clear:
  407.                 //add a fake item to the list
  408.                 clearFields();
  409.                 finish();
  410.                 return true;
  411.             case R.id.opt_transmit:
  412.                 //refreshShipKitListView();
  413.                 removeDialog(DIALOG_TRANSMIT);
  414.                 showDialog(DIALOG_TRANSMIT);
  415.                 return true;
  416.             case R.id.opt_refresh:
  417.                 new SummaryTask().execute();
  418.                 return true;
  419.             default:
  420.                 super.onOptionsItemSelected(item);
  421.                 return true;
  422.         }
  423.     }
  424.    
  425.     // DIALOG STUFF
  426.     ///////////////
  427.     @Override
  428.     protected Dialog onCreateDialog(int id, Bundle b) {
  429.         switch(id) {
  430.             case DIALOG_BT_RECONNECT:
  431.             {
  432.                 Dialog dialog = new Dialog(this);
  433.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  434.                 builder.setTitle("Scanner Error");
  435.                 builder.setMessage("Scanner has been disconnected!");
  436.                 builder.setPositiveButton("Reconnect", new DialogInterface.OnClickListener() {
  437.                     public void onClick(DialogInterface dialog, int id) {
  438.                         Intent reconnectIntent = new Intent();
  439.                         reconnectIntent.setAction(ScannerService.ACTION_RECONNECT);
  440.                         sendBroadcast(reconnectIntent);
  441.                     }
  442.                 });
  443.                 dialog = builder.create();
  444.                 return dialog;
  445.             }
  446.             case DIALOG_TARGET_WEIGHT:
  447.             {
  448.                 Dialog dialog = new Dialog(this);
  449.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  450.                 final View dialogLayout = mInflater.inflate(R.layout.dialog_target_weight, null);
  451.                 builder.setTitle("Max Weight");
  452.                 builder.setView(dialogLayout);
  453.                 final EditText target = (EditText)dialogLayout.findViewById(R.id.target_weight);
  454.                 final SharedPreferences prefFinal = mPrefs;
  455.                 builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
  456.                     public void onClick(DialogInterface dialog, int id) {
  457.                         SharedPreferences.Editor edit = prefFinal.edit();
  458.                         edit.putInt("targetweight", new Integer(target.getText().toString()));
  459.                         new SummaryTask().execute();
  460.                         edit.commit();
  461.                         dialog.dismiss();
  462.                     }
  463.                 });
  464.                
  465.                 dialog = builder.create();
  466.                 return dialog;
  467.             }
  468.             case DIALOG_FAIL:
  469.             {
  470.                 Dialog dialog = new Dialog(this);
  471.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  472.                 builder.setMessage(TX_FAIL_TEXT);
  473.                 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  474.                         public void onClick(DialogInterface dialog, int id) {
  475.                             dialog.dismiss();
  476.                         }
  477.                     });
  478.                 dialog = builder.create();
  479.                 return dialog;
  480.             }
  481.             case DIALOG_INVALID_KIT:
  482.             {
  483.                 //do things
  484.                 Dialog dialog = new Dialog(this);
  485.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  486.                 builder.setMessage("You've scanned a kit that is not in the device's kit list.  Please perform a 'Sync' on the main screen if you feel you have received this message in error.");
  487.                 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  488.                         public void onClick(DialogInterface dialog, int id) {
  489.                             dialog.dismiss();
  490.                         }
  491.                     });
  492.                 dialog = builder.create();
  493.                 return dialog;
  494.             }
  495.             case DIALOG_TX_PROGRESS:
  496.             {
  497.                 ProgressDialog dialog = new ProgressDialog(this);
  498.                 dialog = new ProgressDialog(this);
  499.                 dialog.setMessage("Transmitting...");
  500.                 dialog.setIndeterminate(true);
  501.                 dialog.setCancelable(false);
  502.                 return dialog;
  503.             }
  504.             case DIALOG_TRANSMIT:
  505.             {
  506.                 Dialog dialog = new Dialog(this);
  507.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  508.                 final View dialogLayout = mInflater.inflate(R.layout.dialog_transmit, null);
  509.                 builder.setTitle("Transmit");
  510.                 builder.setView(dialogLayout);
  511.                 final EditText drvIn = (EditText)dialogLayout.findViewById(R.id.drv_in);
  512.                 final Spinner custSpn = (Spinner)dialogLayout.findViewById(R.id.cust_spn);
  513.                 final Cursor custCursor = mDb.rawQuery("SELECT * FROM "+DbSchema.CustomerSchema.TABLE_NAME, null);
  514.                 SimpleCursorAdapter custAdapter = new SimpleCursorAdapter(
  515.                         this,
  516.                         android.R.layout.simple_spinner_item,
  517.                         custCursor,
  518.                         new String[] { DbSchema.CustomerSchema.COLUMN_NAME },
  519.                         new int[] { android.R.id.text1 }
  520.                     );
  521.                 custAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  522.                 custSpn.setAdapter(custAdapter);
  523.                 builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
  524.                     public void onClick(DialogInterface dialog, int id) {
  525.                         showDialog(DIALOG_TX_PROGRESS);
  526.                         Cursor cc = (Cursor)(custSpn.getSelectedItem());
  527.                         mCustIdTX = cc.getString(cc.getColumnIndex(DbSchema.CustomerSchema.COLUMN_ID));
  528.                         mDrvIdTX = drvIn.getText().toString();
  529.                         Thread t = new Thread(txRunnable);
  530.                         t.start();
  531.                         dialog.dismiss();
  532.                         if(cc!=null) {
  533.                             cc.close();
  534.                         }
  535.                         if(custCursor!=null) {
  536.                             custCursor.close();
  537.                         }
  538.                     }
  539.                 });
  540.                
  541.                 dialog = builder.create();
  542.                 return dialog;
  543.             }
  544.             default:
  545.             {
  546.                 return null;
  547.             }
  548.         }
  549.     }
  550.    
  551.     public String prepareData() {
  552.         //
  553.         String dataMessage = "STX,H,";
  554.        
  555.         dataMessage += mPrefs.getString("activation", null) + ",";
  556.        
  557.         dataMessage += mCustIdTX + ",";
  558.        
  559.         dataMessage += mDrvIdTX + ",";
  560.        
  561.         String query = "SELECT * FROM "+DbSchema.ShipmentSchema.TABLE_NAME;
  562.         Cursor kc = mDb.rawQuery(query, null);
  563.         kc.moveToFirst();
  564.        
  565.         dataMessage += kc.getCount() + ",";
  566.        
  567.         boolean firstEntry = true;
  568.        
  569.         do {
  570.             try {
  571.                 if(firstEntry) {
  572.                     dataMessage += kc.getString(kc.getColumnIndex(DbSchema.ShipmentSchema.COLUMN_ID));
  573.                     firstEntry = false;
  574.                 } else {
  575.                     dataMessage += "@";
  576.                     dataMessage += kc.getString(kc.getColumnIndex(DbSchema.ShipmentSchema.COLUMN_ID));
  577.                 }
  578.                
  579.             } catch(Exception e) {
  580.                 //
  581.             }
  582.         } while(kc.moveToNext());
  583.        
  584.         dataMessage += ",ETX";
  585.        
  586.         return dataMessage;
  587.     }
  588.    
  589.     public boolean sendData(String command) {
  590.         String tempLabel = mPrefs.getString("site_id", "New Site");
  591.         String query = "SELECT * FROM " + DbSchema.SiteSchema.TABLE_NAME + " WHERE " + DbSchema.SiteSchema.COLUMN_LABEL + "='" + tempLabel + "'";
  592.         Cursor tempCur = mDb.rawQuery(query, null);
  593.         tempCur.moveToFirst();
  594.         String port = null;
  595.         String ip = null;
  596.         ListIterator<ContentValues> iter = null;
  597.         if(tempCur.getCount()>0) {
  598.             ip = tempCur.getString(tempCur.getColumnIndex(DbSchema.SiteSchema.COLUMN_IP));
  599.             port = tempCur.getString(tempCur.getColumnIndex(DbSchema.SiteSchema.COLUMN_PORT));
  600.             SocketConnection conn;
  601.             boolean success = false;
  602.             try {
  603.                 conn = new SocketConnection(this);
  604.                 conn.open(ip, new Integer(port));
  605.                 conn.sendCommand(command);
  606.                 String reply = conn.getReply();
  607.                 Log.e("smsreply", reply);
  608.                 if(!reply.contains("Shipment Data Transmitted!")) {
  609.                     //removeDialog(DIALOG_FAIL);
  610.                     TX_FAIL_TEXT = reply;
  611.                     //Log.e("smsreply", reply);
  612.                     //showDialog(DIALOG_FAIL);
  613.                     //throw new IOException();
  614.                     conn.close();
  615.                     return false;
  616.                 }
  617.                 conn.close();
  618.                 tempCur.close();
  619.                 return true;
  620.             } catch(Exception e) {
  621.                 Log.e("smsmain", ""+e.toString());
  622.             }
  623.         }
  624.         return false;
  625.     }
  626. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement