From 2b54e80edb8d94aa3af11eea3422ec893ddc81ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8E?= Date: Tue, 22 Mar 2016 19:41:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90android=E7=89=88=E7=9A=84?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=8F=8A=E5=8A=9F=E8=83=BD=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9wpf=E4=B8=ADsimplekey=E6=8F=90=E4=BA=A4=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E8=B5=8B=E5=80=BC=E9=80=BB=E8=BE=91=E5=8F=8Abug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Activities/MainActivity.cs | 247 ++++++++++++++ .../Activities/MainActivity_Definition.cs | 60 ++++ .../PasswordGenerator.Android/MainActivity.cs | 24 -- .../PasswordGenerator.Android.csproj | 28 +- .../Properties/AndroidManifest.xml | 2 +- .../Resources/Resource.Designer.cs | 167 ++++++++-- .../Resources/drawable/rect_frame_gray.xml | 10 + .../Resources/layout/Main.axml | 312 ++++++++++++++---- .../Resources/values/Strings.xml | 6 +- .../PasswordGenerator.WPF/MainWindow.xaml | 2 +- .../PasswordGenerator.WPF/MainWindow.xaml.cs | 22 +- .../PasswordGenerator.WPF.csproj | 1 + .../Properties/AssemblyInfo.cs | 4 +- 13 files changed, 748 insertions(+), 137 deletions(-) create mode 100644 PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity.cs create mode 100644 PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity_Definition.cs delete mode 100644 PasswordGenerator/PasswordGenerator.Android/MainActivity.cs create mode 100644 PasswordGenerator/PasswordGenerator.Android/Resources/drawable/rect_frame_gray.xml diff --git a/PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity.cs b/PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity.cs new file mode 100644 index 0000000..ce04b52 --- /dev/null +++ b/PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity.cs @@ -0,0 +1,247 @@ +using System; +using Android.App; +using Android.Content; +using Android.OS; +using Android.Views; +using Android.Widget; +using Java.Lang; +using PasswordGenerator.Core; +using PasswordGenerator.Core.Enums; +using PasswordGenerator.Core.Models; + +namespace PasswordGenerator.Android.Activities +{ + [Activity(Label = "PasswordGenerator", MainLauncher = true, Icon = "@drawable/icon")] + public partial class MainActivity : Activity + { + private ClipboardManager _clipboardManager; + + private bool _initCompleted; + + private readonly ToggleButton[] _signalArray = new ToggleButton[12]; + + protected override void OnCreate(Bundle bundle) + { + base.OnCreate(bundle); + + // Set our view from the "main" layout resource + SetContentView(Resource.Layout.Main); + + Definition(); + Initialize(); + + RefreshModes(); + } + + private void Initialize() + { + _clipboardManager = (ClipboardManager)GetSystemService(ClipboardService); + + _btnGenerate.Click += btnGenerate_Click; + _btnSubmit.Click += btnSubmit_Click; + _lytOptionExpand.Click += lytOptionExpand_Click; + _btnCopy.Click += btnCopy_Click; + + _cbUpper.Click += (o, e) => + { + _rGroupUpper.Visibility = _cbUpper.Checked ? ViewStates.Visible : ViewStates.Gone; + RefreshModes(); + }; + _cbLower.Click += (o, e) => + { + _rGroupLower.Visibility = _cbLower.Checked ? ViewStates.Visible : ViewStates.Gone; + RefreshModes(); + }; + _cbNum.Click += (o, e) => + { + _rGroupNum.Visibility = _cbNum.Checked ? ViewStates.Visible : ViewStates.Gone; + RefreshModes(); + }; + _cbSignals.Click += (o, e) => + { + _rGroupSignals.Visibility = _cbSignals.Checked ? ViewStates.Visible : ViewStates.Gone; + _tlytSignals.Visibility = _cbSignals.Checked ? ViewStates.Visible : ViewStates.Gone; + + RefreshModes(); + }; + _rGroupUpper.CheckedChange += (o, e) => { RefreshModes(); }; + _rGroupLower.CheckedChange += (o, e) => { RefreshModes(); }; + _rGroupNum.CheckedChange += (o, e) => { RefreshModes(); }; + _rGroupSignals.CheckedChange += (o, e) => { RefreshModes(); }; + + InitSignalList(); + + + _initCompleted = true; + } + + private void InitSignalList() + { + var signalIds = new[] {Resource.Id.tbtn1, + Resource.Id.tbtn2,Resource.Id.tbtn3,Resource.Id.tbtn4, + Resource.Id.tbtn5,Resource.Id.tbtn6,Resource.Id.tbtn7, + Resource.Id.tbtn8,Resource.Id.tbtn9,Resource.Id.tbtn10, + Resource.Id.tbtn11,Resource.Id.tbtn12}; + + for (int i = 0; i < 12; i++) + { + var btn = FindViewById(signalIds[i]); + btn.Click += (o, e) => { RefreshModes(); }; + _signalArray[i] = btn; + } + } + + private void btnGenerate_Click(object sender, EventArgs e) + { + var keywords = _txtKeywords.Text; + var length = int.Parse(_txtLength.Text); + var key = _tvSimpleKey.Text; + + _tvResult.Text = Generator.Generate(keywords, length, key); + } + + private void btnSubmit_Click(object sender, EventArgs e) + { + var modeStateHex = _txtSimpleKey.Text; + if (string.IsNullOrEmpty(modeStateHex)) return; + + var modeStateOct = Generator.ModeStateHexToOct(modeStateHex); + var modeStates = Generator.GetModesFromOct(modeStateOct); + + //将输入的模式简码转换到当前选项中 + foreach (var modeState in modeStates) + { + switch (modeState.RangeType) + { + case EnumValueRangeType.UpperWord: + _rGroupUpper.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbUpper_Must : Resource.Id.rbUpper_Optional); + _cbUpper.Checked = modeState.State != EnumChooseState.None; + _rGroupUpper.Visibility = _cbUpper.Checked ? ViewStates.Visible : ViewStates.Gone; + break; + case EnumValueRangeType.LowerWord: + _rGroupLower.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbLower_Must : Resource.Id.rbLower_Optional); + _cbLower.Checked = modeState.State != EnumChooseState.None; + _rGroupLower.Visibility = _cbLower.Checked ? ViewStates.Visible : ViewStates.Gone; + break; + case EnumValueRangeType.Number: + _rGroupNum.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbNum_Must : Resource.Id.rbNum_Optional); + _cbNum.Checked = modeState.State != EnumChooseState.None; + _rGroupNum.Visibility = _cbNum.Checked ? ViewStates.Visible : ViewStates.Gone; + break; + case EnumValueRangeType.Signal: + _rGroupSignals.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbSignals_Must : Resource.Id.rbSignals_Optional); + _cbSignals.Checked = modeState.State != EnumChooseState.None; + _tlytSignals.Visibility = _rGroupSignals.Visibility = _cbSignals.Checked ? ViewStates.Visible : ViewStates.Gone; + + var chosenSignals = Generator.GetSignalsFromOct(modeStateOct); + foreach (ToggleButton child in _signalArray) + { + child.Checked = chosenSignals.Contains(child.Text); + } + break; + default: + break; + } + } + + _tvSimpleKey.Text = _txtSimpleKey.Text; + } + + private void lytOptionExpand_Click(object sender, EventArgs e) + { + var lytOptions = FindViewById(Resource.Id.lytOptions); + var imgOptions = FindViewById(Resource.Id.imgOptions); + if (lytOptions.Visibility == ViewStates.Visible) + { + lytOptions.Visibility = ViewStates.Gone; + imgOptions.SetImageResource(Resource.Drawable.Expand); + } + else + { + lytOptions.Visibility = ViewStates.Visible; + imgOptions.SetImageResource(Resource.Drawable.Collapse); + } + } + + private void btnCopy_Click(object sender, EventArgs e) + { + var data = ClipData.NewPlainText("text", _tvResult.Text); + _clipboardManager.PrimaryClip = data; + + var dialog = new AlertDialog.Builder(this).Create(); + dialog.SetTitle("成功"); + dialog.SetMessage("复制成功"); + dialog.SetCanceledOnTouchOutside(true); + dialog.Show(); + } + + + #region 私有方法 + + /// + /// 刷新当前模式 + /// + private void RefreshModes() + { + if (!_initCompleted) return; + + StringBuilder keySBuilder = new StringBuilder(); + GetModeState(_cbUpper, _rGroupUpper, keySBuilder); + GetModeState(_cbLower, _rGroupLower, keySBuilder); + GetModeState(_cbNum, _rGroupNum, keySBuilder); + GetModeState(_cbSignals, _rGroupSignals, keySBuilder); + + //子符号的状态获取 + char[] signals = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' }; + if (_cbSignals.Checked) + { + foreach (ToggleButton child in _signalArray) + { + var index = Key.Signals.IndexOf(child.Text, StringComparison.Ordinal); + if (child.Checked) + { + signals[index] = '1'; + } + } + } + keySBuilder.Append(signals); + + _tvSimpleKey.Text = Generator.ModeStateOctToHex(keySBuilder.ToString()); + } + + /// + /// 获取模式状态 + /// + /// 启用控制的复选框 + /// 可选和必选所在的容器 + /// 输出字符串构建器 + private void GetModeState(CheckBox cb, RadioGroup rGroup, StringBuilder sb) + { + var state = EnumChooseState.None; + if (cb.Checked) + { + var cbOption = FindViewById(rGroup.CheckedRadioButtonId); + //第一个选项是“必须”选项,如果不是则不正常,默认按可选 + if (rGroup.CheckedRadioButtonId != -1 && cbOption.Text == "Must") + { + state = EnumChooseState.Must; + } + else + { + state = EnumChooseState.Optional; + } + } + + //保证输出长度为两位 + var octStr = Convert.ToString((short)state, 2); + if (octStr.Length < 2) + { + sb.Append("0"); + } + sb.Append(octStr); + } + + #endregion + } +} + diff --git a/PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity_Definition.cs b/PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity_Definition.cs new file mode 100644 index 0000000..f2acb28 --- /dev/null +++ b/PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity_Definition.cs @@ -0,0 +1,60 @@ +using Android.Views; +using Android.Widget; + +namespace PasswordGenerator.Android.Activities +{ + public partial class MainActivity + { + + private Button _btnGenerate; + private Button _btnSubmit; + private Button _btnCopy; + + private ViewGroup _lytOptionExpand; + private TableLayout _tlytSignals; + + private TextView _tvResult; + private TextView _tvSimpleKey; + + private CheckBox _cbUpper; + private CheckBox _cbLower; + private CheckBox _cbNum; + private CheckBox _cbSignals; + + private RadioGroup _rGroupUpper; + private RadioGroup _rGroupLower; + private RadioGroup _rGroupNum; + private RadioGroup _rGroupSignals; + + private EditText _txtKeywords; + private EditText _txtLength; + private EditText _txtSimpleKey; + + private void Definition() + { + _btnGenerate = FindViewById