summaryrefslogtreecommitdiff
path: root/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/MainActivity.java
blob: 0661d851c238be94f9311a04386de07a8873fadc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package net.brokenfox.vulpirc;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.support.v4.app.*;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.*;
import android.widget.*;

public class MainActivity extends FragmentActivity implements Connection.ConnectionListener, Connection.LoginStateListener {

	private ListView mWindowList;
	private ViewPager mWindowPager;
	private WindowListAdapter mWindowListAdapter;
	private WindowPagerAdapter mWindowPagerAdapter;
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

	    mDrawerLayout = ((DrawerLayout)findViewById(R.id.drawerLayout));
	    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
			    R.drawable.ic_drawer,
			    R.string.drawer_open, R.string.drawer_close) {
		    @Override
		    public void onDrawerOpened(View drawerView) {
			    super.onDrawerOpened(drawerView);
			    getActionBar().setTitle("VulpIRC");
			    invalidateOptionsMenu();
		    }

		    @Override
		    public void onDrawerClosed(View drawerView) {
			    super.onDrawerClosed(drawerView);

			    fixActionBarTitle();
			    invalidateOptionsMenu();
		    }
	    };
	    mDrawerLayout.setDrawerListener(mDrawerToggle);
	    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.LEFT);

	    getActionBar().setDisplayHomeAsUpEnabled(true);
	    getActionBar().setHomeButtonEnabled(true);

	    mWindowList = ((ListView)findViewById(R.id.windowList));
	    mWindowList.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
	    mWindowList.setOnItemClickListener(new WindowListItemClickListener());
	    mWindowList.setBackgroundColor(0x60000000);
	    mWindowListAdapter = new WindowListAdapter();

	    mWindowPager = ((ViewPager)findViewById(R.id.windowPager));
	    mWindowPager.setOnPageChangeListener(new WindowPagerPageChangeListener());
	    mWindowPagerAdapter = new WindowPagerAdapter(getSupportFragmentManager());

	    doBindService();
    }

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onDestroy() {
		if (mService != null) {
			Connection.get().deregisterListener(this);
			Connection.get().deregisterLoginStateListener(this);
		}
		doUnbindService();
		super.onDestroy();
	}

	@Override
	public void handleWindowsUpdated() {
		mWindowPagerAdapter.notifyDataSetChanged();
		mWindowListAdapter.notifyDataSetChanged();
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item))
	        return true;

        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;

	        case R.id.actionLogOut:
		        Connection.get().disconnect();
		        return true;
        }
        return super.onOptionsItemSelected(item);
    }


	private void fixActionBarTitle() {
		if (mDrawerLayout.isDrawerOpen(mWindowList))
			return;

		WindowData w = Connection.get().getActiveWindow();
		if (w == null)
			getActionBar().setTitle("VulpIRC");
		else
			getActionBar().setTitle(w.title);
	}


	@Override
	public void handleLoginStateChanged() {
		BaseConn.SocketState s = Connection.get().getSocketState();
		switch (s) {
			case CONNECTED:
				getActionBar().setSubtitle("Connected!");
				break;
			case CONNECTING:
				getActionBar().setSubtitle("Connecting...");
				break;
			case DISCONNECTED:
				getActionBar().setSubtitle("Disconnected.");
				break;
			case DISCONNECTING:
				getActionBar().setSubtitle("Disconnecting...");
				break;
		}
	}

	private class WindowListItemClickListener implements AdapterView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
			mWindowPager.setCurrentItem(position);
			mWindowList.setItemChecked(position, true);
		}
	}

	private class WindowPagerPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
		@Override
		public void onPageSelected(int position) {
			mWindowList.setItemChecked(position, true);
			if (Connection.get().getActiveWindow() != null)
				Log.i("VulpIRC", "Boop1 : " + Connection.get().getActiveWindow().title);

			if (position == 0)
				Connection.get().setActiveWindow(Connection.get().statusWindow);
			else
				Connection.get().setActiveWindow(Connection.get().windows.get(position - 1));

			Log.i("VulpIRC", "Boop2 : " + Connection.get().getActiveWindow().title);
			fixActionBarTitle();
		}
	}


	private class WindowListItemView extends LinearLayout {
		private ImageView mStatusView;
		private TextView mTitleView;

		public WindowListItemView(Context context) {
			super(context);

			setOrientation(HORIZONTAL);

			mStatusView = new ImageView(context);
			mStatusView.setImageDrawable(getResources().getDrawable(R.drawable.window_status));
			mTitleView = new TextView(context);
			mTitleView.setGravity(0x800003|0x10); // start|center_vertical

			float density = getResources().getDisplayMetrics().density;
			setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, (int) (36 * density + 0.5f)));

			int statusSize = (int)(8 * density + 0.5f);
			LayoutParams statuslp = new LayoutParams(statusSize, LayoutParams.MATCH_PARENT);

			int titlePadding = (int)(16 * density + 0.5f);
			mTitleView.setPadding(titlePadding, 0, 0, 0);

			addView(mStatusView, statuslp);
			addView(mTitleView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));

			// I really don't like this.
			TypedArray a = getTheme().obtainStyledAttributes(new int[] { android.R.attr.activatedBackgroundIndicator });
			//setBackground(getResources().getDrawable(android.R.attr.activatedBackgroundIndicator));
			setBackgroundDrawable(a.getDrawable(0));
			a.recycle();
		}

		public void setPlaceholder() {
			mTitleView.setText("Placeholder");
		}
		public void setWindow(WindowData wd) {
			mTitleView.setText(wd.title);
			mStatusView.setImageLevel(wd.unreadLevel);
		}
	}

	private class WindowListAdapter extends BaseAdapter implements ListAdapter {
		@Override
		public int getCount() {
			return 1 + Connection.get().windows.size();
		}

		@Override
		public Object getItem(int i) {
			if (i == 0)
				return "boop";
			else
				return Connection.get().windows.get(i - 1);
		}

		@Override
		public long getItemId(int i) {
			if (i == 0)
				return -1;
			else
				return Connection.get().windows.get(i - 1).id;
		}

		@Override
		public View getView(int i, View view, ViewGroup viewGroup) {
			WindowListItemView iv;

			if (view != null && view instanceof WindowListItemView) {
				iv = (WindowListItemView)view;
			} else {
				iv = new WindowListItemView(viewGroup.getContext());
			}

			if (i == 0)
				iv.setWindow(Connection.get().statusWindow);
			else
				iv.setWindow(Connection.get().windows.get(i - 1));
			return iv;
		}
	}

	private class WindowPagerAdapter extends FragmentPagerAdapter {
		WindowPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			WindowData window = null;
			if (i == 0)
				window = Connection.get().statusWindow;
			else
				window = Connection.get().windows.get(i - 1);

			return window.createFragment();
		}

		@Override
		public long getItemId(int position) {
			if (position == 0)
				return -1;
			else
				return Connection.get().windows.get(position - 1).id;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			if (position == 0)
				return "Status";
			else
				return Connection.get().windows.get(position - 1).title;
		}

		@Override
		public int getCount() {
			return 1 + Connection.get().windows.size();
		}
	}




	// SERVICE JUNK
	private IRCService mService = null;

	private final ServiceConnection mServiceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
			Log.i("VulpIRC", "[MainActivity connected to IRCService]");

			mService = ((IRCService.LocalBinder)iBinder).getService();
			Connection.get().registerListener(MainActivity.this);
			Connection.get().registerLoginStateListener(MainActivity.this);

			mWindowList.setAdapter(mWindowListAdapter);
			mWindowPager.setAdapter(mWindowPagerAdapter);

			handleLoginStateChanged();
		}

		@Override
		public void onServiceDisconnected(ComponentName componentName) {
			Log.i("VulpIRC", "[MainActivity disconnected from IRCService]");

			mService = null;
		}
	};

	private void doBindService() {
		Log.i("VulpIRC", "[MainActivity binding to IRCService...]");

		Intent i = new Intent(this, IRCService.class);
		bindService(i, mServiceConnection, BIND_AUTO_CREATE);
	}

	private void doUnbindService() {
		Log.i("VulpIRC", "[MainActivity unbinding from IRCService...]");

		unbindService(mServiceConnection);
	}

}