Esempio n. 1
0
  private void startTimer() {
    // change visibility of minute + and - buttons, minutes text and delay
    showMinutes(false);
    // setting color to the progress with color filters
    // rgb color picker http://www.hexcolortool.com/#2f4efe color picker
    // red
    mProgressBar
        .getBackground()
        .setColorFilter(Color.argb(255, 255, 87, 34), PorterDuff.Mode.SRC_IN);
    mProgressBar
        .getProgressDrawable()
        .setColorFilter(Color.argb(255, 255, 87, 34), PorterDuff.Mode.SRC_IN);
    // max needs to be set so progress shows correctly
    mProgressBar.setMax((textViewToInt((minutes)) * 60));

    // change visibility of minute + and - buttons and minutes
    showMinutes(false);
    timerStopped = false;

    // set counter value
    counter = (textViewToInt(minutes) * 60) - 18;

    countDownTimer =
        new CountDownTimer(totalTimeCountInMilliseconds, 500) {
          @Override
          public void onTick(long millisUntilFinished) {

            // show time
            long seconds = millisUntilFinished / 1000;
            mProgressBar.setProgress((int) (millisUntilFinished / 1000));
            textViewShowTime.setText(
                String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));

            // every 10 seconds read volt, if volt decreasing is ongoing it will skip this
            if ((int) seconds == counter && decreaseThread == null) {
              Log.i(TAG, "EVERY 10 SECONDS");

              mBluetoothLeService.readVoltage();
            }
            // every 12 seconds read polarity, if volt decreasing is ongoing it will skip this
            if ((int) seconds == (counter - 2) && decreaseThread == null) {
              Log.i(TAG, "EVERY 12 SECONDS");
              mBluetoothLeService.readPolarity();
              counter -= 10;
            }

            // halftime
            if (millisUntilFinished < (totalTimeCountInMilliseconds / 2)) {
              // blue
              mProgressBar
                  .getBackground()
                  .setColorFilter(Color.argb(255, 48, 79, 254), PorterDuff.Mode.SRC_IN);
              mProgressBar
                  .getProgressDrawable()
                  .setColorFilter(Color.argb(255, 48, 79, 254), PorterDuff.Mode.SRC_IN);

              // check if polarity has changed
              if (!polarityChanged) {
                changePolarity();
              }
            }

            // check if increaseVoltToMax thread is done and max_volt is false
            if (increaseThread.getState() == Thread.State.TERMINATED && !max_volt) {
              setButtonDisabled(true);
              max_volt = true;
            }

            // check if decreaseThread has initialized
            if (decreaseThread != null) {
              // check if decreaseThread is running
              if (decreaseThread.getState() != Thread.State.TERMINATED) {
                setButtonDisabled(false);
              }
            }

            // start decrease volt to zero when until time is equal volts/2 plus 2
            if ((millisUntilFinished / 1000) == (textViewToInt(volts) / 2 + 2)) {
              int volt = textViewToInt(volts);
              decreaseThread = decreaseVoltToZero(volt);
            }
          }

          public void onFinish() {
            // when the timer is finished
            textViewShowTime.setText(getBaseContext().getResources().getString(R.string.done));
            textViewShowTime.setVisibility(View.VISIBLE);
            buttonStartTime.setVisibility(View.GONE);
            buttonStopTime.setVisibility(View.GONE);
            mProgressBar.getBackground().clearColorFilter();
            mProgressBar
                .getBackground()
                .setColorFilter(Color.argb(255, 0, 230, 118), PorterDuff.Mode.SRC_IN);
            mProgressBar
                .getProgressDrawable()
                .setColorFilter(Color.argb(255, 0, 230, 118), PorterDuff.Mode.SRC_IN);
            // turn off bluetooth connection to the device and let the customer know
            stopTreatment();
          }
        }.start();
  }
Esempio n. 2
0
  private void findViews() {

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle =
        new ActionBarDrawerToggle(
            this,
            drawer,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    nestedScrollView = (NestedScrollView) findViewById(R.id.nsv);
    prefs = getSharedPreferences("user_prefs", MODE_PRIVATE);
    // Button
    addVoltButton = (Button) findViewById(R.id.addVoltButton);
    addMinuteButton = (Button) findViewById(R.id.addMinuteButton);
    decreaseVoltButton = (Button) findViewById(R.id.decreaseVoltButton);
    decreaseMinuteButton = (Button) findViewById(R.id.decreaseMinuteButton);
    buttonStartTime = (Button) findViewById(R.id.btnStartTime);
    buttonStopTime = (Button) findViewById(R.id.btnStopTime);
    minuteHelpText = (TextView) findViewById(R.id.minuteHelpText);
    // TextViews
    textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
    volts = (TextView) findViewById(R.id.volts);
    volts.setText(prefs.getInt("volts", 20) + "");

    minutes = (TextView) findViewById(R.id.minutes);
    minutes.setText(prefs.getInt("minutes", 2) + "");

    // checkbox
    delayCheckBox = (CheckBox) findViewById(R.id.delayCheckBox);
    delayCheckBox.setChecked(prefs.getBoolean("delay", false));
    delaytimer = delayCheckBox.isChecked();

    voltageTextView = (TextView) findViewById(R.id.voltage_textview);
    polarityTextView = (TextView) findViewById(R.id.polarity_textview);

    // Progress Bar
    mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
    //  Floating Action Button
    // fab = (FloatingActionButton) findViewById(R.id.fab);

    // Views
    rootview = (RelativeLayout) findViewById(R.id.rootview);
    connect_button_view = (RelativeLayout) findViewById(R.id.connect_button_view);
    connectDeviceButton = (Button) findViewById(R.id.connect_device_button);

    // set connect view
    setVisibility(CONNECT_VIEW);

    // this should make the screen stay on when rootview is visible
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (rootview != null && rootview.getVisibility() == View.VISIBLE) {
      rootview.setKeepScreenOn(true);
    }

    mProgressBar
        .getBackground()
        .setColorFilter(Color.argb(255, 0, 230, 118), PorterDuff.Mode.SRC_IN);
    mProgressBar
        .getProgressDrawable()
        .setColorFilter(Color.argb(255, 0, 230, 118), PorterDuff.Mode.SRC_IN);

    // Listeners
    addMinuteButton.setOnClickListener(this);
    addVoltButton.setOnClickListener(this);
    decreaseVoltButton.setOnClickListener(this);
    decreaseMinuteButton.setOnClickListener(this);
    buttonStartTime.setOnClickListener(this);
    buttonStopTime.setOnClickListener(this);
    // fab.setOnClickListener(this);
    connectDeviceButton.setOnClickListener(this);
    textViewShowTime.setOnClickListener(this);
    delayCheckBox.setOnCheckedChangeListener(this);

    final Snackbar snackbar =
        Snackbar.make(
                nestedScrollView, "Bluetooth has not been enabled", Snackbar.LENGTH_INDEFINITE)
            .setAction(
                "ENABLE",
                new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                    // ENABLE DEVICE'S BLUETOOTH
                    setDeviceBluetooth(true);
                  }
                });

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    boolean isEnabled = bluetoothAdapter.isEnabled();
    if (!isEnabled) {

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder
          .setTitle(getBaseContext().getResources().getString(R.string.use_bluetooth))
          .setMessage(getBaseContext().getResources().getString(R.string.need_bluetooth_pliz))
          .setCancelable(false)
          .setPositiveButton(
              getBaseContext().getResources().getString(R.string.yes),
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  // ENABLE DEVICE'S BLUETOOTH
                  setDeviceBluetooth(true);
                  dialog.dismiss();
                }
              })
          .setNegativeButton(
              getBaseContext().getResources().getString(R.string.no),
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  snackbar.show();
                  dialog.dismiss();
                }
              });
      AlertDialog alertDialog = builder.create();

      alertDialog.show();
    }
  }