Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.takenumber;
- import ...
- public class TakeNumber extends CustomActivity {
- private String frontPage_title;
- private TextView frontPage_title_textView;
- private Intent serviceIntent;
- private static Resources resources;
- public static Handler messageHandler = new MessageHandler();
- public static Context context;
- private static RelativeLayout loadingView;
- private static ArrayList<Category> category_list = new ArrayList<Category>();
- private static TableLayout categoriesTableLayout;
- private ServerService boundServiceServer;
- private ClientService boundServiceClient;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // some stuff
- }
- @Override
- protected void onStart() {
- super.onStart();
- setLayoutView("activity_take_number");
- context = getApplicationContext();
- resources = getResources();
- spEditor.putString("deviceType", StaticValues.DEVICE_TAKENUMBER);
- spEditor.commit();
- // Get data from sp
- frontPage_title = sp.getString(StaticValues.FRONTPAGE_TITLE_TAKENUMBER, "Tag venligst et nummer i køen");
- // Gui elements
- frontPage_title_textView = (TextView) findViewById(R.id.frontpage_title_takenumber);
- frontPage_title_textView.setText(frontPage_title);
- loadingView = (RelativeLayout) findViewById(R.id.loading_view);
- categoriesTableLayout = (TableLayout) findViewById(R.id.categories_tablelayout);
- /*
- * Start service (server/client)
- */
- // If device is server
- if(isServer) {
- if (sp.getBoolean(StaticValues.SERVER_RUNNING, false)) {
- spEditor.putBoolean(StaticValues.SERVER_RUNNING, false);
- spEditor.commit();
- }
- // Set sp
- spEditor.putBoolean(StaticValues.SERVER_RUNNING, true);
- spEditor.putBoolean(StaticValues.CLIENT_RUNNING, false);
- spEditor.commit();
- // Start service
- serviceIntent = new Intent(context, ServerService.class);
- serviceIntent.putExtra("messageType", StaticValues.MESSAGE_START_SERVICE);
- serviceIntent.putExtra("devicetype", StaticValues.DEVICE_TAKENUMBER);
- serviceIntent.putExtra("MESSENGER", new Messenger(messageHandler));
- startService(serviceIntent);
- bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
- System.out.println("server connecting");
- // If device is client
- } else {
- if (sp.getBoolean(StaticValues.CLIENT_RUNNING, false)) {
- spEditor.putBoolean(StaticValues.CLIENT_RUNNING, false);
- spEditor.commit();
- }
- // Set sp
- spEditor.putBoolean(StaticValues.SERVER_RUNNING, false);
- spEditor.putBoolean(StaticValues.CLIENT_RUNNING, true);
- spEditor.commit();
- // Start service
- serviceIntent = new Intent(context, ClientService.class);
- serviceIntent.putExtra("messageType", StaticValues.MESSAGE_START_SERVICE);
- serviceIntent.putExtra("devicetype", StaticValues.DEVICE_TAKENUMBER);
- serviceIntent.putExtra("MESSENGER", new Messenger(messageHandler));
- startService(serviceIntent);
- bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
- System.out.println("client connecting");
- }
- }
- /*
- * Binding service
- */
- private ServiceConnection serviceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- if (isServer) {
- System.out.println("server binding");
- boundServiceServer = ((ServerService.CustomBinder) service).getService();
- } else {
- System.out.println("client binding");
- boundServiceClient = ((ClientService.CustomBinder) service).getService();
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- System.out.println("disconnected");
- boundServiceClient = null;
- boundServiceServer = null;
- }
- };
- /*
- * Message handler - messages from service
- */
- public static class MessageHandler extends Handler {
- private String[] splittedMessages;
- private boolean categoryExists;
- @Override
- public void handleMessage(Message message) {
- if (message.obj instanceof String) {
- String stringMessage = (String) message.obj;
- // Split message
- splittedMessages = stringMessage.split(StaticValues.MESSAGE_DELIMITER);
- String messageType = splittedMessages[0];
- // Get messages
- if(messageType.equals(StaticValues.MESSAGE_SERVICE_LOADED)) {
- loadingView.setVisibility(View.GONE);
- categoriesTableLayout.setVisibility(View.VISIBLE);
- // Get the category buttons
- if (isServer) {
- TakeNumber takenumber = new TakeNumber();
- takenumber.generateCategoryButtons();
- }
- // Add category to arraylist
- } else if(messageType.equals(StaticValues.MESSAGE_ADD_CATEGORY)) {
- System.out.println(stringMessage);
- boolean isVisible = true;
- if (Integer.parseInt(splittedMessages[3]) == 1) {
- isVisible = true;
- } else {
- isVisible = false;
- }
- categoryExists = false;
- Category category = new Category(Integer.parseInt(splittedMessages[1]), splittedMessages[2], isVisible);
- for (Category categories : category_list) {
- if (categories.id == category.id) {
- categoryExists = true;
- break;
- }
- }
- if (!categoryExists) {
- category_list.add(category);
- }
- // Add categories to gui
- } else if(messageType.equals(StaticValues.MESSAGE_ADD_CATEGORY_TO_GUI)) {
- TakeNumber takenumber = new TakeNumber();
- takenumber.generateCategoryButtons();
- // Show Toast message in gui
- } else if(messageType.equals(StaticValues.MESSAGE_SHOW_TOAST)) {
- Toast.makeText(context, splittedMessages[1], Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(context, "Unknown message", Toast.LENGTH_LONG).show();
- }
- }
- }
- }
- /*
- * Send messages to service
- */
- public void sendMessageToService(String messageString) {
- if (isServer) {
- System.out.println("trying to send to server");
- boundServiceServer.getMessagesFromUI(messageString);
- } else {
- System.out.println("trying to send to client");
- boundServiceClient.getMessagesFromUI(messageString);
- }
- }
- /*
- * Generates the category buttons and set position and size
- */
- public void generateCategoryButtons() {
- if (isServer) {
- if (category_list != null || category_list.size() > 0) {
- category_list.clear();
- }
- category_list = db.getAllCategories();
- }
- categoriesTableLayout.removeAllViews();
- if (category_list != null && category_list.size() > 0) {
- /*
- * Screen sizes and buttons on each row
- * < 320 : 1 button
- * < 480 : 2 buttons
- * < 800 : 3 buttons
- * > 800 : 4 buttons
- */
- int buttonsOnRow = 1;
- int maxWidth = 0;
- categoriesTableLayout.setGravity(Gravity.CENTER_HORIZONTAL);
- if (categoriesTableLayout.getWidth() < 320) {
- buttonsOnRow = 1;
- maxWidth = 300;
- } else if(categoriesTableLayout.getWidth() < 480) {
- buttonsOnRow = 2;
- maxWidth = 220;
- } else if(categoriesTableLayout.getWidth() < 800) {
- buttonsOnRow = 3;
- maxWidth = 240;
- } else if(categoriesTableLayout.getWidth() > 800) {
- buttonsOnRow = 5;
- maxWidth = 270;
- } else {
- buttonsOnRow = 1;
- maxWidth = 300;
- }
- if (category_list.size() <= buttonsOnRow) {
- buttonsOnRow = category_list.size();
- }
- // calculate buttons width
- int buttonWidth = (categoriesTableLayout.getWidth() / buttonsOnRow) - 10;
- if (buttonWidth > maxWidth) {
- buttonWidth = maxWidth;
- }
- TableRow tableRow;
- Button categoryButton;
- double countRowsDouble = (double) category_list.size() / buttonsOnRow;
- int countRows = (int) Math.ceil(countRowsDouble);
- int buttonNo = 0;
- for (int i = 1; i <= countRows; i++) {
- tableRow = new TableRow(context);
- tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
- categoriesTableLayout.addView(tableRow);
- for (int p = 1; p <= buttonsOnRow; p++) {
- if (buttonNo < category_list.size()) {
- categoryButton = new Button(context);
- categoryButton.setText(category_list.get(buttonNo).title);
- categoryButton.setId(category_list.get(buttonNo).id);
- categoryButton.setTextColor(resources.getColor(R.color.black));
- categoryButton.setWidth(buttonWidth);
- categoryButton.setHeight(buttonWidth);
- categoryButton.setOnClickListener(new View.OnClickListener() {
- // OnClickListener for button - take number
- @Override
- public void onClick(View v) {
- Button buttonClicked = (Button)v;
- int categoryID = v.getId();
- String categoryTitle = buttonClicked.getText().toString();
- // TODO: Change to send only to showline devices
- sendMessageToService(StaticValues.MESSAGE_NUMBER_TAKEN + StaticValues.MESSAGE_DELIMITER + categoryID + StaticValues.MESSAGE_DELIMITER + categoryTitle);
- }
- });
- tableRow.addView(categoryButton);
- }
- buttonNo++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement