Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.takenumber;
- import java.util.ArrayList;
- import com.classes.Category;
- import com.classes.StaticValues;
- import com.database.DBHandler;
- import com.linesystem.CustomActivity;
- import com.linesystem.R;
- import com.linesystem.R.layout;
- import com.linesystem.R.menu;
- import com.services.ClientService;
- import com.services.ServerService;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.os.Messenger;
- import android.os.RemoteException;
- import android.preference.PreferenceManager;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.content.SharedPreferences;
- import android.content.DialogInterface.OnClickListener;
- import android.content.SharedPreferences.Editor;
- import android.content.res.ColorStateList;
- import android.content.res.Resources;
- import android.graphics.SumPathEffect;
- import android.graphics.drawable.GradientDrawable.Orientation;
- import android.text.Layout;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.webkit.WebView.FindListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.ProgressBar;
- import android.widget.RelativeLayout;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- 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 static Messenger boundService;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @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) {
- boundService = new Messenger(service);
- }
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- boundService = 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) {
- System.out.println("Trying to send: " + messageString);
- System.out.println(boundService);
- if (boundService != null) {
- System.out.println("hmm");
- try {
- Message msg = Message.obtain();
- msg.obj = messageString;
- boundService.send(msg);
- } catch (RemoteException e) {
- }
- }
- }
- /*
- * 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 + StaticValues.SENDTO_DEVICES_SHOWLINE + StaticValues.MESSAGE_DELIMITER + categoryID + StaticValues.MESSAGE_DELIMITER + categoryTitle);
- }
- });
- tableRow.addView(categoryButton);
- }
- buttonNo++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement