-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainActivity.cs
461 lines (411 loc) · 20.3 KB
/
MainActivity.cs
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm 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.
//
// MPfm 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 MPfm. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Net.Wifi.P2p;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Views;
using Android.OS;
using Android.Views.Animations;
using Android.Widget;
using Java.Lang;
using MPfm.Android.Classes.Adapters;
using MPfm.Android.Classes.Cache;
using MPfm.Android.Classes.Fragments;
using MPfm.Android.Classes.Listeners;
using MPfm.Android.Classes.Navigation;
using MPfm.Android.Classes.Receivers;
using MPfm.Android.Classes.Services;
using MPfm.MVP.Bootstrap;
using MPfm.MVP.Messages;
using MPfm.MVP.Navigation;
using MPfm.MVP.Views;
using MPfm.Sound.AudioFiles;
using TinyMessenger;
using org.sessionsapp.android;
using DialogFragment = Android.App.DialogFragment;
using Fragment = Android.App.Fragment;
using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
namespace MPfm.Android
{
[Activity(MainLauncher = true, ScreenOrientation = ScreenOrientation.Sensor, Theme = "@style/MyAppTheme", ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation | ConfigChanges.ScreenSize)]
public class MainActivity : BaseActivity, IMobileOptionsMenuView, View.IOnTouchListener, ActionBar.IOnNavigationListener
{
private ITinyMessengerHub _messengerHub;
private AndroidNavigationManager _navigationManager;
private SplashFragment _splashFragment;
private LinearLayout _miniPlayer;
private List<KeyValuePair<MobileOptionsMenuType, string>> _options;
private ViewPager _viewPager;
private MainTabStatePagerAdapter _tabPagerAdapter;
private TextView _lblArtistName;
private TextView _lblAlbumTitle;
private TextView _lblSongTitle;
private SquareImageView _imageAlbum;
private ImageButton _btnPrevious;
private ImageButton _btnPlayPause;
private ImageButton _btnNext;
private bool _isPlaying;
private ArrayAdapter _spinnerAdapter;
//private IntentFilter _intentFilter;
//private WifiP2pManager _wifiManager;
//private WifiP2pManager.Channel _wifiChannel;
//private WifiDirectReceiver _wifiDirectReceiver;
//private ActionListener _actionListener;
//#if __ANDROID_16__
//private AndroidDiscoveryService _discoveryService;
//#endif
public BitmapCache BitmapCache { get; private set; }
protected override void OnCreate(Bundle bundle)
{
Console.WriteLine("MainActivity - OnCreate");
base.OnCreate(bundle);
RequestWindowFeature(WindowFeatures.ActionBar);
SetContentView(Resource.Layout.Main);
ActionBar.NavigationMode = ActionBarNavigationMode.List;
ActionBar.Title = "";
_spinnerAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.action_list, Resource.Layout.spinner_dropdown_item);
ActionBar.SetListNavigationCallbacks(_spinnerAdapter, this);
// Setup view pager
_viewPager = FindViewById<ViewPager>(Resource.Id.main_pager);
_viewPager.OffscreenPageLimit = 4;
_tabPagerAdapter = new MainTabStatePagerAdapter(FragmentManager, _viewPager);
_viewPager.Adapter = _tabPagerAdapter;
_viewPager.SetOnPageChangeListener(_tabPagerAdapter);
// Setup mini player
_miniPlayer = FindViewById<LinearLayout>(Resource.Id.main_miniplayer);
_lblArtistName = FindViewById<TextView>(Resource.Id.main_miniplayer_lblArtistName);
_lblAlbumTitle = FindViewById<TextView>(Resource.Id.main_miniplayer_lblAlbumTitle);
_lblSongTitle = FindViewById<TextView>(Resource.Id.main_miniplayer_lblSongTitle);
_btnPrevious = FindViewById<ImageButton>(Resource.Id.main_miniplayer_btnPrevious);
_btnPlayPause = FindViewById<ImageButton>(Resource.Id.main_miniplayer_btnPlayPause);
_btnNext = FindViewById<ImageButton>(Resource.Id.main_miniplayer_btnNext);
_imageAlbum = FindViewById<SquareImageView>(Resource.Id.main_miniplayer_imageAlbum);
_miniPlayer.Visibility = ViewStates.Gone;
_miniPlayer.Click += (sender, args) => {
//Console.WriteLine("MainActivity - Mini player click - Showing player view...");
_messengerHub.PublishAsync<MobileNavigationManagerCommandMessage>(new MobileNavigationManagerCommandMessage(this, MobileNavigationManagerCommandMessageType.ShowPlayerView));
};
_btnPrevious.SetOnTouchListener(this);
_btnPlayPause.SetOnTouchListener(this);
_btnNext.SetOnTouchListener(this);
_btnPrevious.Click += BtnPreviousOnClick;
_btnPlayPause.Click += BtnPlayPauseOnClick;
_btnNext.Click += BtnNextOnClick;
// Get screen size
Point size = new Point();
WindowManager.DefaultDisplay.GetSize(size);
// Create bitmap cache
int maxMemory = (int)(Runtime.GetRuntime().MaxMemory() / 1024);
int cacheSize = maxMemory / 16;
BitmapCache = new BitmapCache(this, cacheSize, size.X / 6, size.X / 6);
// Listen to player changes to show/hide the mini player
_messengerHub = Bootstrapper.GetContainer().Resolve<ITinyMessengerHub>();
_messengerHub.Subscribe<PlayerPlaylistIndexChangedMessage>((message) => RunOnUiThread(() => {
// Make sure the UI is available
if (_lblArtistName != null && message.Data.AudioFileStarted != null)
{
_lblArtistName.Text = message.Data.AudioFileStarted.ArtistName;
_lblAlbumTitle.Text = message.Data.AudioFileStarted.AlbumTitle;
_lblSongTitle.Text = message.Data.AudioFileStarted.Title;
Task.Factory.StartNew(() => {
string key = message.Data.AudioFileStarted.ArtistName + "_" + message.Data.AudioFileStarted.AlbumTitle;
//Console.WriteLine("MainActivity - Player Bar - key: {0}", key);
if (_imageAlbum.Tag == null || _imageAlbum.Tag.ToString().ToUpper() != key.ToUpper())
{
//Console.WriteLine("MainActivity - Player Bar - key: {0} is different than tag {1} - Fetching album art...", key, (_imageAlbum.Tag == null) ? "null" : _imageAlbum.Tag.ToString());
_imageAlbum.Tag = key;
byte[] bytesImage = AudioFile.ExtractImageByteArrayForAudioFile(message.Data.AudioFileStarted.FilePath);
if (bytesImage.Length == 0)
_imageAlbum.SetImageBitmap(null);
else
BitmapCache.LoadBitmapFromByteArray(bytesImage, key, _imageAlbum);
}
});
}
}));
_messengerHub.Subscribe<PlayerStatusMessage>((message) => {
bool hasStartedPlaying = !_isPlaying && message.Status == PlayerStatusType.Playing;
_isPlaying = message.Status == PlayerStatusType.Playing;
//Console.WriteLine("MainActivity - PlayerStatusMessage - Status=" + message.Status.ToString());
RunOnUiThread(() => {
if (message.Status == PlayerStatusType.Stopped || message.Status == PlayerStatusType.Initialized)
{
Animation anim = AnimationUtils.LoadAnimation(this, Resource.Animation.slide_out_right);
anim.AnimationEnd += (sender, args) => {
_miniPlayer.Visibility = ViewStates.Gone;
};
_miniPlayer.StartAnimation(anim);
}
if(hasStartedPlaying)
{
_miniPlayer.Visibility = ViewStates.Visible;
Animation anim = AnimationUtils.LoadAnimation(this, Resource.Animation.slide_in_left);
_miniPlayer.StartAnimation(anim);
}
switch (message.Status)
{
case PlayerStatusType.Playing:
_btnPlayPause.SetImageResource(Resource.Drawable.player_pause);
break;
default:
_btnPlayPause.SetImageResource(Resource.Drawable.player_play);
break;
}
});
});
//#if __ANDROID_16__
// if (((int)global::Android.OS.Build.VERSION.SdkInt) >= 16) {
// _discoveryService = new AndroidDiscoveryService();
// _discoveryService.StartDiscovery();
// }
//#endif
Console.WriteLine("MainActivity - OnCreate - Starting navigation manager...");
_navigationManager = (AndroidNavigationManager) Bootstrapper.GetContainer().Resolve<MobileNavigationManager>();
_navigationManager.MainActivity = this; // TODO: Is this OK? Shouldn't the reference be cleared when MainActivity is destroyed? Can lead to memory leaks.
_navigationManager.BindOptionsMenuView(this);
_navigationManager.Start();
}
//private void SetupWifiDirect()
//{
// _intentFilter = new IntentFilter();
// _intentFilter.AddAction(WifiP2pManager.WifiP2pStateChangedAction);
// _intentFilter.AddAction(WifiP2pManager.WifiP2pPeersChangedAction);
// _intentFilter.AddAction(WifiP2pManager.WifiP2pConnectionChangedAction);
// _intentFilter.AddAction(WifiP2pManager.WifiP2pThisDeviceChangedAction);
// _actionListener = new ActionListener();
// _wifiManager = (WifiP2pManager) GetSystemService(Context.WifiP2pService);
// _wifiChannel = _wifiManager.Initialize(this, MainLooper, null);
// _wifiDirectReceiver = new WifiDirectReceiver();
// RegisterReceiver(_wifiDirectReceiver, _intentFilter);
// _wifiManager.DiscoverPeers(_wifiChannel, _actionListener);
//}
public bool OnNavigationItemSelected(int itemPosition, long itemId)
{
return true;
}
public void AddTab(MobileNavigationTabType type, string title, Fragment fragment)
{
//Console.WriteLine("MainActivity - Adding tab {0}", title);
_tabPagerAdapter.SetFragment(type, fragment);
_tabPagerAdapter.NotifyDataSetChanged();
}
public void PushTabView(MobileNavigationTabType type, Fragment fragment)
{
//Console.WriteLine("MainActivity - PushTabView type: {0} fragment: {1} fragmentCount: {2}", type.ToString(), fragment.GetType().FullName, FragmentManager.BackStackEntryCount);
_tabPagerAdapter.SetFragment(type, fragment);
}
public void PushDialogView(string viewTitle, IBaseView sourceView, IBaseView view)
{
//Console.WriteLine("MainActivity - PushDialogView view: {0} fragmentCount: {1}", view.GetType().FullName, FragmentManager.BackStackEntryCount);
var sourceFragment = (Fragment) sourceView;
var dialogFragment = (DialogFragment)view;
dialogFragment.Show(sourceFragment.Activity.FragmentManager, viewTitle);
}
public void PushDialogSubview(string parentViewTitle, IBaseView view)
{
//Console.WriteLine("MainActivity - PushDialogSubview parentViewTitle: {0} view: {1}", parentViewTitle, view.GetType().FullName);
}
public void PushPlayerSubview(IPlayerView playerView, IBaseView view)
{
//Console.WriteLine("MainActivity - PushPlayerSubview - view: {0}", view.GetType().FullName);
var activity = (PlayerActivity)playerView;
activity.AddSubview(view);
}
public void PushPreferencesSubview(IPreferencesView preferencesView, IBaseView view)
{
//Console.WriteLine("MainActivity - PushPreferencesSubview - view: {0}", view.GetType().FullName);
var activity = (PreferencesActivity)preferencesView;
activity.AddSubview(view);
}
protected override void OnStart()
{
Console.WriteLine("MainActivity - OnStart");
base.OnStart();
}
protected override void OnRestart()
{
Console.WriteLine("MainActivity - OnRestart");
base.OnRestart();
}
protected override void OnPause()
{
Console.WriteLine("MainActivity - OnPause");
base.OnPause();
}
protected override void OnResume()
{
Console.WriteLine("MainActivity - OnResume");
base.OnResume();
}
protected override void OnStop()
{
Console.WriteLine("MainActivity - OnStop");
base.OnStop();
}
protected override void OnDestroy()
{
Console.WriteLine("MainActivity - OnDestroy");
base.OnDestroy();
}
public override void OnBackPressed()
{
var tabType = _tabPagerAdapter.GetCurrentTab();
if (_navigationManager.CanGoBackInMobileLibraryBrowserBackstack(tabType))
{
//Console.WriteLine("MainActivity - OnBackPressed - CanRemoveFragment");
_navigationManager.PopMobileLibraryBrowserBackstack(tabType);
}
else
{
//Console.WriteLine("MainActivity - OnBackPressed - CannotRemoveFragment");
base.OnBackPressed();
}
}
public override bool OnPrepareOptionsMenu(IMenu menu)
{
Console.WriteLine("MainActivity - OnPrepareOptionsMenu");
return true;
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
Console.WriteLine("MainActivity - OnCreateOptionsMenu");
//MenuInflater.Inflate(Resource.Menu.main_menu, menu);
//var menuItem = menu.Add(new Java.Lang.String("Test"));
//var menuItem2 = menu.Add(new Java.Lang.String("Test2"));
//var menuItem3 = menu.Add(new Java.Lang.String("Test3"));
foreach (var option in _options)
{
var menuItem = menu.Add(new Java.Lang.String(option.Value));
switch (option.Key)
{
case MobileOptionsMenuType.About:
menuItem.SetIcon(Resource.Drawable.actionbar_info);
break;
case MobileOptionsMenuType.EqualizerPresets:
menuItem.SetShowAsAction(ShowAsAction.IfRoom);
menuItem.SetIcon(Resource.Drawable.actionbar_equalizer);
break;
case MobileOptionsMenuType.Preferences:
menuItem.SetShowAsAction(ShowAsAction.IfRoom);
menuItem.SetIcon(Resource.Drawable.actionbar_settings);
break;
case MobileOptionsMenuType.SyncLibrary:
menuItem.SetShowAsAction(ShowAsAction.IfRoom);
menuItem.SetIcon(Resource.Drawable.actionbar_sync);
break;
case MobileOptionsMenuType.SyncLibraryCloud:
menuItem.SetIcon(Resource.Drawable.actionbar_cloud);
break;
case MobileOptionsMenuType.SyncLibraryFileSharing:
menuItem.SetIcon(Resource.Drawable.actionbar_share);
break;
case MobileOptionsMenuType.SyncLibraryWebBrowser:
menuItem.SetIcon(Resource.Drawable.actionbar_earth);
break;
}
}
return true;
}
public override bool OnOptionsItemSelected(IMenuItem menuItem)
{
var option = _options.FirstOrDefault(x => x.Value == menuItem.TitleFormatted.ToString());
OnItemClick(option.Key);
return base.OnOptionsItemSelected(menuItem);
}
private void BtnPreviousOnClick(object sender, EventArgs eventArgs)
{
_messengerHub.PublishAsync<PlayerCommandMessage>(new PlayerCommandMessage(this, PlayerCommandMessageType.Previous));
}
private void BtnPlayPauseOnClick(object sender, EventArgs eventArgs)
{
_messengerHub.PublishAsync<PlayerCommandMessage>(new PlayerCommandMessage(this, PlayerCommandMessageType.PlayPause));
}
private void BtnNextOnClick(object sender, EventArgs eventArgs)
{
_messengerHub.PublishAsync<PlayerCommandMessage>(new PlayerCommandMessage(this, PlayerCommandMessageType.Next));
}
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
switch (v.Id)
{
case Resource.Id.main_miniplayer_btnPrevious:
_btnPrevious.SetImageResource(Resource.Drawable.player_previous_on);
break;
case Resource.Id.main_miniplayer_btnPlayPause:
if(_isPlaying)
_btnPlayPause.SetImageResource(Resource.Drawable.player_pause_on);
else
_btnPlayPause.SetImageResource(Resource.Drawable.player_play_on);
break;
case Resource.Id.main_miniplayer_btnNext:
_btnNext.SetImageResource(Resource.Drawable.player_next_on);
break;
}
break;
case MotionEventActions.Up:
switch (v.Id)
{
case Resource.Id.main_miniplayer_btnPrevious:
_btnPrevious.SetImageResource(Resource.Drawable.player_previous);
break;
case Resource.Id.main_miniplayer_btnPlayPause:
if (_isPlaying)
_btnPlayPause.SetImageResource(Resource.Drawable.player_pause);
else
_btnPlayPause.SetImageResource(Resource.Drawable.player_play);
break;
case Resource.Id.main_miniplayer_btnNext:
_btnNext.SetImageResource(Resource.Drawable.player_next);
break;
}
break;
}
return false;
}
public void ShowSplash(SplashFragment fragment)
{
//Console.WriteLine("MainActivity - ShowSplash");
_splashFragment = fragment;
_splashFragment.Show(FragmentManager, "Splash");
}
public void HideSplash()
{
//Console.WriteLine("MainActivity - HideSplash");
if(_splashFragment.Dialog != null)
_splashFragment.Dialog.Dismiss();
}
#region IMobileOptionsMenuView implementation
public Action<MobileOptionsMenuType> OnItemClick { get; set; }
public void RefreshMenu(List<KeyValuePair<MobileOptionsMenuType, string>> options)
{
Console.WriteLine("MainActivity - RefreshMenu");
_options = options;
}
#endregion
}
}