Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.conceptualsystems.kitmobile;
- import java.util.*;
- import android.app.*;
- import android.os.*;
- import android.content.*;
- import android.database.*;
- import android.database.sqlite.*;
- import android.widget.*;
- import android.view.*;
- import android.util.Log;
- import com.moonlightcheese.btsrv.*;
- public class ShipActivity extends Activity
- {
- //UI elements n stuff
- private ListView mShipKitListView;
- private LayoutInflater mInflater;
- private SharedPreferences mPrefs;
- private ShipKitAdapter mShipKitAdapter;
- private DbOpenHelper mDbHelper;
- private SQLiteDatabase mDb;
- private Cursor mShipCursor;
- //common queries
- //constants
- private String LOG_TAG = "KitMobile - ShipActivity.java";
- private final static int DIALOG_BT_RECONNECT = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- mPrefs = getSharedPreferences("kitmobileprefs", Context.MODE_PRIVATE);
- mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mDbHelper = new DbOpenHelper(getApplicationContext());
- mDb = mDbHelper.getReadableDatabase();
- //data init
- //mShipKitList = new ArrayList<Map<String, String>>();
- initScreen();
- }
- @Override
- public void onResume() {
- super.onResume();
- IntentFilter filter = new IntentFilter();
- filter.addAction(ScannerService.ACTION_READ_SCANNER);
- filter.addAction(ScannerService.ACTION_REQUEST_RECONNECT);
- registerReceiver(mReceiver, filter);
- }
- @Override
- public void onPause() {
- super.onPause();
- unregisterReceiver(mReceiver);
- }
- /** inner classes **/
- public class ShipKitAdapter extends CursorAdapter {
- public ShipKitAdapter(Context context, Cursor cur) {
- super(context, cur, true);
- }
- public void bindView(View view, Context context, Cursor cursor) {
- ((TextView)view.findViewById(R.id.kit_id)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID)));
- ((TextView)view.findViewById(R.id.net_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET)));
- ((TextView)view.findViewById(R.id.net_uom)).setText("lbs");
- ((TextView)view.findViewById(R.id.product_name)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME)));
- ((TextView)view.findViewById(R.id.gross_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS)));
- ((TextView)view.findViewById(R.id.gross_uom)).setText("lbs");
- }
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- return mInflater.inflate(R.layout.ship_list_item, parent, false);
- /*
- ((TextView)convertView.findViewById(R.id.kit_id)).setText(((Map<String, String>)this.getItem(position)).get("kit_id"));
- ((TextView)convertView.findViewById(R.id.net_units)).setText(((Map<String, String>)this.getItem(position)).get("net_units"));
- ((TextView)convertView.findViewById(R.id.net_uom)).setText(((Map<String, String>)this.getItem(position)).get("net_uom"));
- ((TextView)convertView.findViewById(R.id.product_name)).setText(((Map<String, String>)this.getItem(position)).get("product_name"));
- ((TextView)convertView.findViewById(R.id.gross_units)).setText(((Map<String, String>)this.getItem(position)).get("gross_units"));
- ((TextView)convertView.findViewById(R.id.gross_uom)).setText(((Map<String, String>)this.getItem(position)).get("gross_uom"));
- */
- }
- /*
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Cursor cur = this.getCursor();
- if(convertView==null) {
- convertView = mInflater.inflate(R.layout.ship_list_item, null);
- }
- ((TextView)convertView.findViewById(R.id.kit_id)).setText(cur.getString(cur.getColumnIndex(DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID)));
- ((TextView)convertView.findViewById(R.id.net_units)).setText(cur.getString(cur.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_NET)));
- ((TextView)convertView.findViewById(R.id.net_uom)).setText("lbs");
- ((TextView)convertView.findViewById(R.id.product_name)).setText(cur.getString(cur.getColumnIndex(DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_NAME)));
- ((TextView)convertView.findViewById(R.id.gross_units)).setText(cur.getString(cur.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_GROSS)));
- ((TextView)convertView.findViewById(R.id.gross_uom)).setText("lbs");
- return convertView;
- }
- */
- };
- private class DbOpenHelper extends SQLiteOpenHelper {
- DbOpenHelper(Context context) {
- //
- super(context,DbSchema.DATABASE_NAME, null, DbSchema.DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- }
- @Override
- public void onOpen(SQLiteDatabase db) {
- //open db
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- //upgrade db
- }
- public void resetShipment(SQLiteDatabase db) {
- db.execSQL(DbSchema.ShipmentSchema.DROP_TABLE);
- db.execSQL(DbSchema.ShipmentSchema.CREATE_TABLE);
- }
- public void resetCustomers(SQLiteDatabase db) {
- db.execSQL(DbSchema.CustomerSchema.DROP_TABLE);
- db.execSQL(DbSchema.CustomerSchema.CREATE_TABLE);
- }
- public void resetKits(SQLiteDatabase db) {
- db.execSQL(DbSchema.KitSchema.DROP_TABLE);
- db.execSQL(DbSchema.KitSchema.CREATE_TABLE);
- }
- public void resetProducts(SQLiteDatabase db) {
- db.execSQL(DbSchema.ProductSchema.DROP_TABLE);
- db.execSQL(DbSchema.ProductSchema.CREATE_TABLE);
- }
- }
- // Create a BroadcastReceiver for ACTION_FOUND, ACTION_STATE_CHANGED, ACTION_DISCOVERY_FINISHED
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (ScannerService.ACTION_READ_SCANNER.equals(action)) {
- String extra = intent.getStringExtra("scannerRead");
- Log.i(LOG_TAG, "read message: " + extra);
- //if this kit id exists, add it to the list.
- new ScanTask().execute(extra);
- }
- if(ScannerService.ACTION_REQUEST_RECONNECT.equals(action)) {
- //ask the user real nice if they want the bluetooth scanner reconnected after a disconnect
- Log.i(LOG_TAG, "requested reconnect!");
- //removeDialog(DIALOG_BT_RECONNECT);
- showDialog(DIALOG_BT_RECONNECT);
- }
- }
- };
- private class ScanTask extends AsyncTask<String, Void, Boolean> {
- String kitId;
- protected Boolean doInBackground(String... kitIds) {
- if(kitIds != null && !kitIds[0].equals("") && mDb!=null) {
- kitId = kitIds[0];
- //check for the existence of the kit in the kit list
- Cursor kitCursor;
- String kitquery = "SELECT * FROM " + DbSchema.KitSchema.TABLE_NAME + " WHERE " + DbSchema.KitSchema.COLUMN_ID + "='" + kitId + "'";
- kitCursor = mDb.rawQuery(kitquery, null);
- if(kitCursor.getCount()>0) {
- //we're cool, keep on truckin'
- } else {
- Log.e(LOG_TAG, "kit was not added. no such kit exists. try using 'resync kits'");
- return false;
- }
- //check if the kit is already in the shipment list. we don't want it in there twice.
- String shipquery = "SELECT * FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
- " WHERE " +DbSchema.ShipmentSchema.COLUMN_ID + "='" + kitId + "'";
- Cursor shipmentCursor = mDb.rawQuery(shipquery, null);
- if(shipmentCursor.getCount()<=0) { //if does not exist
- //we're cool, keep on truckin'
- } else { //record already exists, don't add it to the shipment list twice.
- Log.e(LOG_TAG, "kit was not added. record already exists in shipment.");
- return false;
- }
- //insert the record into the database
- ContentValues cv = new ContentValues();
- cv.put(DbSchema.ShipmentSchema.COLUMN_ID, kitId);
- mDb.insert(DbSchema.ShipmentSchema.TABLE_NAME, null, cv);
- //make the query to update the screen
- String screenquery = "SELECT " +
- DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema._ID+" AS " + DbSchema.ShipmentSchema._ID + ", " +
- DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID+" AS " + DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID + ", " +
- DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_FIN_ID + ", " +
- DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_GROSS+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS + ", " +
- DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_NET+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET + ", " +
- DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_NAME+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME + ", " +
- DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_ID +
- " FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
- " LEFT OUTER JOIN " + DbSchema.KitSchema.TABLE_NAME +
- " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_ID+"="+DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID +
- " LEFT OUTER JOIN " + DbSchema.ProductSchema.TABLE_NAME +
- " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+"="+DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID;
- mShipCursor = mDb.rawQuery(screenquery, null);
- } else {
- //null kit id
- Log.e(LOG_TAG, "kitId was null in ScanTask");
- return false;
- }
- return true;
- }
- protected void onPostExecute(Boolean exists) {
- if(exists) {
- Log.i(LOG_TAG, "adding kit to ship list...");
- //refreshShipKitListView();
- initScreen();
- Log.i(LOG_TAG, "added.");
- }
- }
- };
- public void refreshShipKitListView() {
- //mShipKitAdapter.changeCursor(mShipCursor);
- //mShipKitListView.invalidate();
- //mShipKitAdapter.notifyDataSetChanged();
- }
- private void initScreen() {
- setContentView(R.layout.ship);
- mShipKitListView = (ListView)findViewById(R.id.kit_list);
- String screenquery = "SELECT " +
- DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema._ID+" AS " + DbSchema.ShipmentSchema._ID + ", " +
- DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID+" AS " + DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID + ", " +
- DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_FIN_ID + ", " +
- DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_GROSS+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS + ", " +
- DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_NET+" AS "+DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET + ", " +
- DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_NAME+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME + ", " +
- DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID+" AS "+DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_ID +
- " FROM " + DbSchema.ShipmentSchema.TABLE_NAME +
- " LEFT OUTER JOIN " + DbSchema.KitSchema.TABLE_NAME +
- " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_ID+"="+DbSchema.ShipmentSchema.TABLE_NAME+"."+DbSchema.ShipmentSchema.COLUMN_ID +
- " LEFT OUTER JOIN " + DbSchema.ProductSchema.TABLE_NAME +
- " ON " + DbSchema.KitSchema.TABLE_NAME+"."+DbSchema.KitSchema.COLUMN_FIN_ID+"="+DbSchema.ProductSchema.TABLE_NAME+"."+DbSchema.ProductSchema.COLUMN_ID;
- mShipCursor = mDb.rawQuery(screenquery, null);
- mShipKitAdapter = new ShipKitAdapter(
- ShipActivity.this,
- mShipCursor
- );
- mShipKitListView.setAdapter(mShipKitAdapter);
- }
- private void addFakeListItem(String id) {
- int position = mShipKitAdapter.getCount();
- Map<String, String> m = new HashMap<String, String>();
- m.put("kit_id", id);
- m.put("net_units", "5027");
- m.put("net_uom", "lbs");
- m.put("gross_units", "5155");
- m.put("gross_uom", "lbs");
- m.put("product_name", "COPPER #1");
- //mShipKitAdapter.add(m);
- mShipKitAdapter.notifyDataSetChanged();
- }
- // OPTIONS MENU
- ///////////////
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.ship_options, menu);
- return true;
- }
- public boolean onOptionsItemSelected(MenuItem item) {
- switch(item.getItemId()) {
- case R.id.opt_clear:
- //add a fake item to the list
- mDbHelper.resetShipment(mDb);
- initScreen();
- return true;
- case R.id.opt_transmit:
- refreshShipKitListView();
- return true;
- default:
- super.onOptionsItemSelected(item);
- return true;
- }
- }
- // DIALOG STUFF
- ///////////////
- @Override
- protected Dialog onCreateDialog(int id, Bundle b) {
- switch(id) {
- case DIALOG_BT_RECONNECT:
- {
- Dialog dialog = new Dialog(this);
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Scanner Error");
- builder.setMessage("Scanner has been disconnected!");
- builder.setPositiveButton("Reconnect", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- Intent reconnectIntent = new Intent();
- reconnectIntent.setAction(ScannerService.ACTION_RECONNECT);
- sendBroadcast(reconnectIntent);
- }
- });
- dialog = builder.create();
- return dialog;
- }
- default:
- {
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement