internal Uri WriteBinaryToFile(string key, bool writeToCacheDirectory) { ProtectedBinary pb = Entry.Binaries.Get(key); System.Diagnostics.Debug.Assert(pb != null); if (pb == null) { throw new ArgumentException(); } ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); string binaryDirectory = prefs.GetString(GetString(Resource.String.BinaryDirectory_key), GetString(Resource.String.BinaryDirectory_default)); if (writeToCacheDirectory) { binaryDirectory = CacheDir.Path + File.Separator + AttachmentContentProvider.AttachmentCacheSubDir; string filepart = key; Java.Lang.String javaFilename = new Java.Lang.String(filepart); filepart = javaFilename.ReplaceAll("[^a-zA-Z0-9.-]", "_"); var targetFile = new File(binaryDirectory, filepart); File parent = targetFile.ParentFile; if (parent == null || (parent.Exists() && !parent.IsDirectory)) { Toast.MakeText(this, Resource.String.error_invalid_path, ToastLength.Long).Show(); return(null); } if (!parent.Exists()) { // Create parent directory if (!parent.Mkdirs()) { Toast.MakeText(this, Resource.String.error_could_not_create_parent, ToastLength.Long).Show(); return(null); } } string filename = targetFile.AbsolutePath; byte[] pbData = pb.ReadData(); try { System.IO.File.WriteAllBytes(filename, pbData); } catch (Exception exWrite) { Toast.MakeText(this, GetString(Resource.String.SaveAttachment_Failed, new Java.Lang.Object[] { filename }) + exWrite.Message, ToastLength.Long).Show(); return(null); } finally { MemUtil.ZeroByteArray(pbData); } Toast.MakeText(this, GetString(Resource.String.SaveAttachment_doneMessage, new Java.Lang.Object[] { filename }), ToastLength.Short).Show(); return(Uri.Parse("content://" + AttachmentContentProvider.Authority + "/" + filename)); } else { _exportBinaryProcessManager = new ExportBinaryProcessManager(requestCodeSelFileStorageForWriteAttachment, this, key); _exportBinaryProcessManager.StartProcess(); return(null); } }
protected override void OnCreate(Bundle savedInstanceState) { if (savedInstanceState != null) { _exportBinaryProcessManager = new ExportBinaryProcessManager(requestCodeSelFileStorageForWriteAttachment, this, savedInstanceState); } ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); long usageCount = prefs.GetLong(GetString(Resource.String.UsageCount_key), 0); ISharedPreferencesEditor edit = prefs.Edit(); edit.PutLong(GetString(Resource.String.UsageCount_key), usageCount + 1); edit.Commit(); _showPassword = !prefs.GetBoolean(GetString(Resource.String.maskpass_key), Resources.GetBoolean(Resource.Boolean.maskpass_default)); RequestWindowFeature(WindowFeatures.IndeterminateProgress); _activityDesign.ApplyTheme(); base.OnCreate(savedInstanceState); SetEntryView(); Database db = App.Kp2a.GetDb(); // Likely the app has been killed exit the activity if (!db.Loaded || (App.Kp2a.QuickLocked)) { Finish(); return; } SetResult(KeePass.ExitNormal); Intent i = Intent; PwUuid uuid = new PwUuid(MemUtil.HexStringToByteArray(i.GetStringExtra(KeyEntry))); _pos = i.GetIntExtra(KeyRefreshPos, -1); _appTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent); Entry = db.Entries[uuid]; // Refresh Menu contents in case onCreateMenuOptions was called before Entry was set ActivityCompat.InvalidateOptionsMenu(this); // Update last access time. Entry.Touch(false); if (PwDefs.IsTanEntry(Entry) && prefs.GetBoolean(GetString(Resource.String.TanExpiresOnUse_key), Resources.GetBoolean(Resource.Boolean.TanExpiresOnUse_default)) && ((Entry.Expires == false) || Entry.ExpiryTime > DateTime.Now)) { PwEntry backupEntry = Entry.CloneDeep(); Entry.ExpiryTime = DateTime.Now; Entry.Expires = true; Entry.Touch(true); RequiresRefresh(); UpdateEntry update = new UpdateEntry(this, App.Kp2a, backupEntry, Entry, null); ProgressTask pt = new ProgressTask(App.Kp2a, this, update); pt.Run(); } FillData(); SetupEditButtons(); App.Kp2a.GetDb().LastOpenedEntry = new PwEntryOutput(Entry, App.Kp2a.GetDb().KpDatabase); _pluginActionReceiver = new PluginActionReceiver(this); RegisterReceiver(_pluginActionReceiver, new IntentFilter(Strings.ActionAddEntryAction)); _pluginFieldReceiver = new PluginFieldReceiver(this); RegisterReceiver(_pluginFieldReceiver, new IntentFilter(Strings.ActionSetEntryField)); new Thread(NotifyPluginsOnOpen).Start(); //the rest of the things to do depends on the current app task: _appTask.CompleteOnCreateEntryActivity(this); }