Skip to content

Commit

Permalink
Merge pull request openhab#15 from ibaton/5-microphonewidget
Browse files Browse the repository at this point in the history
Added homescreen widget for voice commands
  • Loading branch information
belovictor committed Sep 8, 2014
2 parents ca4b2c5 + 58d1f95 commit 81d9614
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
9 changes: 9 additions & 0 deletions mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,14 @@
</intent-filter>
</receiver>
<service android:name="org.openhab.habdroid.core.GcmIntentService" />
<receiver android:name=".ui.VoiceWidget"
android:label="@string/title_voice_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/voice_widget_info" />
</receiver>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public class OpenHABMainActivity extends FragmentActivity implements OnWidgetSel
private static MyAsyncHttpClient mAsyncHttpClient;
// NFC Launch data
private String mNfcData;
// Voice Launch data
private String mVoiceData;
// Pending NFC page
private String mPendingNfcPage;
// Drawer Layout
Expand Down Expand Up @@ -244,6 +246,13 @@ public void onItemClick(AdapterView<?> adapterView, View view, int item, long l)
} else if (getIntent().getAction().equals("org.openhab.notification.selected")) {
onNotificationSelected(getIntent());
}
}else if(getIntent().getExtras() != null){
Log.d(TAG, "This is Voice action");

List<String> results = getIntent().getExtras().getStringArrayList("android.speech.extra.RESULTS");
if (!results.isEmpty()) {
mVoiceData = results.get(0);
}
}
}
}
Expand Down Expand Up @@ -315,6 +324,9 @@ public void onOpenHABTracked(String baseUrl, String message) {
if (!TextUtils.isEmpty(mNfcData)) {
onNfcTag(mNfcData);
openNFCPageIfPending();
} else if (!TextUtils.isEmpty(mVoiceData)) {
sendItemCommand("VoiceCommand", mVoiceData);
finish();
} else {
selectSitemap(baseUrl, false);
}
Expand Down Expand Up @@ -771,7 +783,7 @@ private void launchVoiceRecognition() {
// Specify the calling package to identify your application
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, ((Object) this).getClass().getPackage().getName());
// Display an hint to the user about what he should say.
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "openHAB, at your command!");
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.info_voice_input));
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
Expand Down
66 changes: 66 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/ui/VoiceWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.openhab.habdroid.ui;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.widget.RemoteViews;
import org.openhab.habdroid.R;

/**
* Implementation of App Widget functionality.
*/
public class VoiceWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
}


@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {

// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.voice_widget);

Intent callbackIntent = new Intent(context, OpenHABMainActivity.class);
PendingIntent openhabPendingIntent = PendingIntent.getActivity(context, 9, callbackIntent, 0);

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Specify the calling package to identify your application
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, VoiceWidget.class.getPackage().getName());
// Display an hint to the user about what he should say.
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, context.getString(R.string.info_voice_input));
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, openhabPendingIntent);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 6, intent, 0);

views.setOnClickPendingIntent(R.id.btn_mic, pendingIntent);
views.setOnClickPendingIntent(R.id.btn_open_main, openhabPendingIntent);

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}


27 changes: 27 additions & 0 deletions mobile/src/main/res/layout/voice_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin"
android:background="#6333" >

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="@+id/btn_mic"

android:layout_centerHorizontal="true"
android:src="@drawable/menu_mic_dark"
android:layout_above="@+id/btn_open_main" />

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_open_main"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:src="@drawable/openhabiconsmall"
android:background="#C333" />
</RelativeLayout>
2 changes: 2 additions & 0 deletions mobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<string name="settings_openhab_sslhost">Ignore SSL hostname</string>
<string name="settings_openhab_sslhost_summary">Ignore hostname check during SSL handshake</string>
<!-- App messages strings -->
<string name="title_voice_widget">OpenHAB Voice Commands</string>
<string name="info_voice_input">"openHAB, at your command!"</string>
<string name="info_demo_mode">HABDroid is running in demo mode. To connect to your openHAB please go to settings menu.</string>
<string name="info_conn_url">Connecting to configured URL</string>
<string name="info_conn_rem_url">Connecting to remote URL</string>
Expand Down
9 changes: 9 additions & 0 deletions mobile/src/main/res/xml/voice_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/openhabicon"
android:initialLayout="@layout/voice_widget"
android:widgetCategory="home_screen">
</appwidget-provider>

0 comments on commit 81d9614

Please sign in to comment.