-
Notifications
You must be signed in to change notification settings - Fork 22
/
MainActivity.java
499 lines (453 loc) · 16.4 KB
/
MainActivity.java
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Copyright (C) 2018. Mikhail Kulesh
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. You should have received a copy of the GNU General
* Public License along with this program.
*/
package com.mkulesh.onpc;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import com.mkulesh.onpc.config.AppLocale;
import com.mkulesh.onpc.config.Configuration;
import com.mkulesh.onpc.config.PreferencesMain;
import com.mkulesh.onpc.iscp.ConnectionState;
import com.mkulesh.onpc.iscp.State;
import com.mkulesh.onpc.iscp.StateHolder;
import com.mkulesh.onpc.iscp.StateManager;
import com.mkulesh.onpc.iscp.messages.AmpOperationCommandMsg;
import com.mkulesh.onpc.iscp.messages.MasterVolumeMsg;
import com.mkulesh.onpc.iscp.messages.PowerStatusMsg;
import com.mkulesh.onpc.iscp.messages.ReceiverInformationMsg;
import com.mkulesh.onpc.utils.HtmlDialogBuilder;
import com.mkulesh.onpc.utils.Logging;
import com.mkulesh.onpc.utils.Utils;
import java.util.HashSet;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements OnPageChangeListener, StateManager.StateListener
{
private static final int SETTINGS_ACTIVITY_REQID = 256;
private Configuration configuration;
private Toolbar toolbar;
private MainPagerAdapter pagerAdapter;
private ViewPager viewPager;
private Menu mainMenu;
private ConnectionState connectionState;
private final StateHolder stateHolder = new StateHolder();
private Toast exitToast = null;
private MainNavigationDrawer navigationDrawer;
private ActionBarDrawerToggle mDrawerToggle;
private boolean autoPower = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
configuration = new Configuration(this);
setTheme(configuration.getTheme(Configuration.ThemeType.MAIN_THEME));
Logging.saveLogging = Logging.isEnabled() && configuration.isDeveloperMode();
// Note that due to onActivityResult, the activity will be started twice
// after the preference activity is closed
String versionName = null;
try
{
final PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
versionName = "v. " + pi.versionName;
Logging.info(this, "Starting application: " + versionName);
}
catch (PackageManager.NameNotFoundException e)
{
Logging.info(this, "Starting application");
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (configuration.isKeepScreenOn())
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_short_name);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(R.string.app_short_name);
actionBar.setElevation(5.0f);
}
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
pagerAdapter = new MainPagerAdapter(this, getSupportFragmentManager(), configuration);
// Set up the ViewPager with the sections adapter.
viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(this);
final TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
connectionState = new ConnectionState(this);
// Initially reset zone state
configuration.initActiveZone(ReceiverInformationMsg.DEFAULT_ACTIVE_ZONE);
// Navigation drawer
navigationDrawer = new MainNavigationDrawer(this, versionName);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, navigationDrawer.getDrawerLayout(), toolbar,
R.string.drawer_open, R.string.drawer_open);
Utils.setDrawerListener(navigationDrawer.getDrawerLayout(), mDrawerToggle);
autoPower = savedInstanceState == null && configuration.isAutoPower();
updateToolbar(null);
}
@Override
protected void attachBaseContext(Context newBase)
{
final Locale prefLocale = AppLocale.ContextWrapper.getPreferredLocale(newBase);
Logging.info(this, "Application locale: " + prefLocale.toString());
super.attachBaseContext(AppLocale.ContextWrapper.wrap(newBase, prefLocale));
}
public Configuration getConfiguration()
{
return configuration;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
mainMenu = menu;
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
for (int i = 0; i < mainMenu.size(); i++)
{
final MenuItem m = mainMenu.getItem(i);
Utils.updateMenuIconColor(this, m);
if (m.getItemId() == R.id.menu_receiver_information)
{
m.setVisible(configuration.isDeveloperMode());
}
if (m.getItemId() == R.id.menu_latest_logging)
{
m.setVisible(Logging.saveLogging);
}
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(menuItem))
{
return true;
}
Logging.info(this, "Selected main menu: " + menuItem.getTitle());
switch (menuItem.getItemId())
{
case R.id.menu_power_standby:
if (isConnected())
{
final PowerStatusMsg.PowerStatus p = getStateManager().getState().isOn() ?
PowerStatusMsg.PowerStatus.STB : PowerStatusMsg.PowerStatus.ON;
getStateManager().sendMessage(
new PowerStatusMsg(getStateManager().getState().getActiveZone(), p));
}
return true;
case R.id.menu_app_settings:
{
Intent settings = new Intent(this, PreferencesMain.class);
startActivityForResult(settings, SETTINGS_ACTIVITY_REQID);
return true;
}
case R.id.menu_receiver_information:
{
final String text = isConnected() ? getStateManager().getState().receiverInformation :
getResources().getString(R.string.state_not_connected);
HtmlDialogBuilder.buildXmlDialog(this,
R.mipmap.ic_launcher, R.string.menu_receiver_information, text).show();
return true;
}
case R.id.menu_latest_logging:
{
HtmlDialogBuilder.buildXmlDialog(this,
R.mipmap.ic_launcher, R.string.menu_latest_logging, Logging.getLatestLogging()).show();
return true;
}
default:
return super.onOptionsItemSelected(menuItem);
}
}
@Override
public void onBackPressed()
{
if (!configuration.isExitConfirm())
{
finish();
}
else if (exitToast != null && exitToast.getView().isShown())
{
exitToast.cancel();
finish();
}
else
{
exitToast = Toast.makeText(this, R.string.action_exit_confirm, Toast.LENGTH_LONG);
exitToast.show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SETTINGS_ACTIVITY_REQID)
{
restartActivity();
}
}
public void restartActivity()
{
Intent intent = getIntent();
finish();
startActivity(intent);
}
public boolean connectToDevice(final String device, final int port)
{
stateHolder.release(false);
stateHolder.waitForRelease();
onStateChanged(stateHolder.getState(), null);
final int zone = configuration.getZone();
try
{
stateHolder.setStateManager(new StateManager(connectionState, this, device, port, zone));
// By default, add all possible device selectors
{
final State s = stateHolder.getState();
s.createDefaultSelectors(this);
configuration.setReceiverInformation(s);
}
if (autoPower)
{
// Auto power-on once at application startup
stateHolder.getStateManager().setAutoPower(true);
autoPower = false;
}
return true;
}
catch (Exception ex)
{
if (Configuration.ENABLE_MOCKUP)
{
stateHolder.setStateManager(new StateManager(connectionState, this, zone));
updateConfiguration(stateHolder.getState());
return true;
}
}
return false;
}
public boolean isConnected()
{
return stateHolder.getStateManager() != null;
}
public StateManager getStateManager()
{
return stateHolder.getStateManager();
}
public ConnectionState getConnectionState()
{
return connectionState;
}
@Override
protected void onResume()
{
super.onResume();
connectionState.setActive(true);
if (!configuration.getDeviceName().isEmpty() && configuration.getDevicePort() > 0)
{
Logging.info(this, "use stored connection data: "
+ configuration.getDeviceName() + "/" + configuration.getDevicePort());
connectToDevice(configuration.getDeviceName(), configuration.getDevicePort());
}
else
{
navigationDrawer.navigationSearchDevice();
}
}
@Override
protected void onPause()
{
super.onPause();
connectionState.setActive(false);
stateHolder.release(true);
}
@Override
public void onStateChanged(State state, @Nullable final HashSet<State.ChangeType> eventChanges)
{
if (state != null && eventChanges != null && eventChanges.contains(State.ChangeType.RECEIVER_INFO))
{
updateConfiguration(state);
}
final BaseFragment f = (BaseFragment) (pagerAdapter.getRegisteredFragment(viewPager.getCurrentItem()));
if (f != null)
{
f.update(state, eventChanges);
}
if (eventChanges == null || eventChanges.contains(State.ChangeType.COMMON))
{
updateToolbar(state);
}
}
private void updateConfiguration(@NonNull State state)
{
configuration.setReceiverInformation(state);
navigationDrawer.updateNavigationContent(state);
updateToolbar(state);
}
@Override
public void onManagerStopped()
{
stateHolder.setStateManager(null);
}
@Override
public void onDeviceDisconnected()
{
Logging.info(this, "device disconnected");
if (!stateHolder.isAppExit())
{
onStateChanged(stateHolder.getState(), null);
}
}
private void updateToolbar(State state)
{
// Logo
if (state == null)
{
toolbar.setSubtitle(R.string.state_not_connected);
}
else
{
final StringBuilder subTitle = new StringBuilder();
subTitle.append(state.getDeviceName(configuration.isFriendlyNames()));
if (state.isExtendedZone())
{
if (!subTitle.toString().isEmpty())
{
subTitle.append("/");
}
subTitle.append(state.zones.get(state.getActiveZone()).getName());
}
if (!state.isOn())
{
subTitle.append(" (").append(getResources().getString(R.string.state_standby)).append(")");
}
toolbar.setSubtitle(subTitle.toString());
}
// Main menu
if (mainMenu != null)
{
for (int i = 0; i < mainMenu.size(); i++)
{
final MenuItem m = mainMenu.getItem(i);
if (m.getItemId() == R.id.menu_power_standby)
{
m.setEnabled(state != null);
Utils.updateMenuIconColor(this, m);
if (m.isEnabled() && state != null)
{
Utils.setDrawableColorAttr(this, m.getIcon(),
state.isOn() ? android.R.attr.textColorTertiary : R.attr.colorAccent);
}
}
}
}
}
@Override
public void onPageScrollStateChanged(int arg0)
{
// empty
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// empty
}
@Override
public void onPageSelected(int p)
{
onStateChanged(stateHolder.getState(), null);
}
void selectRightTab()
{
viewPager.arrowScroll(View.FOCUS_RIGHT);
}
/**
* Navigation drawer: When using the ActionBarDrawerToggle,
* you must call it during onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (isConnected() && configuration.isVolumeKeys())
{
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
Logging.info(this, "Key event: " + event);
changeMasterVolume(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP);
return true;
}
}
}
return super.dispatchKeyEvent(event);
}
private void changeMasterVolume(boolean isUp)
{
switch (configuration.getSoundControl())
{
case "none":
// nothing to do
break;
case "external-amplifier":
getStateManager().sendMessage(new AmpOperationCommandMsg(isUp ?
AmpOperationCommandMsg.Command.MVLUP.getCode() :
AmpOperationCommandMsg.Command.MVLDOWN.getCode()));
break;
case "device":
final int zone = getStateManager().getState().getActiveZone();
getStateManager().sendMessage(new MasterVolumeMsg(zone, isUp ?
MasterVolumeMsg.Command.UP : MasterVolumeMsg.Command.DOWN));
break;
}
}
}