Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToastHelper with ability to override toasts display #21892

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ public void run(InAppPurchaseHelper helper) {
public void purchaseContourLines(@NonNull Activity activity) throws UnsupportedOperationException {
OsmandPlugin plugin = PluginsHelper.getPlugin(SRTMPlugin.class);
if (plugin == null || plugin.getInstallURL() == null) {
Toast.makeText(activity.getApplicationContext(),
activity.getString(R.string.activate_srtm_plugin), Toast.LENGTH_LONG).show();
AndroidUtils.getApp(activity).showToastMessage(R.string.activate_srtm_plugin);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(plugin.getInstallURL()));
AndroidUtils.startActivityIfSafe(activity, intent);
Expand Down
4 changes: 2 additions & 2 deletions OsmAnd/src-huawei/net/osmand/plus/inapp/ExceptionHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public static int handle(@Nullable Activity activity, Exception e) {
}
}

private static void showToast(@Nullable Activity activity, String s) {
private static void showToast(@Nullable Activity activity, String text) {
if (AndroidUtils.isActivityNotDestroyed(activity)) {
Toast.makeText(activity, s, Toast.LENGTH_SHORT).show();
AndroidUtils.getApp(activity).showShortToastMessage(text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.FileProvider;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.R;
Expand Down Expand Up @@ -110,22 +111,16 @@ protected void endThreadOperation(int operationId, Exception e) {
}
if (operationId == DOWNLOAD_BUILDS_LIST) {
if (e != null) {
Toast.makeText(this, getString(R.string.loading_builds_failed) + " : " + e.getMessage(), Toast.LENGTH_LONG).show();
getMyApplication().showToastMessage(getString(R.string.loading_builds_failed) + " : " + e.getMessage());
finish();
} else {
setListAdapter(new OsmandBuildsAdapter(downloadedBuilds));
}
} else if (operationId == INSTALL_BUILD) {
if (currentSelectedBuild != null) {
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", pathToDownload);
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
} else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(pathToDownload), "application/vnd.android.package-archive");
}
Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", pathToDownload);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, ACTIVITY_TO_INSTALL);
updateInstalledApp(false, currentSelectedBuild.date);
Expand All @@ -142,13 +137,12 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

private void updateInstalledApp(boolean showMessage, Date d) {
OsmandApplication app = getMyApplication();
if (showMessage) {
Toast.makeText(this,
MessageFormat.format(getString(R.string.build_installed), currentSelectedBuild.tag,
AndroidUtils.formatDateTime(getMyApplication(), currentSelectedBuild.date.getTime())),
Toast.LENGTH_LONG).show();
app.showToastMessage(MessageFormat.format(getString(R.string.build_installed),
currentSelectedBuild.tag, AndroidUtils.formatDateTime(app, currentSelectedBuild.date.getTime())));
}
getMyApplication().getSettings().CONTRIBUTION_INSTALL_APP_DATE.set(dateFormat.format(d));
app.getSettings().CONTRIBUTION_INSTALL_APP_DATE.set(dateFormat.format(d));
}

protected void executeThreadOperation(int operationId) throws Exception {
Expand Down
4 changes: 2 additions & 2 deletions OsmAnd/src/net/osmand/plus/NavigationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ public void onLocationResult(@NonNull List<net.osmand.Location> locations) {
});
}
} catch (SecurityException e) {
Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show();
getApp().showToastMessage(R.string.no_location_permission);
} catch (IllegalArgumentException e) {
Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show();
getApp().showToastMessage(R.string.gps_not_available);
}
}

Expand Down
101 changes: 34 additions & 67 deletions OsmAnd/src/net/osmand/plus/OsmandApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.car.app.CarToast;
import androidx.core.app.ActivityCompat;
import androidx.annotation.StringRes;
import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import net.osmand.plus.configmap.tracks.TrackSortModesHelper;
import net.osmand.plus.plugins.OsmandPlugin;
import net.osmand.plus.shared.OsmAndContextImpl;
import net.osmand.PlatformUtil;
import net.osmand.aidl.OsmandAidlApi;
import net.osmand.data.LatLon;
Expand All @@ -55,24 +52,15 @@
import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.base.dialog.DialogManager;
import net.osmand.plus.configmap.routes.RouteLayersHelper;
import net.osmand.plus.configmap.tracks.TrackSortModesHelper;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.DownloadService;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.feedback.AnalyticsHelper;
import net.osmand.plus.feedback.FeedbackHelper;
import net.osmand.plus.feedback.RateUsHelper;
import net.osmand.plus.feedback.RateUsState;
import net.osmand.plus.helpers.AndroidApiLocationServiceHelper;
import net.osmand.plus.helpers.ColorPaletteHelper;
import net.osmand.plus.helpers.DayNightHelper;
import net.osmand.plus.helpers.GmsLocationServiceHelper;
import net.osmand.plus.helpers.LauncherShortcutsHelper;
import net.osmand.plus.helpers.LocaleHelper;
import net.osmand.plus.helpers.LocationServiceHelper;
import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.Model3dHelper;
import net.osmand.plus.helpers.TargetPointsHelper;
import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.helpers.*;
import net.osmand.plus.importfiles.ImportHelper;
import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.keyevent.InputDevicesHelper;
Expand All @@ -81,9 +69,9 @@
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.measurementtool.MeasurementEditingContext;
import net.osmand.plus.myplaces.favorites.FavouritesHelper;
import net.osmand.plus.nearbyplaces.NearbyPlacesHelper;
import net.osmand.plus.notifications.NotificationHelper;
import net.osmand.plus.onlinerouting.OnlineRoutingHelper;
import net.osmand.plus.plugins.OsmandPlugin;
import net.osmand.plus.plugins.PluginsHelper;
import net.osmand.plus.plugins.accessibility.AccessibilityMode;
import net.osmand.plus.plugins.accessibility.AccessibilityPlugin;
Expand All @@ -108,6 +96,7 @@
import net.osmand.plus.settings.backend.backup.FileSettingsHelper;
import net.osmand.plus.settings.enums.DrivingRegion;
import net.osmand.plus.settings.enums.LocationSource;
import net.osmand.plus.shared.OsmAndContextImpl;
import net.osmand.plus.simulation.OsmAndLocationSimulation;
import net.osmand.plus.track.helpers.GpsFilterHelper;
import net.osmand.plus.track.helpers.GpxDisplayHelper;
Expand Down Expand Up @@ -135,13 +124,7 @@
import net.osmand.util.Algorithms;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import btools.routingapp.BRouterServiceConnection;
Expand All @@ -161,12 +144,13 @@ public class OsmandApplication extends MultiDexApplication {

NavigationCarAppService navigationCarAppService;
NavigationSession carNavigationSession;
ActivityCompat.OnRequestPermissionsResultCallback androidAutoPermissionRequestResultListener;
OnRequestPermissionsResultCallback carAppPermissionListener;

private final SQLiteAPI sqliteAPI = new SQLiteAPIImpl(this);
private final OsmAndTaskManager taskManager = new OsmAndTaskManager(this);
private final UiUtilities iconsCache = new UiUtilities(this);
private final LocaleHelper localeHelper = new LocaleHelper(this);
private final ToastHelper toastHelper = new ToastHelper(this);

// start variables
ResourceManager resourceManager;
Expand Down Expand Up @@ -345,6 +329,11 @@ protected void onPostExecute(Void result) {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

@NonNull
public Handler getUiHandler() {
return uiHandler;
}

@NonNull
public UiUtilities getUIUtilities() {
return iconsCache;
Expand Down Expand Up @@ -679,8 +668,8 @@ public CommandPlayer getPlayer() {
}

public void initVoiceCommandPlayer(@NonNull Context context, @NonNull ApplicationMode appMode,
@Nullable Runnable onCommandPlayerCreated, boolean warnNoProvider,
boolean showProgress, boolean forceInitialization, boolean applyAllModes) {
@Nullable Runnable onCommandPlayerCreated, boolean warnNoProvider,
boolean showProgress, boolean forceInitialization, boolean applyAllModes) {
String voiceProvider = settings.VOICE_PROVIDER.getModeValue(appMode);
if (OsmandSettings.VOICE_PROVIDER_NOT_USE.equals(voiceProvider)) {
settings.VOICE_MUTE.setModeValue(appMode, true);
Expand Down Expand Up @@ -709,17 +698,17 @@ public NavigationCarAppService getNavigationCarAppService() {
return navigationCarAppService;
}

public void setNavigationCarAppService(@Nullable NavigationCarAppService navigationCarAppService) {
this.navigationCarAppService = navigationCarAppService;
public void setNavigationCarAppService(@Nullable NavigationCarAppService carAppService) {
this.navigationCarAppService = carAppService;
}

@Nullable
public ActivityCompat.OnRequestPermissionsResultCallback getAndroidAutoPermissionRequestResultListener() {
return androidAutoPermissionRequestResultListener;
public OnRequestPermissionsResultCallback getCarAppPermissionListener() {
return carAppPermissionListener;
}

public void setAndroidAutoPermissionRequestResultListener(@Nullable ActivityCompat.OnRequestPermissionsResultCallback callback) {
this.androidAutoPermissionRequestResultListener = callback;
public void setCarAppPermissionListener(@Nullable OnRequestPermissionsResultCallback callback) {
this.carAppPermissionListener = callback;
}

@Nullable
Expand Down Expand Up @@ -805,46 +794,24 @@ public MapMarkersDbHelper getMapMarkersDbHelper() {
return mapMarkersDbHelper;
}

public void showShortToastMessage(int msgId, Object... args) {
uiHandler.post(() -> {
Toast.makeText(this, getString(msgId, args), Toast.LENGTH_SHORT).show();
NavigationSession carNavigationSession = this.carNavigationSession;
if (carNavigationSession != null && carNavigationSession.hasStarted()) {
CarToast.makeText(carNavigationSession.getCarContext(), getString(msgId, args), CarToast.LENGTH_SHORT).show();
}
});
public ToastHelper getToastHelper() {
return toastHelper;
}

public void showShortToastMessage(String msg) {
uiHandler.post(() -> {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
NavigationSession carNavigationSession = this.carNavigationSession;
if (carNavigationSession != null && carNavigationSession.hasStarted()) {
CarToast.makeText(carNavigationSession.getCarContext(), msg, CarToast.LENGTH_SHORT).show();
}
});
public void showToastMessage(@Nullable String text) {
toastHelper.showToast(text, true);
}

public void showToastMessage(int msgId, Object... args) {
uiHandler.post(() -> {
Toast.makeText(this, getString(msgId, args), Toast.LENGTH_LONG).show();
NavigationSession carNavigationSession = this.carNavigationSession;
if (carNavigationSession != null && carNavigationSession.hasStarted()) {
CarToast.makeText(carNavigationSession.getCarContext(), getString(msgId, args), CarToast.LENGTH_LONG).show();
}
});
public void showToastMessage(@StringRes int textId, Object... args) {
toastHelper.showToast(textId, true, args);
}

public void showToastMessage(@Nullable String text) {
if (!Algorithms.isEmpty(text)) {
uiHandler.post(() -> {
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
NavigationSession carNavigationSession = this.carNavigationSession;
if (carNavigationSession != null && carNavigationSession.hasStarted()) {
CarToast.makeText(carNavigationSession.getCarContext(), text, CarToast.LENGTH_LONG).show();
}
});
}
public void showShortToastMessage(@Nullable String text) {
toastHelper.showToast(text, false);
}

public void showShortToastMessage(@StringRes int textId, Object... args) {
toastHelper.showToast(textId, false, args);
}

public SQLiteAPI getSQLiteAPI() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ private ContextMenuItem createOsmAndVersionDrawerItem() {
String text = releaseText == null
? osmAndVersion
: app.getString(R.string.ltr_or_rtl_combine_via_comma, osmAndVersion, releaseText);
ShareMenu.copyToClipboardWithToast(app, text, Toast.LENGTH_SHORT);
ShareMenu.copyToClipboardWithToast(app, text, false);
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void runChosenGPSStatus(GpsStatusApps g) {
builder.setNegativeButton(mapActivity.getString(R.string.shared_string_no), null);
builder.show();
} else {
Toast.makeText(mapActivity, R.string.gps_status_app_not_found, Toast.LENGTH_LONG).show();
app.showToastMessage(R.string.gps_status_app_not_found);
}
}

Expand Down
4 changes: 2 additions & 2 deletions OsmAnd/src/net/osmand/plus/auto/NavigationCarAppService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onCreate() {
super.onCreate();
getApp().setAndroidAutoPermissionRequestResultListener(this);
getApp().setCarAppPermissionListener(this);
}

@Override
public void onDestroy() {
super.onDestroy();
getApp().setAndroidAutoPermissionRequestResultListener(null);
getApp().setCarAppPermissionListener(null);
getApp().setNavigationCarAppService(null);
}

Expand Down
9 changes: 5 additions & 4 deletions OsmAnd/src/net/osmand/plus/auto/NavigationSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ public Screen onCreateScreen(@NonNull Intent intent) {

String action = intent.getAction();
if (ACTION_NAVIGATE.equals(action)) {
CarToast.makeText(getCarContext(), "Navigation intent: " + intent.getDataString(), CarToast.LENGTH_LONG).show();
String text = "Navigation intent: " + intent.getDataString();
getApp().getToastHelper().showCarToast(text, true);
}

landingScreen = new LandingScreen(getCarContext(), settingsAction);
Expand Down Expand Up @@ -566,9 +567,9 @@ public void onLocationResult(@NonNull List<net.osmand.Location> locations) {
});
}
} catch (SecurityException e) {
Toast.makeText(getCarContext(), R.string.no_location_permission, Toast.LENGTH_LONG).show();
getApp().showToastMessage(R.string.no_location_permission);
} catch (IllegalArgumentException e) {
Toast.makeText(getCarContext(), R.string.gps_not_available, Toast.LENGTH_LONG).show();
getApp().showToastMessage(R.string.gps_not_available);
}
}

Expand Down Expand Up @@ -614,7 +615,7 @@ public void onStopNavigation() {

@Override
public void onAutoDriveEnabled() {
CarToast.makeText(carContext, "Auto drive enabled", CarToast.LENGTH_LONG).show();
getApp().getToastHelper().showCarToast("Auto drive enabled", true);
if (!settings.simulateNavigation) {
OsmAndLocationSimulation sim = getApp().getLocationProvider().getLocationSimulation();
sim.startStopRouteAnimation(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@ public Template onGetTemplate() {
// pressing the select button again.
builder.setPanModeListener(isInPanMode -> {
if (isInPanMode) {
CarToast.makeText(getCarContext(),
R.string.exit_pan_mode_descr,
CarToast.LENGTH_LONG).show();
getApp().getToastHelper().showCarToast(getApp().getString(R.string.exit_pan_mode_descr), true);
}
panMode = isInPanMode;
invalidate();
Expand Down
4 changes: 2 additions & 2 deletions OsmAnd/src/net/osmand/plus/avoidroads/AvoidRoadsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void addImpassableRoad(@Nullable MapActivity mapActivity, @NonNull LatLon
public boolean publish(RouteDataObject object) {
if (object == null) {
if (mapActivity != null) {
Toast.makeText(mapActivity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show();
app.showToastMessage(R.string.error_avoid_specific_road);
}
} else {
AvoidRoadInfo avoidRoadInfo = getOrCreateAvoidRoadInfo(object, ll.getLatitude(), ll.getLongitude(), appMode.getStringKey());
Expand Down Expand Up @@ -209,7 +209,7 @@ public void replaceImpassableRoad(@Nullable MapActivity activity,
public boolean publish(RouteDataObject object) {
if (object == null) {
if (activity != null) {
Toast.makeText(activity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show();
app.showToastMessage(R.string.error_avoid_specific_road);
}
if (callback != null) {
callback.onAddImpassableRoad(false, null);
Expand Down
2 changes: 1 addition & 1 deletion OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ public int dpToPx(float dp) {
}

protected void copyToClipboard(@NonNull String text, @NonNull Context ctx) {
ShareMenu.copyToClipboardWithToast(ctx, text, Toast.LENGTH_SHORT);
ShareMenu.copyToClipboardWithToast(ctx, text, false);
}

public static boolean showInstance(@NonNull FragmentManager manager, @NonNull ContextMenuFragment fragment) {
Expand Down
Loading
Loading