From ebf72421cb686f5dd3ed647006f4ee7fdf9b02fa Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Fri, 5 Mar 2010 15:56:57 +0100 Subject: [PATCH] Use a gallery for switching channels --- res/layout/channel.xml | 27 +++++ res/layout/server.xml | 39 +++----- src/org/yaaic/adapter/DeckAdapter.java | 110 ++++++++++++++++++++ src/org/yaaic/listener/FlingListener.java | 53 ---------- src/org/yaaic/view/ServerActivity.java | 116 ++++++++++++---------- 5 files changed, 213 insertions(+), 132 deletions(-) create mode 100644 res/layout/channel.xml create mode 100644 src/org/yaaic/adapter/DeckAdapter.java delete mode 100644 src/org/yaaic/listener/FlingListener.java diff --git a/res/layout/channel.xml b/res/layout/channel.xml new file mode 100644 index 0000000..d134522 --- /dev/null +++ b/res/layout/channel.xml @@ -0,0 +1,27 @@ + + + + \ No newline at end of file diff --git a/res/layout/server.xml b/res/layout/server.xml index c3f70be..30d1177 100644 --- a/res/layout/server.xml +++ b/res/layout/server.xml @@ -42,36 +42,20 @@ along with Yaaic. If not, see . android:textSize="16px" android:layout_weight="1" /> - - - - - - + + + \ No newline at end of file diff --git a/src/org/yaaic/adapter/DeckAdapter.java b/src/org/yaaic/adapter/DeckAdapter.java new file mode 100644 index 0000000..c550638 --- /dev/null +++ b/src/org/yaaic/adapter/DeckAdapter.java @@ -0,0 +1,110 @@ +/* + Yaaic - Yet Another Android IRC Client + +Copyright 2009 Sebastian Kaspari + +This file is part of Yaaic. + +Yaaic is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Yaaic is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Yaaic. If not, see . +*/ +package org.yaaic.adapter; + +import java.util.HashMap; +import java.util.LinkedList; + +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; + +/** + * The adapter for the "DeckView" + * + * @author Sebastian Kaspari + */ +public class DeckAdapter extends BaseAdapter +{ + private HashMap map = new HashMap(); + private LinkedList views = new LinkedList(); + + /** + * Get number of item + */ + public int getCount() + { + return views.size(); + } + + /** + * Get item at position + */ + public View getItem(int position) + { + return views.get(position); + } + + /** + * Get id of item at position + */ + public long getItemId(int position) + { + return position; + } + + /** + * Add an item + * + * @param channel Name of the channel + * @param view The view object + */ + public void addItem(String channel, View view) + { + map.put(channel, view); + views.add(view); + + notifyDataSetChanged(); + } + + /** + * Get an item by the channel's name + * + * @param channel + * @return The item + */ + public View getItemByName(String channel) + { + return map.get(channel); + } + + /** + * Remove an item + * + * @param channel + */ + public void removeItem(String channel) + { + View view = map.get(channel); + views.remove(view); + map.remove(channel); + + notifyDataSetChanged(); + } + + /** + * Get view at given position + */ + public View getView(int position, View convertView, ViewGroup parent) + { + return views.get(position); + } +} diff --git a/src/org/yaaic/listener/FlingListener.java b/src/org/yaaic/listener/FlingListener.java deleted file mode 100644 index b7adb23..0000000 --- a/src/org/yaaic/listener/FlingListener.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.yaaic.listener; - -import org.yaaic.R; - -import android.view.MotionEvent; -import android.view.GestureDetector.SimpleOnGestureListener; -import android.view.animation.Animation; -import android.view.animation.AnimationUtils; -import android.widget.ViewFlipper; - -public class FlingListener extends SimpleOnGestureListener -{ - private static final int SWIPE_MIN_DISTANCE = 120; - private static final int SWIPE_MAX_OFF_PATH = 250; - private static final int SWIPE_THRESHOLD_VELOCITY = 200; - - private Animation slideLeftIn; - private Animation slideLeftOut; - private Animation slideRightIn; - private Animation slideRightOut; - - private ViewFlipper flipper; - - public FlingListener(ViewFlipper flipper) - { - this.flipper = flipper; - - slideLeftIn = AnimationUtils.loadAnimation(flipper.getContext(), R.anim.slide_left_in); - slideLeftOut = AnimationUtils.loadAnimation(flipper.getContext(), R.anim.slide_left_out); - slideRightIn = AnimationUtils.loadAnimation(flipper.getContext(), R.anim.slide_right_in); - slideRightOut = AnimationUtils.loadAnimation(flipper.getContext(), R.anim.slide_right_out); - } - - @Override - public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) - { - if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { - return false; - } - if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { - flipper.setInAnimation(slideLeftIn); - flipper.setOutAnimation(slideLeftOut); - flipper.showNext(); - } - else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { - flipper.setInAnimation(slideRightIn); - flipper.setOutAnimation(slideRightOut); - flipper.showPrevious(); - } - - return false; - } -} diff --git a/src/org/yaaic/view/ServerActivity.java b/src/org/yaaic/view/ServerActivity.java index cfd15e9..a160fa7 100644 --- a/src/org/yaaic/view/ServerActivity.java +++ b/src/org/yaaic/view/ServerActivity.java @@ -20,16 +20,6 @@ along with Yaaic. If not, see . */ package org.yaaic.view; -import org.yaaic.R; -import org.yaaic.Yaaic; -import org.yaaic.irc.IRCBinder; -import org.yaaic.irc.IRCService; -import org.yaaic.listener.ChannelListener; -import org.yaaic.listener.FlingListener; -import org.yaaic.model.Broadcast; -import org.yaaic.model.Server; -import org.yaaic.receiver.ChannelReceiver; - import android.app.Activity; import android.app.Dialog; import android.content.ComponentName; @@ -38,19 +28,30 @@ import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; -import android.view.GestureDetector; +import android.util.Log; +import android.view.Display; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; +import android.widget.Gallery; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; -import android.widget.ViewFlipper; +import android.widget.ViewSwitcher; + +import org.yaaic.R; +import org.yaaic.Yaaic; +import org.yaaic.adapter.DeckAdapter; +import org.yaaic.irc.IRCBinder; +import org.yaaic.irc.IRCService; +import org.yaaic.listener.ChannelListener; +import org.yaaic.model.Broadcast; +import org.yaaic.model.Server; +import org.yaaic.receiver.ChannelReceiver; /** * Connected to server @@ -59,58 +60,35 @@ import android.widget.ViewFlipper; */ public class ServerActivity extends Activity implements ServiceConnection, ChannelListener { - protected static final String TextView = null; - private Server server; - private ChannelReceiver receiver; - private IRCBinder binder; - private int serverId; + public static final String TAG = "Yaaic/ServerActivity"; - private GestureDetector flingDetector; + private int serverId; + private Server server; + private IRCBinder binder; + private ChannelReceiver receiver; + + private ViewSwitcher switcher; + private Gallery deck; + private DeckAdapter deckAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - this.serverId = getIntent().getExtras().getInt("serverId"); - + serverId = getIntent().getExtras().getInt("serverId"); server = (Server) Yaaic.getInstance().getServerById(serverId); + setTitle("Yaaic - " + server.getTitle()); setContentView(R.layout.server); ((TextView) findViewById(R.id.title)).setText(server.getTitle()); ((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon()); - /* - ((Button) findViewById(R.id.next)).setOnClickListener(new OnClickListener() { - public void onClick(View v) { - ViewFlipper vf = (ViewFlipper) v.getRootView().findViewById(R.id.channels); - vf.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.slide_left_in)); - vf.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.slide_left_out)); - vf.showNext(); - } - }); - - ((Button) findViewById(R.id.previous)).setOnClickListener(new OnClickListener() { - public void onClick(View v) { - ViewFlipper vf = (ViewFlipper) v.getRootView().findViewById(R.id.channels); - vf.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.slide_right_in)); - vf.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.slide_right_out)); - vf.showPrevious(); - } - }); - */ - - flingDetector = new GestureDetector(new FlingListener((ViewFlipper) findViewById(R.id.channels))); - - receiver = new ChannelReceiver(this); - registerReceiver(receiver, new IntentFilter(Broadcast.CHANNEL_MESSAGE)); - } - - @Override - public boolean onTouchEvent(MotionEvent event) - { - return flingDetector.onTouchEvent(event); + deckAdapter = new DeckAdapter(); + deck = (Gallery) findViewById(R.id.deck); + deck.setAdapter(deckAdapter); + switcher = (ViewSwitcher) findViewById(R.id.switcher); } @Override @@ -120,6 +98,11 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann Intent intent = new Intent(this, IRCService.class); bindService(intent, this, 0); + + receiver = new ChannelReceiver(this); + registerReceiver(receiver, new IntentFilter(Broadcast.CHANNEL_MESSAGE)); + registerReceiver(receiver, new IntentFilter(Broadcast.CHANNEL_NEW)); + registerReceiver(receiver, new IntentFilter(Broadcast.CHANNEL_REMOVE)); } @Override @@ -128,6 +111,7 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann super.onPause(); unbindService(this); + unregisterReceiver(receiver); } public void onServiceConnected(ComponentName name, IBinder service) @@ -196,7 +180,36 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann */ public void onNewChannel(String target) { + Log.d(TAG, "onNewChannel() " + target); + TextView canvas = new TextView(this); + canvas.setText(target); + canvas.setTextColor(0xff000000); + + // XXX: Refactor this crap :) + + Display d = getWindowManager().getDefaultDisplay(); + int width = d.getWidth(); + int height = d.getHeight(); + + float fw = (float) width; + float fh = (float) height; + + float vwf = fw / 100 * 80; + float vhf = fh / 100 * 80; + + int w = (int) vwf; + int h = (int) vhf; + + canvas.setPadding(10, 10, 10, 10); + canvas.setBackgroundColor(0xff888888); + canvas.setLayoutParams(new Gallery.LayoutParams(w, h)); + + deckAdapter.addItem(target, canvas); + /* + deck.addView(child) + containers.put(target, new ChannelContainer(channel, canvas)); + */ } /** @@ -204,6 +217,5 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann */ public void onRemoveChannel(String target) { - } }