diff --git a/app/src/main/java/com/limelight/PcView.java b/app/src/main/java/com/limelight/PcView.java index 0683dce6..395fa58b 100644 --- a/app/src/main/java/com/limelight/PcView.java +++ b/app/src/main/java/com/limelight/PcView.java @@ -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 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(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; } } } diff --git a/app/src/main/java/com/limelight/grid/GenericGridAdapter.java b/app/src/main/java/com/limelight/grid/GenericGridAdapter.java new file mode 100644 index 00000000..c8d2a219 --- /dev/null +++ b/app/src/main/java/com/limelight/grid/GenericGridAdapter.java @@ -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 extends BaseAdapter { + protected Context context; + protected int defaultImageRes; + protected int layoutId; + protected ArrayList itemList = new ArrayList(); + 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; + } +} diff --git a/app/src/main/java/com/limelight/grid/PcGridAdapter.java b/app/src/main/java/com/limelight/grid/PcGridAdapter.java new file mode 100644 index 00000000..a87a5b12 --- /dev/null +++ b/app/src/main/java/com/limelight/grid/PcGridAdapter.java @@ -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 { + + 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; + } +} diff --git a/app/src/main/res/drawable/computer.png b/app/src/main/res/drawable/computer.png new file mode 100644 index 00000000..c92475da Binary files /dev/null and b/app/src/main/res/drawable/computer.png differ diff --git a/app/src/main/res/drawable/list_view_unselected.xml b/app/src/main/res/drawable/list_view_unselected.xml deleted file mode 100644 index 5150c083..00000000 --- a/app/src/main/res/drawable/list_view_unselected.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - diff --git a/app/src/main/res/layout-land/activity_pc_view.xml b/app/src/main/res/layout-land/activity_pc_view.xml index 8c7b4df4..7f9cbdc8 100644 --- a/app/src/main/res/layout-land/activity_pc_view.xml +++ b/app/src/main/res/layout-land/activity_pc_view.xml @@ -8,35 +8,23 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context=".PcView" > - - - - - + android:layout_below="@+id/settingsButton"/>