RSS

Monthly Archives: August 2013

Proximity sensor tweek to give you Samsung galaxy s4 airwave feature

The recently released samsung galaxy s4 phone comes with some interesting features. This will be a look at how you can emulate the airwave feature on any ordinary android phone.

proximity-callThe proximity sensor is common on most smart-phones. This is because the primary function of a proximity sensor is to disable accidental touch events. The most common scenario being- The ear coming in contact with the screen and generating touch events, To solve this issue, device manufacturers came up with the idea of a placing a proximity sensor close to the speaker, which will then detect any object in the vicinity of the speaker.while on a call. We can use this to detect gestures made by user. The technology behind it is pretty neat and can be found by googling around.

The code below will allow one to detect when a user swipes across the phone without touching the screen, as soon as that event is registered the list view will scroll.

Lets start with the layout, place a list view on the layout:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

<ListView
 android:id="@+id/lvlist"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true" >
 </ListView>

</RelativeLayout>

And then inflate that layout on the java end. And place the following code


package com.example.proximity;

import java.util.ArrayList;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

SensorManager mSensorManager;
 Sensor mproximity;

 ArrayList<String> items = new ArrayList<String>();

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

 //populate the arraylist
 for(int i = 0; i< 100; i++ ){
 items.add("Item " + i);
 }


 final ListView list = (ListView) findViewById(R.id.lvlist);
 list.setAdapter(new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1, items));

 //Access the sensor manager
 mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
 mproximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

//register a listener for the proximity sensor
 SensorEventListener proximityListener = new SensorEventListener() {

 //Incase of a gesture scroll the list
 @Override
 public void onSensorChanged(SensorEvent event) {
 // TODO Auto-generated method stub
 list.smoothScrollBy(200, 1000);
 }

 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
 // TODO Auto-generated method stub

 }
 };

 mSensorManager.registerListener(proximityListener,
 mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),
 SensorManager.SENSOR_DELAY_UI);

 }

&nbsp;

}

There are no permissions needed.

Screenshot_2013-08-16-15-32-49

This is just some basic implementation which has a lot of short comings, but with a few tweeks can bring up amazing features. You are welcomed to use the code and modify it.

 

Advertisements
 
7 Comments

Posted by on August 16, 2013 in Uncategorized

 

Voice To Text on Android

This is a code snippet in response to a request on how to recognise voice of a person. This is useful for the translator app, I built in some previous tutorial.

package com.gilo.voice;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

	Button speak;
	ListView options;
	ArrayList<String> results;

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

		speak = (Button) findViewById(R.id.bSpeak);
		options = (ListView) findViewById(R.id.lvOptions);

		speak.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// This are the intents needed to start the Voice recognizer
				Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
						RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
				i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");

				startActivityForResult(i, 1010);
			}
		});

		// retrieves data from the previous state. This is incase the phones
		// orientation changes
		if (savedInstanceState != null) {
			results = savedInstanceState.getStringArrayList("results");

			if (results != null)
				options.setAdapter(new ArrayAdapter<String>(this,
						android.R.layout.simple_list_item_1, results));
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// retrieves data from the VoiceRecognizer
		if (requestCode == 1010 && resultCode == RESULT_OK) {
			results = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			options.setAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, results));
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// This should save all the data so that when the phone changes
		// orientation the data is saved
		super.onSaveInstanceState(outState);

		outState.putStringArrayList("results", results);
	}

}

And for the xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lvOptions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1" >
    </ListView>

    <Button
        android:id="@+id/bSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Speak" />

</RelativeLayout>

Screenshot_2013-08-09-08-22-09

Screenshot_2013-08-09-08-24-06

 
9 Comments

Posted by on August 9, 2013 in Uncategorized