2013-02-23 11:43:42 -05:00
|
|
|
/*
|
|
|
|
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
|
|
|
|
|
|
|
|
Keepass2Android is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Keepass2Android is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
2013-09-28 01:46:44 -04:00
|
|
|
using System.Collections;
|
2013-02-23 11:43:42 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using Android.App;
|
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
|
|
|
using Android.Runtime;
|
|
|
|
using Android.Views;
|
|
|
|
using Android.Widget;
|
2013-03-23 16:19:37 -04:00
|
|
|
using Android.Content.PM;
|
2013-09-28 01:46:44 -04:00
|
|
|
using Uri = Android.Net.Uri;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
|
|
|
|
|
|
|
public class Util {
|
|
|
|
public static String getClipboard(Context context) {
|
|
|
|
Android.Text.ClipboardManager clipboard = (Android.Text.ClipboardManager) context.GetSystemService(Context.ClipboardService);
|
|
|
|
return clipboard.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void copyToClipboard(Context context, String text) {
|
|
|
|
Android.Text.ClipboardManager clipboard = (Android.Text.ClipboardManager) context.GetSystemService(Context.ClipboardService);
|
|
|
|
clipboard.Text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void gotoUrl(Context context, String url) {
|
|
|
|
if ( url != null && url.Length > 0 ) {
|
2013-06-01 09:51:54 -04:00
|
|
|
|
|
|
|
if (url.StartsWith("androidapp://"))
|
|
|
|
{
|
|
|
|
string packageName = url.Substring("androidapp://".Length);
|
|
|
|
Intent startKp2aIntent = context.PackageManager.GetLaunchIntentForPackage(packageName);
|
|
|
|
if (startKp2aIntent != null)
|
|
|
|
{
|
|
|
|
startKp2aIntent.AddCategory(Intent.CategoryLauncher);
|
|
|
|
startKp2aIntent.AddFlags(ActivityFlags.NewTask);
|
|
|
|
context.StartActivity(startKp2aIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Android.Net.Uri uri = Android.Net.Uri.Parse(url);
|
|
|
|
context.StartActivity(new Intent(Intent.ActionView, uri));
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void gotoUrl(Context context, int resId) {
|
|
|
|
gotoUrl(context, context.GetString(resId));
|
|
|
|
}
|
2013-03-23 16:19:37 -04:00
|
|
|
|
|
|
|
public static void gotoMarket(Context context)
|
|
|
|
{
|
|
|
|
gotoUrl(context, context.GetString(Resource.String.MarketURL)+context.PackageName);
|
|
|
|
}
|
2013-04-09 16:59:33 -04:00
|
|
|
|
|
|
|
public static void gotoDonateUrl(Context context)
|
|
|
|
{
|
|
|
|
string donateUrl = context.GetString(Resource.String.donate_url,
|
|
|
|
new Java.Lang.Object[]{context.Resources.Configuration.Locale.Language,
|
|
|
|
context.PackageName
|
|
|
|
});
|
|
|
|
gotoUrl(context, donateUrl);
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-25 15:27:41 -04:00
|
|
|
public static String GetEditText(Activity act, int resId) {
|
2013-02-23 11:43:42 -05:00
|
|
|
TextView te = (TextView) act.FindViewById(resId);
|
|
|
|
System.Diagnostics.Debug.Assert(te != null);
|
|
|
|
|
|
|
|
if (te != null) {
|
2013-06-25 15:27:41 -04:00
|
|
|
return te.Text;
|
2013-02-23 11:43:42 -05:00
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:27:41 -04:00
|
|
|
public static void SetEditText(Activity act, int resId, String str) {
|
2013-02-23 11:43:42 -05:00
|
|
|
TextView te = (TextView) act.FindViewById(resId);
|
|
|
|
System.Diagnostics.Debug.Assert(te != null);
|
|
|
|
|
|
|
|
if (te != null) {
|
|
|
|
te.Text = str;
|
|
|
|
}
|
|
|
|
}
|
2013-06-15 16:02:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether the specified action can be used as an intent. This
|
|
|
|
* method queries the package manager for installed packages that can
|
|
|
|
* respond to an intent with the specified action. If no suitable package is
|
|
|
|
* found, this method returns false.
|
|
|
|
*
|
|
|
|
* @param context The application's environment.
|
|
|
|
* @param action The Intent action to check for availability.
|
|
|
|
*
|
|
|
|
* @return True if an Intent with the specified action can be sent and
|
|
|
|
* responded to, false otherwise.
|
|
|
|
*/
|
|
|
|
static bool IsIntentAvailable(Context context, String action, String type)
|
|
|
|
{
|
|
|
|
PackageManager packageManager = context.PackageManager;
|
|
|
|
Intent intent = new Intent(action);
|
|
|
|
if (type != null)
|
|
|
|
intent.SetType(type);
|
|
|
|
IList<ResolveInfo> list =
|
|
|
|
packageManager.QueryIntentActivities(intent,
|
|
|
|
PackageInfoFlags.MatchDefaultOnly);
|
|
|
|
foreach (ResolveInfo i in list)
|
2013-07-06 10:11:38 -04:00
|
|
|
Kp2aLog.Log(i.ActivityInfo.ApplicationInfo.PackageName);
|
2013-06-15 16:02:48 -04:00
|
|
|
return list.Count > 0;
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-04-26 06:43:06 -04:00
|
|
|
public static void showBrowseDialog(string filename, Activity act, int requestCodeBrowse, bool forSaving)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 16:02:48 -04:00
|
|
|
if ((!forSaving) && (IsIntentAvailable(act, Intent.ActionGetContent, "file/*"))) {
|
2013-04-26 06:43:06 -04:00
|
|
|
Intent i = new Intent(Intent.ActionGetContent);
|
|
|
|
i.SetType("file/*");
|
|
|
|
|
|
|
|
act.StartActivityForResult(i, requestCodeBrowse);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-15 16:02:48 -04:00
|
|
|
if (IsIntentAvailable(act, Intents.FileBrowse, null))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
Intent i = new Intent(Intents.FileBrowse);
|
2013-04-26 06:43:06 -04:00
|
|
|
if (filename != null)
|
|
|
|
i.SetData(Android.Net.Uri.Parse("file://" + filename));
|
2013-02-23 11:43:42 -05:00
|
|
|
try
|
|
|
|
{
|
2013-04-26 04:10:46 -04:00
|
|
|
act.StartActivityForResult(i, requestCodeBrowse);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
catch (ActivityNotFoundException)
|
|
|
|
{
|
|
|
|
BrowserDialog diag = new BrowserDialog(act);
|
|
|
|
diag.Show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BrowserDialog diag = new BrowserDialog(act);
|
|
|
|
diag.Show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 01:46:44 -04:00
|
|
|
public static string IntentToFilename(Intent data, Context ctx)
|
2013-04-26 06:43:06 -04:00
|
|
|
{
|
2013-09-28 15:14:21 -04:00
|
|
|
#if !EXCLUDE_FILECHOOSER
|
2013-09-28 01:46:44 -04:00
|
|
|
string EXTRA_RESULTS = "group.pals.android.lib.ui.filechooser.FileChooserActivity.results";
|
|
|
|
if (data.HasExtra(EXTRA_RESULTS))
|
|
|
|
{
|
|
|
|
IList uris = data.GetParcelableArrayListExtra(EXTRA_RESULTS);
|
|
|
|
Uri uri = (Uri) uris[0];
|
|
|
|
return Group.Pals.Android.Lib.UI.Filechooser.Providers.BaseFileProviderUtils.GetRealUri(ctx, uri).ToString();
|
|
|
|
}
|
2013-09-28 15:14:21 -04:00
|
|
|
#endif
|
2013-09-28 01:46:44 -04:00
|
|
|
|
2013-04-26 06:43:06 -04:00
|
|
|
String filename = data.Data.Path;
|
|
|
|
if (String.IsNullOrEmpty(filename))
|
|
|
|
filename = data.DataString;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-04-28 16:46:31 -04:00
|
|
|
public static bool HasActionBar(Activity activity)
|
|
|
|
{
|
|
|
|
//Actionbar is available since 11, but the layout has its own "pseudo actionbar" until 13
|
|
|
|
return ((int)Android.OS.Build.VERSION.SdkInt >= 14) && (activity.ActionBar != null);
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|