Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Commit

Permalink
added voice commands to the "toggle wifi" activity menu
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainemery committed Nov 21, 2014
1 parent c739652 commit 5e1e7dd
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 19 deletions.
116 changes: 98 additions & 18 deletions app/src/main/java/com/hackncheese/glassnetinfo/ToggleWifiActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;

import com.google.android.glass.media.Sounds;
import com.google.android.glass.view.WindowUtils;
import com.google.android.glass.widget.CardBuilder;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;
import com.google.android.glass.widget.Slider;
import com.google.android.glass.widget.Slider.GracePeriod;

/**
* Toggles the WiFi state
* Shows a grace period slider before enabling/disabling WiFi
*/
public class ToggleWifiActivity extends Activity {

Expand All @@ -30,7 +36,7 @@ public class ToggleWifiActivity extends Activity {

private Slider mSlider;
private Slider.GracePeriod mGracePeriod;
private final GracePeriod.Listener mGracePeriodListener = new GracePeriod.Listener() {
private final Slider.GracePeriod.Listener mGracePeriodListener = new Slider.GracePeriod.Listener() {
@Override
public void onGracePeriodEnd() {
mGracePeriod = null;
Expand All @@ -52,12 +58,18 @@ public void onGracePeriodCancel() {
am.playSoundEffect(Sounds.DISMISSED);
}
};

//current WiFi state : true = enabled, false = disabled
private boolean wifiState;

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

// Request a voice menu
getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);

// get the current WiFi state
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
wifiState = wifiManager.isWifiEnabled();

Expand Down Expand Up @@ -86,25 +98,98 @@ public int getPosition(Object item) {
return AdapterView.INVALID_POSITION;
}
});
// Handle the TAP event.
mCardScroller.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mGracePeriod = Slider.from(parent).startGracePeriod(mGracePeriodListener);
mView = buildView();
mCardScroller.getAdapter().notifyDataSetChanged();
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(Sounds.TAP);
}
});
setContentView(mCardScroller);

// when entering the activity, the WiFi toggles directly after the grace period
mSlider = Slider.from(mCardScroller);
mGracePeriod = mSlider.startGracePeriod(mGracePeriodListener);

mView = buildView();
}

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
Log.d(TAG, "onCreatePanelMenu");
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS ||
featureId == Window.FEATURE_OPTIONS_PANEL) {
getMenuInflater().inflate(R.menu.toggle_wifi, menu);
return true;
}

// Pass through to super to setup touch menu.
return super.onCreatePanelMenu(featureId, menu);
}

/**
* onPreparePanel is called every time the menu is shown
* Here, we change what's in the menu based on the current state of the activity:
* - if in a grace period, we only show the Cancel item
* - if not in a grace period, we offer the choice of toggling WiFi or returning to the previous activity
*/
@Override
public boolean onPreparePanel(int featureId, View view, Menu menu) {
Log.d(TAG, "onPreparePanel");
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS ||
featureId == Window.FEATURE_OPTIONS_PANEL) {
if (mGracePeriod == null) {
// not in a grace period
menu.setGroupVisible(R.id.tw_toggling, false);
menu.setGroupVisible(R.id.tw_not_toggling, true);
// we change the "Toggle WiFi" menu label to be more precise
if (wifiState) {
menu.findItem(R.id.tw_toggle_wifi).setTitle(R.string.toggle_wifi_disable_wifi);
} else {

menu.findItem(R.id.tw_toggle_wifi).setTitle(R.string.toggle_wifi_enable_wifi);
}
} else {
// in a grace period, we only show the cancel toggling menu item
menu.setGroupVisible(R.id.tw_toggling, true);
menu.setGroupVisible(R.id.tw_not_toggling, false);
}
return true;
}
// Pass through to super to setup touch menu.
return super.onPreparePanel(featureId, view, menu);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
openOptionsMenu();
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Log.d(TAG, "onMenuItemSelected");
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS ||
featureId == Window.FEATURE_OPTIONS_PANEL) {
switch (item.getItemId()) {
case R.id.tw_toggle_wifi:
// we start a grace period
mGracePeriod = Slider.from(mCardScroller).startGracePeriod(mGracePeriodListener);
mView = buildView();
mCardScroller.getAdapter().notifyDataSetChanged();
break;
case R.id.tw_close_activity:
// we go back to the previous activity (if any)
this.onBackPressed();
break;
case R.id.tw_cancel_toggle:
// cancel toggle = cancel grace period
mGracePeriod.cancel();
mView = buildView();
mCardScroller.getAdapter().notifyDataSetChanged();
break;
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}

@Override
protected void onResume() {
super.onResume();
Expand Down Expand Up @@ -150,27 +235,22 @@ private View buildView() {
CardBuilder card = new CardBuilder(this, CardBuilder.Layout.MENU);

String txt;
String footnote;

if (mGracePeriod != null) {
if (wifiState) {
txt = "Turning WiFi off";
} else {
txt = "Turning WiFi on";
}
footnote = "Dismiss to cancel";
} else {
if (wifiState) {
txt = "WiFi is enabled";
footnote = "Tap to turn off";
} else {
txt = "WiFi is disabled";
footnote = "Tap to turn on";
}
}

card.setText(txt);
card.setFootnote(footnote);
return card.getView();
}

Expand Down
Binary file added app/src/main/res/drawable-hdpi/ic_no_50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions app/src/main/res/menu/toggle_wifi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/tw_toggling">
<item
android:id="@+id/tw_cancel_toggle"
android:icon="@drawable/ic_no_50"
android:title="@string/toggle_wifi_cancel_toggle" />
</group>
<group android:id="@+id/tw_not_toggling">
<item
android:id="@+id/tw_toggle_wifi"
android:icon="@drawable/ic_wifi_50"
android:title="@string/toggle_wifi_enable_wifi" />
<item
android:id="@+id/tw_close_activity"
android:icon="@drawable/ic_no_50"
android:title="@string/toggle_wifi_close_activity" />
</group>

</menu>
7 changes: 6 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
<string name="wlan_na">n/a</string>
<string name="ssid_na">n/a</string>
<string name="http_response_na">n/a</string>
<string name="http_response_timeout">(timed out)</string>
<string name="http_response_timeout">(timeout)</string>

<string name="menu_refresh">Refresh</string>
<string name="menu_toggle_wifi">Toggle WiFi</string>

<string name="toggle_wifi_cancel_toggle">Cancel</string>
<string name="toggle_wifi_close_activity">Return</string>
<string name="toggle_wifi_enable_wifi">Enable WiFi</string>
<string name="toggle_wifi_disable_wifi">Disable WiFi</string>

</resources>

0 comments on commit 5e1e7dd

Please sign in to comment.