-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMainActivity.java
More file actions
485 lines (434 loc) · 18.1 KB
/
MainActivity.java
File metadata and controls
485 lines (434 loc) · 18.1 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package com.getirkit.example.activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.getirkit.irkit.IRKit;
import com.getirkit.irkit.IRKitEventListener;
import com.getirkit.irkit.IRPeripheral;
import com.getirkit.irkit.IRPeripherals;
import com.getirkit.irkit.IRSignal;
import com.getirkit.irkit.activity.DeviceActivity;
import com.getirkit.irkit.activity.IRKitSetupActivity;
import com.getirkit.irkit.activity.SignalActivity;
import com.getirkit.irkit.activity.WaitSignalActivity;
import com.getirkit.example.R;
import com.getirkit.example.adapter.DeviceListAdapter;
import com.getirkit.example.adapter.SignalListAdapter;
import com.getirkit.example.fragment.DevicesFragment;
import com.getirkit.example.fragment.NavigationDrawerFragment;
import com.getirkit.example.fragment.SelectSignalActionDialogFragment;
import com.getirkit.example.fragment.SignalsFragment;
import com.getirkit.irkit.net.IRAPIError;
import com.getirkit.irkit.net.IRAPIResult;
public class MainActivity extends AppCompatActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks, IRKitEventListener, DevicesFragment.DevicesFragmentListener,
SelectSignalActionDialogFragment.SelectSignalActionDialogFragmentListener, SignalsFragment.SignalsFragmentListener {
public static final String TAG = MainActivity.class.getSimpleName();
// Activity request codes
private static final int REQUEST_IRKIT_SETUP = 1;
private static final int REQUEST_SIGNAL_DETAIL = 2;
private static final int REQUEST_WAIT_SIGNAL = 3;
private static final int REQUEST_DEVICE_DETAIL = 4;
private int currentSection;
private int editingPeripheralPosition = -1;
private int selectedSignalPosition = -1;
private SignalListAdapter signalListAdapter;
private DeviceListAdapter deviceListAdapter;
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
if (mTitle == null) {
mTitle = getTitle();
}
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
if (savedInstanceState != null) {
editingPeripheralPosition = savedInstanceState.getInt("editingPeripheralPosition");
selectedSignalPosition = savedInstanceState.getInt("selectedSignalPosition");
}
IRKit irkit = IRKit.sharedInstance();
irkit.setIRKitEventListener(this);
// Set context, then initialize if not initialized yet.
// contextをセットして初期化する。すでに初期化済みの場合は
// contextのセットのみ行うので何回呼んでもOK。
irkit.init(getApplicationContext());
// Show dialog after orientation change (support library bug?)
showSelectSignalActionDialogIfNeeded();
}
/**
* Show SelectSignalActionDialog if the dialog exists in fragment manager.
*/
private void showSelectSignalActionDialogIfNeeded() {
final SelectSignalActionDialogFragment dialog = (SelectSignalActionDialogFragment) getSupportFragmentManager().findFragmentByTag("SelectSignalActionDialogFragment");
if (dialog != null) {
new Handler().post(new Runnable() {
@Override
public void run() {
dialog.show(getSupportFragmentManager(), "SelectSignalActionDialogFragment");
}
});
}
}
/**
* Save UI state
*
* @param outState
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
// Save UI state changes to the outState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
outState.putInt("editingPeripheralPosition", editingPeripheralPosition);
outState.putInt("selectedSignalPosition", selectedSignalPosition);
super.onSaveInstanceState(outState);
}
@Override
protected void onResume() {
super.onResume();
IRKit irkit = IRKit.sharedInstance();
irkit.setIRKitEventListener(this);
// Start Bonjour discovery
// Bonjour検索を開始
irkit.startServiceDiscovery();
// Watch Wi-Fi state change
// Wi-Fi状態の変化を監視する
irkit.registerWifiStateChangeListener();
// Get clientkey if we have not received it yet
// clientkeyをまだ取得していない場合は取得する
irkit.registerClient();
}
@Override
protected void onPause() {
super.onPause();
IRKit irkit = IRKit.sharedInstance();
// Stop Bonjour discovery
// Bonjour検索を停止
irkit.stopServiceDiscovery();
// Unset listener
// リスナーを解除
irkit.setIRKitEventListener(null);
// Unwatch Wi-Fi state change
// Wi-Fi状態の変化の監視をやめる
irkit.unregisterWifiStateChangeListener();
}
@Override
public void onNavigationDrawerItemSelected(int position) {
String tag = String.valueOf(position + 1);
Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
if (fragment == null) {
if (position == 0) { // Signals (Buttons)
fragment = SignalsFragment.newInstance(position + 1);
} else if (position == 1) { // Devices
fragment = DevicesFragment.newInstance(position + 1);
} else {
throw new IllegalStateException("Unknown drawer item position: " + position);
}
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment, tag)
.commit();
}
public void onSectionAttached(int number) {
currentSection = number;
switch (number) {
case 1:
mTitle = getString(R.string.title_signals);
break;
case 2:
mTitle = getString(R.string.title_devices);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
if (currentSection == 1) { // Signals (Buttons)
getMenuInflater().inflate(R.menu.signals, menu);
} else if (currentSection == 2) { // Devices
getMenuInflater().inflate(R.menu.devices, menu);
}
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_signals__add) {
// Start WaitSignalActivity
Intent intent = new Intent(this, WaitSignalActivity.class);
startActivityForResult(intent, REQUEST_WAIT_SIGNAL);
return true;
} else if (id == R.id.menu_devices__add) {
// Start IRKitSetupActivity
Intent intent = new Intent(this, IRKitSetupActivity.class);
startActivityForResult(intent, REQUEST_IRKIT_SETUP);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IRKIT_SETUP) { // Returned from IRKitSetupActivity
if (resultCode == RESULT_OK) {
if (deviceListAdapter != null) {
deviceListAdapter.notifyDataSetChanged();
}
}
} else if (requestCode == REQUEST_WAIT_SIGNAL) { // Returned from WaitSignalActivity
if (resultCode == RESULT_OK) {
Bundle args = data.getExtras();
IRSignal signal = args.getParcelable("signal");
if (signal == null) {
Log.e(TAG, "failed to receive signal");
return;
}
Log.d(TAG, "received signal: " + signal);
IRKit irkit = IRKit.sharedInstance();
signal.setId(irkit.signals.getNewId());
if (signal.hasBitmapImage()) {
// Do not call renameToSuggestedImageFilename before
// assigning an ID to the signal.
if (!signal.renameToSuggestedImageFilename(this)) {
Log.e(TAG, "Failed to rename bitmap file");
}
}
// Add and save the signal
irkit.signals.add(signal);
irkit.signals.save();
if (signalListAdapter != null) {
signalListAdapter.notifyDataSetChanged();
}
}
} else if (requestCode == REQUEST_DEVICE_DETAIL) { // Returned from DeviceActivity
if (resultCode == RESULT_OK) {
Bundle args = data.getExtras();
String action = args.getString("action");
IRPeripheral peripheral = args.getParcelable("peripheral");
switch (action) {
case "save":
saveEditingPeripheral(peripheral);
break;
case "delete":
deleteEditingPeripheral();
break;
default:
Log.e(TAG, "unknown action: " + action);
break;
}
}
editingPeripheralPosition = -1;
} else if (requestCode == REQUEST_SIGNAL_DETAIL) {
if (resultCode == RESULT_OK) {
Bundle args = data.getExtras();
String action = args.getString("action");
IRSignal signal = args.getParcelable("signal");
IRKit irkit = IRKit.sharedInstance();
switch (action) {
case "save":
if (selectedSignalPosition != -1) {
IRSignal signalToEdit = irkit.signals.get(selectedSignalPosition);
if (signalToEdit != null) {
signalToEdit.copyFrom(signal, this);
}
irkit.signals.save();
if (signalListAdapter != null) {
signalListAdapter.notifyDataSetChanged();
}
}
break;
case "delete":
if (selectedSignalPosition != -1) {
irkit.signals.remove(selectedSignalPosition);
irkit.signals.save();
if (signalListAdapter != null) {
signalListAdapter.notifyDataSetChanged();
}
}
break;
default:
Log.e(TAG, "unknown action: " + action);
break;
}
}
selectedSignalPosition = -1;
} else {
Log.e(TAG, "unknown requestCode: " + requestCode);
}
}
private void saveEditingPeripheral(IRPeripheral peripheral) {
if (editingPeripheralPosition == -1) {
Log.e(TAG, "invalid editingPeripheral");
return;
}
IRPeripherals peripherals = IRKit.sharedInstance().peripherals;
IRPeripheral editingPeripheral = peripherals.get(editingPeripheralPosition);
if (editingPeripheral == null) {
Log.e(TAG, "editingPeripheral is null");
return;
}
editingPeripheral.setCustomizedName(peripheral.getCustomizedName());
peripherals.save();
if (deviceListAdapter != null) {
deviceListAdapter.notifyDataSetChanged();
}
}
private void deleteEditingPeripheral() {
if (editingPeripheralPosition == -1) {
Log.e(TAG, "Invalid editingPeripheral");
return;
}
IRPeripheral peripheral = IRKit.sharedInstance().peripherals.get(editingPeripheralPosition);
if (peripheral == null) {
Log.e(TAG, "editingPeripheral is null");
return;
}
// Remove device and also signals tied to the device
IRKit irkit = IRKit.sharedInstance();
IRPeripheral removedPeripheral = irkit.peripherals.remove(editingPeripheralPosition);
irkit.peripherals.save();
irkit.signals.removeIRSignalsForDeviceId(removedPeripheral.getDeviceId());
irkit.signals.save();
if (deviceListAdapter != null) {
deviceListAdapter.notifyDataSetChanged();
}
}
/**
* Called when a new IRKit is found.
* 新しいIRKitが見つかったときに呼ばれるメソッド。
*
* @param peripheral
*/
@Override
public void onNewIRKitFound(IRPeripheral peripheral) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, R.string.new_irkit_found, Toast.LENGTH_SHORT).show();
if (deviceListAdapter != null) {
deviceListAdapter.notifyDataSetChanged();
}
}
});
}
/**
* Called when an existing IRKit is found.
* 既存のIRKitが見つかったときに呼ばれるメソッド。
*
* @param peripheral
*/
@Override
public void onExistingIRKitFound(IRPeripheral peripheral) {
}
@Override
public void onDeviceClick(int position) {
final IRPeripheral peripheral = IRKit.sharedInstance().peripherals.get(position);
// Start DeviceActivity
Bundle args = new Bundle();
args.putParcelable("peripheral", peripheral);
editingPeripheralPosition = position;
Intent intent = new Intent(this, DeviceActivity.class);
intent.putExtras(args);
startActivityForResult(intent, MainActivity.REQUEST_DEVICE_DETAIL);
}
@Override
public void onSelectSignalActionSend() {
if (selectedSignalPosition == -1) {
return;
}
final IRSignal signal = IRKit.sharedInstance().signals.get(selectedSignalPosition);
if (signal != null) {
IRKit.sharedInstance().sendSignal(signal, new IRAPIResult() {
@Override
public void onSuccess() {
}
@Override
public void onError(IRAPIError error) {
String msg = "Error sending " + signal.getName() + ": " + error.message;
Log.e(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeout() {
String msg = "Error sending " + signal.getName() + ": timeout";
Log.e(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onSelectSignalActionEdit() {
if (selectedSignalPosition == -1) {
return;
}
final IRSignal signal = IRKit.sharedInstance().signals.get(selectedSignalPosition);
if (signal != null) {
// Start SignalActivity
Bundle args = new Bundle();
args.putInt("mode", SignalActivity.MODE_EDIT);
args.putParcelable("signal", signal);
Intent intent = new Intent(this, SignalActivity.class);
intent.putExtras(args);
startActivityForResult(intent, REQUEST_SIGNAL_DETAIL);
}
}
@Override
public void onSignalClick(int position) {
selectedSignalPosition = position;
SelectSignalActionDialogFragment dialog = new SelectSignalActionDialogFragment();
dialog.show(getSupportFragmentManager(), "SelectSignalActionDialogFragment");
}
public SignalListAdapter getSignalListAdapter() {
return signalListAdapter;
}
public void setSignalListAdapter(SignalListAdapter signalListAdapter) {
this.signalListAdapter = signalListAdapter;
}
public DeviceListAdapter getDeviceListAdapter() {
return deviceListAdapter;
}
public void setDeviceListAdapter(DeviceListAdapter deviceListAdapter) {
this.deviceListAdapter = deviceListAdapter;
}
}