示例#1
0
        public void Startup()
        {
            var nativeBluetoothAdapter  = Helper.EnableBluetooth(this);
            var androidBluetoothAdapter = new AndroidBluetoothAdapterFactory().Create(this, ApplicationContext, nativeBluetoothAdapter);

            var prefs = Application.Context.GetSharedPreferences("CampfireChat", FileCreationMode.Private);

            var userName = prefs.GetString("Name", null);

            if (userName == null)
            {
                Globals.CampfireNetClient = CampfireNetClientBuilder.CreateNew()
                                            .WithBluetoothAdapter(androidBluetoothAdapter)
                                            .Build();
                Helper.UpdateIdentity(prefs, Globals.CampfireNetClient.Identity);
            }
            else
            {
                var rsa        = Helper.InitRSA(prefs);
                var identity   = new Identity(new IdentityManager(), rsa, userName);
                var trustChain = prefs.GetString("TC", null);
                if (trustChain != null)
                {
                    identity.AddTrustChain(Helper.HexStringToByteArray(trustChain));
                }
                Globals.CampfireNetClient = CampfireNetClientBuilder.CreateNew()
                                            .WithBluetoothAdapter(androidBluetoothAdapter)
                                            .WithIdentity(identity).Build();
            }

            if (Globals.CampfireChatClient == null)
            {
                Globals.CampfireChatClient = CampfireChatClientFactory.Create(Globals.CampfireNetClient);
                Globals.CampfireNetClient.RunAsync().Forget();
            }

            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
        }
示例#2
0
        public void Setup()
        {
            var nativeBluetoothAdapter = BluetoothAdapter.DefaultAdapter;

            if (!nativeBluetoothAdapter.IsEnabled)
            {
                System.Console.WriteLine("Enabling bluetooth");
                Intent enableBtIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
                StartActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                return;
            }

            var androidBluetoothAdapter = new AndroidBluetoothAdapterFactory().Create(this, ApplicationContext, nativeBluetoothAdapter);
            var client = CampfireNetClientBuilder.CreateNew()
                         .WithDevelopmentNetworkClaims()
                         .WithBluetoothAdapter(androidBluetoothAdapter)
                         .Build();
            var identity = client.Identity;

            var sync = new object();

            client.MessageReceived += e => {
                lock (sync) {
                    var s = Encoding.UTF8.GetString(e.Message.DecryptedPayload, 0, e.Message.DecryptedPayload.Length);
                    uiDispatchHandler.ObtainMessage(LOG_MESSAGE, "RECV: " + s).SendToTarget();
                }
            };


            generateButton.Click += (s, e) => {
                var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                path = Path.Combine(path, $"trust_chain_{IdentityManager.GetIdentityString(identity.PublicIdentityHash)}.bin");

                if (!File.Exists(path) && identity.TrustChain == null)
                {
                    identity.GenerateRootChain();

                    using (var stream = new FileStream(path, FileMode.Create))
                        using (var writer = new BinaryWriter(stream)) {
                            writer.Write(TrustChainUtil.SerializeTrustChain(identity.TrustChain));
                        }
                }
                else
                {
                    Toast.MakeText(ApplicationContext, "Trust chain already exists.", ToastLength.Short).Show();
                }
            };

            sendButton.Click += (s, e) => {
                var filename = $"trust_chain_{IdentityManager.GetIdentityString(identity.PublicIdentityHash)}.bin";
                var path     = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                path = Path.Combine(path, filename);

                // TODO fix
                if (!File.Exists(path))
                {
                    Toast.MakeText(ApplicationContext, "Cannot find file in Download folder.", ToastLength.Short).Show();
                    return;
                }

                var tmpFolder = Environment.ExternalStorageDirectory.ToString();
                var tmpFile   = Path.Combine(tmpFolder, filename);
                File.Copy(path, tmpFile, true);

                var file = new Java.IO.File(tmpFile);
                var uri  = Android.Net.Uri.FromFile(file);

                Intent email = new Intent(Intent.ActionSend);
                email.SetType("message/rfc822");
                email.PutExtra(Intent.ExtraSubject, "Trust chain addition");
                email.PutExtra(Intent.ExtraStream, uri);
                email.AddFlags(ActivityFlags.GrantReadUriPermission);

                try {
                    StartActivityForResult(Intent.CreateChooser(email, "Send mail with: "), 0);
                } catch (ActivityNotFoundException) {
                    Toast.MakeText(ApplicationContext, "There are no email clients installed.", ToastLength.Short).Show();
                }
            };

            loadButton.Click += (s, e) => {
                var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                path = Path.Combine(path, $"trust_chain_{IdentityManager.GetIdentityString(identity.PublicIdentityHash)}.bin");

                if (!File.Exists(path))
                {
                    Toast.MakeText(ApplicationContext, "Cannot find own trust chain file in Download folder.", ToastLength.Short).Show();
                    return;
                }

                byte[] data = File.ReadAllBytes(path);

                try {
                    identity.AddTrustChain(data);
                    Toast.MakeText(ApplicationContext, "Successfully loaded trust chain.", ToastLength.Short).Show();
                } catch (BadTrustChainException) {
                    Toast.MakeText(ApplicationContext, "Invalid trust chain found.", ToastLength.Short).Show();
                }
            };

            clearButton.Click += (s, e) => {
                var privateFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                var files         = Directory.GetFiles(privateFolder);

                foreach (var file in files)
                {
                    File.Delete(file);
                }

                //var path = Path.Combine(privateFolder, PRIVATE_KEY_FILE);
                //identity.SaveKey(path);
            };


            sendTextButton.Click += (s, e) => {
                var text = inputText.Text;
                client.BroadcastAsync(Encoding.UTF8.GetBytes(text)).Forget();
                inputText.Text = "";
            };

            client.RunAsync().Forget();
        }