Skip to content

Commit

Permalink
Implemented start service when permission granted (osmandapp/OsmAnd-I…
Browse files Browse the repository at this point in the history
  • Loading branch information
Corwin-Kh committed Oct 24, 2024
1 parent 7336d26 commit b69242d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
36 changes: 23 additions & 13 deletions OsmAnd/src/net/osmand/plus/auto/NavigationCarAppService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.osmand.plus.auto;

import android.Manifest;
import android.app.Notification;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
Expand All @@ -16,7 +17,9 @@
import net.osmand.plus.OsmAndLocationProvider;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.notifications.OsmandNotification.NotificationType;
import net.osmand.plus.utils.AndroidUtils;

import java.util.Arrays;
import java.util.List;

/**
* Entry point for the templated app.
Expand All @@ -28,6 +31,7 @@
public final class NavigationCarAppService extends CarAppService {

private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(NavigationCarAppService.class);
private boolean foreground = false;

private OsmandApplication getApp() {
return (OsmandApplication) getApplication();
Expand All @@ -41,13 +45,6 @@ public static Uri createDeepLinkUri(@NonNull String deepLinkAction) {
return Uri.fromParts(NavigationSession.URI_SCHEME, NavigationSession.URI_HOST, deepLinkAction);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);
getApp().setNavigationCarAppService(this);
return result;
}

@Override
public void onDestroy() {
super.onDestroy();
Expand All @@ -58,11 +55,8 @@ public void onDestroy() {
@NonNull
public Session onCreateSession() {
OsmandApplication app = getApp();
Notification notification = app.getNotificationHelper().buildCarAppNotification();
if(OsmAndLocationProvider.isLocationPermissionAvailable(app)) {
startForeground(app.getNotificationHelper().getOsmandNotificationId(NotificationType.CAR_APP), notification);
}

getApp().setNavigationCarAppService(this);
startForegroundWithPermission(app);
NavigationSession session = new NavigationSession();
session.getLifecycle()
.addObserver(new DefaultLifecycleObserver() {
Expand All @@ -75,6 +69,22 @@ public void onDestroy(@NonNull LifecycleOwner owner) {
return session;
}

private void startForegroundWithPermission(OsmandApplication app) {
if (!foreground && OsmAndLocationProvider.isLocationPermissionAvailable(app)) {
foreground = true;
Notification notification = app.getNotificationHelper().buildCarAppNotification();
startForeground(app.getNotificationHelper().getOsmandNotificationId(NotificationType.CAR_APP), notification);
}
}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
List<String> permissionsList = Arrays.asList(permissions);
if (permissionsList.contains(Manifest.permission.ACCESS_FINE_LOCATION) ||
permissionsList.contains(Manifest.permission.ACCESS_COARSE_LOCATION)) {
startForegroundWithPermission(getApp());
}
}

@NonNull
@Override
public HostValidator createHostValidator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.MapActivityActions;
import net.osmand.plus.auto.NavigationCarAppService;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.firstusage.FirstUsageWizardFragment;
import net.osmand.plus.plugins.PluginsHelper;
Expand Down Expand Up @@ -41,6 +42,11 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
controlsHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

NavigationCarAppService navigationCarAppService = app.getNavigationCarAppService();
if (navigationCarAppService != null) {
navigationCarAppService.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

if (requestCode == DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
&& permissions.length > 0
&& Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])) {
Expand Down

0 comments on commit b69242d

Please sign in to comment.