package cgeo.geocaching.apps.navi;

import cgeo.geocaching.apps.navi.NavigationAppFactory.NavigationAppsEnum;
import cgeo.geocaching.models.Geocache;
import cgeo.geocaching.ui.AbstractMenuActionProvider;

import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;

import androidx.core.view.ActionProvider;
import androidx.core.view.MenuItemCompat;

/**
 * Action provider listing all available navigation actions as sub menu.
 */
public class NavigationSelectionActionProvider extends AbstractMenuActionProvider {

    private Geocache geocache;
    private final Activity activity;

    public NavigationSelectionActionProvider(final Activity activity) {
        super(activity);
        this.activity = activity;
    }

    public void setTarget(final Geocache cache) {
        geocache = cache;
    }

    @Override
    public void onPrepareSubMenu(final SubMenu subMenu) {
        subMenu.clear();
        if (geocache == null || geocache.getCoords() == null) {
            return;
        }
        for (final NavigationAppsEnum app : NavigationAppFactory.getActiveNavigationApps()) {
            if (!(app.app instanceof CacheNavigationApp)) {
                continue;
            }
            final CacheNavigationApp cacheApp = (CacheNavigationApp) app.app;
            if (app.app.isEnabled(geocache)) {
                subMenu.add(Menu.NONE, app.id, Menu.NONE, app.app.getName()).setOnMenuItemClickListener(item -> {
                    cacheApp.navigate(activity, geocache);
                    return true;
                });
            }
        }
    }

    public static void initialize(final MenuItem menuItem, final Geocache cache) {
        final ActionProvider actionProvider = MenuItemCompat.getActionProvider(menuItem);
        if (actionProvider instanceof NavigationSelectionActionProvider) {
            final NavigationSelectionActionProvider navigateAction = (NavigationSelectionActionProvider) actionProvider;
            navigateAction.setTarget(cache);
        }
    }

}

@@ -201,36 +201,141 @@
 er;%0A
-%0Aimport android.app.Activity
+import cgeo.geocaching.utils.Log;%0A%0Aimport android.app.Activity;%0Aimport android.content.Context;%0Aimport android.content.ContextWrapper
 ;%0Aim
@@ -746,73 +746,423 @@
     
-public NavigationSelectionActionProvider(final Activity a
+/** Constructor MUST (!) be of type Context, NOT Activity! Otherwise it can't be instantiated by Android infrastructure. See #11251 */%0A    public NavigationSelectionActionProvider(final Context context) %7B%0A        super(context);%0A%0A        //try to extract an Activity from given Context%0A        Context baseContext = context;%0A        while (baseContext instanceof ContextWrapper && !(baseContext instanceof A
 ctivity)
  %7B%0A 
@@ -1157,16 +1157,17 @@
 ctivity)
+)
  %7B%0A     
@@ -1173,23 +1173,172 @@
     
+    
-super(activity)
+baseContext = ((ContextWrapper) baseContext).getBaseContext();%0A        %7D%0A        this.activity = baseContext instanceof Activity ? (Activity) baseContext : null
 ;%0A  
@@ -1335,32 +1335,36 @@
 : null;%0A        
+if (
 this.activity = 
@@ -1362,26 +1362,290 @@
 tivity =
- activity;
+= null) %7B%0A            Log.w(%22NavigationSelectionActionProvider called with Non-Activity-class: %22 +%0A                (context == null ? %22null%22 : context.getClass().getName()) + %22/%22 +%0A                (baseContext == null ? %22null%22 : baseContext.getClass().getName()));%0A        %7D
 %0A    %7D%0A%0A
@@ -2233,16 +2233,41 @@
 eocache)
+ && this.activity != null
 ) %7B%0A    

fix #11251: make NavigationSelectionActionProvider constructure take Context parameter (#11397)