com.hollowsoft.smarthome.view.MainScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.hollowsoft.smarthome.view.MainScreen.java

Source

/*
 * Copyright (c) 2014 HollowSoft @Igor Morais
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 *
 *          You may obtain a copy of the License at
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.hollowsoft.smarthome.view;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.hollowsoft.smarthome.R;

/**
 * @author Igor Morais
 * @author Mor41s.1gor@gmail.com
 */
public class MainScreen extends BaseActivity implements OnItemClickListener {

    private ActionBarDrawerToggle drawerToggle;

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

        setContentView(R.layout.main_screen);

        findViews();
    }

    private void findViews() {

        final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.main_screen_drawer_layout);

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.app_name,
                R.string.app_name);

        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout.setDrawerListener(drawerToggle);

        final ListView listView = (ListView) findViewById(R.id.main_screen_list_view);
        listView.setOnItemClickListener(this);
    }

    @Override
    protected void onPostCreate(final Bundle savedInstance) {
        super.onPostCreate(savedInstance);

        drawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem menuItem) {
        return drawerToggle.onOptionsItemSelected(menuItem) ? true : super.onOptionsItemSelected(menuItem);
    }

    @Override
    public void onItemClick(final AdapterView<?> adapterView, final View view, final int position, final long id) {
    }

    public void onSpeak(final View view) {
        //
        // final SpeechRecognizer recognizer =
        // SpeechRecognizer.createSpeechRecognizer(this);
        //
        // final Intent intent = new
        // Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        //
        // intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "pt-BR");
        //
        // recognizer.startListening(intent);
        //
        // recognizer.setRecognitionListener(this);
        final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "pt-BR");
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

        startActivityForResult(intent, 1337);
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {

        if (requestCode == 1337) {

            if (resultCode == RESULT_OK) {

                final TextView textView = (TextView) findViewById(R.id.text_name);

                final ArrayList<String> textList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                final StringBuilder builder = new StringBuilder();

                for (final String string : textList) {

                    builder.append(string).append("\n");
                }

                textView.setText(builder.toString());
            }
        }
    }
}