com.sweetiepiggy.littlepro.LittleProActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.sweetiepiggy.littlepro.LittleProActivity.java

Source

/*
Copyright (C) 2014 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
    
This file is part of Little Pro.
    
Little Pro 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.
    
Little Pro 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 Little Pro; if not, see <http://www.gnu.org/licenses/>.
*/

package com.sweetiepiggy.littlepro;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class LittleProActivity extends ActionBarActivity {
    private static final String SOURCE_URL = "https://github.com/sweetiepiggy/Little-Pro";

    private static final int ACTIVITY_ENABLE_BLUETOOTH = 1;

    private BluetoothHelper mBluetoothHelper;
    private QuestionsPagerAdapter mQuestionsPagerAdapter;
    private ViewPager mViewPager;
    private BluetoothHandler mBluetoothHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_littlepro);

        mBluetoothHandler = new BluetoothHandler();
        mBluetoothHelper = new BluetoothHelper(BluetoothAdapter.getDefaultAdapter(), mBluetoothHandler);
        mQuestionsPagerAdapter = new QuestionsPagerAdapter(getSupportFragmentManager());

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

        //      List<QuestionFragment> questionFragments = new ArrayList<QuestionFragment>();
        //      questionFragments.add(QuestionFragment.newInstance("What does 2+2 equal?"));
        //      questionFragments.add(QuestionFragment.newInstance("What is 25x10?"));
        //      questionFragments.add(QuestionFragment.newInstance("Calculate 503-24."));
        //      mQuestionsPagerAdapter.setQuestionFragments(questionFragments);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case ACTIVITY_ENABLE_BLUETOOTH:
            if (resultCode == RESULT_OK) {
                //            initBluetooth();
            } else {
                Toast.makeText(getApplicationContext(), new Error("Bluetooth not enabled").getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
        case R.id.action_connect:
            //         initBluetooth();
            intent = new Intent(getApplicationContext(), BluetoothSetupActivity.class);
            startActivity(intent);
            return true;
        case R.id.action_bluetooth:
            intent = new Intent();
            intent.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
            startActivity(intent);
            return true;
        case R.id.action_about:
            intent = new Intent(getApplicationContext(), AboutActivity.class);
            startActivity(intent);
            return true;
        case R.id.action_source:
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(SOURCE_URL), "text/html");
            startActivity(Intent.createChooser(intent, null));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void initBluetooth() {
        BluetoothDevice pairedDevice = getBluetoothPair();
        if (pairedDevice != null) {
            mBluetoothHelper.connectAndTransfer(pairedDevice);
        }
    }

    private BluetoothDevice getBluetoothPair() {
        BluetoothDevice pairedDevice = null;
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(), new Error("Bluetooth not found").getMessage(),
                    Toast.LENGTH_SHORT).show();
        } else if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, ACTIVITY_ENABLE_BLUETOOTH);
        } else {
            Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
            Toast.makeText(getApplicationContext(), pairedDevices.size() + " paired devices found",
                    Toast.LENGTH_SHORT).show();
            switch (pairedDevices.size()) {
            case 0:
                //               bluetoothAdapter.startDiscovery();
                break;
            case 1:
                pairedDevice = pairedDevices.iterator().next();
                break;
            /* choose device */
            default:
                //               for (pd : pairedDevices) {
                //               }
                break;
            }
        }

        return pairedDevice;
    }

    private class BluetoothHandler extends Handler {
        public BluetoothHandler() {
        }

        @Override
        public void handleMessage(Message inputMessage) {
            byte[] data = (byte[]) inputMessage.obj;
            android.util.Log.d("LittlePro", "handle_data:[" + data[0] + "]");

            List<QuestionFragment> questionFragments = new ArrayList<QuestionFragment>();
            questionFragments.add(QuestionFragment.newInstance("What does 4+4 equal?"));
            questionFragments.add(QuestionFragment.newInstance("What is 15x10?"));
            questionFragments.add(QuestionFragment.newInstance("Calculate 303-14."));
            mQuestionsPagerAdapter.setQuestionFragments(questionFragments);
        }
    }

    private class QuestionsPagerAdapter extends FragmentPagerAdapter {
        private List<QuestionFragment> mQuestionFragments;

        public QuestionsPagerAdapter(FragmentManager fm) {
            super(fm);
            mQuestionFragments = new ArrayList<QuestionFragment>();
        }

        @Override
        public Fragment getItem(int position) {
            return mQuestionFragments.get(position);
        }

        @Override
        public int getCount() {
            return mQuestionFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Question " + position + " of " + mQuestionFragments.size();
        }

        public void setQuestionFragments(List<QuestionFragment> questionFragments) {
            mQuestionFragments = questionFragments;
            notifyDataSetChanged();
        }

    }

    private static class QuestionFragment extends Fragment {
        private static final String ARG_QUESTION = "question";
        private String mQuestion;

        public static QuestionFragment newInstance(String question) {
            QuestionFragment fragment = new QuestionFragment();
            Bundle args = new Bundle();
            args.putString(ARG_QUESTION, question);
            fragment.setArguments(args);
            return fragment;
        }

        public QuestionFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mQuestion = getArguments() == null ? null : getArguments().getString(ARG_QUESTION);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_question, container, false);
            ((TextView) view.findViewById(R.id.question)).setText(mQuestion);
            return view;
        }
    }
}