com.heron.btcommand.Main.java Source code

Java tutorial

Introduction

Here is the source code for com.heron.btcommand.Main.java

Source

/*
 * @file   Main.java
 * @brief  .
 * 
 * @author Stanislas Bach <stanislasbach@gmail.com>
 * 
 * @see    http://sbach.fr/
 * 
 * @copyright
 *     
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.heron.btcommand;

import com.heron.btserial.BtSerial;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class Main extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    Boolean isConnected = false;

    BtSerial mBtSerial;

    MainSectionFragment mMainFragment = new MainSectionFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem connectItem = menu.findItem(R.id.connect);
        connectItem.setTitle(!this.isConnected ? "Connect" : "Disconnect");

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_settings:
            // Intent intent = new Intent(this, PreferencesActivity.class);
            // startActivityForResult(intent, REQUEST_PREFERENCE);
            return true;
        case R.id.connect:

            if (!this.isConnected)
                connectBt();
            else
                shutdownBt();

            item.setTitle(!this.isConnected ? "Connect" : "Disconnect");

            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void connectBt() {
        this.isConnected = true;

        mBtSerial = new BtSerial();
        String[] list = mBtSerial.list();

        mBtSerial.connect(list[0]);

        mMainFragment.fillList();
    }

    public void shutdownBt() {
        this.isConnected = false;

        mBtSerial.disconnect();
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return mMainFragment;
            case 1:
                Fragment fragment = new TerminalSectionFragment();
                return fragment;
            default:
                return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "Main";
            case 1:
                return "Terminal";
            }
            return null;
        }
    }
}