2015-09-07 01:49:12 -07:00
|
|
|
package org.kde.kdeconnect.UserInterface;
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2015-09-03 09:00:57 -07:00
|
|
|
import android.app.Activity;
|
2015-09-03 03:04:36 -07:00
|
|
|
import android.app.AlertDialog;
|
2015-08-20 01:00:05 -07:00
|
|
|
import android.content.Context;
|
2015-09-03 09:00:57 -07:00
|
|
|
import android.content.Intent;
|
2015-08-20 01:00:05 -07:00
|
|
|
import android.content.SharedPreferences;
|
2017-05-31 15:51:07 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2015-08-20 00:59:21 -07:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.design.widget.NavigationView;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.view.GravityCompat;
|
|
|
|
import android.support.v4.widget.DrawerLayout;
|
|
|
|
import android.support.v7.app.ActionBar;
|
2015-09-16 09:52:55 +02:00
|
|
|
import android.support.v7.app.ActionBarDrawerToggle;
|
2015-08-20 00:59:21 -07:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.text.TextUtils;
|
2015-08-20 01:00:05 -07:00
|
|
|
import android.util.Log;
|
2015-08-20 00:59:21 -07:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2018-10-17 15:09:29 +02:00
|
|
|
import android.view.SubMenu;
|
2015-09-03 03:04:36 -07:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.EditText;
|
2015-08-20 00:59:21 -07:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.kde.kdeconnect.BackgroundService;
|
|
|
|
import org.kde.kdeconnect.Device;
|
2015-09-03 03:04:36 -07:00
|
|
|
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
2015-08-20 00:59:21 -07:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2018-10-17 15:09:29 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-12-07 19:04:34 +01:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2018-03-03 17:21:16 +01:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
private static final int MENU_ENTRY_ADD_DEVICE = 1; //0 means no-selection
|
|
|
|
private static final int MENU_ENTRY_SETTINGS = 2;
|
|
|
|
private static final int MENU_ENTRY_DEVICE_FIRST_ID = 1000; //All subsequent ids are devices in the menu
|
|
|
|
private static final int MENU_ENTRY_DEVICE_UNKNOWN = 9999; //It's still a device, but we don't know which one yet
|
|
|
|
|
|
|
|
private static final String STATE_SELECTED_MENU_ENTRY = "selected_entry"; //Saved only in onSaveInstanceState
|
|
|
|
private static final String STATE_SELECTED_DEVICE = "selected_device"; //Saved persistently in preferences
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2015-09-03 09:00:57 -07:00
|
|
|
public static final int RESULT_NEEDS_RELOAD = Activity.RESULT_FIRST_USER;
|
|
|
|
|
2017-02-05 15:19:46 +01:00
|
|
|
public static final String PAIR_REQUEST_STATUS = "pair_req_status";
|
|
|
|
public static final String PAIRING_ACCEPTED = "accepted";
|
|
|
|
public static final String PAIRING_REJECTED = "rejected";
|
|
|
|
|
2018-12-07 19:04:34 +01:00
|
|
|
@BindView(R.id.navigation_drawer) NavigationView mNavigationView;
|
|
|
|
@BindView(R.id.drawer_layout) DrawerLayout mDrawerLayout;
|
|
|
|
@BindView(R.id.toolbar) Toolbar mToolbar;
|
|
|
|
TextView mNavViewDeviceName;
|
2015-08-20 00:59:21 -07:00
|
|
|
|
|
|
|
private String mCurrentDevice;
|
2018-10-17 15:09:29 +02:00
|
|
|
private int mCurrentMenuEntry;
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2015-08-20 01:00:05 -07:00
|
|
|
private SharedPreferences preferences;
|
|
|
|
|
|
|
|
private final HashMap<MenuItem, String> mMapMenuToDeviceId = new HashMap<>();
|
2015-08-20 00:59:21 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2018-12-09 12:16:28 +01:00
|
|
|
// We need to set the theme before the call to 'super.onCreate' below
|
Add a dark theme
Summary:
BUG: 375376
This revision adds dark mode support to the app ( T7044 ). It does so by injecting
theme information into each activity, and making sure that all Views
define their colors by reference to theme attributes.
In order to make this work, all of the buttons with images (like the list
of available devices) now are tinted according to the theme.
While all versions of android support the theme, only devices running
Android ICS or higher will have a toggle button in the drawer.
Test Plan: Open all the screens, both with and without the dark theme on.
Reviewers: #kde_connect, mtijink, #vdg, nicolasfella
Reviewed By: #kde_connect, mtijink, nicolasfella
Subscribers: apol, ngraham, nicolasfella, mtijink
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D11694
2018-04-23 18:31:44 +02:00
|
|
|
ThemeUtil.setUserPreferredTheme(this);
|
2018-12-09 12:16:28 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
Add a dark theme
Summary:
BUG: 375376
This revision adds dark mode support to the app ( T7044 ). It does so by injecting
theme information into each activity, and making sure that all Views
define their colors by reference to theme attributes.
In order to make this work, all of the buttons with images (like the list
of available devices) now are tinted according to the theme.
While all versions of android support the theme, only devices running
Android ICS or higher will have a toggle button in the drawer.
Test Plan: Open all the screens, both with and without the dark theme on.
Reviewers: #kde_connect, mtijink, #vdg, nicolasfella
Reviewed By: #kde_connect, mtijink, nicolasfella
Subscribers: apol, ngraham, nicolasfella, mtijink
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D11694
2018-04-23 18:31:44 +02:00
|
|
|
|
2015-09-07 01:49:12 -07:00
|
|
|
setContentView(R.layout.activity_main);
|
2018-12-07 19:04:34 +01:00
|
|
|
|
|
|
|
ButterKnife.bind(this);
|
|
|
|
|
2016-06-16 23:45:59 +02:00
|
|
|
View mDrawerHeader = mNavigationView.getHeaderView(0);
|
2018-12-07 19:04:34 +01:00
|
|
|
mNavViewDeviceName = mDrawerHeader.findViewById(R.id.device_name);
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-12-07 19:04:34 +01:00
|
|
|
setSupportActionBar(mToolbar);
|
2015-08-20 00:59:21 -07:00
|
|
|
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
|
2016-06-16 23:45:59 +02:00
|
|
|
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
|
2015-09-16 09:52:55 +02:00
|
|
|
mDrawerLayout, /* DrawerLayout object */
|
|
|
|
R.string.open, /* "open drawer" description */
|
|
|
|
R.string.close /* "close drawer" description */
|
|
|
|
);
|
|
|
|
|
2018-03-03 16:28:16 +01:00
|
|
|
mDrawerLayout.addDrawerListener(mDrawerToggle);
|
2015-08-20 00:59:21 -07:00
|
|
|
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
|
2018-12-09 12:37:01 +01:00
|
|
|
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
}
|
2015-09-16 09:52:55 +02:00
|
|
|
|
|
|
|
mDrawerToggle.setDrawerIndicatorEnabled(true);
|
|
|
|
mDrawerToggle.syncState();
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2015-09-03 03:04:36 -07:00
|
|
|
String deviceName = DeviceHelper.getDeviceName(this);
|
2018-12-09 12:37:01 +01:00
|
|
|
mNavViewDeviceName.setText(deviceName);
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
preferences = getSharedPreferences("stored_menu_selection", Context.MODE_PRIVATE);
|
Add a dark theme
Summary:
BUG: 375376
This revision adds dark mode support to the app ( T7044 ). It does so by injecting
theme information into each activity, and making sure that all Views
define their colors by reference to theme attributes.
In order to make this work, all of the buttons with images (like the list
of available devices) now are tinted according to the theme.
While all versions of android support the theme, only devices running
Android ICS or higher will have a toggle button in the drawer.
Test Plan: Open all the screens, both with and without the dark theme on.
Reviewers: #kde_connect, mtijink, #vdg, nicolasfella
Reviewed By: #kde_connect, mtijink, nicolasfella
Subscribers: apol, ngraham, nicolasfella, mtijink
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D11694
2018-04-23 18:31:44 +02:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
mNavigationView.setNavigationItemSelectedListener(menuItem -> {
|
2018-10-17 15:09:29 +02:00
|
|
|
mCurrentMenuEntry = menuItem.getItemId();
|
|
|
|
switch (mCurrentMenuEntry) {
|
|
|
|
case MENU_ENTRY_ADD_DEVICE:
|
|
|
|
mCurrentDevice = null;
|
|
|
|
preferences.edit().putString(STATE_SELECTED_DEVICE, null).apply();
|
|
|
|
setContentFragment(new PairingFragment());
|
|
|
|
break;
|
|
|
|
case MENU_ENTRY_SETTINGS:
|
|
|
|
mCurrentDevice = null;
|
|
|
|
preferences.edit().putString(STATE_SELECTED_DEVICE, null).apply();
|
|
|
|
setContentFragment(new SettingsFragment());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
String deviceId = mMapMenuToDeviceId.get(menuItem);
|
|
|
|
onDeviceSelected(deviceId);
|
|
|
|
break;
|
|
|
|
}
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
mDrawerLayout.closeDrawer(mNavigationView);
|
|
|
|
return true;
|
2015-08-20 00:59:21 -07:00
|
|
|
});
|
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
// Decide which menu entry should be selected at start
|
2015-08-20 01:00:05 -07:00
|
|
|
String savedDevice;
|
2018-10-17 15:09:29 +02:00
|
|
|
int savedMenuEntry;
|
2017-01-23 09:07:18 +01:00
|
|
|
if (getIntent().hasExtra("forceOverview")) {
|
2018-03-03 17:21:16 +01:00
|
|
|
Log.i("MainActivity", "Requested to start main overview");
|
2017-01-23 09:07:18 +01:00
|
|
|
savedDevice = null;
|
2018-10-17 15:09:29 +02:00
|
|
|
savedMenuEntry = MENU_ENTRY_ADD_DEVICE;
|
2017-01-23 09:07:18 +01:00
|
|
|
} else if (getIntent().hasExtra("deviceId")) {
|
2018-03-03 17:21:16 +01:00
|
|
|
Log.i("MainActivity", "Loading selected device from parameter");
|
2015-09-07 00:11:46 -07:00
|
|
|
savedDevice = getIntent().getStringExtra("deviceId");
|
2018-10-17 15:09:29 +02:00
|
|
|
savedMenuEntry = MENU_ENTRY_DEVICE_UNKNOWN;
|
|
|
|
// If pairStatus is not empty, then the user has accepted/reject the pairing from the notification
|
|
|
|
String pairStatus = getIntent().getStringExtra(PAIR_REQUEST_STATUS);
|
|
|
|
if (pairStatus != null) {
|
|
|
|
Log.i("MainActivity", "pair status is " + pairStatus);
|
2018-10-29 20:06:52 +01:00
|
|
|
savedDevice = onPairResultFromNotification(savedDevice, pairStatus);
|
|
|
|
if (savedDevice == null) {
|
|
|
|
savedMenuEntry = MENU_ENTRY_ADD_DEVICE;
|
|
|
|
}
|
2017-02-05 15:19:46 +01:00
|
|
|
}
|
2015-09-07 00:11:46 -07:00
|
|
|
} else if (savedInstanceState != null) {
|
2018-03-03 17:21:16 +01:00
|
|
|
Log.i("MainActivity", "Loading selected device from saved activity state");
|
2015-08-20 01:00:05 -07:00
|
|
|
savedDevice = savedInstanceState.getString(STATE_SELECTED_DEVICE);
|
2018-10-17 15:09:29 +02:00
|
|
|
savedMenuEntry = savedInstanceState.getInt(STATE_SELECTED_MENU_ENTRY, MENU_ENTRY_ADD_DEVICE);
|
2015-08-20 01:00:05 -07:00
|
|
|
} else {
|
2018-03-03 17:21:16 +01:00
|
|
|
Log.i("MainActivity", "Loading selected device from persistent storage");
|
2015-08-20 01:00:05 -07:00
|
|
|
savedDevice = preferences.getString(STATE_SELECTED_DEVICE, null);
|
2018-10-17 15:09:29 +02:00
|
|
|
savedMenuEntry = (savedDevice != null)? MENU_ENTRY_DEVICE_UNKNOWN : MENU_ENTRY_ADD_DEVICE;
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
2017-02-05 15:19:46 +01:00
|
|
|
|
2018-12-07 18:13:28 +01:00
|
|
|
mCurrentMenuEntry = savedMenuEntry;
|
|
|
|
mCurrentDevice = savedDevice;
|
|
|
|
mNavigationView.setCheckedItem(savedMenuEntry);
|
2018-10-17 15:09:29 +02:00
|
|
|
|
2018-12-07 18:13:28 +01:00
|
|
|
//FragmentManager will restore whatever fragment was there
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-17 15:09:29 +02:00
|
|
|
|
2018-12-07 18:13:28 +01:00
|
|
|
// Activate the chosen fragment and select the entry in the menu
|
2018-10-17 15:09:29 +02:00
|
|
|
if (savedMenuEntry >= MENU_ENTRY_DEVICE_FIRST_ID && savedDevice != null) {
|
|
|
|
onDeviceSelected(savedDevice);
|
|
|
|
} else {
|
|
|
|
if (mCurrentMenuEntry == MENU_ENTRY_SETTINGS) {
|
|
|
|
setContentFragment(new SettingsFragment());
|
|
|
|
} else {
|
|
|
|
setContentFragment(new PairingFragment());
|
Add a dark theme
Summary:
BUG: 375376
This revision adds dark mode support to the app ( T7044 ). It does so by injecting
theme information into each activity, and making sure that all Views
define their colors by reference to theme attributes.
In order to make this work, all of the buttons with images (like the list
of available devices) now are tinted according to the theme.
While all versions of android support the theme, only devices running
Android ICS or higher will have a toggle button in the drawer.
Test Plan: Open all the screens, both with and without the dark theme on.
Reviewers: #kde_connect, mtijink, #vdg, nicolasfella
Reviewed By: #kde_connect, mtijink, nicolasfella
Subscribers: apol, ngraham, nicolasfella, mtijink
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D11694
2018-04-23 18:31:44 +02:00
|
|
|
}
|
2018-10-17 15:09:29 +02:00
|
|
|
}
|
Add a dark theme
Summary:
BUG: 375376
This revision adds dark mode support to the app ( T7044 ). It does so by injecting
theme information into each activity, and making sure that all Views
define their colors by reference to theme attributes.
In order to make this work, all of the buttons with images (like the list
of available devices) now are tinted according to the theme.
While all versions of android support the theme, only devices running
Android ICS or higher will have a toggle button in the drawer.
Test Plan: Open all the screens, both with and without the dark theme on.
Reviewers: #kde_connect, mtijink, #vdg, nicolasfella
Reviewed By: #kde_connect, mtijink, nicolasfella
Subscribers: apol, ngraham, nicolasfella, mtijink
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D11694
2018-04-23 18:31:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 20:06:52 +01:00
|
|
|
private String onPairResultFromNotification(String deviceId, String pairStatus) {
|
2018-10-17 15:09:29 +02:00
|
|
|
assert(deviceId != null);
|
|
|
|
|
2018-10-29 20:06:52 +01:00
|
|
|
BackgroundService.RunCommand(this, service -> {
|
|
|
|
Device device = service.getDevice(deviceId);
|
|
|
|
if (device == null) {
|
|
|
|
Log.w("rejectPairing", "Device no longer exists: " + deviceId);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-05 15:19:46 +01:00
|
|
|
|
2018-10-29 20:06:52 +01:00
|
|
|
if (pairStatus.equals(PAIRING_ACCEPTED)) {
|
|
|
|
device.acceptPairing();
|
|
|
|
} else {
|
|
|
|
device.rejectPairing();
|
|
|
|
}
|
|
|
|
});
|
2017-02-05 15:19:46 +01:00
|
|
|
|
|
|
|
if (pairStatus.equals(PAIRING_ACCEPTED)) {
|
2018-10-29 20:06:52 +01:00
|
|
|
return deviceId;
|
2017-02-05 15:19:46 +01:00
|
|
|
} else {
|
2018-10-29 20:06:52 +01:00
|
|
|
return null;
|
2018-10-17 15:09:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int deviceIdToMenuEntryId(String deviceId) {
|
|
|
|
for (HashMap.Entry<MenuItem, String> entry : mMapMenuToDeviceId.entrySet()) {
|
|
|
|
if (TextUtils.equals(entry.getValue(), deviceId)) { //null-safe
|
|
|
|
return entry.getKey().getItemId();
|
|
|
|
}
|
2017-02-05 15:19:46 +01:00
|
|
|
}
|
2018-10-17 15:09:29 +02:00
|
|
|
return MENU_ENTRY_DEVICE_UNKNOWN;
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
if (mDrawerLayout.isDrawerOpen(mNavigationView)) {
|
|
|
|
mDrawerLayout.closeDrawer(mNavigationView);
|
|
|
|
} else {
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
mDrawerLayout.openDrawer(mNavigationView);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
private void updateDeviceList() {
|
2018-05-09 14:02:56 +02:00
|
|
|
BackgroundService.RunCommand(MainActivity.this, service -> {
|
|
|
|
|
|
|
|
Menu menu = mNavigationView.getMenu();
|
|
|
|
menu.clear();
|
|
|
|
mMapMenuToDeviceId.clear();
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
SubMenu devicesMenu = menu.addSubMenu(R.string.devices);
|
|
|
|
|
|
|
|
int id = MENU_ENTRY_DEVICE_FIRST_ID;
|
2018-05-09 14:02:56 +02:00
|
|
|
Collection<Device> devices = service.getDevices().values();
|
|
|
|
for (Device device : devices) {
|
|
|
|
if (device.isReachable() && device.isPaired()) {
|
2018-10-17 15:09:29 +02:00
|
|
|
MenuItem item = devicesMenu.add(Menu.FIRST, id++, 1, device.getName());
|
2018-05-09 14:02:56 +02:00
|
|
|
item.setIcon(device.getIcon());
|
|
|
|
item.setCheckable(true);
|
|
|
|
mMapMenuToDeviceId.put(item, device.getDeviceId());
|
|
|
|
}
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
MenuItem addDeviceItem = devicesMenu.add(Menu.FIRST, MENU_ENTRY_ADD_DEVICE, 1000, R.string.pair_new_device);
|
|
|
|
addDeviceItem.setIcon(R.drawable.ic_action_content_add_circle_outline);
|
|
|
|
addDeviceItem.setCheckable(true);
|
|
|
|
|
|
|
|
MenuItem settingsItem = menu.add(Menu.FIRST, MENU_ENTRY_SETTINGS, 1000, R.string.settings);
|
|
|
|
settingsItem.setIcon(R.drawable.ic_action_settings);
|
|
|
|
settingsItem.setCheckable(true);
|
|
|
|
|
|
|
|
//Ids might have changed
|
|
|
|
if (mCurrentMenuEntry >= MENU_ENTRY_DEVICE_FIRST_ID) {
|
|
|
|
mCurrentMenuEntry = deviceIdToMenuEntryId(mCurrentDevice);
|
|
|
|
}
|
|
|
|
mNavigationView.setCheckedItem(mCurrentMenuEntry);
|
2015-08-20 00:59:21 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
|
2015-08-20 00:59:21 -07:00
|
|
|
@Override
|
|
|
|
protected void onStart() {
|
|
|
|
super.onStart();
|
2015-09-10 02:35:33 -07:00
|
|
|
BackgroundService.addGuiInUseCounter(this, true);
|
2018-10-17 15:09:29 +02:00
|
|
|
BackgroundService.RunCommand(this, service -> service.addDeviceListChangedCallback("MainActivity", this::updateDeviceList));
|
|
|
|
updateDeviceList();
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
2015-09-09 12:34:42 -07:00
|
|
|
BackgroundService.removeGuiInUseCounter(this);
|
2018-05-09 14:02:56 +02:00
|
|
|
BackgroundService.RunCommand(this, service -> service.removeDeviceListChangedCallback("MainActivity"));
|
2015-08-20 00:59:21 -07:00
|
|
|
super.onStop();
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
private static void uncheckAllMenuItems(Menu menu) {
|
|
|
|
int size = menu.size();
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
MenuItem item = menu.getItem(i);
|
|
|
|
if(item.hasSubMenu()) {
|
|
|
|
uncheckAllMenuItems(item.getSubMenu());
|
|
|
|
} else {
|
|
|
|
item.setChecked(false);
|
|
|
|
}
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
2018-10-17 15:09:29 +02:00
|
|
|
}
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
public void onDeviceSelected(String deviceId, boolean fromDeviceList) {
|
|
|
|
mCurrentDevice = deviceId;
|
|
|
|
preferences.edit().putString(STATE_SELECTED_DEVICE, deviceId).apply();
|
|
|
|
|
|
|
|
if (mCurrentDevice != null) {
|
|
|
|
mCurrentMenuEntry = deviceIdToMenuEntryId(deviceId);
|
|
|
|
if (mCurrentMenuEntry == MENU_ENTRY_DEVICE_UNKNOWN) {
|
|
|
|
uncheckAllMenuItems(mNavigationView.getMenu());
|
|
|
|
} else {
|
|
|
|
mNavigationView.setCheckedItem(mCurrentMenuEntry);
|
|
|
|
}
|
2018-12-06 19:09:47 +01:00
|
|
|
setContentFragment(DeviceFragment.newInstance(deviceId, fromDeviceList));
|
2015-08-20 00:59:21 -07:00
|
|
|
} else {
|
2018-10-17 15:09:29 +02:00
|
|
|
mCurrentMenuEntry = MENU_ENTRY_ADD_DEVICE;
|
|
|
|
mNavigationView.setCheckedItem(mCurrentMenuEntry);
|
|
|
|
setContentFragment(new PairingFragment());
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
2018-10-17 15:09:29 +02:00
|
|
|
}
|
2015-08-20 00:59:21 -07:00
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
private void setContentFragment(Fragment fragment) {
|
2015-08-20 00:59:21 -07:00
|
|
|
getSupportFragmentManager()
|
|
|
|
.beginTransaction()
|
|
|
|
.replace(R.id.container, fragment)
|
|
|
|
.commit();
|
2015-09-07 00:11:46 -07:00
|
|
|
}
|
2015-08-20 01:00:05 -07:00
|
|
|
|
2015-09-07 00:11:46 -07:00
|
|
|
public void onDeviceSelected(String deviceId) {
|
|
|
|
onDeviceSelected(deviceId, false);
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putString(STATE_SELECTED_DEVICE, mCurrentDevice);
|
2018-10-17 15:09:29 +02:00
|
|
|
outState.putInt(STATE_SELECTED_MENU_ENTRY, mCurrentMenuEntry);
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
|
|
|
|
2015-09-03 09:00:57 -07:00
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2015-09-16 09:52:55 +02:00
|
|
|
switch (requestCode) {
|
2015-09-03 09:00:57 -07:00
|
|
|
case RESULT_NEEDS_RELOAD:
|
2018-05-09 14:02:56 +02:00
|
|
|
BackgroundService.RunCommand(this, service -> {
|
|
|
|
Device device = service.getDevice(mCurrentDevice);
|
|
|
|
device.reloadPluginsFromSettings();
|
2015-09-03 09:00:57 -07:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
2015-09-07 00:07:39 -07:00
|
|
|
}
|
2015-09-03 09:00:57 -07:00
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
for (int result : grantResults) {
|
|
|
|
if (result == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
//New permission granted, reload plugins
|
2018-05-09 14:02:56 +02:00
|
|
|
BackgroundService.RunCommand(this, service -> {
|
|
|
|
Device device = service.getDevice(mCurrentDevice);
|
|
|
|
device.reloadPluginsFromSettings();
|
2017-05-31 15:51:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:09:29 +02:00
|
|
|
|
|
|
|
interface NameChangeCallback {
|
|
|
|
void onNameChanged(String newName);
|
|
|
|
}
|
|
|
|
|
2018-10-27 00:01:30 +02:00
|
|
|
private final Set<NameChangeCallback> nameChangeSubscribers = new HashSet<>();
|
2018-10-17 15:09:29 +02:00
|
|
|
|
|
|
|
public void addNameChangeCallback(NameChangeCallback cb) {
|
|
|
|
nameChangeSubscribers.add(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeNameChangeCallback(NameChangeCallback cb) {
|
|
|
|
nameChangeSubscribers.remove(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void openRenameDeviceDialog() {
|
|
|
|
final EditText deviceNameEdit = new EditText(this);
|
|
|
|
String deviceName = DeviceHelper.getDeviceName(this);
|
2015-09-07 00:07:39 -07:00
|
|
|
deviceNameEdit.setText(deviceName);
|
2018-10-17 15:09:29 +02:00
|
|
|
float dpi = this.getResources().getDisplayMetrics().density;
|
|
|
|
deviceNameEdit.setPadding( ((int) (18 * dpi)), ((int) (16 * dpi)), ((int) (18 * dpi)), ((int) (12 * dpi)) );
|
|
|
|
new AlertDialog.Builder(this)
|
2015-09-07 00:07:39 -07:00
|
|
|
.setView(deviceNameEdit)
|
2018-05-09 14:02:56 +02:00
|
|
|
.setPositiveButton(R.string.device_rename_confirm, (dialog, which) -> {
|
2018-10-17 15:09:29 +02:00
|
|
|
String newDeviceName = deviceNameEdit.getText().toString();
|
|
|
|
DeviceHelper.setDeviceName(this, newDeviceName);
|
|
|
|
this.updateDeviceNameFromMenu(newDeviceName);
|
|
|
|
BackgroundService.RunCommand(this, BackgroundService::onNetworkChange);
|
|
|
|
for (NameChangeCallback callback : nameChangeSubscribers) {
|
|
|
|
callback.onNameChanged(newDeviceName);
|
|
|
|
}
|
2015-09-07 00:07:39 -07:00
|
|
|
})
|
2018-10-17 15:09:29 +02:00
|
|
|
.setNegativeButton(R.string.cancel, (dialog, which) -> { })
|
2015-09-07 00:07:39 -07:00
|
|
|
.setTitle(R.string.device_rename_title)
|
|
|
|
.show();
|
2015-09-03 09:00:57 -07:00
|
|
|
}
|
2018-10-17 15:09:29 +02:00
|
|
|
|
2018-10-26 23:53:58 +02:00
|
|
|
private void updateDeviceNameFromMenu(String newDeviceName) {
|
2018-12-07 19:04:34 +01:00
|
|
|
mNavViewDeviceName.setText(newDeviceName);
|
2018-10-17 15:09:29 +02:00
|
|
|
}
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|