forked from openhab/openhab1-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openhab#15 from ibaton/5-microphonewidget
Added homescreen widget for voice commands
- Loading branch information
Showing
6 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
mobile/src/main/java/org/openhab/habdroid/ui/VoiceWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |