2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-09-05 16:45:08 +00:00
Files
kdeconnect-android/src/org/kde/kdeconnect/UserInterface/MainActivity.java

393 lines
15 KiB
Java
Raw Normal View History

2015-09-07 01:49:12 -07:00
package org.kde.kdeconnect.UserInterface;
2015-08-20 00:59:21 -07:00
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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;
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;
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;
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;
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
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
public static final int RESULT_NEEDS_RELOAD = Activity.RESULT_FIRST_USER;
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;
2018-10-17 15:09:29 +02:00
private int mCurrentMenuEntry;
2015-08-20 00:59:21 -07:00
private SharedPreferences preferences;
private final HashMap<MenuItem, String> mMapMenuToDeviceId = new HashMap<>();
2018-10-17 15:09:29 +02:00
private String pairRequestStatus;
2015-08-20 00:59:21 -07:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 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);
2018-10-17 15:09:29 +02:00
mDrawerLayout = findViewById(R.id.drawer_layout);
mNavigationView = 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
2018-10-17 15:09:29 +02:00
Toolbar toolbar = findViewById(R.id.toolbar);
2015-08-20 00:59:21 -07:00
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
2016-06-16 23:45:59 +02:00
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.open, /* "open drawer" description */
R.string.close /* "close drawer" description */
);
mDrawerLayout.addDrawerListener(mDrawerToggle);
2015-08-20 00:59:21 -07:00
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
actionBar.setDisplayHomeAsUpEnabled(true);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
2015-08-20 00:59:21 -07:00
String deviceName = DeviceHelper.getDeviceName(this);
2018-10-17 15:09:29 +02:00
TextView nameView = mDrawerHeader.findViewById(R.id.device_name);
2015-08-20 00:59:21 -07:00
nameView.setText(deviceName);
2018-10-17 15:09:29 +02:00
preferences = getSharedPreferences("stored_menu_selection", Context.MODE_PRIVATE);
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
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
String savedDevice;
2018-10-17 15:09:29 +02:00
int savedMenuEntry;
if (getIntent().hasExtra("forceOverview")) {
Log.i("MainActivity", "Requested to start main overview");
savedDevice = null;
2018-10-17 15:09:29 +02:00
savedMenuEntry = MENU_ENTRY_ADD_DEVICE;
} else if (getIntent().hasExtra("deviceId")) {
Log.i("MainActivity", "Loading selected device from parameter");
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);
savedDevice = onPairResultFromNotification(savedDevice, pairStatus);
if (savedDevice == null) {
savedMenuEntry = MENU_ENTRY_ADD_DEVICE;
}
}
} else if (savedInstanceState != null) {
Log.i("MainActivity", "Loading selected device from saved activity state");
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);
} else {
Log.i("MainActivity", "Loading selected device from persistent storage");
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
}
2018-10-17 15:09:29 +02:00
// Activate the chosen fragment and select the entry in the menu
if (savedMenuEntry >= MENU_ENTRY_DEVICE_FIRST_ID && savedDevice != null) {
onDeviceSelected(savedDevice);
} else {
mCurrentMenuEntry = savedMenuEntry;
mNavigationView.setCheckedItem(savedMenuEntry);
if (mCurrentMenuEntry == MENU_ENTRY_SETTINGS) {
setContentFragment(new SettingsFragment());
} else {
setContentFragment(new PairingFragment());
}
2018-10-17 15:09:29 +02:00
}
}
private String onPairResultFromNotification(String deviceId, String pairStatus) {
2018-10-17 15:09:29 +02:00
assert(deviceId != null);
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
if (device == null) {
Log.w("rejectPairing", "Device no longer exists: " + deviceId);
return;
}
if (pairStatus.equals(PAIRING_ACCEPTED)) {
device.acceptPairing();
} else {
device.rejectPairing();
}
});
if (pairStatus.equals(PAIRING_ACCEPTED)) {
return deviceId;
} else {
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();
}
}
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() {
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;
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());
item.setIcon(device.getIcon());
item.setCheckable(true);
mMapMenuToDeviceId.put(item, device.getDeviceId());
}
2015-08-20 00:59:21 -07: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();
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() {
BackgroundService.removeGuiInUseCounter(this);
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);
}
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();
}
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
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_NEEDS_RELOAD:
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(mCurrentDevice);
device.reloadPluginsFromSettings();
});
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
@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, service -> {
Device device = service.getDevice(mCurrentDevice);
device.reloadPluginsFromSettings();
});
}
}
}
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);
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)
.setView(deviceNameEdit)
.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);
}
})
2018-10-17 15:09:29 +02:00
.setNegativeButton(R.string.cancel, (dialog, which) -> { })
.setTitle(R.string.device_rename_title)
.show();
}
2018-10-17 15:09:29 +02:00
2018-10-26 23:53:58 +02:00
private void updateDeviceNameFromMenu(String newDeviceName) {
2018-10-17 15:09:29 +02:00
final TextView nameView = mNavigationView.findViewById(R.id.device_name);
nameView.setText(newDeviceName);
}
2015-08-20 00:59:21 -07:00
}