Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jfversluis committed Jan 26, 2024
2 parents fb2b0ec + 1ebb08e commit 6b4a1ca
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 24 deletions.
13 changes: 11 additions & 2 deletions samples/ScreenRecordingSample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@
<Switch x:Name="saveToGallery" VerticalOptions="Center" />
<Label Text="Save to gallery (iOS/macOS only)" VerticalOptions="Center" />
</HorizontalStackLayout>
</VerticalStackLayout>
<HorizontalStackLayout Spacing="5">
<Switch x:Name="setCustomNotification" VerticalOptions="Center" Toggled="OnToggled"/>
<Label Text="Set custom text for notification (Android only)" VerticalOptions="Center" />
</HorizontalStackLayout>
</VerticalStackLayout>

<Button Text="Start Recording" Clicked="StartRecordingClicked" x:Name="btnStart" />
<VerticalStackLayout>
<Entry x:Name="ContentTitle" Text="Screen Recording" Placeholder="Enter the notification content title" IsVisible="false"/>
<Entry x:Name="ContentText" Text="Recording screen..." Placeholder="Enter the notification content text" IsVisible="false"/>
</VerticalStackLayout>

<Button Text="Start Recording" Clicked="StartRecordingClicked" x:Name="btnStart" />
<Button Text="Stop Recording" Clicked="StopRecordingClicked" x:Name="btnStop" />
</VerticalStackLayout>
</ScrollView>
Expand Down
29 changes: 24 additions & 5 deletions samples/ScreenRecordingSample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,24 @@ async void StartRecordingClicked(object sender, EventArgs e)

btnStart.IsEnabled = false;
btnStop.IsEnabled = true;
screenRecording.StartRecording(new()
{
EnableMicrophone = recordMicrophone.IsToggled,
SaveToGallery = saveToGallery.IsToggled,
});
if (setCustomNotification.IsToggled)
{
screenRecording.StartRecording(new()
{
EnableMicrophone = recordMicrophone.IsToggled,
SaveToGallery = saveToGallery.IsToggled,
NotificationContentTitle = ContentTitle.Text,
NotificationContentText = ContentText.Text
});
}
else
{
screenRecording.StartRecording(new()
{
EnableMicrophone = recordMicrophone.IsToggled,
SaveToGallery = saveToGallery.IsToggled
});
}
}

async void StopRecordingClicked(object sender, EventArgs e)
Expand All @@ -51,4 +64,10 @@ async void StopRecordingClicked(object sender, EventArgs e)
btnStart.IsEnabled = true;
btnStop.IsEnabled = false;
}

void OnToggled(object sender, ToggledEventArgs e)
{
ContentTitle.IsVisible = e.Value;
ContentText.IsVisible = e.Value;
}
}
44 changes: 30 additions & 14 deletions src/Plugin.Maui.ScreenRecording/ScreenRecording.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public partial class ScreenRecordingImplementation : MediaProjection.Callback, I
string? filePath;
bool enableMicrophone;

string NotificationContentTitle { get; set; } =
ScreenRecordingOptions.defaultAndroidNotificationTitle;

string NotificationContentText { get; set; } =
ScreenRecordingOptions.defaultAndroidNotificationText;

MediaProjectionManager? ProjectionManager { get; set; }
MediaProjection? MediaProjection { get; set; }
VirtualDisplay? VirtualDisplay { get; set; }
Expand All @@ -29,27 +35,35 @@ public ScreenRecordingImplementation()

public void StartRecording(ScreenRecordingOptions? options = null)
{
if (IsSupported)
if (!IsSupported)
{
enableMicrophone = options?.EnableMicrophone ?? false;
throw new NotSupportedException("Screen recording not supported on this device.");
}

enableMicrophone = options?.EnableMicrophone ?? false;

var saveOptions = options ?? new();
var savePath = saveOptions.SavePath;
if (!string.IsNullOrWhiteSpace(options?.NotificationContentTitle))
{
NotificationContentTitle = options.NotificationContentTitle;
}

if (string.IsNullOrWhiteSpace(savePath))
{
savePath = Path.Combine(Path.GetTempPath(),
$"screenrecording_{DateTime.Now:ddMMyyyy_HHmmss}.mp4");
}
if (!string.IsNullOrWhiteSpace(options?.NotificationContentText))
{
NotificationContentText = options.NotificationContentText;
}

filePath = savePath;
var saveOptions = options ?? new();
var savePath = saveOptions.SavePath;

Setup();
}
else
if (string.IsNullOrWhiteSpace(savePath))
{
throw new NotSupportedException("Screen recording not supported on this device.");
savePath = Path.Combine(Path.GetTempPath(),
$"screenrecording_{DateTime.Now:ddMMyyyy_HHmmss}.mp4");
}

filePath = savePath;

Setup();
}

public Task<ScreenRecordingFile?> StopRecording()
Expand Down Expand Up @@ -96,6 +110,8 @@ public void StartRecording(ScreenRecordingOptions? options = null)
internal async void OnScreenCapturePermissionGranted(int resultCode, Intent? data)
{
Intent intent = new(Application.Context, typeof(ScreenRecordingService));
intent.PutExtra("ContentTitle", NotificationContentTitle);
intent.PutExtra("ContentText", NotificationContentText);

// Android O
if (OperatingSystem.IsAndroidVersionAtLeast(26))
Expand Down
17 changes: 17 additions & 0 deletions src/Plugin.Maui.ScreenRecording/ScreenRecordingOptions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

public class ScreenRecordingOptions
{
internal const string defaultAndroidNotificationTitle = "Screen recording in progress...";
internal const string defaultAndroidNotificationText = "A screen recording is currently in progress, be careful with any sensitive information.";

/// <summary>
/// Gets or sets the path to save the recording to.
/// The default is the device's temporary folder with a timestamped file,
Expand All @@ -21,4 +24,18 @@ public class ScreenRecordingOptions
/// Default value is <see langword="false"/>.
/// </summary>
public bool EnableMicrophone { get; set; }

/// <summary>
/// Gets or sets the notification content title.
/// Default value is "Screen recording in progress...".
/// </summary>
/// <remarks>This property only has effect on Android. On other platforms no notification is shown when a screen recording is being made.</remarks>
public string NotificationContentTitle { get; set; } = defaultAndroidNotificationTitle;

/// <summary>
/// Gets or sets the notification content text.
/// Default value is "A screen recording is currently in progress, be careful with any sensitive information.".
/// </summary>
/// <remarks>This property only has effect on Android. On other platforms no notification is shown when a screen recording is being made.</remarks>
public string NotificationContentText { get; set; } = defaultAndroidNotificationText;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ public override StartCommandResult OnStartCommand(Intent? intent, StartCommandFl

notificationManager.CreateNotificationChannel(channel);

// TODO we probably want to find a way for people to set these values themselves for localization for instance
string contentTitle = intent?.GetStringExtra("ContentTitle") ??
ScreenRecordingOptions.defaultAndroidNotificationTitle;

string contentText = intent?.GetStringExtra("ContentText") ??
ScreenRecordingOptions.defaultAndroidNotificationText;

// Create a notification for the foreground service
var notificationBuilder = new Notification.Builder(this, CHANNEL_ID)
.SetContentTitle("Screen Recording")
.SetContentText("Recording screen...")
.SetContentTitle(contentTitle)
.SetContentText(contentText)
.SetSmallIcon(Resource.Drawable.notification_template_icon_low_bg); // Ensure you have 'ic_notification' in Resources/drawable

foregroundNotification = notificationBuilder.Build();
Expand Down

0 comments on commit 6b4a1ca

Please sign in to comment.