Advertisement
Guest User

TakeNumber

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