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

Commit

Permalink
added menu and activity to toggel WiFi state
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainemery committed Nov 20, 2014
1 parent d4a662b commit 171056e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 8 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<!-- to retrieve the SSID -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- to toggle WiFi state -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
android:allowBackup="true"
Expand All @@ -25,6 +27,11 @@
android:name="com.google.android.glass.VoiceTrigger"
android:resource="@xml/voice_trigger" />
</activity>
<activity
android:name="com.hackncheese.glassnetinfo.ToggleWifiActivity"
android:icon="@drawable/ic_world_50"
android:label="@string/title_activity_toggle_wifi" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
Expand All @@ -12,8 +13,6 @@
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.google.android.glass.media.Sounds;
import com.google.android.glass.widget.CardScrollView;
Expand Down Expand Up @@ -95,7 +94,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
updateInfo();
return true;
case R.id.toggle_wifi:
/*startActivity(new Intent(this, ToggleWifiActivity.class));*/
startActivity(new Intent(this, ToggleWifiActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
Expand Down
118 changes: 118 additions & 0 deletions app/src/main/java/com/hackncheese/glassnetinfo/ToggleWifiActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.hackncheese.glassnetinfo;

import com.google.android.glass.media.Sounds;
import com.google.android.glass.widget.CardBuilder;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

/**
* Toggles the WiFi state
*/
public class ToggleWifiActivity extends Activity {

private CardScrollView mCardScroller;

private View mView;

private boolean wifiState;

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

toggleWifiState();
mView = buildView();

mCardScroller = new CardScrollView(this);
mCardScroller.setAdapter(new CardScrollAdapter() {
@Override
public int getCount() {
return 1;
}

@Override
public Object getItem(int position) {
return mView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mView;
}

@Override
public int getPosition(Object item) {
if (mView.equals(item)) {
return 0;
}
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) {
toggleWifiState();
mView = buildView();
mCardScroller.getAdapter().notifyDataSetChanged();
// play a nice sound
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(Sounds.SUCCESS);
}
});
setContentView(mCardScroller);

// play a nice sound
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(Sounds.SUCCESS);

}

@Override
protected void onResume() {
super.onResume();
mCardScroller.activate();
}

@Override
protected void onPause() {
mCardScroller.deactivate();
super.onPause();
}

/**
* Toggles the state of WiFi
*/
private void toggleWifiState() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
wifiState = !wifiManager.isWifiEnabled();
wifiManager.setWifiEnabled(wifiState);
}

/**
* Builds a Glass styled view showing the WiFi state.
*/
private View buildView() {
CardBuilder card = new CardBuilder(this, CardBuilder.Layout.TEXT);

String txt;
if (wifiState) {
txt = "WiFi is now enabled";
} else {
txt = "WiFi is now disabled";
}

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

}
3 changes: 1 addition & 2 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
android:title="@string/menu_refresh" />
<item android:id="@+id/toggle_wifi"
android:icon="@drawable/ic_world_50"
android:title="@string/menu_toggle_wifi"
android:enabled="false"/>
android:title="@string/menu_toggle_wifi" />

</menu>
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
<resources>

<string name="app_name">GlassNetInfo</string>
<string name="title_activity_main">Show Network Info</string>

<string name="title_activity_main">Show Network Info</string>
<string name="glass_voice_trigger">Show network info</string>

<string name="title_activity_toggle_wifi">Toggle WiFi</string>

<string name="wifi_ip_label">WiFi IP</string>
<string name="wifi_ssid_label">SSID</string>
<string name="ext_ip_label">Ext IP</string>
<string name="ext_provider_label">Provider</string>
<string name="retrieving">retrieving&#8230;</string>

<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>

Expand Down

0 comments on commit 171056e

Please sign in to comment.