/**
   * Helper method to dump flag state to the log.
   *
   * @param uiFlags Set of UI flags to inspect
   */
  public void dumpFlagStateToLog(int uiFlags) {
    if ((uiFlags & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
      Log.i(TAG, "SYSTEM_UI_FLAG_LOW_PROFILE is set");
    } else {
      Log.i(TAG, "SYSTEM_UI_FLAG_LOW_PROFILE is unset");
    }

    if ((uiFlags & View.SYSTEM_UI_FLAG_FULLSCREEN) != 0) {
      Log.i(TAG, "SYSTEM_UI_FLAG_FULLSCREEN is set");
    } else {
      Log.i(TAG, "SYSTEM_UI_FLAG_FULLSCREEN is unset");
    }

    if ((uiFlags & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0) {
      Log.i(TAG, "SYSTEM_UI_FLAG_HIDE_NAVIGATION is set");
    } else {
      Log.i(TAG, "SYSTEM_UI_FLAG_HIDE_NAVIGATION is unset");
    }

    if ((uiFlags & View.SYSTEM_UI_FLAG_IMMERSIVE) != 0) {
      Log.i(TAG, "SYSTEM_UI_FLAG_IMMERSIVE is set");
    } else {
      Log.i(TAG, "SYSTEM_UI_FLAG_IMMERSIVE is unset");
    }

    if ((uiFlags & View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) != 0) {
      Log.i(TAG, "SYSTEM_UI_FLAG_IMMERSIVE_STICKY is set");
    } else {
      Log.i(TAG, "SYSTEM_UI_FLAG_IMMERSIVE_STICKY is unset");
    }
  }
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   switch (requestCode) {
     case REQUEST_CONNECT_DEVICE_SECURE:
       // When DeviceListActivity returns with a device to connect
       if (resultCode == Activity.RESULT_OK) {
         connectDevice(data, true);
       }
       break;
     case REQUEST_CONNECT_DEVICE_INSECURE:
       // When DeviceListActivity returns with a device to connect
       if (resultCode == Activity.RESULT_OK) {
         connectDevice(data, false);
       }
       break;
     case REQUEST_ENABLE_BT:
       // When the request to enable Bluetooth returns
       if (resultCode == Activity.RESULT_OK) {
         // Bluetooth is now enabled, so set up a chat session
         setupChat();
       } else {
         // User did not enable Bluetooth or an error occurred
         Log.d(TAG, "BT not enabled");
         Toast.makeText(getActivity(), R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
         getActivity().finish();
       }
   }
 }
 /**
  * The main processing method. This happens in a background task. In this case we are just
  * sampling down the bitmap and returning it from a resource.
  *
  * @param resId
  * @return
  */
 private Bitmap processBitmap(int resId) {
   if (BuildConfig.DEBUG) {
     Log.d(TAG, "processBitmap - " + resId);
   }
   return decodeSampledBitmapFromResource(
       mResources, resId, mImageWidth, mImageHeight, getImageCache());
 }
  // BEGIN_INCLUDE(recyclerViewOnBindViewHolder)
  // Replace the contents of a view (invoked by the layout manager)
  @Override
  public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    Log.d(TAG, "Element " + position + " set.");

    // Get element from your dataset at this position and replace the contents of the view
    // with that element
    // viewHolder.getTextView().setText(mDataSet[position]);
    if (mViewSet[position].getParent() != null) {
      ((LinearLayout) mViewSet[position].getParent()).removeAllViews();
    }
    viewHolder.getLinearLayout().removeAllViews();
    try {
      viewHolder.getLinearLayout().addView(mViewSet[position]);
    } catch (Exception e) {
      Log.d(TAG, "" + position);
    }
  }
  /** Create a chain of targets that will receive log data */
  @Override
  public void initializeLogging() {
    // Wraps Android's native log framework.
    LogWrapper logWrapper = new LogWrapper();
    // Using Log, front-end to the logging chain, emulates android.util.log method signatures.
    Log.setLogNode(logWrapper);

    // Filter strips out everything except the message text.
    MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
    logWrapper.setNext(msgFilter);

    // On screen logging via a fragment with a TextView.
    LogFragment logFragment =
        (LogFragment) getSupportFragmentManager().findFragmentById(R.id.log_fragment);
    msgFilter.setNext(logFragment.getLogView());

    Log.i(TAG, "Ready");
  }
        @Override
        public void handleMessage(Message msg) {
          FragmentActivity activity = getActivity();
          switch (msg.what) {
            case Constants.MESSAGE_STATE_CHANGE:
              switch (msg.arg1) {
                case BluetoothChatService.STATE_CONNECTED:
                  setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
                  mConversationArrayAdapter.clear();
                  break;
                case BluetoothChatService.STATE_CONNECTING:
                  setStatus(R.string.title_connecting);
                  break;
                case BluetoothChatService.STATE_NOT_CONNECTED:
                  setStatus(R.string.title_not_connected);
                  break;
              }
              break;
            case Constants.MESSAGE_WRITE:
              byte[] writeBuf = (byte[]) msg.obj;
              // construct a string from the buffer
              String writeMessage = new String(writeBuf);
              mConversationArrayAdapter.add("Me:  " + writeMessage);
              break;

            case Constants.MESSAGE_READ:
              String readMessage = (String) msg.obj;
              mConversationArrayAdapter.add(mConnectedDeviceName + ":  " + readMessage);

              TelemetryData data = new TelemetryData(readMessage);

              View ownView = getView();
              if (ownView == null) {
                break;
              }
              View rootView = ownView.getRootView();
              if (rootView != null) {
                TextView view = (TextView) rootView.findViewById(R.id.voltage);
                view.setText(data.getVoltageString() + "V");

                view = (TextView) rootView.findViewById(R.id.current);
                view.setText(data.getCurrentString() + "A");

                view = (TextView) rootView.findViewById(R.id.mbtemp);
                view.setText(data.getBoardTemperature() + "\u00B0 C");

                view = (TextView) rootView.findViewById(R.id.battemp);
                view.setText(data.getBatteryTemperature() + "\u00B0 C");

                view = (TextView) rootView.findViewById(R.id.inclination);
                view.setText(data.getInclinationString() + "\u00B0");

                view = (TextView) rootView.findViewById(R.id.rps);
                view.setText(data.getRPSString() + " RPS");

                view = (TextView) rootView.findViewById(R.id.power);
                view.setText(data.getWattSeconds() + "ws");

                view = (TextView) rootView.findViewById(R.id.buzzer);
                view.setText(data.getBuzzer() ? "BUZZER" : "-");
              }

              // Update graph, if possible
              // TODO: TESTING CODE
              try {
                if (((CheckBox) getView().findViewById(R.id.checkbox_chart)).isChecked()) {
                  int divisor = 1;
                  switch (graphItemIndex) {
                    case 0:
                    case 1:
                    case 4:
                      divisor = 10;
                      break;
                    case 5:
                      divisor = 100;
                  }
                  graphControl.addDataPoint(
                      graphIndex++,
                      Double.parseDouble(readMessage.split(",")[graphItemIndex]) / divisor,
                      ((CheckBox) getView().findViewById(R.id.checkbox_scroll)).isChecked());
                }
              } catch (Exception e) {
                Log.e(TAG, "Exception updating graph", e);
              }

              break;

            case Constants.MESSAGE_DEVICE_NAME:
              // save the connected device's name
              mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
              if (null != activity) {
                Toast.makeText(activity, "Connected to " + mConnectedDeviceName, Toast.LENGTH_SHORT)
                    .show();
              }
              break;
            case Constants.MESSAGE_TOAST:
              if (null != activity) {
                Toast.makeText(
                        activity, msg.getData().getString(Constants.TOAST), Toast.LENGTH_SHORT)
                    .show();
              }
              break;
          }
        }
  /** Set up the UI and background operations for chat. */
  private void setupChat() {
    Log.d(TAG, "setupChat()");

    // Initialize the array adapter for the conversation thread
    mConversationArrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.message);

    mConversationView.setAdapter(mConversationArrayAdapter);

    // Initialize the compose field with a listener for the return key
    mOutEditText.setOnEditorActionListener(mWriteListener);

    // Initialize the send button with a listener that for click events
    mSendButton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            // Send a message using content of the edit text widget
            View view = getView();
            if (null != view) {
              TextView textView = (TextView) view.findViewById(R.id.edit_text_out);
              String message = textView.getText().toString();
              sendMessage(message);
            }
          }
        });

    mClearGraphButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            graphIndex = 0;
            graphControl.clearGraph();
          }
        });

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter =
        ArrayAdapter.createFromResource(
            getActivity(), R.array.chart_spinner_values, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    mGraphSpinner.setAdapter(adapter);
    mGraphSpinner.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            graphIndex = 0;
            graphItemIndex = (int) id;
            mClearGraphButton.callOnClick();
          }

          @Override
          public void onNothingSelected(AdapterView<?> parent) {
            // ?
          }
        });
    // Initialize the BluetoothChatService to perform bluetooth connections
    mChatService = new BluetoothChatService(getActivity(), mHandler);

    mRecordButton.setOnClickListener(
        new RecordButtonClickListener(getActivity(), mChatService.getTelemetryFileService()));

    // Initialize the buffer for outgoing messages
    mOutStringBuffer = new StringBuffer("");
  }