Example #1
0
        private async void InitService(Intent intent, StartCommandFlags flags, int startId)
        {
            try
            {
                Log.Debug(TAG, $"OnStartCommand called at {startTime}, flags={flags}, startid={startId}");
                if (isStarted)
                {
                    TimeSpan runtime = DateTime.UtcNow.Subtract(startTime);
                    Log.Debug(TAG, $"This service was already started, it's been running for {runtime:c}.");
                }
                else
                {
                    isStarted = true;
                    startTime = DateTime.UtcNow;
                    Log.Debug(TAG, $"Starting the service, at {startTime}.");
                    timer = new Timer(HandleTimerCallback, startTime, 0, TimerWait);

                    MessageReceiveHelper.ClearEventRegistrations();
                    MessageReceiveHelper.Activity += MessageReceiveHelper_Activity;
                    MessageReceiveHelper.Finish   += MessageReceiveHelper_Finish;
                    MessageReceiveHelper.Init(this);
                }

                lastActiveTime = DateTime.UtcNow;

                string dataJson = intent.GetStringExtra("Data");
                var    data     = JsonConvert.DeserializeObject <Dictionary <string, object> >(dataJson);
                await MessageReceiveHelper.ProcessReceivedMessage(data);
            }
            catch (Exception ex)
            {
                Log.Debug(TAG, "Unhandled exception occured: " + ex.ToString());
                StopSelf();
            }
        }
Example #2
0
        public override void OnDestroy()
        {
            timer.Dispose();
            timer     = null;
            isStarted = false;
            MessageReceiveHelper.ClearEventRegistrations();

            TimeSpan runtime = DateTime.UtcNow.Subtract(startTime);

            Log.Debug(TAG, $"Service destroyed at {DateTime.UtcNow} after running for {runtime:c}.");
            base.OnDestroy();
        }
        private async void InitService(Intent intent, StartCommandFlags flags, int startId)
        {
            try
            {
                Platform.FetchAuthCode -= Platform_FetchAuthCode;
                Platform.FetchAuthCode += Platform_FetchAuthCode;
            }
            catch { }

            try
            {
                Log.Debug(TAG, $"OnStartCommand called at {startTime}, flags={flags}, startid={startId}");
                if (isStarted)
                {
                    TimeSpan runtime = DateTime.UtcNow.Subtract(startTime);
                    Log.Debug(TAG, $"This service was already started, it's been running for {runtime:c}.");
                }
                else
                {
                    isStarted = true;
                    startTime = DateTime.UtcNow;
                    Log.Debug(TAG, $"Starting the service, at {startTime}.");
                    timer = new Timer(HandleTimerCallback, startTime, 0, TimerWait);

                    MessageReceiveHelper.ClearEventRegistrations();
                    MessageReceiveHelper.Activity += MessageReceiveHelper_Activity;
                    MessageReceiveHelper.Init(this);
                }

                lastActiveTime = DateTime.UtcNow;

                Log.Debug(TAG, $"InitMessageCarrierPackageManagerIfNecessary()");
                await InitMessageCarrierPackageManagerIfNecessary();

                Log.Debug(TAG, $"InitMessageCarrierPackageManagerIfNecessary() Finished.");

                if ((intent.HasExtra("Action")) && (intent.HasExtra("DeviceId")))
                {
                    if (intent.GetStringExtra("Action") == "SendCarrier")
                    {
                        Android.Util.Log.Debug(TAG, "2: SendCarrier");
                        SendCarrier(intent.GetStringExtra("DeviceId"));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Debug(TAG, "Unhandled exception occured: " + ex.ToString());
                StopSelf();
            }
        }
Example #4
0
        private void OpenFile(Android.Net.Uri file, string mimeType)
        {
            try
            {
                Intent openFile = new Intent(Intent.ActionView);
                openFile.SetDataAndType(file, mimeType);
                openFile.AddFlags(ActivityFlags.GrantReadUriPermission);

                StartActivity(openFile);
            }
            catch (Exception ex)
            {
                MessageReceiveHelper.ShowToast(this, "Cannot open file.", ToastLength.Long);
                Log.Debug(TAG, "Cannot open file: " + ex.ToString());
            }
        }
        public override async void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);

            try
            {
                if (!message.Data.ContainsKey("Action"))
                {
                    throw new InvalidOperationException();
                }
                else if (message.Data["Action"] == "Wake")
                {
#if DEBUG
                    SendNotification("Wake", "Wake");
#endif
                    var intent = new Intent(this, typeof(MessageCarrierService));
                    intent.PutExtra("Action", "Wake");
                    StartService(intent);
                }
                else if (message.Data["Action"] == "SendCarrier")
                {
#if DEBUG
                    SendNotification("SendCarrier", "SendCarrier");
#endif
                    var intent = new Intent(this, typeof(MessageCarrierService));
                    intent.PutExtra("Action", "SendCarrier");
                    intent.PutExtra("DeviceId", message.Data["WakerDeviceId"]);

                    Android.Util.Log.Debug("CARRIER_DEBUG", "1");

                    StartService(intent);
                }
                else if (message.Data["Action"] == "Payload")
                {
#if DEBUG
                    SendNotification("Payload", "Payload");
#endif
                    var intent = new Intent(this, typeof(WaiterService));
                    intent.PutExtra("Data", message.Data["Data"]);

                    StartService(intent);
                }
                else if ((message.Data["Action"] == "LaunchUrl") && (message.Data.ContainsKey("Url")))
                {
                    try
                    {
                        string url = message.Data["Url"];

                        Intent i = new Intent(Intent.ActionView);
                        i.SetData(Android.Net.Uri.Parse(url));
                        i.SetFlags(ActivityFlags.NewTask);
                        StartActivity(i);
                    }
                    catch (Exception ex)
                    {
                        Log.Debug(TAG, ex.Message);
                        MessageReceiveHelper.ShowToast(this, "Couldn't launch URL.", Android.Widget.ToastLength.Long);
                    }
                }
                else if ((message.Data["Action"] == "FastClipboard") && (message.Data.ContainsKey("SenderName")) && (message.Data.ContainsKey("Text")))
                {
                    string senderName = message.Data["SenderName"];
                    string text       = message.Data["Text"];

                    Guid guid = await TextReceiver.QuickTextReceivedAsync(senderName, text);

                    MessageReceiveHelper.CopyTextToClipboard(this, guid);
                }
                else if ((message.Data["Action"] == "CloudClipboard") && (message.Data.ContainsKey("Data")))
                {
                    string text = message.Data["Data"];

                    var settings = new Settings(this);

                    if (message.Data.ContainsKey("AccountId"))
                    {
                        if (CrossSecureStorage.Current.HasKey("RoamitAccountId"))
                        {
                            CrossSecureStorage.Current.DeleteKey("RoamitAccountId");
                        }

                        CrossSecureStorage.Current.SetValue("RoamitAccountId", message.Data["AccountId"]);
                    }

                    if (settings.CloudClipboardReceiveMode == CloudClipboardReceiveMode.Automatic)
                    {
                        CloudClipboardNotifier.SetCloudClipboardValue(this, text);
                    }
                    else
                    {
                        settings.CloudClipboardText = text;
                        CloudClipboardNotifier.SendCloudClipboardNotification(this, text);
                    }
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            catch (InvalidOperationException)
            {
                SendNotification($"Action '{message.Data["Action"]}' not supported.", "Please make sure the app is updated to enjoy latest features.");
            }
        }
        private async void SendCarrier(string deviceId)
        {
            RemoteSystem rs = null;

            Android.Util.Log.Debug(TAG, "3 " + deviceId);

            MessageReceiveHelper.InitProgressNotifier();

            //try finding remote system for 15 seconds
            for (int i = 0; i < 30; i++)
            {
                rs = Common.MessageCarrierPackageManager.RemoteSystems.FirstOrDefault(x => x.Id == deviceId);
                RemoteSystem rs2;
                try
                {
                    rs2 = Common.PackageManager.RemoteSystems.FirstOrDefault(x => x.Id == deviceId);
                }
                catch
                {
                }


                if (rs != null)
                {
                    break;
                }

                await Task.Delay(TimeSpan.FromSeconds(0.5));
            }

            Android.Util.Log.Debug(TAG, "4");

            if (rs == null)
            {
                System.Diagnostics.Debug.WriteLine($"Couldn't find device {deviceId}");
                StopSelf();
                return;
            }

            Android.Util.Log.Debug(TAG, "5");

            string thisDeviceUniqueId = ServiceFunctions.GetDeviceUniqueId();

            Android.Util.Log.Debug(TAG, "6 " + thisDeviceUniqueId);

            try
            {
                while (true)
                {
                    lastActiveTime = DateTime.UtcNow;

                    Android.Util.Log.Debug(TAG, "Connecting to message carrier service...");
                    var c = await Common.MessageCarrierPackageManager.Connect(rs, false);

                    if (c != RomeAppServiceConnectionStatus.Success)
                    {
                        Android.Util.Log.Debug(TAG, $"Connection failed. {c.ToString()}");
                        throw new Exception($"Connection failed. {c.ToString()}");
                    }

                    Android.Util.Log.Debug(TAG, "Connected.");
                    Android.Util.Log.Debug(TAG, "Sending message carrier...");

                    var data = new Dictionary <string, object>()
                    {
                        { "SenderId", thisDeviceUniqueId },
                    };

                    var response = await Common.MessageCarrierPackageManager.Send(data);

                    Android.Util.Log.Debug(TAG, $"Response received. ({response.Status.ToString()})");

                    if ((response.Message == null) || (response.Message.Count == 0))
                    {
                        Android.Util.Log.Debug(TAG, "Response is empty.");
                        await Task.Delay(TimeSpan.FromSeconds(1));

                        continue;
                    }

                    var isFinished = await MessageReceiveHelper.ProcessReceivedMessage(response.Message);

                    Android.Util.Log.Debug(TAG, "Finished.");

                    if (isFinished)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Debug(TAG, "Exception in SendCarrier()");
                Log.Debug(TAG, ex.Message);
                Log.Debug(TAG, ex.ToString());
            }
            finally
            {
                Common.MessageCarrierPackageManager.CloseAppService();
            }

            //StopSelf();
        }