Android Eclipse Project L.O.R.A

package com.kolor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import android.R.*;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import java.util.Date;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements Callback {

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH-MM");
private TextToSpeech mTTS;
private static final Intent SMS_SENT = null;
private Button talk;
private SurfaceView surfaceView;
private Camera camera;
private SurfaceHolder surfaceHolder;
private Camera.PictureCallback picCall;

int hours = date.getHours();
long time = date.getTime();
String apM;
TextView textView1;
TextView textView2;
String Shout = null;
String num=null;
String mg=null;
String text=null;
String text2=null;
String sent = "SMS sent";
String delivered = "SMS Delivered";
SpeechRecognizer mSR;
Intent mSRI,mSRI2;

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

talk = (Button) findViewById(R.id.button1);
surfaceView =(SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
picCall = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera arg1) {
Bitmap bmp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
Bitmap cbmp = Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),null,true);
String pathFileName = currentDateFormat();
storePhotoToStorage(cbmp,pathFileName);
Toast.makeText(getApplicationContext(),"Done!!",Toast.LENGTH_SHORT).show();
MainActivity.this.camera.startPreview();
}
};
if(hours >=00 && hours<12) 
{
mTTS.speak("Hello Sir. Good Morning. It's "+ sdf.format(date)+ "AM" , TextToSpeech.QUEUE_FLUSH, null);
}else if(hours >= 12 && hours<16 ) 
{
mTTS.speak("Hello Sir. Good Afternoon. It's "+ sdf.format(date)+ "PM" , TextToSpeech.QUEUE_FLUSH, null);
}else if(hours >= 16 && hours<24 ) 
{
mTTS.speak("Hello Sir. Good Evening. It's "+ sdf.format(date)+ "PM" , TextToSpeech.QUEUE_FLUSH, null);
}
mSR = SpeechRecognizer.createSpeechRecognizer(this);

mSRI = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSRI.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSRI.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

mSR.setRecognitionListener(new RecognitionListener() {

@Override
public void onRmsChanged(float arg0) {
// TODO Auto-generated method stub

}

@Override
public void onResults(Bundle bundle) {

ArrayList<String> matches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null) {
text = (matches.get(0));
String WORD = null;
if(text.contains("hello") || text.contains("hey lora")||text.contains("lora")) 
{
WORD = "Hello Sir.";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("HOLA") || text.contains("hola")) 
{
WORD ="hola, amigo";
mTTS.speak(WORD,TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("your") && text.contains("name") || text.contains("who are you")) 
{
WORD = "Hello Sir. I'm lora. Your Personal Assistant";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("version") && text.contains("version")) 
{
WORD = "10 24M 56D";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("your") && text.contains("birthday")) 
{
WORD = "20 20 october 2 on Friday, sir.";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("lora stop") || text.contains("hey lora stop") || text.contains("stop listening")) 
{
WORD = "OK Sir.";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("happy") && text.contains("you") && text.contains("birthday")) 
{
WORD = "Thank You So much, sir.";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("help") || text.contains("emergency") ||text.contains("i'm") && text.contains("in") && text.contains("danger") || text.contains("i") && text.contains("in") && text.contains("danger")) 
{
WORD = "Calling and Sending Request";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:"+"8639861311"));
startActivity(call);
star();
sms();
}else if(text.contains("what") && text.contains("are you doing" ) || text.contains("lora whats you doing" ))
{
WORD = "Waiting for your Command Sir.";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("are you there"))
{
WORD = "Yes Sir. At your services";
mTTS.speak(WORD, TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("capture me" ) || text.contains("take photo"))
{
camera.takePicture(null,null,picCall);
mTTS.speak("Ok sir,Photo Captured", TextToSpeech.QUEUE_FLUSH, null);
star();
}else if(text.contains("the time"))
{
mTTS.speak(sdf.format(date), TextToSpeech.QUEUE_FLUSH, null);
star();
}else 
{
mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
star();
}
/*
* else if(text.contains("send a message" ) || text.contains("send text")) {
* messageMe(); 
* mTTS.speak("Ok Sir sending message",
* TextToSpeech.QUEUE_FLUSH, null); 
* star(); }
*/
}

}

@Override
public void onReadyForSpeech(Bundle arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPartialResults(Bundle arg0) {
// TODO Auto-generated method stub

}

@Override
public void onEvent(int arg0, Bundle arg1) {
// TODO Auto-generated method stub

}

@Override
public void onError(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub

}

@Override
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub

}

@Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub

}
});
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTTS.setLanguage(Locale.ENGLISH);

if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not Supported");
} else {
talk.setEnabled(true);
}
} else {
Log.e("TTS", "Initialization Failed");
}

}
});
mSR.startListening(mSRI);
talk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mSR.startListening(mSRI);
}
});

}
public void star() 
{
try {Thread.sleep(500);} catch (InterruptedException e) {}
mSR.startListening(mSRI);
}

protected void storePhotoToStorage(Bitmap cbmp, String pathFileName) {
File outputFile = new File(Environment.getExternalStorageDirectory(),"/DCIM/" + pathFileName + ".jpg");
try
{
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
cbmp.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}catch(FileNotFoundException f) 
{
f.printStackTrace();
}catch(IOException i) 
{
i.printStackTrace();
}
}

protected String currentDateFormat() {
SimpleDateFormat dateForm = new SimpleDateFormat("yyyymmdd_hh_mm_ss");
String currentTime = dateForm.format(new Date());
return currentTime;
}

public void sms() 
{
PendingIntent p1 = PendingIntent.getBroadcast(this,0,new Intent(sent),0);
PendingIntent p2 = PendingIntent.getBroadcast(this,0,new Intent(delivered),0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch(getResultCode()) 
{
case Activity.RESULT_OK:
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
mTTS.speak("Sir,message sent successfully", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
}
}
},new IntentFilter(sent));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch(getResultCode()) 
{
case Activity.RESULT_OK:
Toast.makeText(getApplicationContext(), "Delivered", Toast.LENGTH_LONG).show();
mTTS.speak("Sir, message Delivered successfully", TextToSpeech.QUEUE_FLUSH, null);
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getApplicationContext(), "Delivery error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , message delivery failed", TextToSpeech.QUEUE_FLUSH, null);
break;
}
}
},new IntentFilter(delivered));
SmsManager msg = SmsManager.getDefault();
msg.sendTextMessage("8639861311", null ," Hello I'm Friday \n" + "Help my sir needs a emegency help please", p1,p2);
Toast.makeText(getApplicationContext(), "Dont Worry i sent a message Sir Stay Still",Toast.LENGTH_LONG).show();
}
public void messageMe() 
{
PendingIntent p1 = PendingIntent.getBroadcast(this,0,new Intent(sent),0);
PendingIntent p2 = PendingIntent.getBroadcast(this,0,new Intent(delivered),0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch(getResultCode()) 
{
case Activity.RESULT_OK:
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , message sent succesfully", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getApplicationContext(), "Sending error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , sending message failed", TextToSpeech.QUEUE_FLUSH, null);
break;
}
}
},new IntentFilter(sent));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch(getResultCode()) 
{
case Activity.RESULT_OK:
Toast.makeText(getApplicationContext(), "Delivered", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , message delivered successfully", TextToSpeech.QUEUE_FLUSH, null);
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getApplicationContext(), "Delivery error", Toast.LENGTH_LONG).show();
mTTS.speak("Sir , message delivery failed", TextToSpeech.QUEUE_FLUSH, null);
break;
}
}
},new IntentFilter(delivered));
SmsManager msg = SmsManager.getDefault();
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
mTTS.speak("whom do you want to send me a message sir", TextToSpeech.QUEUE_FLUSH, null);
mSR.startListening(mSRI);
num = text; 
}
});
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
mSR.startListening(mSRI);
mTTS.speak("what is the message you want to send", TextToSpeech.QUEUE_FLUSH, null);
mg = text;
}
});
t1.start();
t2.start();
try {t1.join();} catch (InterruptedException e) {}
try {t2.join();} catch (InterruptedException e) {}
msg.sendTextMessage(num, null ,mg, p1,p2);
Toast.makeText(getApplicationContext(), "Message Sent",Toast.LENGTH_LONG).show();
}

@Override
protected void onDestroy() {

if (mTTS != null) {
mTTS.stop();
mTTS.shutdown();
}

super.onDestroy();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
try 
{
camera = Camera.open();
}catch(Exception e) 
{
}
Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(28);
parameters.setPreviewSize(352,288);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
try 
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch(Exception i) 
{
i.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera.release();
camera=null;
}
}




                            -----------activity_main-------------

<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="com.pro.MainActivity" >

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:enabled="true"
        android:text="listen"
        android:visibility="visible" />

</RelativeLayout>









Comments

Popular posts from this blog

PyCharm Project

APP