From 2bff468242b82309816fb21a95238e8d0c8511f7 Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Thu, 1 Oct 2015 19:40:28 +0200 Subject: [PATCH] Allow to add new custom icons ChangeLog/Manifest 0.9.9-pre1 --- src/keepass2android/ChangeLog.cs | 1 + src/keepass2android/IconPickerActivity.cs | 85 ++++++++++++++++++- .../Properties/AndroidManifest_net.xml | 4 +- .../Resources/values/strings.xml | 12 +++ 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/src/keepass2android/ChangeLog.cs b/src/keepass2android/ChangeLog.cs index 7670d75b..b55aaced 100644 --- a/src/keepass2android/ChangeLog.cs +++ b/src/keepass2android/ChangeLog.cs @@ -26,6 +26,7 @@ namespace keepass2android AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(ctx, Android.Resource.Style.ThemeHoloLightDialog)); builder.SetTitle(ctx.GetString(Resource.String.ChangeLog_title)); List changeLog = new List{ + ctx.GetString(Resource.String.ChangeLog_0_9_9), ctx.GetString(Resource.String.ChangeLog_0_9_8c), ctx.GetString(Resource.String.ChangeLog_0_9_8b), ctx.GetString(Resource.String.ChangeLog_0_9_8), diff --git a/src/keepass2android/IconPickerActivity.cs b/src/keepass2android/IconPickerActivity.cs index 68e36a0f..40576a39 100644 --- a/src/keepass2android/IconPickerActivity.cs +++ b/src/keepass2android/IconPickerActivity.cs @@ -16,6 +16,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file */ using System; +using System.IO; using Android.App; using Android.Content; using Android.Graphics; @@ -24,6 +25,8 @@ using Android.Views; using Android.Widget; using KeePassLib; using KeePassLib.Utility; +using FileNotFoundException = Java.IO.FileNotFoundException; +using IOException = Java.IO.IOException; namespace keepass2android { @@ -67,8 +70,86 @@ namespace keepass2android Finish(); }; } - - public class ImageAdapter : BaseAdapter + + private const int AddCustomIconId = 1; + private const int RequestCodePickImage = 2; + + public override bool OnCreateOptionsMenu(IMenu menu) + { + + base.OnCreateOptionsMenu(menu); + + menu.Add(0, AddCustomIconId, 0, GetString(Resource.String.AddCustomIcon)); + + return true; + + } + + public override bool OnOptionsItemSelected(IMenuItem item) + { + if (item.ItemId == AddCustomIconId) + { + Intent intent = new Intent(); + intent.SetType("image/*"); + intent.SetAction(Intent.ActionGetContent); + intent.AddCategory(Intent.CategoryOpenable); + StartActivityForResult(intent, RequestCodePickImage); + } + + return base.OnOptionsItemSelected(item); + } + + protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) + { + + base.OnActivityResult(requestCode, resultCode, data); + + if (requestCode == RequestCodePickImage && resultCode == Result.Ok) + try + { + var stream = ContentResolver.OpenInputStream(data.Data); + var bitmap = BitmapFactory.DecodeStream(stream); + + stream.Close(); + + float maxSize = 128; + + using (MemoryStream ms = new MemoryStream()) + { + if ((bitmap.Width > maxSize) || (bitmap.Height > maxSize)) + { + float scale = Math.Min(maxSize / bitmap.Width, maxSize / bitmap.Height); + var scaleWidth = (int)(bitmap.Width * scale); + var scaleHeight = (int)(bitmap.Height * scale); + var scaledBitmap = Bitmap.CreateScaledBitmap(bitmap, scaleWidth, scaleHeight, true); + Bitmap newRectBitmap = Bitmap.CreateBitmap((int)maxSize, (int)maxSize, Bitmap.Config.Argb8888); + + Canvas c = new Canvas(newRectBitmap); + c.DrawBitmap(scaledBitmap, (maxSize - scaledBitmap.Width)/2.0f, (maxSize - scaledBitmap.Height)/2.0f, null); + bitmap = newRectBitmap; + } + ; + bitmap.Compress(Bitmap.CompressFormat.Png, 90, ms); + PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true), ms.ToArray()); + + App.Kp2a.GetDb().KpDatabase.CustomIcons.Add(pwci); + + } + var gridView = ((GridView)FindViewById(Resource.Id.IconGridView)); + ((BaseAdapter)gridView.Adapter).NotifyDataSetInvalidated(); + gridView.SmoothScrollToPosition(((BaseAdapter)gridView.Adapter).Count-1); + } + catch (FileNotFoundException e) + { + e.PrintStackTrace(); + } + catch (IOException e) + { + e.PrintStackTrace(); + } + } + + public class ImageAdapter : BaseAdapter { readonly IconPickerActivity _act; private readonly PwDatabase _db; diff --git a/src/keepass2android/Properties/AndroidManifest_net.xml b/src/keepass2android/Properties/AndroidManifest_net.xml index f5a74df0..b7e73c51 100644 --- a/src/keepass2android/Properties/AndroidManifest_net.xml +++ b/src/keepass2android/Properties/AndroidManifest_net.xml @@ -1,7 +1,7 @@  diff --git a/src/keepass2android/Resources/values/strings.xml b/src/keepass2android/Resources/values/strings.xml index 69a0d064..927f93a7 100644 --- a/src/keepass2android/Resources/values/strings.xml +++ b/src/keepass2android/Resources/values/strings.xml @@ -531,6 +531,8 @@ To edit it, you must copy the file to another location. Click OK to select a location where the file should be copied. Cancel, open read-only. + + Add icon from file... Copying file... @@ -547,6 +549,16 @@ * Bug fix: Previous release contained two input methods (one crashing)\n + + Version 0.9.9\n + * Complete UI redesign. Thanks a lot to Stefano Pignataro (http://www.spstudio.at) for his support!\n + * Support for Multi Window mode on Samsung devices\n + * Increased default number of encryption rounds for new databases\n + * Check for duplicate keys of additional fields to avoid losing data\n + * Allow to add custom icons + + + Version 0.9.8b\n * Bug fixes (Saving failed for some databases, exporting to local device not working, selecting some preference options crashed the app)\n