mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 21:55:10 +00:00
Simplified device activity layout to use a single ListView
This fixes some layout problems that prevented us of having the height of the lists adapted automatically to the content and had to use fixed height.
This commit is contained in:
@@ -9,8 +9,6 @@ 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;
|
||||
@@ -19,15 +17,16 @@ 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.CustomItem;
|
||||
import org.kde.kdeconnect.UserInterface.List.ListAdapter;
|
||||
import org.kde.kdeconnect.UserInterface.List.SectionItem;
|
||||
import org.kde.kdeconnect.UserInterface.List.SmallEntryItem;
|
||||
import org.kde.kdeconnect.UserInterface.List.TextItem;
|
||||
import org.kde.kdeconnect_tp.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class DeviceActivity extends ActionBarActivity {
|
||||
|
||||
@@ -47,55 +46,10 @@ public class DeviceActivity extends ActionBarActivity {
|
||||
public void run() {
|
||||
|
||||
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();
|
||||
}
|
||||
ListView errorList = (ListView)findViewById(R.id.errors_list);
|
||||
if (!failedPlugins.isEmpty()) {
|
||||
if (errorList.getHeaderViewsCount() == 0) {
|
||||
if (errorHeader == null) {
|
||||
errorHeader = new TextView(DeviceActivity.this);
|
||||
errorHeader.setPadding(0,24,0,0);
|
||||
errorHeader.setOnClickListener(null);
|
||||
errorHeader.setOnLongClickListener(null);
|
||||
|
||||
errorHeader.setText(getResources().getString(R.string.plugins_failed_to_load));
|
||||
}
|
||||
errorList.addHeaderView(errorHeader);
|
||||
}
|
||||
} else {
|
||||
errorList.removeHeaderView(errorHeader);
|
||||
}
|
||||
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();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
//Buttons list
|
||||
ArrayList<ListAdapter.Item> items = new ArrayList<ListAdapter.Item>();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!device.isReachable()) {
|
||||
//Not reachable, show unpair button
|
||||
Button b = new Button(DeviceActivity.this);
|
||||
b.setText(R.string.device_menu_unpair);
|
||||
b.setOnClickListener(new View.OnClickListener() {
|
||||
@@ -107,10 +61,42 @@ public class DeviceActivity extends ActionBarActivity {
|
||||
});
|
||||
items.add(new TextItem(getString(R.string.device_not_reachable)));
|
||||
items.add(new ButtonItem(b));
|
||||
} else {
|
||||
//Plugins button list
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
//Failed plugins List
|
||||
final Collection<Plugin> failed = device.getFailedPlugins().values();
|
||||
if (!failed.isEmpty()) {
|
||||
if (errorHeader == null) {
|
||||
errorHeader = new TextView(DeviceActivity.this);
|
||||
errorHeader.setPadding(0, 48, 0, 0);
|
||||
errorHeader.setOnClickListener(null);
|
||||
errorHeader.setOnLongClickListener(null);
|
||||
errorHeader.setText(getResources().getString(R.string.plugins_failed_to_load));
|
||||
}
|
||||
items.add(new CustomItem(errorHeader));
|
||||
for (final Plugin p : failed) {
|
||||
items.add(new SmallEntryItem(p.getDisplayName(), new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
p.getErrorDialog(DeviceActivity.this).show();
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ListView buttonsList = (ListView)findViewById(R.id.buttons_list);
|
||||
buttonsList.setAdapter(new ListAdapter(DeviceActivity.this, items));
|
||||
ListAdapter adapter = new ListAdapter(DeviceActivity.this, items);
|
||||
buttonsList.setAdapter(adapter);
|
||||
|
||||
} catch(ConcurrentModificationException e) {
|
||||
Log.e("DeviceActivity", "ConcurrentModificationException");
|
||||
|
@@ -0,0 +1,20 @@
|
||||
package org.kde.kdeconnect.UserInterface.List;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
public class CustomItem implements ListAdapter.Item {
|
||||
|
||||
private final View view;
|
||||
|
||||
public CustomItem(View v) {
|
||||
this.view = v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View inflateView(LayoutInflater layoutInflater) {
|
||||
return view;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
package org.kde.kdeconnect.UserInterface.List;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.kde.kdeconnect_tp.R;
|
||||
|
||||
public class SmallEntryItem implements ListAdapter.Item {
|
||||
|
||||
private final String title;
|
||||
private final View.OnClickListener clickListener;
|
||||
|
||||
public SmallEntryItem(String title) {
|
||||
this.title = title;
|
||||
this.clickListener = null;
|
||||
}
|
||||
|
||||
public SmallEntryItem(String title, View.OnClickListener clickListener) {
|
||||
this.title = title;
|
||||
this.clickListener = clickListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View inflateView(LayoutInflater layoutInflater) {
|
||||
View v = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
|
||||
|
||||
TextView titleView = (TextView)v.findViewById(android.R.id.text1);
|
||||
if (titleView != null) titleView.setText(title);
|
||||
if (clickListener != null) {
|
||||
titleView.setOnClickListener(clickListener);
|
||||
v.setBackgroundDrawable(layoutInflater.getContext().getResources().getDrawable(R.drawable.kitkatcompatselector_list_selector_holo_dark));
|
||||
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
}
|
@@ -1,26 +1,13 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/buttons_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:fillViewport="true"
|
||||
android:divider="@null"
|
||||
tools:context=".MainActivity"
|
||||
android:orientation="vertical">
|
||||
|
||||
<ListView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:id="@+id/buttons_list"
|
||||
android:layout_weight="1"
|
||||
/>
|
||||
|
||||
<ListView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:id="@+id/errors_list"
|
||||
android:layout_weight="1"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
|
Reference in New Issue
Block a user