From 374952ea62dd5199b38da51cc7efe075d999120a Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Fri, 7 Aug 2015 23:25:19 -0600 Subject: Allow indexing into bridges to assist with ViewPager Since ViewPager seems to have no way to inspect its items, we need to query the adapter for the item at the selected index. --- .../main/java/org/connectbot/ConsoleActivity.java | 45 ++++++++++++++-------- .../org/connectbot/service/TerminalManager.java | 5 ++- 2 files changed, 32 insertions(+), 18 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/org/connectbot/ConsoleActivity.java b/app/src/main/java/org/connectbot/ConsoleActivity.java index 91abb82..e011c93 100644 --- a/app/src/main/java/org/connectbot/ConsoleActivity.java +++ b/app/src/main/java/org/connectbot/ConsoleActivity.java @@ -47,6 +47,7 @@ import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.preference.PreferenceManager; +import android.support.v4.app.ActivityCompat; import android.support.v4.view.MotionEventCompat; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; @@ -197,8 +198,9 @@ public class ConsoleActivity extends Activity { TerminalBridge bridge = (TerminalBridge) msg.obj; adapter.notifyDataSetChanged(); - if (bridge.isAwaitingClose()) + if (bridge.isAwaitingClose()) { closeBridge(bridge); + } } }; @@ -218,9 +220,10 @@ public class ConsoleActivity extends Activity { } protected View findCurrentView(int id) { - if (adapter.getCount() == 0) return null; - View view = pager.getChildAt(pager.getCurrentItem()); - if (view == null) return null; + TerminalView view = adapter.getCurrentTerminalView(); + if (view == null) { + return null; + } return view.findViewById(id); } @@ -308,11 +311,7 @@ public class ConsoleActivity extends Activity { new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { - View overlay = findCurrentView(R.id.terminal_overlay); - if (overlay != null) - overlay.startAnimation(fade_out_delayed); - updateDefault(); - updatePromptVisible(); + onTerminalChanged(); } }); @@ -1083,6 +1082,18 @@ public class ConsoleActivity extends Activity { } } + /** + * Called whenever the displayed terminal is changed. + */ + private void onTerminalChanged() { + View overlay = findCurrentView(R.id.terminal_overlay); + if (overlay != null) + overlay.startAnimation(fade_out_delayed); + updateDefault(); + updatePromptVisible(); + ActivityCompat.invalidateOptionsMenu(ConsoleActivity.this); + } + /** * Displays the child in the ViewPager at the requestedIndex and updates the prompts. * @@ -1090,8 +1101,7 @@ public class ConsoleActivity extends Activity { */ private void setDisplayedTerminal(int requestedIndex) { pager.setCurrentItem(requestedIndex); - updatePromptVisible(); - updateEmptyVisible(); + onTerminalChanged(); } private void pasteIntoTerminal() { @@ -1135,9 +1145,8 @@ public class ConsoleActivity extends Activity { terminal.setId(R.id.console_flip); view.addView(terminal, 0); - // Tag the view with its position so that it can be retrieved later. - // Unfortunately, position here != child position in the ViewPager. - view.setTag(position); + // Tag the view with its bridge so it can be retrieved later. + view.setTag(bridge); container.addView(view); overlay.startAnimation(fade_out_delayed); @@ -1157,7 +1166,7 @@ public class ConsoleActivity extends Activity { TerminalView terminal = (TerminalView) view.findViewById(R.id.console_flip); final HostBean host = terminal.bridge.host; int itemIndex = -1; - int i = 0; + int i = 0; for (TerminalBridge bridge : bound.getBridges()) { if (bridge.host.equals(host)) { itemIndex = i; @@ -1172,6 +1181,10 @@ public class ConsoleActivity extends Activity { } } + public TerminalBridge getItemAtPosition(int position) { + return bound.getBridges().get(position); + } + @Override public boolean isViewFromObject(View view, Object object) { return view == object; @@ -1183,7 +1196,7 @@ public class ConsoleActivity extends Activity { } public TerminalView getCurrentTerminalView() { - View currentView = pager.findViewWithTag(pager.getCurrentItem()); + View currentView = pager.findViewWithTag(adapter.getItemAtPosition(pager.getCurrentItem())); if (currentView == null) return null; return (TerminalView) currentView.findViewById(R.id.console_flip); } diff --git a/app/src/main/java/org/connectbot/service/TerminalManager.java b/app/src/main/java/org/connectbot/service/TerminalManager.java index b5d891d..8001561 100644 --- a/app/src/main/java/org/connectbot/service/TerminalManager.java +++ b/app/src/main/java/org/connectbot/service/TerminalManager.java @@ -22,6 +22,7 @@ import java.lang.ref.WeakReference; import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; @@ -70,7 +71,7 @@ import android.util.Log; public class TerminalManager extends Service implements BridgeDisconnectedListener, OnSharedPreferenceChangeListener { public final static String TAG = "CB.TerminalManager"; - private List bridges = new LinkedList(); + private ArrayList bridges = new ArrayList(); public Map> mHostBridgeMap = new HashMap>(); public Map> mNicknameBridgeMap = @@ -471,7 +472,7 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen } } - public List getBridges() { + public ArrayList getBridges() { return bridges; } -- cgit v1.2.3