Skip to content

Commit

Permalink
Android: Added connection change receiver.
Browse files Browse the repository at this point in the history
SyncDeviceSpecifications: Added event and method for connection change notifications.
SyncListenerServer: Now stopping the HTTP service when wifi is unavailable; starting the service again when wifi returns.

Related to issue #406.
  • Loading branch information
ycastonguay committed Jul 21, 2013
1 parent bffddb9 commit 4945faa
Show file tree
Hide file tree
Showing 11 changed files with 787 additions and 612 deletions.
17 changes: 16 additions & 1 deletion MPfm/MPfm.Android/Classes/Application.cs
Expand Up @@ -22,6 +22,7 @@
using Android.Runtime;
using MPfm.Android.Classes.Fragments;
using MPfm.Android.Classes.Navigation;
using MPfm.Android.Classes.Receivers;
using MPfm.Library;
using MPfm.MVP.Bootstrap;
using MPfm.MVP.Navigation;
Expand All @@ -37,7 +38,8 @@ namespace MPfm.Android.Classes
#endif
public class MPfmApplication : Application
{
private static Context _context;
static Context _context;
ConnectionChangeReceiver _connectionChangeReceiver;

public MPfmApplication(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
Expand Down Expand Up @@ -80,6 +82,19 @@ public override void OnCreate()
// Set player plugin directory path
ApplicationInfo appInfo = PackageManager.GetApplicationInfo(PackageName, 0);
Player.Player.PluginDirectoryPath = appInfo.NativeLibraryDir;

try
{
// Setup connection change receiver
_connectionChangeReceiver = new ConnectionChangeReceiver();
IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
filter.AddCategory(Intent.CategoryDefault);
RegisterReceiver(_connectionChangeReceiver, filter);
}
catch (Exception ex)
{
Console.WriteLine("Application - Error: Failed to setup connection change receiver! {0}", ex);
}
}

public override void OnTerminate()
Expand Down
Expand Up @@ -35,6 +35,8 @@ public class AndroidSyncDeviceSpecifications : ISyncDeviceSpecifications
{
private readonly Context _context;

public event NetworkStateChanged OnNetworkStateChanged;

public AndroidSyncDeviceSpecifications()
{
_context = MPfmApplication.GetApplicationContext();
Expand Down Expand Up @@ -73,5 +75,11 @@ public string GetMusicFolderPath()
{
return global::Android.OS.Environment.GetExternalStoragePublicDirectory(global::Android.OS.Environment.DirectoryMusic).ToString();
}

public void ReportNetworkStateChange(NetworkState networkState)
{
if(OnNetworkStateChanged != null)
OnNetworkStateChanged(networkState);
}
}
}
60 changes: 60 additions & 0 deletions MPfm/MPfm.Android/Classes/Receivers/ConnectionChangeReceiver.cs
@@ -0,0 +1,60 @@
// 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.Net;
using MPfm.Library;
using MPfm.Library.Objects;
using MPfm.MVP.Bootstrap;
using MPfm.MVP.Messages;
using TinyMessenger;

namespace MPfm.Android.Classes.Receivers
{
[BroadcastReceiver]
public class ConnectionChangeReceiver : BroadcastReceiver
{
readonly ITinyMessengerHub _messageHub;
readonly ISyncDeviceSpecifications _deviceSpecifications;

public ConnectionChangeReceiver()
{
_messageHub = Bootstrapper.GetContainer().Resolve<ITinyMessengerHub>();
_deviceSpecifications = Bootstrapper.GetContainer().Resolve<ISyncDeviceSpecifications>();
}

public override void OnReceive(Context context, Intent intent)
{
ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
NetworkInfo activeNetInfo = connectivityManager.ActiveNetworkInfo;
NetworkInfo wifiNetInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
NetworkInfo mobileNetInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);
var networkState = new NetworkState() {
IsNetworkAvailable = (activeNetInfo != null) && activeNetInfo.IsAvailable,
IsWifiAvailable = (wifiNetInfo != null) && wifiNetInfo.IsAvailable,
IsCellularAvailable = (mobileNetInfo != null) && mobileNetInfo.IsAvailable
};

Console.WriteLine("ConnectionChangeReceiver - active: {0} - wifi: {1} - mobile: {2}", networkState.IsNetworkAvailable, networkState.IsWifiAvailable, networkState.IsCellularAvailable);
_deviceSpecifications.ReportNetworkStateChange(networkState);
_messageHub.PublishAsync<ConnectionStatusChangedMessage>(new ConnectionStatusChangedMessage(this) {
NetworkState = networkState
});
}
}
}
1 change: 1 addition & 0 deletions MPfm/MPfm.Android/MPfm.Android.csproj
Expand Up @@ -105,6 +105,7 @@
<Compile Include="Classes\Navigation\AndroidNavigationManager.cs" />
<Compile Include="Classes\Objects\ApplicationState.cs" />
<Compile Include="Classes\Objects\GenericListItem.cs" />
<Compile Include="Classes\Receivers\ConnectionChangeReceiver.cs" />
<Compile Include="Classes\Services\WidgetService.cs" />
<Compile Include="Classes\Widgets\PlayerWidget.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions MPfm/MPfm.Android/Properties/AndroidManifest.xml
Expand Up @@ -4,4 +4,6 @@
<application android:label="Sessions" android:theme="@style/MyAppTheme" android:icon="@drawable/icon"></application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
5 changes: 5 additions & 0 deletions MPfm/MPfm.Library/ISyncDeviceSpecifications.cs
Expand Up @@ -20,12 +20,17 @@

namespace MPfm.Library
{
public delegate void NetworkStateChanged(NetworkState networkState);

public interface ISyncDeviceSpecifications
{
event NetworkStateChanged OnNetworkStateChanged;

SyncDeviceType GetDeviceType();
string GetDeviceName();
long GetFreeSpace();
string GetIPAddress();
string GetMusicFolderPath();
void ReportNetworkStateChange(NetworkState networkState);
}
}
1 change: 1 addition & 0 deletions MPfm/MPfm.Library/MPfm.Library.Android.csproj
Expand Up @@ -152,6 +152,7 @@
<Compile Include="Database\Interfaces\ISQLiteGateway.cs" />
<Compile Include="Database\DatabaseFacade.cs" />
<Compile Include="ILibrary.cs" />
<Compile Include="Objects\NetworkState.cs" />
<Compile Include="Objects\PlaylistFile.cs" />
<Compile Include="Objects\History.cs" />
<Compile Include="Objects\Setting.cs" />
Expand Down
33 changes: 33 additions & 0 deletions MPfm/MPfm.Library/Objects/NetworkState.cs
@@ -0,0 +1,33 @@
// 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;

namespace MPfm.Library.Objects
{
/// <summary>
/// Data structure repesenting network state and information.
/// </summary>
public class NetworkState
{
public bool IsNetworkAvailable { get; set; }
public bool IsWifiAvailable { get; set; }
public bool IsCellularAvailable { get; set; }
public string WifiName { get; set; }
}
}

0 comments on commit 4945faa

Please sign in to comment.