GridView WIP

This commit is contained in:
Cameron Gutman 2014-11-07 00:27:58 -08:00
parent 9758276f1c
commit 94b1c04fa6
11 changed files with 171 additions and 134 deletions

View File

@ -9,6 +9,7 @@ import com.limelight.binding.PlatformBinding;
import com.limelight.binding.crypto.AndroidCryptoProvider;
import com.limelight.computers.ComputerManagerListener;
import com.limelight.computers.ComputerManagerService;
import com.limelight.grid.PcGridAdapter;
import com.limelight.nvstream.http.ComputerDetails;
import com.limelight.nvstream.http.NvHTTP;
import com.limelight.nvstream.http.PairingManager;
@ -36,16 +37,15 @@ import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class PcView extends Activity {
private Button settingsButton, addComputerButton;
private ListView pcList;
private ArrayAdapter<ComputerObject> pcListAdapter;
private GridView pcGrid;
private PcGridAdapter pcGridAdapter;
private ComputerManagerService.ComputerManagerBinder managerBinder;
private boolean freezeUpdates, runningPolling;
private ServiceConnection serviceConnection = new ServiceConnection() {
@ -102,14 +102,13 @@ public class PcView extends Activity {
settingsButton = (Button)findViewById(R.id.settingsButton);
addComputerButton = (Button)findViewById(R.id.manuallyAddPc);
pcList = (ListView)findViewById(R.id.pcListView);
pcList.setAdapter(pcListAdapter);
pcList.setItemsCanFocus(true);
pcList.setOnItemClickListener(new OnItemClickListener() {
pcGrid = (GridView)findViewById(R.id.pcGridView);
pcGrid.setAdapter(pcGridAdapter);
pcGrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long id) {
ComputerObject computer = pcListAdapter.getItem(pos);
ComputerObject computer = (ComputerObject) pcGridAdapter.getItem(pos);
if (computer.details == null) {
// Placeholder item; no context menu for it
return;
@ -127,7 +126,7 @@ public class PcView extends Activity {
}
}
});
registerForContextMenu(pcList);
registerForContextMenu(pcGrid);
settingsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
@ -142,11 +141,11 @@ public class PcView extends Activity {
}
});
if (pcListAdapter.isEmpty()) {
if (pcGridAdapter.isEmpty()) {
addListPlaceholder();
}
else {
pcListAdapter.notifyDataSetChanged();
pcGridAdapter.notifyDataSetChanged();
}
}
@ -157,9 +156,8 @@ public class PcView extends Activity {
// Bind to the computer manager service
bindService(new Intent(PcView.this, ComputerManagerService.class), serviceConnection,
Service.BIND_AUTO_CREATE);
pcListAdapter = new ArrayAdapter<ComputerObject>(this, R.layout.simplerow, R.id.rowTextView);
pcListAdapter.setNotifyOnChange(false);
pcGridAdapter = new PcGridAdapter(this);
initializeViews();
}
@ -244,7 +242,7 @@ public class PcView extends Activity {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
ComputerObject computer = pcListAdapter.getItem(info.position);
ComputerObject computer = (ComputerObject) pcGridAdapter.getItem(info.position);
if (computer == null || computer.details == null) {
startComputerUpdates();
return;
@ -485,7 +483,7 @@ public class PcView extends Activity {
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
ComputerObject computer = pcListAdapter.getItem(info.position);
ComputerObject computer = (ComputerObject) pcGridAdapter.getItem(info.position);
switch (item.getItemId())
{
case PAIR_ID:
@ -518,65 +516,33 @@ public class PcView extends Activity {
return super.onContextItemSelected(item);
}
}
private static String generateString(ComputerDetails details) {
StringBuilder str = new StringBuilder();
str.append(details.name).append(" - ");
if (details.state == ComputerDetails.State.ONLINE) {
str.append("Online ");
if (details.reachability == ComputerDetails.Reachability.LOCAL) {
str.append("(Local) - ");
}
else {
str.append("(Remote) - ");
}
if (details.pairState == PairState.PAIRED) {
if (details.runningGameId == 0) {
str.append("Available");
}
else {
str.append("In Game");
}
}
else {
str.append("Not Paired");
}
}
else {
str.append("Offline");
}
return str.toString();
}
private void addListPlaceholder() {
pcListAdapter.add(new ComputerObject("Discovery is running. No computers found yet. " +
"If your PC doesn't show up in about 15 seconds, " +
"make sure your computer is running GFE or add your PC manually using the button above.", null));
}
private void removeListView(ComputerDetails details) {
for (int i = 0; i < pcListAdapter.getCount(); i++) {
ComputerObject computer = pcListAdapter.getItem(i);
for (int i = 0; i < pcGridAdapter.getCount(); i++) {
ComputerObject computer = (ComputerObject) pcGridAdapter.getItem(i);
if (details.equals(computer.details)) {
pcListAdapter.remove(computer);
pcGridAdapter.removeComputer(computer);
break;
}
}
if (pcListAdapter.getCount() == 0) {
if (pcGridAdapter.getCount() == 0) {
// Add the placeholder if we're down to 0 computers
addListPlaceholder();
}
}
private void updateListView(ComputerDetails details) {
String computerString = generateString(details);
ComputerObject existingEntry = null;
boolean placeholderPresent = false;
for (int i = 0; i < pcListAdapter.getCount(); i++) {
ComputerObject computer = pcListAdapter.getItem(i);
for (int i = 0; i < pcGridAdapter.getCount(); i++) {
ComputerObject computer = (ComputerObject) pcGridAdapter.getItem(i);
// If there's a placeholder, there's nothing else
if (computer.details == null) {
@ -593,35 +559,32 @@ public class PcView extends Activity {
if (existingEntry != null) {
// Replace the information in the existing entry
existingEntry.text = computerString;
existingEntry.details = details;
}
else {
// If the placeholder is the only object, remove it
if (placeholderPresent) {
pcListAdapter.remove(pcListAdapter.getItem(0));
pcGridAdapter.removeComputer((ComputerObject) pcGridAdapter.getItem(0));
}
// Add a new entry
pcListAdapter.add(new ComputerObject(computerString, details));
pcGridAdapter.addComputer(new ComputerObject(details));
}
// Notify the view that the data has changed
pcListAdapter.notifyDataSetChanged();
pcGridAdapter.notifyDataSetChanged();
}
public class ComputerObject {
public String text;
public ComputerDetails details;
public ComputerObject(String text, ComputerDetails details) {
this.text = text;
public ComputerObject(ComputerDetails details) {
this.details = details;
}
@Override
public String toString() {
return text;
return details.name;
}
}
}

View File

@ -0,0 +1,67 @@
package com.limelight.grid;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.limelight.PcView;
import com.limelight.R;
import java.util.ArrayList;
public abstract class GenericGridAdapter<T> extends BaseAdapter {
protected Context context;
protected int defaultImageRes;
protected int layoutId;
protected ArrayList<T> itemList = new ArrayList<T>();
protected LayoutInflater inflater;
public GenericGridAdapter(Context context, int layoutId, int defaultImageRes) {
this.context = context;
this.layoutId = layoutId;
this.defaultImageRes = defaultImageRes;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int i) {
return itemList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
public abstract boolean populateImageView(ImageView imgView, T obj);
public abstract boolean populateTextView(TextView txtView, T obj);
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
convertView = inflater.inflate(layoutId, null);
}
ImageView imgView = (ImageView) convertView.findViewById(R.id.grid_image);
TextView txtView = (TextView) convertView.findViewById(R.id.grid_text);
if (!populateImageView(imgView, itemList.get(i))) {
imgView.setImageResource(defaultImageRes);
}
if (!populateTextView(txtView, itemList.get(i))) {
txtView.setText(itemList.get(i).toString());
}
return convertView;
}
}

View File

@ -0,0 +1,35 @@
package com.limelight.grid;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import com.limelight.PcView;
import com.limelight.R;
public class PcGridAdapter extends GenericGridAdapter<PcView.ComputerObject> {
public PcGridAdapter(Context context) {
super(context, R.layout.generic_grid_item, R.drawable.computer);
}
public void addComputer(PcView.ComputerObject computer) {
itemList.add(computer);
}
public boolean removeComputer(PcView.ComputerObject computer) {
return itemList.remove(computer);
}
@Override
public boolean populateImageView(ImageView imgView, PcView.ComputerObject obj) {
// Return false to use the default drawable
return false;
}
@Override
public boolean populateTextView(TextView txtView, PcView.ComputerObject obj) {
// Return false to use the computer's toString method
return false;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="1dip" android:color="#ffffff"/>
</shape>

View File

@ -8,35 +8,23 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PcView" >
<ListView
android:id="@+id/pcListView"
android:layout_width="match_parent"
<GridView
android:id="@+id/pcGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/settingsButton"
android:background="@drawable/list_view_unselected"
android:fastScrollEnabled="true"
android:longClickable="false"
android:stackFromBottom="false" >
</ListView>
<TextView
android:id="@+id/discoveryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerHorizontal="true"
android:layout_alignBaseline="@+id/settingsButton"
android:text="@string/title_pc_view" />
android:layout_below="@+id/settingsButton"/>
<Button
android:id="@+id/settingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pcListView"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
@ -46,7 +34,7 @@
android:id="@+id/manuallyAddPc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/pcListView"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"

View File

@ -8,37 +8,23 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PcView" >
<ListView
android:id="@+id/pcListView"
android:layout_width="match_parent"
<GridView
android:id="@+id/pcGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/discoveryText"
android:background="@drawable/list_view_unselected"
android:fastScrollEnabled="true"
android:longClickable="false"
android:stackFromBottom="false" >
</ListView>
<TextView
android:id="@+id/discoveryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="@+id/manuallyAddPc"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="@string/title_pc_view" />
android:layout_below="@+id/settingsButton"/>
<Button
android:id="@+id/settingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/button_stream_settings" />
@ -46,8 +32,8 @@
android:id="@+id/manuallyAddPc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/settingsButton"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="@string/button_add_pc_manually" />
</RelativeLayout>

View File

@ -18,7 +18,6 @@
android:layout_below="@+id/appListText"
android:fastScrollEnabled="true"
android:longClickable="false"
android:background="@drawable/list_view_unselected"
android:stackFromBottom="false">
</ListView>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/grid_image"
android:layout_centerHorizontal="true"
android:layout_width="100dp"
android:layout_height="100dp">
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/grid_image"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="20sp" >
</TextView>
</RelativeLayout>

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="16sp" >
</TextView>
</LinearLayout>

View File

@ -6,8 +6,8 @@
<!-- PC view activity -->
<string name="title_pc_view">PC List</string>
<string name="button_stream_settings">Streaming Settings</string>
<string name="button_add_pc_manually">Add PC Manually</string>
<string name="button_stream_settings">Settings</string>
<string name="button_add_pc_manually">Add PC</string>
<!-- Add computer manually activity -->
<string name="button_add_pc">Manually Add PC</string>