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 03:04:36 -07:00
|
|
|
import android.content.DialogInterface;
|
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;
|
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
|
|
|
import android.os.Build;
|
2015-08-20 00:59:21 -07:00
|
|
|
import android.os.Bundle;
|
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
|
|
|
import android.preference.PreferenceManager;
|
2015-08-20 00:59:21 -07:00
|
|
|
import android.support.annotation.NonNull;
|
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
|
|
|
import android.support.annotation.RequiresApi;
|
2015-08-20 00:59:21 -07:00
|
|
|
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;
|
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
|
|
|
import android.support.v7.widget.SwitchCompat;
|
2015-08-20 00:59:21 -07:00
|
|
|
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;
|
2015-09-03 03:04:36 -07:00
|
|
|
import android.view.View;
|
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
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CompoundButton;
|
2015-09-03 03:04:36 -07:00
|
|
|
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-03-03 17:21:16 +01:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
2015-08-20 00:59:21 -07:00
|
|
|
|
|
|
|
private static final String STATE_SELECTED_DEVICE = "selected_device";
|
|
|
|
|
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";
|
|
|
|
|
2015-08-20 00:59:21 -07:00
|
|
|
private NavigationView mNavigationView;
|
|
|
|
private DrawerLayout mDrawerLayout;
|
|
|
|
|
|
|
|
private String mCurrentDevice;
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
|
|
|
// We need to set this theme before the call to 'setContentView' below
|
|
|
|
ThemeUtil.setUserPreferredTheme(this);
|
|
|
|
|
2015-09-07 01:49:12 -07:00
|
|
|
setContentView(R.layout.activity_main);
|
2015-08-20 00:59:21 -07:00
|
|
|
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
|
|
|
|
mNavigationView = (NavigationView) findViewById(R.id.navigation_drawer);
|
2016-06-16 23:45:59 +02:00
|
|
|
View mDrawerHeader = mNavigationView.getHeaderView(0);
|
2015-08-20 00:59:21 -07:00
|
|
|
|
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
|
|
|
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);
|
2015-09-16 09:52:55 +02:00
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
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);
|
2016-01-10 08:22:56 -08:00
|
|
|
TextView nameView = (TextView) mDrawerHeader.findViewById(R.id.device_name);
|
2015-08-20 00:59:21 -07:00
|
|
|
nameView.setText(deviceName);
|
|
|
|
|
2015-11-12 08:14:12 -08:00
|
|
|
View.OnClickListener renameListener = new View.OnClickListener() {
|
2015-09-03 03:04:36 -07:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2015-09-07 00:07:39 -07:00
|
|
|
renameDevice();
|
2015-09-03 03:04:36 -07:00
|
|
|
}
|
2015-11-12 08:14:12 -08:00
|
|
|
};
|
2016-01-10 08:22:56 -08:00
|
|
|
mDrawerHeader.findViewById(R.id.kdeconnect_label).setOnClickListener(renameListener);
|
|
|
|
mDrawerHeader.findViewById(R.id.device_name).setOnClickListener(renameListener);
|
2015-09-03 03:04:36 -07: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
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
|
|
|
addDarkModeSwitch((ViewGroup) mDrawerHeader);
|
|
|
|
}
|
|
|
|
|
2015-08-20 00:59:21 -07:00
|
|
|
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onNavigationItemSelected(MenuItem menuItem) {
|
|
|
|
|
|
|
|
String deviceId = mMapMenuToDeviceId.get(menuItem);
|
|
|
|
onDeviceSelected(deviceId);
|
|
|
|
|
|
|
|
mDrawerLayout.closeDrawer(mNavigationView);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-20 01:00:05 -07:00
|
|
|
preferences = getSharedPreferences(STATE_SELECTED_DEVICE, Context.MODE_PRIVATE);
|
|
|
|
|
|
|
|
String savedDevice;
|
2017-02-05 15:19:46 +01:00
|
|
|
String pairStatus = "";
|
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;
|
|
|
|
} 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-03-03 16:06:52 +01:00
|
|
|
if (getIntent().hasExtra(PAIR_REQUEST_STATUS)) {
|
2017-02-05 15:19:46 +01:00
|
|
|
pairStatus = getIntent().getStringExtra(PAIR_REQUEST_STATUS);
|
|
|
|
}
|
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);
|
|
|
|
} 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);
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
2017-02-05 15:19:46 +01:00
|
|
|
//if pairStatus is not empty, then the decision has been made...
|
|
|
|
if (!pairStatus.equals("")) {
|
2018-03-03 17:21:16 +01:00
|
|
|
Log.i("MainActivity", "pair status is " + pairStatus);
|
2017-02-05 15:19:46 +01:00
|
|
|
onNewDeviceSelected(savedDevice, pairStatus);
|
|
|
|
}
|
2015-08-20 01:00:05 -07:00
|
|
|
onDeviceSelected(savedDevice);
|
2017-02-05 15:19:46 +01: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
|
|
|
/**
|
|
|
|
* Adds a {@link SwitchCompat} to the bottom of the navigation header for
|
|
|
|
* toggling dark mode on and off. Call from {@link #onCreate(Bundle)}.
|
|
|
|
* <p>
|
|
|
|
* Only supports android ICS and higher because {@link SwitchCompat}
|
|
|
|
* requires that.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param drawerHeader the layout which should contain the switch
|
|
|
|
*/
|
|
|
|
@RequiresApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
|
|
|
|
private void addDarkModeSwitch(ViewGroup drawerHeader) {
|
|
|
|
getLayoutInflater().inflate(R.layout.nav_dark_mode_switch, drawerHeader);
|
|
|
|
|
|
|
|
SwitchCompat darkThemeSwitch = (SwitchCompat) drawerHeader.findViewById(R.id.dark_theme);
|
|
|
|
darkThemeSwitch.setChecked(ThemeUtil.shouldUseDarkTheme(this));
|
|
|
|
darkThemeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@RequiresApi(Build.VERSION_CODES.HONEYCOMB)
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton darkThemeSwitch, boolean isChecked) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
|
|
|
|
boolean isDarkAlready = prefs.getBoolean("darkTheme", false);
|
|
|
|
if (isDarkAlready != isChecked) {
|
|
|
|
prefs.edit().putBoolean("darkTheme", isChecked).apply();
|
|
|
|
MainActivity.this.recreate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-05 15:19:46 +01:00
|
|
|
//like onNewDeviceSelected but assumes that the new device is simply requesting to be paired
|
|
|
|
//and can't be null
|
2018-03-03 16:06:52 +01:00
|
|
|
private void onNewDeviceSelected(String deviceId, String pairStatus) {
|
2017-02-05 15:19:46 +01:00
|
|
|
mCurrentDevice = deviceId;
|
2015-08-20 01:00:05 -07:00
|
|
|
|
2017-02-05 15:19:46 +01:00
|
|
|
preferences.edit().putString(STATE_SELECTED_DEVICE, mCurrentDevice).apply();
|
|
|
|
|
|
|
|
for (HashMap.Entry<MenuItem, String> entry : mMapMenuToDeviceId.entrySet()) {
|
|
|
|
boolean selected = TextUtils.equals(entry.getValue(), deviceId); //null-safe
|
|
|
|
entry.getKey().setChecked(selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pairStatus.equals(PAIRING_ACCEPTED)) {
|
|
|
|
DeviceFragment.acceptPairing(deviceId, this);
|
|
|
|
} else {
|
|
|
|
DeviceFragment.rejectPairing(deviceId, this);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateComputerList() {
|
|
|
|
|
2018-03-03 17:21:16 +01:00
|
|
|
//Log.e("MainActivity", "UpdateComputerList");
|
2015-09-07 00:11:46 -07:00
|
|
|
|
2018-03-03 17:21:16 +01:00
|
|
|
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
2015-08-20 00:59:21 -07:00
|
|
|
@Override
|
|
|
|
public void onServiceStart(final BackgroundService service) {
|
|
|
|
|
|
|
|
Menu menu = mNavigationView.getMenu();
|
|
|
|
|
|
|
|
menu.clear();
|
|
|
|
mMapMenuToDeviceId.clear();
|
|
|
|
|
|
|
|
int id = 0;
|
|
|
|
Collection<Device> devices = service.getDevices().values();
|
|
|
|
for (Device device : devices) {
|
|
|
|
if (device.isReachable() && device.isPaired()) {
|
2015-09-04 06:04:22 -07:00
|
|
|
MenuItem item = menu.add(0, id++, 0, device.getName());
|
2015-08-20 00:59:21 -07:00
|
|
|
item.setIcon(device.getIcon());
|
|
|
|
item.setCheckable(true);
|
2015-08-20 01:00:05 -07:00
|
|
|
item.setChecked(device.getDeviceId().equals(mCurrentDevice));
|
2015-08-20 00:59:21 -07:00
|
|
|
mMapMenuToDeviceId.put(item, device.getDeviceId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 08:15:55 -08:00
|
|
|
MenuItem item = menu.add(99, id++, 0, R.string.pair_new_device);
|
2015-08-20 00:59:21 -07:00
|
|
|
item.setIcon(R.drawable.ic_action_content_add_circle_outline);
|
|
|
|
item.setCheckable(true);
|
2015-08-20 01:00:05 -07:00
|
|
|
item.setChecked(mCurrentDevice == null);
|
2015-08-20 00:59:21 -07:00
|
|
|
mMapMenuToDeviceId.put(item, null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart() {
|
|
|
|
super.onStart();
|
2015-09-10 02:35:33 -07:00
|
|
|
BackgroundService.addGuiInUseCounter(this, true);
|
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(BackgroundService service) {
|
2018-03-03 17:21:16 +01:00
|
|
|
service.addDeviceListChangedCallback("MainActivity", new BackgroundService.DeviceListChangedCallback() {
|
2015-09-10 02:35:33 -07:00
|
|
|
@Override
|
|
|
|
public void onDeviceListChanged() {
|
|
|
|
updateComputerList();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateComputerList();
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
2015-09-09 12:34:42 -07:00
|
|
|
BackgroundService.removeGuiInUseCounter(this);
|
2015-09-10 02:35:33 -07:00
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(BackgroundService service) {
|
2018-03-03 17:21:16 +01:00
|
|
|
service.removeDeviceListChangedCallback("MainActivity");
|
2015-09-10 02:35:33 -07:00
|
|
|
}
|
|
|
|
});
|
2015-08-20 00:59:21 -07:00
|
|
|
super.onStop();
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:04:36 -07:00
|
|
|
//TODO: Make it accept two parameters, a constant with the type of screen and the device id in
|
|
|
|
//case the screen is for a device, or even three parameters and the third one be the plugin id?
|
2016-03-02 15:25:10 -08:00
|
|
|
//This way we can keep adding more options with null device id (eg: about, help...)
|
2017-07-24 16:39:00 +02:00
|
|
|
public void onDeviceSelected(String deviceId, boolean fromDeviceList) {
|
2015-08-20 00:59:21 -07:00
|
|
|
|
|
|
|
mCurrentDevice = deviceId;
|
|
|
|
|
2015-08-20 01:00:05 -07:00
|
|
|
preferences.edit().putString(STATE_SELECTED_DEVICE, mCurrentDevice).apply();
|
|
|
|
|
2015-09-16 09:52:55 +02:00
|
|
|
for (HashMap.Entry<MenuItem, String> entry : mMapMenuToDeviceId.entrySet()) {
|
2015-08-20 00:59:21 -07:00
|
|
|
boolean selected = TextUtils.equals(entry.getValue(), deviceId); //null-safe
|
|
|
|
entry.getKey().setChecked(selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
Fragment fragment;
|
|
|
|
if (deviceId == null) {
|
|
|
|
fragment = new PairingFragment();
|
|
|
|
} else {
|
2017-07-24 16:39:00 +02:00
|
|
|
fragment = new DeviceFragment(deviceId, fromDeviceList);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
|
String savedDevice = savedInstanceState.getString(STATE_SELECTED_DEVICE);
|
|
|
|
onDeviceSelected(savedDevice);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(BackgroundService service) {
|
|
|
|
Device device = service.getDevice(mCurrentDevice);
|
|
|
|
device.reloadPluginsFromSettings();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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
|
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(BackgroundService service) {
|
|
|
|
Device device = service.getDevice(mCurrentDevice);
|
|
|
|
device.reloadPluginsFromSettings();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 08:14:24 -08:00
|
|
|
public void renameDevice() {
|
2016-01-10 08:22:56 -08:00
|
|
|
final TextView nameView = (TextView) mNavigationView.findViewById(R.id.device_name);
|
2018-03-03 17:21:16 +01:00
|
|
|
final EditText deviceNameEdit = new EditText(MainActivity.this);
|
|
|
|
String deviceName = DeviceHelper.getDeviceName(MainActivity.this);
|
2015-09-07 00:07:39 -07:00
|
|
|
deviceNameEdit.setText(deviceName);
|
2015-11-12 08:14:24 -08:00
|
|
|
deviceNameEdit.setPadding(
|
|
|
|
((int) (18 * getResources().getDisplayMetrics().density)),
|
|
|
|
((int) (16 * getResources().getDisplayMetrics().density)),
|
|
|
|
((int) (18 * getResources().getDisplayMetrics().density)),
|
|
|
|
((int) (12 * getResources().getDisplayMetrics().density))
|
|
|
|
);
|
2018-03-03 17:21:16 +01:00
|
|
|
new AlertDialog.Builder(MainActivity.this)
|
2015-09-07 00:07:39 -07:00
|
|
|
.setView(deviceNameEdit)
|
|
|
|
.setPositiveButton(R.string.device_rename_confirm, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
String deviceName = deviceNameEdit.getText().toString();
|
2018-03-03 17:21:16 +01:00
|
|
|
DeviceHelper.setDeviceName(MainActivity.this, deviceName);
|
2015-09-07 00:07:39 -07:00
|
|
|
nameView.setText(deviceName);
|
2018-03-03 17:21:16 +01:00
|
|
|
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
2017-07-14 03:30:32 +02:00
|
|
|
@Override
|
|
|
|
public void onServiceStart(final BackgroundService service) {
|
|
|
|
service.onNetworkChange();
|
|
|
|
}
|
|
|
|
});
|
2015-09-07 00:07:39 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setTitle(R.string.device_rename_title)
|
|
|
|
.show();
|
2015-09-03 09:00:57 -07:00
|
|
|
}
|
2015-08-20 00:59:21 -07:00
|
|
|
}
|