Exemplo n.º 1
0
        private Intent GetFileServiceIntent(string action, IParcelable uri)
        {
            var intent = AndroidUtil.CreateExplicitFromImplicitIntent(this, new Intent(action));

            switch (action)
            {
            case FileBoundService.ActionFeedback:
            case FileBoundService.ActionZipLogFiles:
                _compressFileName = Path.Combine(_tmpFolder,
                                                 string.Format("log_{0}_{1:yyyyMMddHHmmss}.zip", _productName, DateTime.Now));
                intent.PutExtra(FileBoundService.ExtraZipFileName, _compressFileName);
                intent.PutExtra(FileBoundService.ExtraLogFileName, AppSettings.Default.LogFileName);
                intent.PutExtra(FileBoundService.ExtraComment, _versionFull);
                break;

            case FileBoundService.ActionExportLog:
                intent.PutExtra(FileBoundService.ExtraZipFileName, _compressFileName);
                if (uri != null)
                {
                    intent.PutExtra(Intent.ExtraStream, uri);
                }
                break;

            default:
                throw new NotImplementedException(action);
            }

            return(intent);
        }
Exemplo n.º 2
0
        protected override void OnResume()
        {
            base.OnResume();

            ValidateServiceReceiver();
            var intentFilter = new IntentFilter(FileBoundService.ActionFeedback)
            {
                Priority = (int)IntentFilterPriority.HighPriority
            };

            intentFilter.AddAction(FileBoundService.ActionZipLogFiles);
            intentFilter.AddAction(FileBoundService.ActionExportLog);

            RegisterReceiver(_serviceReceiver, intentFilter);

            ServiceConnectionDispose();
            _serviceConnection = new ServiceConnection(this);

            var serviceIntent = AndroidUtil.CreateExplicitFromImplicitIntent(this,
                                                                             new Intent(FileBoundService.ActionZipLogFiles));

            BindService(serviceIntent, _serviceConnection, Bind.AutoCreate);

            try
            {
                StartFileService();
            }
            catch (Exception ex)
            {
                ShowProgressbar(false);
                ShowError(ex, Resource.String.InternalError);
            }
        }
Exemplo n.º 3
0
        public override void OnCreate()
        {
            base.OnCreate();

            // A great place to initialize Xamarin.Insights and Dependency Services!

            // конфигурируем приложение
            var container = new UnityContainer();

            Configure(container);

            // start app
            _log?.InfoFormat("====== Start on '{0}'. OS: '{1}'. Ver. '{2} ({3})'.",
                             AndroidUtil.GetDeviceName(),
                             AndroidUtil.GetAndroidVersion(),
                             AppSettings.Default.VersionName,
                             AssemblyAttributeAccessors.GetAssemblyFileVersion(GetType()));

            try
            {
                // json settings
                //http://www.newtonsoft.com/json/help/html/PreserveReferencesHandlingObject.htm
                //http://stackoverflow.com/questions/13510204/json-net-self-referencing-loop-detected
                //https://gist.github.com/carlin-q-scott/4c8a9cce734fa5b10a97
                JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                {
#if DEBUG
                    Formatting = Formatting.Indented,
#endif
                    ContractResolver = ShouldSerializeContractResolver.Instance,
                    TypeNameHandling = TypeNameHandling.Auto
                                       //PreserveReferencesHandling = PreserveReferencesHandling.Objects
                                       //ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                };

                Task.Run(() =>
                {
                    try
                    {
                        var api = new ServiceApi();
                        api.StartService(this, ControlService.ActionAppStart);

                        _log?.Info("------ Init is completed.");
                    }
                    catch (Exception ex)
                    {
                        _log?.Error(ex);
                    }
                });
            }
            catch (Exception ex)
            {
                _log?.Error(ex);
            }
        }
Exemplo n.º 4
0
        private void OnFileBoundServiceResult(string action)
        {
            //http://stackoverflow.com/questions/2197741/how-can-i-send-emails-from-my-android-application

            try
            {
                if (!_isBound)
                {
                    return;
                }

                var service = _binder.Service as FileBoundService;
                if (service == null)
                {
                    return;
                }

                if (service.ServiceExceptions != null && service.ServiceExceptions.ContainsKey(action))
                {
                    var exception = service.ServiceExceptions[action];
                    if (exception != null)
                    {
                        ShowError(exception, Resource.String.InternalError);
                        return;
                    }
                }

                if (service.Result != Result.Ok)
                {
                    return;
                }

                Intent activityIntent;
                var    requestCode = 0;

                switch (action)
                {
                case FileBoundService.ActionFeedback:
                    var emailIntent = new Intent(Intent.ActionSend);
                    //emailIntent.SetData(Uri.Parse("mailto:" + GetString(Resource.String.FeedbackMail)));
                    emailIntent.PutExtra(Intent.ExtraEmail, new[] { GetString(Resource.String.FeedbackMail) });
                    var applicationName = GetString(Resource.String.ApplicationName);
                    emailIntent.PutExtra(Intent.ExtraSubject,
                                         string.Format("{0} ver. {1}", applicationName, _version));
                    var bodytext = string.Format("Model: {0}{1}OS: {2}{1}Application: {3}", AndroidUtil.GetDeviceName(),
                                                 System.Environment.NewLine, AndroidUtil.GetAndroidVersion(),
                                                 string.Format("{0} ver. {1}", applicationName, _versionFull));

                    var intentType = "text/plain; charset=UTF-8;";

                    var zipFile = new Java.IO.File(_compressFileName);
                    if (zipFile.Exists())
                    {
                        //emailIntent.SetType("application/zip");
                        intentType += " " + MimeTypeZip;
                        var uri = FileProvider.GetUriForFile(this,
                                                             Application.Context.PackageName + ".FileProvider", zipFile);
                        //emailIntent.PutExtra(Intent.ExtraStream, Uri.Parse("file://" + _compressFileName));
                        emailIntent.PutExtra(Intent.ExtraStream, uri);
                    }
                    else
                    {
                        //throw new FileNotFoundException(string.Format("File '{0}' not found.", _compressFileName));
                        bodytext += string.Format("{0}No log-file (logToFile: {1})", System.Environment.NewLine,
                                                  AppSettings.Default.LoggingUseFile);
                    }

                    emailIntent.SetType(intentType);
                    emailIntent.PutExtra(Intent.ExtraText, bodytext);

                    activityIntent =
                        Intent.CreateChooser(emailIntent, GetString(Resource.String.EmailChooserTitle));
                    break;

                case FileBoundService.ActionZipLogFiles:
                    activityIntent = new Intent(Intent.ActionCreateDocument);
                    activityIntent.AddCategory(Intent.CategoryOpenable);
                    activityIntent.SetType(MimeTypeZip);
                    var title = Path.GetFileName(_compressFileName)?.ToLower();
                    if (!string.IsNullOrEmpty(title))
                    {
                        activityIntent.PutExtra(Intent.ExtraTitle, title);
                    }
                    requestCode = RequestCodeExportLog;
                    break;

                case FileBoundService.ActionExportLog:
                    return;

                default:
                    throw new NotImplementedException(action);
                }

                if (activityIntent != null)
                {
                    StartActivityForResult(activityIntent, requestCode);
                }
            }
            catch (ActivityNotFoundException ex)
            {
                ShowError(ex, Resource.String.NoEmailClientsInstalled);
            }
            catch (Exception ex)
            {
                ShowError(ex, Resource.String.InternalError);
            }
            finally
            {
                ShowProgressbar(false);
            }
        }