From b7dfc45b1e761e6734d14a14d08d3e206bd2e9ad Mon Sep 17 00:00:00 2001
From: str4d <str4d@mail.i2p>
Date: Wed, 21 Aug 2013 11:03:39 +0000
Subject: [PATCH] Prep for I2PTunnel details fragment

---
 .../router/activity/AddressbookActivity.java  |  4 +-
 .../router/activity/I2PTunnelActivity.java    |  9 ++-
 .../router/fragment/I2PTunnelFragment.java    | 63 ++++++++++++++-----
 3 files changed, 57 insertions(+), 19 deletions(-)

diff --git a/src/net/i2p/android/router/activity/AddressbookActivity.java b/src/net/i2p/android/router/activity/AddressbookActivity.java
index 5c8cb3984..3ea7ea70a 100644
--- a/src/net/i2p/android/router/activity/AddressbookActivity.java
+++ b/src/net/i2p/android/router/activity/AddressbookActivity.java
@@ -13,8 +13,8 @@ import android.view.Menu;
 import android.view.MenuItem;
 
 public class AddressbookActivity extends I2PActivityBase
-                                 implements AddressbookFragment.OnAddressSelectedListener,
-                                 SearchView.OnQueryTextListener {
+        implements AddressbookFragment.OnAddressSelectedListener,
+        SearchView.OnQueryTextListener {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
diff --git a/src/net/i2p/android/router/activity/I2PTunnelActivity.java b/src/net/i2p/android/router/activity/I2PTunnelActivity.java
index c4e21c872..0fe8ad0be 100644
--- a/src/net/i2p/android/router/activity/I2PTunnelActivity.java
+++ b/src/net/i2p/android/router/activity/I2PTunnelActivity.java
@@ -2,12 +2,14 @@ package net.i2p.android.router.activity;
 
 import net.i2p.android.router.R;
 import net.i2p.android.router.fragment.I2PTunnelFragment;
+import net.i2p.android.router.loader.TunnelEntry;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.ActionBar.Tab;
 
-public class I2PTunnelActivity extends I2PActivityBase {
+public class I2PTunnelActivity extends I2PActivityBase
+        implements I2PTunnelFragment.OnTunnelSelectedListener {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -36,4 +38,9 @@ public class I2PTunnelActivity extends I2PActivityBase {
                 .setTabListener(new TabListener(f));
         actionBar.addTab(tab);
     }
+
+    // I2PTunnelFragment.OnTunnelSelectedListener
+
+    public void onTunnelSelected(TunnelEntry tunnel) {
+    }
 }
diff --git a/src/net/i2p/android/router/fragment/I2PTunnelFragment.java b/src/net/i2p/android/router/fragment/I2PTunnelFragment.java
index 73227f98b..52e9e7c34 100644
--- a/src/net/i2p/android/router/fragment/I2PTunnelFragment.java
+++ b/src/net/i2p/android/router/fragment/I2PTunnelFragment.java
@@ -7,6 +7,7 @@ import net.i2p.android.router.adapter.TunnelEntryAdapter;
 import net.i2p.android.router.loader.TunnelEntryLoader;
 import net.i2p.android.router.loader.TunnelEntry;
 import net.i2p.i2ptunnel.TunnelControllerGroup;
+import android.app.Activity;
 import android.os.Bundle;
 import android.support.v4.app.ListFragment;
 import android.support.v4.app.LoaderManager;
@@ -14,6 +15,8 @@ import android.support.v4.content.Loader;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
+import android.view.View;
+import android.widget.ListView;
 
 public class I2PTunnelFragment extends ListFragment
         implements LoaderManager.LoaderCallbacks<List<TunnelEntry>> {
@@ -22,10 +25,31 @@ public class I2PTunnelFragment extends ListFragment
     private static final int CLIENT_LOADER_ID = 1;
     private static final int SERVER_LOADER_ID = 2;
 
+    OnTunnelSelectedListener mCallback;
     private TunnelControllerGroup mGroup;
     private TunnelEntryAdapter mAdapter;
     private boolean mClientTunnels;
 
+    // Container Activity must implement this interface
+    public interface OnTunnelSelectedListener {
+        public void onTunnelSelected(TunnelEntry host);
+    }
+
+    @Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+
+        // This makes sure that the container activity has implemented
+        // the callback interface. If not, it throws an exception
+        try {
+            mCallback = (OnTunnelSelectedListener) activity;
+        } catch (ClassCastException e) {
+            throw new ClassCastException(activity.toString()
+                    + " must implement OnTunnelSelectedListener");
+        }
+
+    }
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -63,6 +87,29 @@ public class I2PTunnelFragment extends ListFragment
                 : SERVER_LOADER_ID, null, this);
     }
 
+    @Override
+    public void onListItemClick(ListView parent, View view, int pos, long id) {
+        mCallback.onTunnelSelected(mAdapter.getItem(pos));
+    }
+
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        inflater.inflate(R.menu.fragment_i2ptunnel_actions, menu);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        // Handle presses on the action bar items
+        switch (item.getItemId()) {
+            case R.id.action_add_tunnel:
+                return true;
+            default:
+                return super.onOptionsItemSelected(item);
+        }
+    }
+
+    // LoaderManager.LoaderCallbacks<List<TunnelEntry>>
+
     public Loader<List<TunnelEntry>> onCreateLoader(int id, Bundle args) {
         return new TunnelEntryLoader(getActivity(), mGroup, mClientTunnels);
     }
@@ -87,20 +134,4 @@ public class I2PTunnelFragment extends ListFragment
             mAdapter.setData(null);
         }
     }
-
-    @Override
-    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
-        inflater.inflate(R.menu.fragment_i2ptunnel_actions, menu);
-    }
-
-    @Override
-    public boolean onOptionsItemSelected(MenuItem item) {
-        // Handle presses on the action bar items
-        switch (item.getItemId()) {
-            case R.id.action_add_tunnel:
-                return true;
-            default:
-                return super.onOptionsItemSelected(item);
-        }
-    }
 }
-- 
GitLab