2013-09-05 01:37:59 +02:00
|
|
|
package org.kde.kdeconnect.UserInterface;
|
2013-09-03 17:58:59 +02:00
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.app.ActionBarActivity;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.BackgroundService;
|
|
|
|
import org.kde.kdeconnect.Device;
|
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
|
|
|
import org.kde.kdeconnect.UserInterface.List.ButtonItem;
|
|
|
|
import org.kde.kdeconnect.UserInterface.List.ListAdapter;
|
|
|
|
import org.kde.kdeconnect.UserInterface.List.SectionItem;
|
2014-06-09 13:25:02 +02:00
|
|
|
import org.kde.kdeconnect.UserInterface.List.TextItem;
|
2013-09-05 01:35:12 +02:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
2013-09-03 17:58:59 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2013-11-23 01:30:13 +01:00
|
|
|
import java.util.ConcurrentModificationException;
|
2013-09-03 17:58:59 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
public class DeviceActivity extends ActionBarActivity {
|
|
|
|
|
2013-10-03 15:51:15 +02:00
|
|
|
static private String deviceId; //Static because if we get here by using the back button in the action bar, the extra deviceId will not be set.
|
2013-09-03 17:58:59 +02:00
|
|
|
private Device device;
|
|
|
|
|
2014-03-29 01:47:15 +01:00
|
|
|
private final Device.PluginsChangedListener pluginsChangedListener = new Device.PluginsChangedListener() {
|
2013-09-03 17:58:59 +02:00
|
|
|
@Override
|
|
|
|
public void onPluginsChanged(final Device device) {
|
|
|
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
|
2014-06-09 13:25:02 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
//Errors list
|
|
|
|
final HashMap<String, Plugin> failedPlugins = device.getFailedPlugins();
|
|
|
|
final String[] ids = failedPlugins.keySet().toArray(new String[failedPlugins.size()]);
|
|
|
|
String[] names = new String[failedPlugins.size()];
|
|
|
|
for(int i = 0; i < ids.length; i++) {
|
|
|
|
Plugin p = failedPlugins.get(ids[i]);
|
|
|
|
names[i] = p.getDisplayName();
|
2013-09-03 17:58:59 +02:00
|
|
|
}
|
2014-06-09 13:25:02 +02:00
|
|
|
ListView errorList = (ListView)findViewById(R.id.errors_list);
|
|
|
|
if (!failedPlugins.isEmpty() && errorList.getHeaderViewsCount() == 0) {
|
|
|
|
TextView header = new TextView(DeviceActivity.this);
|
|
|
|
header.setPadding(0,24,0,0);
|
|
|
|
header.setText(getResources().getString(R.string.plugins_failed_to_load));
|
|
|
|
errorList.addHeaderView(header);
|
|
|
|
}
|
|
|
|
errorList.setAdapter(new ArrayAdapter<String>(DeviceActivity.this, android.R.layout.simple_list_item_1, names));
|
|
|
|
errorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
|
|
|
|
if (position == 0) return;
|
|
|
|
Plugin p = failedPlugins.get(ids[position - 1]); //Header is position 0, so we have to subtract one
|
|
|
|
p.getErrorDialog(DeviceActivity.this).show();
|
|
|
|
}
|
|
|
|
});
|
2013-09-03 17:58:59 +02:00
|
|
|
|
2013-11-23 01:30:13 +01:00
|
|
|
//Buttons list
|
|
|
|
ArrayList<ListAdapter.Item> items = new ArrayList<ListAdapter.Item>();
|
2014-06-09 13:25:02 +02:00
|
|
|
|
|
|
|
if (device.isReachable()) {
|
|
|
|
final Collection<Plugin> plugins = device.getLoadedPlugins().values();
|
|
|
|
for (Plugin p : plugins) {
|
|
|
|
Button b = p.getInterfaceButton(DeviceActivity.this);
|
|
|
|
if (b != null) {
|
|
|
|
items.add(new SectionItem(p.getDisplayName()));
|
|
|
|
items.add(new ButtonItem(b));
|
|
|
|
}
|
2013-11-23 01:30:13 +01:00
|
|
|
}
|
2014-06-09 13:25:02 +02:00
|
|
|
} else {
|
|
|
|
Button b = new Button(DeviceActivity.this);
|
|
|
|
b.setText(R.string.device_menu_unpair);
|
|
|
|
b.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
device.unpair();
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
items.add(new TextItem(getString(R.string.device_not_reachable)));
|
|
|
|
items.add(new ButtonItem(b));
|
2013-09-03 17:58:59 +02:00
|
|
|
}
|
2013-11-23 01:30:13 +01:00
|
|
|
|
|
|
|
ListView buttonsList = (ListView)findViewById(R.id.buttons_list);
|
|
|
|
buttonsList.setAdapter(new ListAdapter(DeviceActivity.this, items));
|
|
|
|
|
|
|
|
} catch(ConcurrentModificationException e) {
|
|
|
|
Log.e("DeviceActivity", "ConcurrentModificationException");
|
|
|
|
this.run(); //Try again
|
2013-09-03 17:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_device);
|
|
|
|
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
2013-10-03 15:51:15 +02:00
|
|
|
if (getIntent().hasExtra("deviceId")) {
|
|
|
|
deviceId = getIntent().getStringExtra("deviceId");
|
|
|
|
}
|
2013-09-03 17:58:59 +02:00
|
|
|
|
|
|
|
BackgroundService.RunCommand(DeviceActivity.this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(BackgroundService service) {
|
|
|
|
device = service.getDevice(deviceId);
|
2013-10-03 15:51:15 +02:00
|
|
|
if (device == null) return;
|
2013-09-03 17:58:59 +02:00
|
|
|
setTitle(device.getName());
|
|
|
|
device.addPluginsChangedListener(pluginsChangedListener);
|
|
|
|
pluginsChangedListener.onPluginsChanged(device);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
BackgroundService.RunCommand(DeviceActivity.this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(BackgroundService service) {
|
|
|
|
Device device = service.getDevice(deviceId);
|
|
|
|
device.removePluginsChangedListener(pluginsChangedListener);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
menu.clear();
|
|
|
|
if (device.isPaired()) {
|
2013-09-05 01:33:54 +02:00
|
|
|
menu.add(R.string.device_menu_plugins).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
|
2013-09-03 17:58:59 +02:00
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem menuItem) {
|
|
|
|
Intent intent = new Intent(DeviceActivity.this, SettingsActivity.class);
|
|
|
|
intent.putExtra("deviceId", deviceId);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2013-09-05 01:33:54 +02:00
|
|
|
menu.add(R.string.device_menu_unpair).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
|
2013-09-03 17:58:59 +02:00
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem menuItem) {
|
|
|
|
device.unpair();
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|