Exemple #1
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    // start the settings activity
    if (id == R.id.action_settings) {

      if (isSafe()) {
        // run a toast
        Toast.makeText(
                MainActivity.this,
                "Stopping Bluetooth. Settings are being loaded. Please wait ....",
                Toast.LENGTH_SHORT)
            .show();

        (new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    // give the toast a moment to appear
                    try {
                      Thread.sleep(100);
                    } catch (InterruptedException e) {
                      e.printStackTrace();
                    }

                    if (device != null) {
                      // stop the BT device
                      device.stopAndJoin();
                      device.clearFields();
                      device.registerFilters();
                      BluetoothManager.getInstance().disconnect();
                    }

                    // load the activity
                    Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivityForResult(intent, SETTINGS_ACTIVITY);
                  }
                }))
            .start();
        return true;
      }
    }
    // see AppSectionsPagerAdapter for the right sequence
    else if (id == R.id.action_main) {
      // loadFragement(new MainFragment());
      viewPager.setCurrentItem(0, true);
      updateActionBar();

    } else if (id == R.id.action_technical) {
      // loadFragement(new TechnicalFragment());
      viewPager.setCurrentItem(1, true);
      updateActionBar();

    } else if (id == R.id.action_experimental) {
      // loadFragement(new ExperimentalFragment());
      viewPager.setCurrentItem(2, true);
      updateActionBar();
    }
    // else if (id == R.id.action_bluetooth) {
    // }

    return super.onOptionsItemSelected(item);
  }
Exemple #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // always create an instance
    // dataLogger = DataLogger.getInstance();
    dataLogger = new DataLogger();

    debug("MainActivity: onCreate");

    instance = this;

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // navigation bar
    appSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
    actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
    // actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    viewPager = (ViewPager) findViewById(R.id.main);
    viewPager.setAdapter(appSectionsPagerAdapter);
    viewPager.setOnPageChangeListener(
        new ViewPager.SimpleOnPageChangeListener() {
          @Override
          public void onPageSelected(int position) {
            // actionBar.setSelectedNavigationItem(position);
            updateActionBar();
          }
        });
    updateActionBar();

    /*
    for (int i = 0; i < appSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(appSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(MainActivity.this));
    }
    */

    // load the initial "main" fragment
    // loadFragement(new MainFragment());

    setTitle(TAG + " - not connected");
    setBluetoothState(BLUETOOTH_DISCONNECTED);

    // tabs
    // final ActionBar actionBar = getSupportActionBar();
    // Specify that tabs should be displayed in the action bar.

    // open the database
    CanzeDataSource.getInstance(getBaseContext()).open();
    // cleanup
    CanzeDataSource.getInstance().cleanUp();

    // setup cleaning (once every hour)
    Runnable cleanUpRunnable =
        new Runnable() {
          @Override
          public void run() {
            CanzeDataSource.getInstance().cleanUp();
          }
        };
    Handler handler = new Handler();
    handler.postDelayed(cleanUpRunnable, 60 * 1000);

    // register for bluetooth changes
    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(broadcastReceiver, intentFilter);

    // configure Bluetooth manager
    BluetoothManager.getInstance()
        .setBluetoothEvent(
            new BluetoothEvent() {
              @Override
              public void onBeforeConnect() {
                setBluetoothState(BLUETOOTH_SEARCH);
              }

              @Override
              public void onAfterConnect(BluetoothSocket bluetoothSocket) {
                device.init(visible);
                device.registerFilters();

                // set title
                debug("MainActivity: onAfterConnect > set title");
                runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        setTitle(
                            TAG
                                + " - connected to <"
                                + bluetoothDeviceName
                                + "@"
                                + bluetoothDeviceAddress
                                + ">");
                        setBluetoothState(BLUETOOTH_CONNECTED);
                      }
                    });
              }

              @Override
              public void onBeforeDisconnect(BluetoothSocket bluetoothSocket) {}

              @Override
              public void onAfterDisconnect() {
                runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        setTitle(TAG + " - disconnected");
                      }
                    });
              }
            });
    // detect hardware status
    int BT_STATE = BluetoothManager.getInstance().getHardwareState();
    if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_AVAILABLE)
      Toast.makeText(
              this.getBaseContext(),
              "Sorry, but your device doesn't seem to have Bluetooth support!",
              Toast.LENGTH_LONG)
          .show();
    else if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_ACTIVE) {
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBtIntent, 1);
    }

    // load settings
    // - includes the reader
    // - includes the decoder
    // loadSettings(); --> done in onResume

    // load fields from static code
    debug("Loaded fields: " + fields.size());

    // load fields
    // final SharedPreferences settings = getSharedPreferences(PREFERENCES_FILE, 0);
    (new Thread(
            new Runnable() {
              @Override
              public void run() {
                debug("Loading fields last field values from database");
                for (int i = 0; i < fields.size(); i++) {
                  Field field = fields.get(i);
                  field.setCalculatedValue(CanzeDataSource.getInstance().getLast(field.getSID()));
                  // debug("MainActivity: Setting "+field.getSID()+" = "+field.getValue());
                  // f.setValue(settings.getFloat(f.getUniqueID(), 0));
                }
                debug("Loading fields last field values from database (done)");
              }
            }))
        .start();
  }