~phroa/phone-app

a6987c9734067eb7193ff7ca88aa326022faf5b6 — Jack Stratton a month ago 61842a2
Push texts to DB!
4 files changed, 91 insertions(+), 15 deletions(-)

A phms/FileReceiver.cs
M phms/MainActivity.cs
M phms/SmsReceiver.cs
M phms/phms.csproj
A phms/FileReceiver.cs => phms/FileReceiver.cs +42 -0
@@ 0,0 1,42 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Net;
using Android.OS;
using Android.Support.V7.App;

namespace phms
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme")]
    public class FileReceiver : AppCompatActivity
    {
        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (Intent == null)
            {
                return;
            }

            switch (Intent.Action)
            {
                case Intent.ActionSend:
                    Upload(new List<Uri> {(Intent.GetParcelableExtra(Intent.ExtraStream) as Uri)!});
                    break;
                case Intent.ActionSendMultiple:
                    Upload(Intent.GetParcelableArrayListExtra(Intent.ExtraStream)!.OfType<Uri>());
                    break;
            }
        }

        private void Upload(IEnumerable<Uri> files)
        {
            throw new System.NotImplementedException();
        }
    }
}
\ No newline at end of file

M phms/MainActivity.cs => phms/MainActivity.cs +6 -12
@@ 30,6 30,7 @@ namespace phms
            var databaseUri = FindViewById<EditText>(R.Id.databaseUri) ??
                              throw new Exception("Couldn't FindViewById.");
            databaseUri.Text = prefs.GetString(DatabaseUriPref, "");
            Database.DatabaseUri = databaseUri.Text!;
            databaseUri.AfterTextChanged += (sender, e) =>
            {
                using var prefsEditor = prefs.Edit() ??


@@ 37,6 38,7 @@ namespace phms
                var newValue = databaseUri.Text;
                prefsEditor.PutString(DatabaseUriPref, newValue);
                prefsEditor.Apply();
                Database.DatabaseUri = databaseUri.Text!;
            };

            var connect = FindViewById<Button>(R.Id.connect) ??


@@ 45,25 47,17 @@ namespace phms
            {
                try
                {
                    using var conn = new NpgsqlConnection(databaseUri.Text);
                    using var conn = Database.Connection;
                    conn.Open();
                    using var cmd = new NpgsqlCommand("SHOW server_version", conn);
                    var result = cmd.ExecuteScalar();
                    SendNotification($"Success, {result}");
                    Notes.Send($"Success, {result}");
                }
                catch
                catch (Exception ex)
                {
                    SendNotification("Failed!");
                    Notes.Send($"Failed! {ex}");
                }
            };
        }

        private void SendNotification(string message)
        {
            var intent = new Intent(this, typeof(BackgroundService));
            intent.SetAction(BackgroundService.UpdateNotificationAction);
            intent.PutExtra("message", message);
            StartService(intent);
        }
    }
}
\ No newline at end of file

M phms/SmsReceiver.cs => phms/SmsReceiver.cs +40 -2
@@ 1,11 1,13 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

using System;
using System.Collections.Generic;
using Android;
using Android.App;
using Android.Content;
using Android.Provider;
using Android.Util;
using Npgsql;

namespace phms
{


@@ 14,14 16,50 @@ namespace phms
    // ReSharper disable once UnusedType.Global
    public class SmsReceiver : BroadcastReceiver
    {
        private const string INSERT_STATEMENT = @"insert into sms (""status"",
                 ""email_from"",
                 ""email_body"",
                 ""originating_address"",
                 ""display_originating_address"",
                 ""message_body"",
                 ""display_message_body"",
                 ""time_server"",
                 ""time_stored"")
values (@status,
        @email_from,
        @email_body,
        @originating_address,
        @display_originating_address,
        @message_body,
        @display_message_body,
        @time_server,
        @time_stored);";

        public override void OnReceive(Context? context, Intent? intent)
        {
            Log.Info("xxxxx", $"SMS Received {intent}");
            var messages = Telephony.Sms.Intents.GetMessagesFromIntent(intent);

            using var conn = Database.Connection;
            conn.Open();
            
            foreach (var m in messages)
            {
                Log.Info("xxxxx",
                    $"sms {m.OriginatingAddress} / {m.DisplayOriginatingAddress} / {m.MessageBody} / {m.DisplayMessageBody}");
                var serverTimestampMillis = m.TimestampMillis;
                var serverTimestamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
                    .AddMilliseconds(serverTimestampMillis);
                
                using var cmd = new NpgsqlCommand(INSERT_STATEMENT, conn);
                cmd.Parameters.AddWithValue("status", m.Status);
                cmd.Parameters.AddWithValue("email_from", m.EmailFrom ?? "");
                cmd.Parameters.AddWithValue("email_body", m.EmailBody ?? "");
                cmd.Parameters.AddWithValue("originating_address", m.OriginatingAddress ?? "");
                cmd.Parameters.AddWithValue("display_originating_address", m.DisplayOriginatingAddress ?? "");
                cmd.Parameters.AddWithValue("message_body", m.MessageBody ?? "");
                cmd.Parameters.AddWithValue("display_message_body", m.DisplayMessageBody ?? "");
                cmd.Parameters.AddWithValue("time_server", serverTimestamp);
                cmd.Parameters.AddWithValue("time_stored", DateTime.Now);
                cmd.ExecuteScalar();
            }
        }
    }

M phms/phms.csproj => phms/phms.csproj +3 -1
@@ 58,7 58,9 @@
    </ItemGroup>
    <ItemGroup>
        <Compile Include="BackgroundService.cs" />
        <Compile Include="Database.cs" />
        <Compile Include="MainActivity.cs" />
        <Compile Include="Notes.cs" />
        <Compile Include="Resources\Resource.Designer.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
        <Compile Include="SmsReceiver.cs" />


@@ 78,7 80,7 @@
        <AndroidResource Include="Resources\values\styles.xml" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="Npgsql" Version="6.0.0-preview5" />
        <PackageReference Include="Npgsql" Version="6.0.0-rc.1" />
        <PackageReference Include="Xamarin.Android.Support.Design" Version="28.0.0.3" />
    </ItemGroup>
    <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />