Skip to content

Commit

Permalink
Android: Added widget compatible with lock screen on Android 4.2+. Th…
Browse files Browse the repository at this point in the history
…e widget only shows "Loading" for now.

Related to issue #406.
  • Loading branch information
ycastonguay committed Jul 2, 2013
1 parent 5dc027d commit e69bac3
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 195 deletions.
79 changes: 79 additions & 0 deletions MPfm/MPfm.Android/Classes/Services/WidgetService.cs
@@ -0,0 +1,79 @@
// 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.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Views.Animations;
using MPfm.MVP.Bootstrap;
using MPfm.MVP.Messages;
using MPfm.MVP.Services.Interfaces;
using TinyMessenger;

namespace MPfm.Android.Classes.Services
{
[Service]
public class WidgetService : Service
{
private ITinyMessengerHub _messengerHub;
private IPlayerService _playerService;
private Context _context;

public override void OnStart(Intent intent, int startId)
{
Console.WriteLine("TestService - OnStart - startId: {0}", startId);
Initialize();
base.OnStart(intent, startId);
}

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Console.WriteLine("TestService - OnStartCommand - startId: {0}", startId);
Initialize();
return base.OnStartCommand(intent, flags, startId);
}

private void Initialize()
{
_messengerHub = Bootstrapper.GetContainer().Resolve<ITinyMessengerHub>();
_playerService = Bootstrapper.GetContainer().Resolve<IPlayerService>();
_messengerHub.Subscribe<PlayerPlaylistIndexChangedMessage>((message) =>
{
Console.WriteLine("WidgetService - PlayerPlaylistIndexChangedMessage");
});
_messengerHub.Subscribe<PlayerStatusMessage>((message) =>
{
Console.WriteLine("WidgetService - PlayerStatusMessage - Status=" + message.Status.ToString());
});

// Force updating the view for the first time
UpdateView();
}

private void UpdateView()
{
}

public override IBinder OnBind(Intent intent)
{
// We don't need to bind to this service
return null;
}
}
}
37 changes: 37 additions & 0 deletions MPfm/MPfm.Android/Classes/Widgets/PlayerWidget.cs
@@ -0,0 +1,37 @@
// 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.App;
using Android.Appwidget;
using Android.Content;
using MPfm.Android.Classes.Services;

namespace MPfm.Android.Classes.Widgets
{
[BroadcastReceiver(Label = "Sessions")]
[IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
[MetaData("android.appwidget.provider", Resource = "@xml/widget_player")]
public class PlayerWidget : AppWidgetProvider
{
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Console.WriteLine("PlayerWidget - OnUpdate");
context.StartService(new Intent(context, typeof (WidgetService)));
}
}
}
5 changes: 5 additions & 0 deletions MPfm/MPfm.Android/MPfm.Android.csproj
Expand Up @@ -89,6 +89,8 @@
<Compile Include="Classes\Navigation\AndroidNavigationManager.cs" />
<Compile Include="Classes\Objects\ApplicationState.cs" />
<Compile Include="Classes\Objects\GenericListItem.cs" />
<Compile Include="Classes\Services\WidgetService.cs" />
<Compile Include="Classes\Widgets\PlayerWidget.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down Expand Up @@ -160,6 +162,8 @@
<AndroidResource Include="Resources\Layout\Preferences.axml">
<SubType>Designer</SubType>
</AndroidResource>
<AndroidResource Include="Resources\Layout\WidgetPlayer.axml" />
<AndroidResource Include="Resources\Layout\WidgetPlayerLoading.axml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\Values\Strings.xml" />
Expand Down Expand Up @@ -236,6 +240,7 @@
<AndroidResource Include="Resources\animator\slide_in.xml" />
<AndroidResource Include="Resources\animator\slide_out.xml" />
<AndroidResource Include="Resources\animator\fade_in.xml" />
<AndroidResource Include="Resources\xml\widget_player.xml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Lib\armeabi\" />
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Android/Properties/AndroidManifest.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly">
<uses-sdk android:targetSdkVersion="15" />
<uses-sdk android:targetSdkVersion="17" />
<application android:label="Sessions" android:theme="@style/MyAppTheme" android:icon="@drawable/icon"></application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
16 changes: 16 additions & 0 deletions MPfm/MPfm.Android/Resources/Layout/WidgetPlayer.axml
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetPlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FF0000">
<TextView
android:id="@+id/widgetPlayer_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dip"
android:padding="10dip"
android:gravity="center"
android:text="Loading" />
</LinearLayout>
16 changes: 16 additions & 0 deletions MPfm/MPfm.Android/Resources/Layout/WidgetPlayerLoading.axml
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetPlayerLoading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#36454F">
<TextView
android:id="@+id/widgetPlayerLoading_Message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dip"
android:padding="10dip"
android:gravity="center"
android:text="Loading..." />
</LinearLayout>

0 comments on commit e69bac3

Please sign in to comment.