launch android app packages

This commit is contained in:
Kyle Spearrin 2017-06-08 16:22:11 -04:00
parent ae806da3f1
commit 957db1ec11
5 changed files with 66 additions and 3 deletions

View file

@ -14,6 +14,7 @@ using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using System.Threading.Tasks;
using Bit.App.Models.Page;
using Bit.App;
namespace Bit.Android
{
@ -24,6 +25,7 @@ namespace Bit.Android
public class MainActivity : FormsAppCompatActivity
{
private const string HockeyAppId = "d3834185b4a643479047b86c65293d42";
private DateTime? _lastAction;
protected override void OnCreate(Bundle bundle)
{
@ -94,6 +96,12 @@ namespace Bit.Android
{
MoveTaskToBack(true);
});
MessagingCenter.Subscribe<Xamarin.Forms.Application, string>(
Xamarin.Forms.Application.Current, "LaunchApp", (sender, args) =>
{
LaunchApp(args);
});
}
private void ReturnCredentials(VaultListPageModel.Login login)
@ -199,5 +207,26 @@ namespace Bit.Android
var intent = new Intent(global::Android.Provider.Settings.ActionAccessibilitySettings);
StartActivity(intent);
}
private void LaunchApp(string packageName)
{
if(_lastAction.LastActionWasRecent())
{
return;
}
_lastAction = DateTime.UtcNow;
packageName = packageName.Replace("androidapp://", string.Empty);
var launchIntent = PackageManager.GetLaunchIntentForPackage(packageName);
if(launchIntent == null)
{
var dialog = Resolver.Resolve<IUserDialogs>();
dialog.Alert(string.Format(App.Resources.AppResources.CannotOpenApp, packageName));
}
else
{
StartActivity(launchIntent);
}
}
}
}

View file

@ -114,7 +114,18 @@ namespace Bit.App.Models.Page
{
get
{
if(!ShowUri || !Uri.StartsWith("http"))
if(!ShowUri)
{
return false;
}
if(Device.RuntimePlatform == Device.Android && !Uri.StartsWith("http") &&
!Uri.StartsWith("androidapp://"))
{
return false;
}
if(Device.RuntimePlatform != Device.Android && !Uri.StartsWith("http"))
{
return false;
}

View file

@ -75,7 +75,17 @@ namespace Bit.App.Pages
UriCell = new LabeledValueCell(AppResources.Website, button1Text: AppResources.Launch);
UriCell.Value.SetBinding(Label.TextProperty, nameof(VaultViewLoginPageModel.UriHost));
UriCell.Button1.SetBinding(IsVisibleProperty, nameof(VaultViewLoginPageModel.ShowLaunch));
UriCell.Button1.Command = new Command(() => Device.OpenUri(new Uri(Model.Uri)));
UriCell.Button1.Command = new Command(() =>
{
if(Device.RuntimePlatform == Device.Android && Model.Uri.StartsWith("androidapp://"))
{
MessagingCenter.Send(Application.Current, "LaunchApp", Model.Uri);
}
else if(Model.Uri.StartsWith("http://") || Model.Uri.StartsWith("https://"))
{
Device.OpenUri(new Uri(Model.Uri));
}
});
// Notes
NotesCell = new LabeledValueCell();

View file

@ -394,6 +394,15 @@ namespace Bit.App.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Cannot open the app &quot;{0}&quot;..
/// </summary>
public static string CannotOpenApp {
get {
return ResourceManager.GetString("CannotOpenApp", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Change Email.
/// </summary>

View file

@ -849,4 +849,8 @@
<data name="AutofillAlwaysDescription" xml:space="preserve">
<value>Always scan the screen for fields and only offer an auto-fill notification if password fields are found. This is the default setting.</value>
</data>
<data name="CannotOpenApp" xml:space="preserve">
<value>Cannot open the app "{0}".</value>
<comment>Message shown when trying to launch an app that does not exist on the user's device.</comment>
</data>
</root>