Advertisement
moonlightcheese

Untitled

Jul 7th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.75 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.     private SharedPreferences mPrefs;
  22.     private ShipKitAdapter mShipKitAdapter;
  23.     private DbOpenHelper mDbHelper;
  24.     private SQLiteDatabase mDb;
  25.     private Cursor mShipCursor;
  26.    
  27.     //common queries
  28.    
  29.    
  30.     //constants
  31.     private String LOG_TAG = "KitMobile - ShipActivity.java";
  32.     private final static int DIALOG_BT_RECONNECT = 1;
  33.    
  34.     /** Called when the activity is first created. */
  35.     @Override
  36.     public void onCreate(Bundle savedInstanceState)
  37.     {
  38.         super.onCreate(savedInstanceState);
  39.        
  40.         mPrefs = getSharedPreferences("kitmobileprefs", Context.MODE_PRIVATE);
  41.         mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  42.         mDbHelper = new DbOpenHelper(getApplicationContext());
  43.         mDb = mDbHelper.getReadableDatabase();
  44.        
  45.         //data init
  46.         //mShipKitList = new ArrayList<Map<String, String>>();
  47.        
  48.         initScreen();
  49.     }
  50.    
  51.     @Override
  52.     public void onResume() {
  53.         super.onResume();
  54.         IntentFilter filter = new IntentFilter();
  55.         filter.addAction(ScannerService.ACTION_READ_SCANNER);
  56.         filter.addAction(ScannerService.ACTION_REQUEST_RECONNECT);
  57.         registerReceiver(mReceiver, filter);
  58.     }
  59.    
  60.     @Override
  61.     public void onPause() {
  62.         super.onPause();
  63.         unregisterReceiver(mReceiver);
  64.     }
  65.    
  66.     /** inner classes **/
  67.     public class ShipKitAdapter extends CursorAdapter {
  68.        
  69.         public ShipKitAdapter(Context context, Cursor cur) {
  70.             super(context, cur, true);
  71.         }
  72.        
  73.         public void bindView(View view, Context context, Cursor cursor) {
  74.             ((TextView)view.findViewById(R.id.kit_id)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID)));
  75.             ((TextView)view.findViewById(R.id.net_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET)));
  76.             ((TextView)view.findViewById(R.id.net_uom)).setText("lbs");
  77.             ((TextView)view.findViewById(R.id.product_name)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME)));
  78.             ((TextView)view.findViewById(R.id.gross_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS)));
  79.             ((TextView)view.findViewById(R.id.gross_uom)).setText("lbs");
  80.         }
  81.        
  82.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  83.             return mInflater.inflate(R.layout.ship_list_item, parent, false);
  84.            
  85.             /*
  86.             ((TextView)convertView.findViewById(R.id.kit_id)).setText(((Map<String, String>)this.getItem(position)).get("kit_id"));
  87.             ((TextView)convertView.findViewById(R.id.net_units)).setText(((Map<String, String>)this.getItem(position)).get("net_units"));
  88.             ((TextView)convertView.findViewById(R.id.net_uom)).setText(((Map<String, String>)this.getItem(position)).get("net_uom"));
  89.             ((TextView)convertView.findViewById(R.id.product_name)).setText(((Map<String, String>)this.getItem(position)).get("product_name"));
  90.             ((TextView)convertView.findViewById(R.id.gross_units)).setText(((Map<String, String>)this.getItem(position)).get("gross_units"));
  91.             ((TextView)convertView.findViewById(R.id.gross_uom)).setText(((Map<String, String>)this.getItem(position)).get("gross_uom"));
  92.             */
  93.         }
  94.         /*
  95.         @Override
  96.         public View getView(int position, View convertView, ViewGroup parent) {
  97.             Cursor cur = this.getCursor();
  98.             if(convertView==null) {
  99.                 convertView = mInflater.inflate(R.layout.ship_list_item, null);
  100.             }
  101.             ((TextView)convertView.findViewById(R.id.kit_id)).setText(cur.getString(cur.getColumnIndex(DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID)));
  102.             ((TextView)convertView.findViewById(R.id.net_units)).setText(cur.getString(cur.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_NET)));
  103.             ((TextView)convertView.findViewById(R.id.net_uom)).setText("lbs");
  104.             ((TextView)convertView.findViewById(R.id.product_name)).setText(cur.getString(cur.getColumnIndex(DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_NAME)));
  105.             ((TextView)convertView.findViewById(R.id.gross_units)).setText(cur.getString(cur.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_GROSS)));
  106.             ((TextView)convertView.findViewById(R.id.gross_uom)).setText("lbs");
  107.             return convertView;
  108.         }
  109.         */
  110.     };
  111.    
  112.     private class DbOpenHelper extends SQLiteOpenHelper {
  113.        
  114.         DbOpenHelper(Context context) {
  115.             //
  116.             super(context,DbSchema.DATABASE_NAME, null, DbSchema.DATABASE_VERSION);
  117.         }
  118.        
  119.         @Override
  120.         public void onCreate(SQLiteDatabase db) {
  121.         }
  122.        
  123.         @Override
  124.         public void onOpen(SQLiteDatabase db) {
  125.             //open db
  126.         }
  127.        
  128.         @Override
  129.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  130.             //upgrade db
  131.         }
  132.        
  133.         public void resetShipment(SQLiteDatabase db) {
  134.             db.execSQL(DbSchema.ShipmentSchema.DROP_TABLE);
  135.             db.execSQL(DbSchema.ShipmentSchema.CREATE_TABLE);
  136.         }
  137.        
  138.         public void resetCustomers(SQLiteDatabase db) {
  139.             db.execSQL(DbSchema.CustomerSchema.DROP_TABLE);
  140.             db.execSQL(DbSchema.CustomerSchema.CREATE_TABLE);
  141.         }
  142.        
  143.         public void resetKits(SQLiteDatabase db) {
  144.             db.execSQL(DbSchema.KitSchema.DROP_TABLE);
  145.             db.execSQL(DbSchema.KitSchema.CREATE_TABLE);
  146.         }
  147.        
  148.         public void resetProducts(SQLiteDatabase db) {
  149.             db.execSQL(DbSchema.ProductSchema.DROP_TABLE);
  150.             db.execSQL(DbSchema.ProductSchema.CREATE_TABLE);
  151.         }
  152.     }
  153.    
  154.     // Create a BroadcastReceiver for ACTION_FOUND, ACTION_STATE_CHANGED, ACTION_DISCOVERY_FINISHED
  155.     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  156.         public void onReceive(Context context, Intent intent) {
  157.             String action = intent.getAction();
  158.             if (ScannerService.ACTION_READ_SCANNER.equals(action)) {
  159.                 String extra = intent.getStringExtra("scannerRead");
  160.                 Log.i(LOG_TAG, "read message: " + extra);
  161.                 //if this kit id exists, add it to the list.
  162.                 new ScanTask().execute(extra);
  163.             }
  164.             if(ScannerService.ACTION_REQUEST_RECONNECT.equals(action)) {
  165.                 //ask the user real nice if they want the bluetooth scanner reconnected after a disconnect
  166.                 Log.i(LOG_TAG, "requested reconnect!");
  167.                 //removeDialog(DIALOG_BT_RECONNECT);
  168.                 showDialog(DIALOG_BT_RECONNECT);
  169.             }
  170.         }
  171.     };
  172.    
  173.     private class ScanTask extends AsyncTask<String, Void, Boolean> {
  174.         String kitId;
  175.        
  176.         protected Boolean doInBackground(String... kitIds) {
  177.             if(kitIds != null && !kitIds[0].equals("") && mDb!=null) {
  178.                 kitId = kitIds[0];
  179.                
  180.                 //check for the existence of the kit in the kit list
  181.                 Cursor kitCursor;
  182.                 String kitquery = "SELECT * FROM " + DbSchema.KitSchema.TABLE_NAME + " WHERE " + DbSchema.KitSchema.COLUMN_ID + "='" + kitId + "'";
  183.                 kitCursor = mDb.rawQuery(kitquery, null);
  184.                 if(kitCursor.getCount()>0) {
  185.                     //we're cool, keep on truckin'
  186.                 } else {
  187.                     Log.e(LOG_TAG, "kit was not added.  no such kit exists.  try using 'resync kits'");
  188.                     return false;
  189.                 }
  190.                
  191.                 //check if the kit is already in the shipment list.  we don't want it in there twice.
  192.                 String shipquery = "SELECT * FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
  193.                         " WHERE " +DbSchema.ShipmentSchema.COLUMN_ID + "='" + kitId + "'";
  194.                 Cursor shipmentCursor = mDb.rawQuery(shipquery, null);
  195.                 if(shipmentCursor.getCount()<=0) {  //if does not exist
  196.                     //we're cool, keep on truckin'
  197.                 } else {        //record already exists, don't add it to the shipment list twice.
  198.                     Log.e(LOG_TAG, "kit was not added.  record already exists in shipment.");
  199.                     return false;
  200.                 }
  201.                
  202.                 //insert the record into the database
  203.                 ContentValues cv = new ContentValues();
  204.                 cv.put(DbSchema.ShipmentSchema.COLUMN_ID, kitId);
  205.                 mDb.insert(DbSchema.ShipmentSchema.TABLE_NAME, null, cv);
  206.                
  207.                 //make the query to update the screen
  208.                 String screenquery = "SELECT " +
  209.                     DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema._ID+" AS " + DbSchema.ShipmentSchema._ID + ", " +
  210.                     DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID+" AS " + DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID + ", " +
  211.                     DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_FIN_ID + ", " +
  212.                     DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_GROSS+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS + ", " +
  213.                     DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_NET+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET + ", " +
  214.                     DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_NAME+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME + ", " +
  215.                     DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_ID +
  216.                     " FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
  217.                     " LEFT OUTER JOIN " + DbSchema.KitSchema.TABLE_NAME +
  218.                     " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_ID+"="+DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID +
  219.                     " LEFT OUTER JOIN " + DbSchema.ProductSchema.TABLE_NAME +
  220.                     " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+"="+DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID;
  221.                 mShipCursor = mDb.rawQuery(screenquery, null);
  222.             } else {
  223.                 //null kit id
  224.                 Log.e(LOG_TAG, "kitId was null in ScanTask");
  225.                 return false;
  226.             }
  227.            
  228.             return true;
  229.         }
  230.        
  231.         protected void onPostExecute(Boolean exists) {
  232.             if(exists) {
  233.                 Log.i(LOG_TAG, "adding kit to ship list...");
  234.                 //refreshShipKitListView();
  235.                 initScreen();
  236.                 Log.i(LOG_TAG, "added.");
  237.             }
  238.         }
  239.     };
  240.    
  241.     public void refreshShipKitListView() {
  242.         //mShipKitAdapter.changeCursor(mShipCursor);
  243.         //mShipKitListView.invalidate();
  244.         //mShipKitAdapter.notifyDataSetChanged();
  245.     }
  246.    
  247.     private void initScreen() {
  248.         setContentView(R.layout.ship);
  249.         mShipKitListView = (ListView)findViewById(R.id.kit_list);
  250.         String screenquery = "SELECT " +
  251.             DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema._ID+" AS " + DbSchema.ShipmentSchema._ID + ", " +
  252.             DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID+" AS " + DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID + ", " +
  253.             DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_FIN_ID + ", " +
  254.             DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_GROSS+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS + ", " +
  255.             DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_NET+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET + ", " +
  256.             DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_NAME+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME + ", " +
  257.             DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_ID +
  258.             " FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
  259.             " LEFT OUTER JOIN " + DbSchema.KitSchema.TABLE_NAME +
  260.             " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_ID+"="+DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID +
  261.             " LEFT OUTER JOIN " + DbSchema.ProductSchema.TABLE_NAME +
  262.             " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+"="+DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID;
  263.         mShipCursor = mDb.rawQuery(screenquery, null);
  264.         mShipKitAdapter = new ShipKitAdapter(
  265.             ShipActivity.this,
  266.             mShipCursor
  267.         );
  268.         mShipKitListView.setAdapter(mShipKitAdapter);
  269.     }
  270.    
  271.     private void addFakeListItem(String id) {
  272.         int position = mShipKitAdapter.getCount();
  273.         Map<String, String> m = new HashMap<String, String>();
  274.         m.put("kit_id", id);
  275.         m.put("net_units", "5027");
  276.         m.put("net_uom", "lbs");
  277.         m.put("gross_units", "5155");
  278.         m.put("gross_uom", "lbs");
  279.         m.put("product_name", "COPPER #1");
  280.         //mShipKitAdapter.add(m);
  281.        
  282.         mShipKitAdapter.notifyDataSetChanged();
  283.     }
  284.    
  285.     // OPTIONS MENU
  286.     ///////////////
  287.     public boolean onCreateOptionsMenu(Menu menu) {
  288.         MenuInflater inflater = getMenuInflater();
  289.         inflater.inflate(R.menu.ship_options, menu);
  290.         return true;
  291.     }
  292.    
  293.     public boolean onOptionsItemSelected(MenuItem item) {
  294.         switch(item.getItemId()) {
  295.             case R.id.opt_clear:
  296.                 //add a fake item to the list
  297.                 mDbHelper.resetShipment(mDb);
  298.                 initScreen();
  299.                 return true;
  300.             case R.id.opt_transmit:
  301.                 refreshShipKitListView();
  302.                 return true;
  303.             default:
  304.                 super.onOptionsItemSelected(item);
  305.                 return true;
  306.         }
  307.     }
  308.    
  309.     // DIALOG STUFF
  310.     ///////////////
  311.     @Override
  312.     protected Dialog onCreateDialog(int id, Bundle b) {
  313.         switch(id) {
  314.             case DIALOG_BT_RECONNECT:
  315.             {
  316.                 Dialog dialog = new Dialog(this);
  317.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  318.                 builder.setTitle("Scanner Error");
  319.                 builder.setMessage("Scanner has been disconnected!");
  320.                 builder.setPositiveButton("Reconnect", new DialogInterface.OnClickListener() {
  321.                     public void onClick(DialogInterface dialog, int id) {
  322.                         Intent reconnectIntent = new Intent();
  323.                         reconnectIntent.setAction(ScannerService.ACTION_RECONNECT);
  324.                         sendBroadcast(reconnectIntent);
  325.                     }
  326.                 });
  327.                 dialog = builder.create();
  328.                 return dialog;
  329.             }
  330.             default:
  331.             {
  332.                 return null;
  333.             }
  334.         }
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement