Ejemplo n.º 1
0
        internal static ShortcutInfo ToShortcutInfo(this AppAction action)
        {
            var context = Application.Context;

            var shortcut = new ShortcutInfo.Builder(context, action.Id)
                           .SetShortLabel(action.Title);

            if (!string.IsNullOrWhiteSpace(action.Subtitle))
            {
                shortcut.SetLongLabel(action.Subtitle);
            }

            if (!string.IsNullOrWhiteSpace(action.Icon))
            {
                var iconResId = context.Resources.GetIdentifier(action.Icon, "drawable", context.PackageName);

                shortcut.SetIcon(Icon.CreateWithResource(context, iconResId));
            }

            var intent = new Intent(AppActionsImplementation.IntentAction);

            intent.SetPackage(context.PackageName);
            intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
            intent.PutExtra(extraAppActionId, action.Id);
            intent.PutExtra(extraAppActionTitle, action.Title);
            intent.PutExtra(extraAppActionSubtitle, action.Subtitle);
            intent.PutExtra(extraAppActionIcon, action.Icon);

            shortcut.SetIntent(intent);

            return(shortcut.Build());
        }
Ejemplo n.º 2
0
        private void RefreshList()
        {
            Adapter?.Refresh();
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O &&
                GetSystemService(ShortcutService) is ShortcutManager shortcutManager)
            {
                var shortcuts = new List <ShortcutInfo>();
                foreach (var cat in DataHolder.GetSelectedCategories())
                {
                    if (!DataHolder.CategoriesDictionary.ContainsKey(cat))
                    {
                        DataHolder.Initialize(this);
                    }

                    var data = DataHolder.CategoriesDictionary[cat];

                    var info = new Intent(this, typeof(CategoryInfoActivity));
                    info.PutExtra("category", cat);
                    info.SetAction(Intent.ActionDefault);

                    var shortcutOne = new ShortcutInfo.Builder(this, cat)
                                      .SetShortLabel(data.Title)
                                      .SetIcon(Icon.CreateWithResource(this, Resource.Mipmap.Icon))
                                      .SetIntent(info).Build();
                    shortcuts.Add(shortcutOne);

                    if (shortcuts.Count == shortcutManager.MaxShortcutCountPerActivity)
                    {
                        break;
                    }
                }
                shortcutManager.SetDynamicShortcuts(shortcuts);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a dynamic shortcut that will open the app and display the relevant info
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="flightNumber"></param>
        public void CreateFlightShortcut(object sender, string flightNumber)
        {
            // Create a shortcut builder
            var shortcutBuilder = new ShortcutInfo.Builder(this, flightNumber);

            // Set the shortcut info
            shortcutBuilder.SetIcon(Android.Graphics.Drawables.Icon.CreateWithResource(this, Resource.Drawable.flight));
            shortcutBuilder.SetShortLabel($"Flight {flightNumber}");
            shortcutBuilder.SetLongLabel($"Flight {flightNumber}");
            Intent openFlightInfoIntent = new Intent(
                Intent.ActionView,                                                      // Action
                Android.Net.Uri.Parse($"content:////actions//flight-{flightNumber}"),   // Intent data
                this,                                                                   // Context
                typeof(MainActivity));                                                  // Activity class

            shortcutBuilder.SetIntent(openFlightInfoIntent);

            // Build the shortcut
            ShortcutInfo myFlightShortcut = shortcutBuilder.Build();

            // Get the shortcut manager
            ShortcutManager shortcutManager = (ShortcutManager)this.GetSystemService(Java.Lang.Class.FromType(typeof(ShortcutManager)));

            // Set the list of shortcuts as the App's shortcut
            shortcutManager.SetDynamicShortcuts(new System.Collections.Generic.List <ShortcutInfo>()
            {
                myFlightShortcut
            });
        }
Ejemplo n.º 4
0
        private void SetShortcut()
        {
            List <ShortcutInfo> shortcutInfoList = new List <ShortcutInfo>();

            var shortcuts = data.Where(x => x.IsShortcut == true)
                            .OrderBy(x => x.ID).ToList();
            int count = (shortcuts.Count > 4) ? 4 : shortcuts.Count;

            for (int i = 0; i < count; i++)
            {
                Intent intent = new Intent();
                intent.SetClass(this, typeof(WakeActivity));
                intent.SetAction(Intent.ActionMain);
                intent.PutExtra("BroadcastAddress", shortcuts[i].BroadcastAddress);
                intent.PutExtra("MacAddress", shortcuts[i].MacAddress);
                intent.PutExtra("Port", shortcuts[i].Port);
                intent.PutExtra("SendingCount", shortcuts[i].SendingCount);
                ShortcutInfo info = new ShortcutInfo.Builder(this, $"intent{i}")
                                    .SetRank(i)
                                    .SetIcon(Icon.CreateWithResource(this, Resource.Drawable.device))
                                    .SetShortLabel(shortcuts[i].Name)
                                    .SetLongLabel(shortcuts[i].Name)
                                    .SetIntent(intent)
                                    .Build();
                shortcutInfoList.Add(info);
            }

            ShortcutManager shortcutManager = (ShortcutManager)GetSystemService(Context.ShortcutService);

            shortcutManager.RemoveAllDynamicShortcuts();
            shortcutManager.SetDynamicShortcuts(shortcutInfoList);
        }
        ShortcutInfo.Builder SetExtras(ShortcutInfo.Builder b)
        {
            var extras = new PersistableBundle();

            extras.PutLong(EXTRA_LAST_REFRESH, Java.Lang.JavaSystem.CurrentTimeMillis());
            b.SetExtras(extras);
            return(b);
        }
Ejemplo n.º 6
0
        // TODO move wifi staff to some other panel, wifi and shortcuts do not belong to same logical group
        ShortcutInfo.Builder CreateBasicShortcut()
        {
            var builder = new ShortcutInfo.Builder(ShortcutId)
                          .SetIcon(_icon)
                          .SetRank(1)
                          .SetLongLabel("Long Label");

            return(builder);
        }
Ejemplo n.º 7
0
        private ShortcutInfo androidShortcut(ApplicationShortcut shortcut)
        {
            var droidShortcut =
                new ShortcutInfo.Builder(context, shortcut.Title)
                .SetLongLabel($"{shortcut.Title} {shortcut.Subtitle}")
                .SetShortLabel(shortcut.Title)
                .SetIcon(getIcon(shortcut.Type))
                .SetIntent(new Intent(Intent.ActionView).SetData(Uri.Parse(shortcut.Url)))
                .Build();

            return(droidShortcut);
        }
        ShortcutInfo.Builder SetSiteInformation(ShortcutInfo.Builder b, Uri uri)
        {
            // TODO Get the actual site <title> and use it.
            // TODO Set the current locale to accept-language to get localized title.
            b.SetShortLabel(uri.Host);
            b.SetLongLabel(uri.ToString());

            var bmp = FetchFavicon(uri);

            b.SetIcon(bmp != null ? Icon.CreateWithBitmap(bmp) : Icon.CreateWithResource(mContext, Resource.Drawable.link));

            return(b);
        }
        ShortcutInfo CreateShortcutForUrl(string urlAsString)
        {
            Log.Info(TAG, "createShortcutForUrl: " + urlAsString);

            var b = new ShortcutInfo.Builder(mContext, urlAsString);

            var uri = Uri.Parse(urlAsString);

            b.SetIntent(new Intent(Intent.ActionView, uri));

            SetSiteInformation(b, uri);
            SetExtras(b);

            return(b.Build());
        }
            protected override Void RunInBackground(params bool[] @params)
            {
                Log.Info(TAG, "refreshingShortcuts...");

                var force = @params[0];

                var now            = Java.Lang.JavaSystem.CurrentTimeMillis();
                var staleThreshold = force ? now : now - REFRESH_INTERVAL_MS;

                // Check all existing dynamic and pinned shortcut, and if their last refresh
                // time is older than a certain threshold, update them.

                List <ShortcutInfo> updateList = new List <ShortcutInfo>();

                foreach (var shortcut in updateList)
                {
                    if (shortcut.IsImmutable)
                    {
                        continue;
                    }

                    var extras = shortcut.Extras;

                    if (extras != null && extras.GetLong(EXTRA_LAST_REFRESH) >= staleThreshold)
                    {
                        // Shortcut still fresh.
                        continue;
                    }

                    Log.Info(TAG, "Refreshing shortcut: " + shortcut.Id);

                    var b = new ShortcutInfo.Builder(Context, shortcut.Id);

                    Helper.SetSiteInformation(b, shortcut.Intent.Data);

                    Helper.SetExtras(b);

                    updateList.Add(b.Build());
                }

                // Call update.
                if ((ShortcutManager != null) && (updateList.Count > 0))
                {
                    Helper.CallShortcutManager(() => ShortcutManager.UpdateShortcuts(updateList));
                }

                return(null);
            }
        public async Task AddShortcut(Shortcut shortcut)
        {
            if (!_isShortcutsSupported)
            {
                throw new NotSupportedOnDeviceException(NOT_SUPPORTED_ERROR_MESSAGE);
            }


            var context = Application.Context;
            var builder = new ShortcutInfo.Builder(context, shortcut.ShortcutId);

            var uri = AUri.Parse(shortcut.Uri);

            builder.SetIntent(new Intent(Intent.ActionView, uri));
            builder.SetShortLabel(shortcut.Label);
            builder.SetLongLabel(shortcut.Description);

            var icon = await CreateIcon(shortcut.Icon);

            if (icon != null)
            {
                builder.SetIcon(icon);
            }

            var scut = builder.Build();

            if (_manager.DynamicShortcuts == null || !_manager.DynamicShortcuts.Any())
            {
                _manager.SetDynamicShortcuts(new List <ShortcutInfo> {
                    scut
                });
            }
            else
            {
                _manager.AddDynamicShortcuts(new List <ShortcutInfo> {
                    scut
                });
            }
        }
Ejemplo n.º 12
0
        internal static async Task <ShortcutInfo> ToShortcutInfoAsync(this JumpListItem jumpListItem)
        {
            var builder = new ShortcutInfo.Builder(Application.Context, jumpListItem.Arguments);
            var pm      = Application.Context.PackageManager;
            var intent  = pm.GetLaunchIntentForPackage(Application.Context.PackageName);

            intent.PutExtra(JumpListItem.ArgumentsExtraKey, jumpListItem.Arguments);
            builder.SetIntent(intent);
            builder.SetShortLabel(
                !string.IsNullOrEmpty(jumpListItem.DisplayName) ?
                jumpListItem.DisplayName : " ");                         //Android requires non-empty DisplayName
            if (!string.IsNullOrEmpty(jumpListItem.Description))
            {
                builder.SetLongLabel(jumpListItem.Description);
            }

            var persistableBundle = new PersistableBundle();

            persistableBundle.PutString(JumpListItem.UnoShortcutKey, "true");

            if (jumpListItem.Logo != null)
            {
                persistableBundle.PutString(JumpListItem.ImagePathKey, jumpListItem.Logo.ToString());
                var imageResourceId = DrawableHelper.FindResourceId(jumpListItem.Logo.LocalPath);
                if (imageResourceId != null)
                {
                    var bitmap = await BitmapFactory.DecodeResourceAsync(
                        ContextHelper.Current.Resources,
                        imageResourceId.Value,
                        new BitmapFactory.Options());

                    builder.SetIcon(Icon.CreateWithBitmap(bitmap));
                }
            }

            builder.SetExtras(persistableBundle);

            return(builder.Build());
        }
Ejemplo n.º 13
0
        private void SetShortcut()
        {
            List <ShortcutInfo> shortcutInfoList = new List <ShortcutInfo>();

            Intent page1 = new Intent();

            page1.SetClass(this, typeof(ShortcutContainerActivity));
            page1.SetAction(Intent.ActionMain);
            page1.PutExtra("PageName", "Page1");
            ShortcutInfo page1Info = new ShortcutInfo.Builder(this, "Page1")
                                     .SetRank(0)
                                     .SetIcon(Icon.CreateWithResource(this, Resource.Drawable.Page1))
                                     .SetShortLabel("Page1")
                                     .SetLongLabel("Page1")
                                     .SetIntent(page1)
                                     .Build();

            shortcutInfoList.Add(page1Info);

            Intent page2 = new Intent();

            page2.SetClass(this, typeof(ShortcutContainerActivity));
            page2.SetAction(Intent.ActionMain);
            page2.PutExtra("PageName", "Page2");
            ShortcutInfo page2Info = new ShortcutInfo.Builder(this, "Page2")
                                     .SetRank(1)
                                     .SetIcon(Icon.CreateWithResource(this, Resource.Drawable.Page2))
                                     .SetShortLabel("Page2")
                                     .SetLongLabel("Page2")
                                     .SetIntent(page2)
                                     .Build();

            shortcutInfoList.Add(page2Info);

            ShortcutManager shortcutManager = (ShortcutManager)GetSystemService(Context.ShortcutService);

            shortcutManager.SetDynamicShortcuts(shortcutInfoList);
        }
        public override Task UpdateAsync(bool showAddItemShortcuts)
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.NMr1)
            {
                return(Task.CompletedTask);
            }

            var shortcutManager = (ShortcutManager)Application.Context.GetSystemService(Context.ShortcutService);

            if (shortcutManager == null)
            {
                return(Task.CompletedTask);
            }

            List <ShortcutInfo> shortcuts = new List <ShortcutInfo>();

            // Add task shortcut
            var addTaskIntent = new Intent(Application.Context, typeof(MainActivity));

            addTaskIntent.SetData(Android.Net.Uri.Parse("powerplanner:?" + new QuickAddTaskToCurrentAccountArguments().SerializeToString()));
            addTaskIntent.SetAction(Intent.ActionView);

            ShortcutInfo addTaskShortcut = new ShortcutInfo.Builder(Application.Context, "add-task")
                                           .SetShortLabel(PowerPlannerResources.GetString("String_Task"))   // "Task"
                                           .SetLongLabel(PowerPlannerResources.GetString("String_AddTask")) // "Add task" (this one is almost always used)
                                           .SetIcon(Icon.CreateWithResource(Application.Context, Resource.Drawable.ic_add_24px))
                                           .SetIntent(addTaskIntent)
                                           .Build();

            shortcuts.Add(addTaskShortcut);

            // Add event shortcut
            var addEventIntent = new Intent(Application.Context, typeof(MainActivity));

            addEventIntent.SetData(Android.Net.Uri.Parse("powerplanner:?" + new QuickAddEventToCurrentAccountArguments().SerializeToString()));
            addEventIntent.SetAction(Intent.ActionView);

            ShortcutInfo addEventShortcut = new ShortcutInfo.Builder(Application.Context, "add-event")
                                            .SetShortLabel(PowerPlannerResources.GetString("String_Event"))   // "Event"
                                            .SetLongLabel(PowerPlannerResources.GetString("String_AddEvent")) // "Add event"
                                            .SetIcon(Icon.CreateWithResource(Application.Context, Resource.Drawable.ic_add_24px))
                                            .SetIntent(addEventIntent)
                                            .Build();

            shortcuts.Add(addEventShortcut);

            var currShortcuts = shortcutManager.DynamicShortcuts;

            if (currShortcuts.Count == 2)
            {
                // Update existing
                shortcutManager.UpdateShortcuts(shortcuts);
            }
            else
            {
                // Add/reset
                if (currShortcuts.Count != 0)
                {
                    shortcutManager.RemoveAllDynamicShortcuts();
                }

                shortcutManager.AddDynamicShortcuts(shortcuts);
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 15
0
        public static async Task UpdateShortcuts()
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.NMr1)
            {
                var wm = WeatherManager.GetInstance();

                var locations = new List <LocationData>(await Settings.GetLocationData());
                if (Settings.FollowGPS)
                {
                    locations.Insert(0, Settings.HomeData);
                }

                ShortcutManager      shortcutMan = (ShortcutManager)App.Context.GetSystemService(Java.Lang.Class.FromType(typeof(ShortcutManager)));
                IList <ShortcutInfo> shortcuts   = new List <ShortcutInfo>();

                shortcutMan.RemoveAllDynamicShortcuts();

                int MAX_SHORTCUTS = 4;
                if (locations.Count < MAX_SHORTCUTS)
                {
                    MAX_SHORTCUTS = locations.Count;
                }

                for (int i = 0; i < MAX_SHORTCUTS; i++)
                {
                    LocationData location = locations[i];
                    Weather      weather  = await Settings.GetWeatherData(location.query);

                    if (weather == null || shortcuts.Any(s => s.Id == location.query))
                    {
                        locations.RemoveAt(i);
                        i--;
                        if (locations.Count < MAX_SHORTCUTS)
                        {
                            MAX_SHORTCUTS = locations.Count;
                        }
                        continue;
                    }

                    // Start WeatherNow Activity with weather data
                    Intent intent = new Intent(App.Context, typeof(MainActivity))
                                    .SetAction(Intent.ActionMain)
                                    .PutExtra("shortcut-data", location.ToJson())
                                    .SetFlags(ActivityFlags.NewTask | ActivityFlags.MultipleTask | ActivityFlags.NoHistory);

                    var bmp = await BitmapFactory.DecodeResourceAsync(App.Context.Resources, wm.GetWeatherIconResource(weather.condition.icon),
                                                                      new BitmapFactory.Options()
                    {
                        InMutable = true
                    });

                    var   newImage = Bitmap.CreateBitmap(bmp.Width, bmp.Height, bmp.GetConfig());
                    Paint paint    = new Paint();
                    paint.SetColorFilter(new PorterDuffColorFilter(new Color(ContextCompat.GetColor(App.Context, Resource.Color.colorPrimary)),
                                                                   PorterDuff.Mode.SrcIn));
                    var canvas = new Canvas(newImage);
                    canvas.DrawBitmap(bmp, 0, 0, paint);
                    bmp.Recycle();
                    ShortcutInfo shortcut = new ShortcutInfo.Builder(App.Context, location.query)
                                            .SetShortLabel(weather.location.name)
                                            .SetIcon(Icon.CreateWithBitmap(newImage))
                                            .SetIntent(intent)
                                            .Build();

                    shortcuts.Add(shortcut);
                }

                shortcutMan.SetDynamicShortcuts(shortcuts);
            }
        }