Skip to content

Commit

Permalink
Android: Trying to make the playlist list cells movable; there's no s…
Browse files Browse the repository at this point in the history
…tandard way in Android to do this. Thank you Google once again for a job well done. Apple = 2 lines of code. Android = Dude, this is rocket science, you should create a class based on ListView and do it yourself.

Fixed big notification/standard notification switch. Added rounded corners to all buttons.

Related to issue #406.
  • Loading branch information
ycastonguay committed Aug 31, 2013
1 parent 3ae0233 commit 5a45830
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 175 deletions.
13 changes: 11 additions & 2 deletions MPfm/MPfm.Android/Classes/Activities/PlaylistActivity.cs
Expand Up @@ -30,6 +30,7 @@
using MPfm.MVP.Views;
using MPfm.Sound.AudioFiles;
using MPfm.Sound.Playlists;
using org.sessionsapp.android;

namespace MPfm.Android
{
Expand All @@ -39,7 +40,7 @@ public class PlaylistActivity : BaseActivity, IPlaylistView
private MobileNavigationManager _navigationManager;
Button _btnNew;
Button _btnShuffle;
ListView _listView;
CustomListView _listView;
PlaylistListAdapter _listAdapter;
Playlist _playlist;

Expand All @@ -58,7 +59,7 @@ protected override void OnCreate(Bundle bundle)
_btnShuffle = FindViewById<Button>(Resource.Id.playlist_btnNew);
_btnShuffle.Click += BtnShuffleOnClick;

_listView = FindViewById<ListView>(Resource.Id.playlist_listView);
_listView = FindViewById<CustomListView>(Resource.Id.playlist_listView);
_listAdapter = new PlaylistListAdapter(this, _listView, new Playlist());
_listView.SetAdapter(_listAdapter);
_listView.ItemClick += ListViewOnItemClick;
Expand All @@ -68,6 +69,14 @@ protected override void OnCreate(Bundle bundle)
((AndroidNavigationManager)_navigationManager).SetPlaylistActivityInstance(this);
}

public override void OnAttachedToWindow()
{
Console.WriteLine("PlaylistActivity - OnAttachedToWindow");
var window = this.Window;
window.AddFlags(WindowManagerFlags.ShowWhenLocked);
window.SetWindowAnimations(0);
}

private void BtnNewOnClick(object sender, EventArgs eventArgs)
{
OnNewPlaylist();
Expand Down
34 changes: 31 additions & 3 deletions MPfm/MPfm.Android/Classes/Adapters/PlaylistListAdapter.cs
Expand Up @@ -25,21 +25,22 @@
using MPfm.Library.Objects;
using MPfm.Sound.AudioFiles;
using MPfm.Sound.Playlists;
using org.sessionsapp.android;

namespace MPfm.Android.Classes.Adapters
{
public class PlaylistListAdapter : BaseAdapter<AudioFile>, View.IOnClickListener
public class PlaylistListAdapter : BaseAdapter<AudioFile>, View.IOnClickListener, View.IOnTouchListener
{
readonly PlaylistActivity _context;
readonly ListView _listView;
readonly CustomListView _listView;
Playlist _playlist;
int _nowPlayingRowPosition;
int _editingRowPosition;
Guid _nowPlayingAudioFileId;

public bool IsEditingRow { get; private set; }

public PlaylistListAdapter(PlaylistActivity context, ListView listView, Playlist playlist)
public PlaylistListAdapter(PlaylistActivity context, CustomListView listView, Playlist playlist)
{
_context = context;
_listView = listView;
Expand Down Expand Up @@ -77,6 +78,9 @@ public override View GetView(int position, View convertView, ViewGroup parent)
if (item == null)
return view;

view.Tag = position;
view.SetOnTouchListener(this);

var index = view.FindViewById<TextView>(Resource.Id.playlistCell_lblIndex);
var title = view.FindViewById<TextView>(Resource.Id.playlistCell_lblTitle);
var subtitle = view.FindViewById<TextView>(Resource.Id.playlistCell_lblSubtitle);
Expand Down Expand Up @@ -207,5 +211,29 @@ public void OnClick(View v)
break;
}
}

public bool OnTouch(View v, MotionEvent e)
{
Console.WriteLine("PlaylistListAdapter - OnTouch - action: {0} buttonState: {1} downTime: {2} eventTime: {3} x: {4} y: {5}", e.Action, e.ButtonState, e.DownTime, e.EventTime, e.GetX(), e.GetY());

float x = e.GetX();
float y = e.GetY();

// Keep cancel on top because the flag can contain both move and cancel.
if (e.Action.HasFlag(MotionEventActions.Cancel))
{
Console.WriteLine("PlaylistListAdapter - OnTouch - Cancel - (x,y): ({0},{1})", x, y);
_listView.IsScrollable = true;
}
else if (e.Action.HasFlag(MotionEventActions.Move))
{
Console.WriteLine("PlaylistListAdapter - OnTouch - Move - (x,y): ({0},{1})", x, y);
_listView.IsScrollable = false;
}

//Console.WriteLine("PlaylistListAdapter - OnTouch - Current cell - height: {0} - position: {1}", v.Height, (int)v.Tag);

return true;
}
}
}
70 changes: 70 additions & 0 deletions MPfm/MPfm.Android/Classes/Controls/CustomListView.cs
@@ -0,0 +1,70 @@
// 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 Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace org.sessionsapp.android
{
/// <summary>
/// This custom list view is based on the Android list view but adds a few things such as the IsScrollable property and the ability to change row order
/// (because Google is clearly too lazy to do anything like that).
/// </summary>
public class CustomListView : ListView
{
public bool IsScrollable { get; set; }

protected CustomListView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
Initialize();
}

public CustomListView(Context context) : base(context)
{
Initialize();
}

public CustomListView(Context context, IAttributeSet attrs) : base(context, attrs)
{
Initialize();
}

public CustomListView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
Initialize();
}

private void Initialize()
{
Console.WriteLine("CustomListView - Initialize");
IsScrollable = true;
}

public override bool DispatchTouchEvent(Android.Views.MotionEvent e)
{
// Cancel scrolling
if(!IsScrollable && e.Action.HasFlag(MotionEventActions.Move))
return true;

return base.DispatchTouchEvent(e);
}
}
}
20 changes: 7 additions & 13 deletions MPfm/MPfm.Android/Classes/Services/NotificationService.cs
Expand Up @@ -92,7 +92,6 @@ public override StartCommandResult OnStartCommand(Intent intent, StartCommandFla
var notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService);
notificationManager.Cancel(1);
StopSelf();

}

return StartCommandResult.NotSticky;
Expand Down Expand Up @@ -149,27 +148,22 @@ private Notification CreateNotificationView()
.SetSmallIcon(Resource.Drawable.Icon)
.Build();

// Use the big notification style for Android 4.1+; use the standard notification style for Android 4.0.3
// Use the big notification style for Android 4.1+;
//#if __ANDROID_16__
if (((int) global::Android.OS.Build.VERSION.SdkInt) >= 16)
{
Console.WriteLine("NotificationService - Android 4.1+ detected; using Big View style for notification");
Console.WriteLine("NotificationService - Android 4.1+ detected; adding Big View style for notification");
RemoteViews viewBigNotificationPlayer = new RemoteViews(ApplicationContext.PackageName, Resource.Layout.BigNotificationPlayer);
viewBigNotificationPlayer.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, "Hello World!");
notification.BigContentView = viewBigNotificationPlayer;
}
//#else
else
{
Console.WriteLine("NotificationService - Android 4.0.3+ (<4.1) detected; using standard style for notification");
RemoteViews viewNotificationPlayer = new RemoteViews(ApplicationContext.PackageName, Resource.Layout.NotificationPlayer);
viewNotificationPlayer.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, "Hello World!");
notification.ContentView = viewNotificationPlayer;
}
//#endif

Console.WriteLine("NotificationService - Android 4.0.3+ detected; adding standard style for notification");
RemoteViews viewNotificationPlayer = new RemoteViews(ApplicationContext.PackageName, Resource.Layout.NotificationPlayer);
notification.ContentView = viewNotificationPlayer;

_notification = notification;

// Create intents for buttons
Intent intentPlayPause = new Intent(this, typeof(NotificationService));
intentPlayPause.SetAction(SessionsWidgetActions.SessionsWidgetPlayPause.ToString());
PendingIntent pendingIntentPlayPause = PendingIntent.GetService(this, 0, intentPlayPause, PendingIntentFlags.UpdateCurrent);
Expand Down
1 change: 1 addition & 0 deletions MPfm/MPfm.Android/MPfm.Android.csproj
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Classes\Cache\BitmapWorkerTask.cs" />
<Compile Include="Classes\Cache\AsyncBitmapDrawable.cs" />
<Compile Include="Classes\Clients\MyWebViewClient.cs" />
<Compile Include="Classes\Controls\CustomListView.cs" />
<Compile Include="Classes\Controls\SquareImageView.cs" />
<Compile Include="Classes\Controls\EqualizerPresetGraphView.cs" />
<Compile Include="Classes\Controls\OutputMeterView.cs" />
Expand Down
Expand Up @@ -2,7 +2,7 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bigNotificationPlayer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="100dp"
android:background="@color/background">
<ImageButton
android:id="@+id/bigNotificationPlayer_btnClose"
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Android/Resources/Layout/Playlist.axml
Expand Up @@ -4,7 +4,7 @@
android:layout_height="fill_parent"
android:background="@color/background"
android:orientation="vertical">
<ListView
<org.sessionsapp.android.CustomListView
android:id="@+id/playlist_listView"
android:layout_width="fill_parent"
android:layout_height="0dp"
Expand Down
7 changes: 7 additions & 0 deletions MPfm/MPfm.Android/Resources/Layout/PlaylistCell.axml
Expand Up @@ -38,6 +38,13 @@
android:textColor="@color/list_secondarytext"
android:textSize="12dp" />
</LinearLayout>
<ImageView
android:id="@+id/playlistCell_imageMove"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="6dp"
android:background="@android:color/transparent"
android:src="@drawable/icon_trash" />
<ImageView
android:id="@+id/playlistCell_imagePlay"
android:layout_width="48dp"
Expand Down

0 comments on commit 5a45830

Please sign in to comment.