Advertisement
Guest User

TakeNumber

a guest
Mar 18th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.19 KB | None | 0 0
  1. package com.takenumber;
  2.  
  3. import ...
  4.  
  5. public class TakeNumber extends CustomActivity {
  6.  
  7.     private String frontPage_title;
  8.     private TextView frontPage_title_textView;
  9.     private Intent serviceIntent;
  10.     private static Resources resources;
  11.     public static Handler messageHandler = new MessageHandler();
  12.     public static Context context;
  13.     private static RelativeLayout loadingView;
  14.     private static ArrayList<Category> category_list = new ArrayList<Category>();
  15.     private static TableLayout categoriesTableLayout;
  16.     private ServerService boundServiceServer;
  17.     private ClientService boundServiceClient;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22. // some stuff
  23.     }
  24.    
  25.    
  26.     @Override
  27.     protected void onStart() {
  28.         super.onStart();
  29.         setLayoutView("activity_take_number");
  30.         context                  = getApplicationContext();
  31.         resources                = getResources();
  32.        
  33.         spEditor.putString("deviceType", StaticValues.DEVICE_TAKENUMBER);
  34.         spEditor.commit();
  35.        
  36.         // Get data from sp
  37.         frontPage_title          = sp.getString(StaticValues.FRONTPAGE_TITLE_TAKENUMBER, "Tag venligst et nummer i køen");
  38.        
  39.         // Gui elements
  40.         frontPage_title_textView = (TextView) findViewById(R.id.frontpage_title_takenumber);
  41.         frontPage_title_textView.setText(frontPage_title);
  42.         loadingView = (RelativeLayout) findViewById(R.id.loading_view);
  43.         categoriesTableLayout = (TableLayout) findViewById(R.id.categories_tablelayout);
  44.  
  45.        
  46.         /*
  47.          * Start service (server/client)
  48.          */
  49.  
  50.             // If device is server
  51.             if(isServer) {
  52.                 if (sp.getBoolean(StaticValues.SERVER_RUNNING, false)) {
  53.                     spEditor.putBoolean(StaticValues.SERVER_RUNNING, false);
  54.                     spEditor.commit();
  55.                 }
  56.                     // Set sp
  57.                     spEditor.putBoolean(StaticValues.SERVER_RUNNING, true);
  58.                     spEditor.putBoolean(StaticValues.CLIENT_RUNNING, false);
  59.                     spEditor.commit();
  60.  
  61.                     // Start service
  62.                     serviceIntent = new Intent(context, ServerService.class);                  
  63.                     serviceIntent.putExtra("messageType", StaticValues.MESSAGE_START_SERVICE);
  64.                     serviceIntent.putExtra("devicetype", StaticValues.DEVICE_TAKENUMBER);
  65.                     serviceIntent.putExtra("MESSENGER", new Messenger(messageHandler));
  66.                     startService(serviceIntent);
  67.                     bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
  68.                     System.out.println("server connecting");
  69.                    
  70.             // If device is client
  71.             } else {
  72.  
  73.                 if (sp.getBoolean(StaticValues.CLIENT_RUNNING, false)) {
  74.                     spEditor.putBoolean(StaticValues.CLIENT_RUNNING, false);
  75.                     spEditor.commit();
  76.                 }
  77.                     // Set sp
  78.                     spEditor.putBoolean(StaticValues.SERVER_RUNNING, false);
  79.                     spEditor.putBoolean(StaticValues.CLIENT_RUNNING, true);
  80.                     spEditor.commit();
  81.                    
  82.                     // Start service
  83.                     serviceIntent = new Intent(context, ClientService.class);          
  84.                     serviceIntent.putExtra("messageType", StaticValues.MESSAGE_START_SERVICE);     
  85.                     serviceIntent.putExtra("devicetype", StaticValues.DEVICE_TAKENUMBER);
  86.                     serviceIntent.putExtra("MESSENGER", new Messenger(messageHandler));
  87.                     startService(serviceIntent);
  88.                     bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
  89.                     System.out.println("client connecting");
  90.             }
  91.        
  92.     }
  93.        
  94.     /*
  95.      * Binding service
  96.      */
  97.     private ServiceConnection serviceConnection = new ServiceConnection() {
  98.  
  99.         @Override
  100.         public void onServiceConnected(ComponentName name, IBinder service) {
  101.             if (isServer) {
  102.                 System.out.println("server binding");
  103.                 boundServiceServer = ((ServerService.CustomBinder) service).getService();
  104.             } else {
  105.                 System.out.println("client binding");
  106.                 boundServiceClient = ((ClientService.CustomBinder) service).getService();
  107.             }
  108.         }
  109.  
  110.         @Override
  111.         public void onServiceDisconnected(ComponentName arg0) {
  112.             System.out.println("disconnected");
  113.             boundServiceClient = null;
  114.             boundServiceServer = null;
  115.         }
  116.     };
  117.    
  118.    
  119.    
  120.    
  121.     /*
  122.      * Message handler - messages from service
  123.      */
  124.     public static class MessageHandler extends Handler {
  125.         private String[] splittedMessages;
  126.         private boolean categoryExists;
  127.  
  128.         @Override
  129.         public void handleMessage(Message message) {
  130.             if (message.obj instanceof String) {
  131.                 String stringMessage = (String) message.obj;
  132.                 // Split message
  133.                 splittedMessages = stringMessage.split(StaticValues.MESSAGE_DELIMITER);
  134.                 String messageType = splittedMessages[0];  
  135.  
  136.  
  137.                 // Get messages
  138.                     if(messageType.equals(StaticValues.MESSAGE_SERVICE_LOADED)) {
  139.                         loadingView.setVisibility(View.GONE);
  140.                         categoriesTableLayout.setVisibility(View.VISIBLE);
  141.  
  142.                         // Get the category buttons
  143.                         if (isServer) {
  144.                             TakeNumber takenumber = new TakeNumber();
  145.                             takenumber.generateCategoryButtons();
  146.                         }
  147.  
  148.                 // Add category to arraylist
  149.                     } else if(messageType.equals(StaticValues.MESSAGE_ADD_CATEGORY)) {
  150.                         System.out.println(stringMessage);
  151.                        
  152.                         boolean isVisible = true;
  153.                         if (Integer.parseInt(splittedMessages[3]) == 1) {
  154.                             isVisible = true;
  155.                         } else {
  156.                             isVisible = false;
  157.                         }
  158.                        
  159.                         categoryExists = false;
  160.                         Category category = new Category(Integer.parseInt(splittedMessages[1]), splittedMessages[2], isVisible);
  161.                         for (Category categories : category_list) {
  162.                             if (categories.id == category.id) {
  163.                                 categoryExists = true;
  164.                                 break;
  165.                             }
  166.                         }
  167.                         if (!categoryExists) {
  168.                             category_list.add(category);
  169.                         }
  170.    
  171.                 // Add categories to gui
  172.                     } else if(messageType.equals(StaticValues.MESSAGE_ADD_CATEGORY_TO_GUI)) {
  173.  
  174.                         TakeNumber takenumber = new TakeNumber();
  175.                         takenumber.generateCategoryButtons();
  176.                        
  177.                 // Show Toast message in gui
  178.                     } else if(messageType.equals(StaticValues.MESSAGE_SHOW_TOAST)) {
  179.                         Toast.makeText(context, splittedMessages[1], Toast.LENGTH_LONG).show();
  180.                  
  181.                     } else {
  182.                         Toast.makeText(context, "Unknown message", Toast.LENGTH_LONG).show();
  183.  
  184.                     }
  185.                    
  186.             }
  187.         }
  188.        
  189.        
  190.     }
  191.    
  192.     /*
  193.      * Send messages to service
  194.      */
  195.  
  196.     public void sendMessageToService(String messageString) {
  197.         if (isServer) {
  198.             System.out.println("trying to send to server");
  199.             boundServiceServer.getMessagesFromUI(messageString);
  200.         } else {
  201.             System.out.println("trying to send to client");
  202.             boundServiceClient.getMessagesFromUI(messageString);
  203.         }
  204.     }
  205.    
  206.    
  207.    
  208.    
  209.    
  210.    
  211.     /*
  212.      * Generates the category buttons and set position and size
  213.      */
  214.    
  215.     public void generateCategoryButtons() {
  216.        
  217.         if (isServer) {
  218.             if (category_list != null || category_list.size() > 0) {
  219.                 category_list.clear();
  220.             }
  221.             category_list = db.getAllCategories();         
  222.         }
  223.         categoriesTableLayout.removeAllViews();
  224.        
  225.         if (category_list != null && category_list.size() > 0) {
  226.            
  227.             /*
  228.              * Screen sizes and buttons on each row
  229.              * < 320 : 1 button
  230.              * < 480 : 2 buttons
  231.              * < 800 : 3 buttons
  232.              * > 800 : 4 buttons
  233.              */
  234.            
  235.             int buttonsOnRow = 1;
  236.             int maxWidth     = 0;
  237.             categoriesTableLayout.setGravity(Gravity.CENTER_HORIZONTAL);
  238.            
  239.             if (categoriesTableLayout.getWidth() < 320) {
  240.                 buttonsOnRow = 1;
  241.                 maxWidth     = 300;
  242.             } else if(categoriesTableLayout.getWidth() < 480) {
  243.                 buttonsOnRow = 2;
  244.                 maxWidth     = 220;
  245.             } else if(categoriesTableLayout.getWidth() < 800) {
  246.                 buttonsOnRow = 3;
  247.                 maxWidth     = 240;
  248.             } else if(categoriesTableLayout.getWidth() > 800) {
  249.                 buttonsOnRow = 5;
  250.                 maxWidth     = 270;
  251.             } else {
  252.                 buttonsOnRow = 1;      
  253.                 maxWidth     = 300;    
  254.             }          
  255.             if (category_list.size() <= buttonsOnRow) {
  256.                 buttonsOnRow = category_list.size();
  257.             }          
  258.            
  259.             // calculate buttons width
  260.             int buttonWidth = (categoriesTableLayout.getWidth() / buttonsOnRow) - 10;
  261.             if (buttonWidth > maxWidth) {
  262.                 buttonWidth = maxWidth;
  263.             }
  264.            
  265.             TableRow tableRow;
  266.             Button categoryButton; 
  267.             double countRowsDouble = (double) category_list.size() / buttonsOnRow;
  268.             int countRows = (int) Math.ceil(countRowsDouble);
  269.            
  270.             int buttonNo  = 0;
  271.            
  272.             for (int i = 1; i <= countRows; i++) {
  273.                 tableRow = new TableRow(context);
  274.                 tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
  275.                 categoriesTableLayout.addView(tableRow);
  276.                 for (int p = 1; p <= buttonsOnRow; p++) {
  277.                     if (buttonNo < category_list.size()) {
  278.                         categoryButton = new Button(context);
  279.                         categoryButton.setText(category_list.get(buttonNo).title);
  280.                         categoryButton.setId(category_list.get(buttonNo).id);
  281.                         categoryButton.setTextColor(resources.getColor(R.color.black));
  282.                         categoryButton.setWidth(buttonWidth);
  283.                         categoryButton.setHeight(buttonWidth);
  284.                         categoryButton.setOnClickListener(new View.OnClickListener() {
  285.                            
  286.                             // OnClickListener for button - take number
  287.                             @Override
  288.                             public void onClick(View v) {
  289.                                 Button buttonClicked = (Button)v;
  290.                                 int categoryID = v.getId();
  291.                                 String categoryTitle = buttonClicked.getText().toString();
  292.                                
  293.                                 // TODO: Change to send only to showline devices
  294.                                 sendMessageToService(StaticValues.MESSAGE_NUMBER_TAKEN + StaticValues.MESSAGE_DELIMITER + categoryID + StaticValues.MESSAGE_DELIMITER + categoryTitle);
  295.                                
  296.                             }
  297.                         });
  298.                         tableRow.addView(categoryButton);
  299.                     }
  300.                     buttonNo++;
  301.                 }
  302.             }
  303.            
  304.  
  305.         }
  306.        
  307.     }
  308.  
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement