public
Description: Android Garage Door Opener
Homepage:
Clone URL: git://github.com/bradfitz/android-garage-opener.git
Brad Fitzpatrick (author)
Sun Nov 16 11:34:32 -0800 2008
android-garage-opener / src / com / danga / garagedoor / GarageDoorActivity.java
100644 252 lines (222 sloc) 7.39 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.danga.garagedoor;
 
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration;
 
import java.util.List;
 
public class GarageDoorActivity extends Activity {
private static final String TAG = "GarageDoorActivity";
 
private static final int MENU_OPEN = 2;
private static final int MENU_WIFI_OFF = 3;
  private static final int MENU_JUST_SCAN = 4;
 
private TextView textView;
  private TextView scanResultTextView;
 
private IntentFilter intentFilter;
 
private Handler logHandler = new Handler() {
    public void handleMessage(Message m) {
      String logMessage = m.getData().getString("logmessage");
      textView.setText(logMessage + "\n" + textView.getText());
    }
  };
 
private IGarageScanService scanServiceStub = null;
 
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
scanServiceStub = IGarageScanService.Stub.asInterface(service);
log("Service bound");
checkScanningState();
try {
        scanServiceStub.registerCallback(garageCallback);
      } catch (RemoteException e) {
        e.printStackTrace();
      }
}
 
public void onServiceDisconnected(ComponentName name) {
scanServiceStub = null;
};
};
 
 
private IGarageScanCallback garageCallback = new IGarageScanCallback.Stub() {
    public void logToClient(String message) throws RemoteException {
      log(message);
    }
 
    public void onScanResults(String scanResults) throws RemoteException {
      scanResultTextView.setText(scanResults);
    }
};
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scanResultTextView = (TextView) findViewById(R.id.scanresults);
 
textView = (TextView) findViewById(R.id.textthing);
textView.setText("Some test");
 
Button startScan = (Button) findViewById(R.id.StartScan);
startScan.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startScanningService(false);
}
});
 
Button stopScan = (Button) findViewById(R.id.StopScan);
stopScan.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (scanServiceStub == null) {
textView.setText("service stub is null");
return;
}
try {
scanServiceStub.setScanning(false);
scanResultTextView.setText("");
log("Stopped scanning.");
} catch (RemoteException e) {
log("Exception changing state: " + e);
}
}
});
 
intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
}
 
protected void startScanningService(boolean debugMode) {
if (scanServiceStub == null) {
log("Not bound to service.");
return;
}
    try {
      scanServiceStub.setDebugMode(debugMode);
      scanResultTextView.setText("");
      startService(new Intent(this, InRangeService.class));
      if (debugMode) {
        log("Scan-only mode started.");
      } else {
        log("Scanning mod started.");
      }
    } catch (RemoteException e) {
      log("Exception changing state: " + e);
    }
  }
 
  /**
* Check if we're currently scanning and log a message about it.
*/
protected void checkScanningState() {
if (scanServiceStub == null) {
textView.setText("service stub is null");
} else {
boolean running;
try {
running = scanServiceStub.isScanning();
textView.setText(running ? "Scanning." : "NOT scanning.");
} catch (RemoteException e) {
textView.setText("Exception error checking scanning: " + e);
}
}
}
 
@Override
public void onResume() {
super.onResume();
textView.setText("onResume");
bindService(new Intent(this, InRangeService.class),
serviceConnection, Context.BIND_AUTO_CREATE);
}
 
@Override
public void onPause() {
super.onPause();
 
if (scanServiceStub != null) {
      try {
        scanServiceStub.unregisterCallback(garageCallback);
      } catch (RemoteException e) {
        e.printStackTrace();
      }
}
 
if (serviceConnection != null) {
unbindService(serviceConnection);
}
}
 
View.OnClickListener createWifiAPairer(final String ssid) {
return new OnClickListener() {
public void onClick(View v) {
Settings.System.putInt(getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, 0);
 
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> networks = wifiManager.getConfiguredNetworks();
WifiConfiguration foundConfig = null;
for (WifiConfiguration config : networks) {
if (config.SSID.equals("\"" + ssid + "\"")) {
foundConfig = config;
break;
}
textView.setText(config.SSID + ", " + new Integer(config.SSID.length()));
}
if (foundConfig != null) {
textView.setText(new StringBuilder().append(foundConfig.networkId));
int n = 0;
try {
boolean success = wifiManager.enableNetwork(foundConfig.networkId, true);
textView.setText(new Boolean(success).toString());
if (success) {
textView.setText("Connecting to: " + ssid);
}
} catch (Exception e) {
textView.setText(e.toString() + ", " + n);
}
} else {
textView.setText(ssid + " not found");
}
}
};
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_WIFI_OFF, 0, "Wifi Off");
menu.add(Menu.NONE, MENU_OPEN, 0, "Open");
menu.add(Menu.NONE, MENU_JUST_SCAN, 0, "Just Scan");
return true;
}
 
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_OPEN:
try {
          scanServiceStub.openGarageNow();
        } catch (RemoteException e) {
          log(e.toString());
        }
break;
case MENU_WIFI_OFF:
wifi().disconnect();
break;
case MENU_JUST_SCAN:
startScanningService(true);
break;
}
return true;
}
 
private WifiManager wifi() {
return (WifiManager) getSystemService(Context.WIFI_SERVICE);
}
 
private void log(String message) {
Message m = new Message();
Bundle b = new Bundle();
b.putString("logmessage", message);
m.setData(b);
logHandler.sendMessage(m);
}
}