Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button to send text and clipboard #185

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using AndroidX.AppCompat.App;
using AndroidX.Core.App;
using Google.Android.Material.Card;
using Google.Android.Material.Dialog;
using Google.Android.Material.TextField;

namespace NearShare.Droid;

Expand All @@ -25,6 +27,10 @@ protected override void OnCreate(Bundle? savedInstanceState)
Button receiveButton = FindViewById<Button>(Resource.Id.receiveButton)!;
receiveButton.Click += ReceiveButton_Click;

FindViewById<MaterialCardView>(Resource.Id.sendFileCard)!.Click += SendButton_Click;
FindViewById<MaterialCardView>(Resource.Id.sendClipBoardCard)!.Click += SendClipBoard_Click;
FindViewById<MaterialCardView>(Resource.Id.sendTextCard)!.Click += SendText_Click;

FindViewById<MaterialCardView>(Resource.Id.enableBluetoothButton)!.Click += (_, _) => EnableBluetooth();
FindViewById<MaterialCardView>(Resource.Id.setupWindowButton)!.Click += (_, _) => UIHelper.OpenSetup(this);
FindViewById<MaterialCardView>(Resource.Id.openFAQButton)!.Click += (_, _) => UIHelper.OpenFAQ(this);
Expand All @@ -47,6 +53,67 @@ private void SendButton_Click(object? sender, System.EventArgs e)
);
}

private void SendClipBoard_Click(object? sender, EventArgs e)
{
try
{
var clipboard = (ClipboardManager?)GetSystemService(ClipboardService);
if (clipboard is null)
return;

var clip = clipboard.PrimaryClip;
if (clip is null)
return;

List<string> values = [];
for (int i = 0; i < clip.ItemCount; i++)
{
var value = clip.GetItemAt(0)?.CoerceToText(this);
if (value is not null)
values.Add(value);
}

if (values.Count == 0)
return;

Intent intent = new(this, typeof(SendActivity));
intent.SetAction(Intent.ActionSendMultiple);
intent.PutStringArrayListExtra(Intent.ExtraText, values);
StartActivity(intent);
}
catch (Exception ex)
{
this.ShowErrorDialog(ex);
}
}

private void SendText_Click(object? sender, EventArgs e)
{
try
{
TextInputEditText editText = new(this)
{
LayoutParameters = new(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
};
new MaterialAlertDialogBuilder(this)
.SetTitle(Resource.String.share_text)!
.SetView(editText)!
.SetPositiveButton(Resource.String.generic_send, (s, e) =>
{
Intent intent = new(this, typeof(SendActivity));
intent.SetAction(Intent.ActionSend);
intent.PutExtra(Intent.ExtraText, editText.Text);
StartActivity(intent);
})
.SetNegativeButton(Resource.String.generic_cancel, (s, e) => { })
.Show();
}
catch (Exception ex)
{
this.ShowErrorDialog(ex);
}
}

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent? data)
{
if (resultCode != Result.Ok || data == null)
Expand Down
53 changes: 53 additions & 0 deletions src/Resources/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,59 @@
app:icon="@drawable/ic_fluent_mail_inbox_24_regular" />
</LinearLayout>

<!-- Quick send actions -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">

<!-- Send File -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/sendFileCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="8dp"
style="?attr/materialCardViewFilledStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:src="@drawable/ic_fluent_document_24_regular"/>
</com.google.android.material.card.MaterialCardView>

<!-- Send Clipboard -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/sendClipBoardCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="8dp"
style="?attr/materialCardViewFilledStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:src="@drawable/ic_fluent_clipboard_24_regular"/>
</com.google.android.material.card.MaterialCardView>

<!-- Send Text -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/sendTextCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="8dp"
style="?attr/materialCardViewFilledStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:src="@drawable/ic_fluent_text_description_24_regular"/>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>

<!-- Enable Bluetooth -->
<com.google.android.material.card.MaterialCardView
style="?attr/materialCardViewFilledStyle"
Expand Down
10 changes: 10 additions & 0 deletions src/SendActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ void OnTransportUpgrade(object? sender, CdpTransportType transportType)

if (Intent.Action == Intent.ActionSendMultiple)
{
if (Intent.HasExtra(Intent.ExtraText))
{
return (
files: (Intent.GetStringArrayListExtra(Intent.ExtraText) ?? throw new InvalidDataException("Could not get extra files from intent"))
.Select(SendText)
.ToArray(),
null
);
}

return (
files: (Intent.GetParcelableArrayListExtra<AndroidUri>(Intent.ExtraStream) ?? throw new InvalidDataException("Could not get extra files from intent"))
.Select(ContentResolver!.CreateNearShareFileFromContentUri)
Expand Down
Loading