-
Notifications
You must be signed in to change notification settings - Fork 0
Selectable location providers #124
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
Open
jackpf
wants to merge
5
commits into
main
Choose a base branch
from
selectable-providers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,9 +15,11 @@ | |||||||||
| import java.util.function.Consumer; | ||||||||||
| import java.util.function.Function; | ||||||||||
| import java.util.stream.Collectors; | ||||||||||
| import java.util.stream.Stream; | ||||||||||
|
|
||||||||||
| import lombok.AllArgsConstructor; | ||||||||||
| import lombok.Getter; | ||||||||||
| import lombok.ToString; | ||||||||||
|
|
||||||||||
| public class LocationService { | ||||||||||
| private final LocationManager locationManager; | ||||||||||
|
|
@@ -36,13 +38,18 @@ public class LocationService { | |||||||||
| * Network should be relatively fast (~5s for radio wakeup or near instant if using WiFi location) | ||||||||||
| */ | ||||||||||
| private static final int NETWORK_TIMEOUT = 10_000; | ||||||||||
| /** | ||||||||||
| * If we don't know what we're calling | ||||||||||
| */ | ||||||||||
| private static final int DEFAULT_TIMEOUT = 30_000; | ||||||||||
| /** | ||||||||||
| * Timeout doesn't apply to cache requests | ||||||||||
| */ | ||||||||||
| private static final int CACHE_TIMEOUT = -1; | ||||||||||
|
|
||||||||||
| @Getter | ||||||||||
| @AllArgsConstructor | ||||||||||
| @ToString | ||||||||||
| private static class ProviderRequest { | ||||||||||
| private String source; | ||||||||||
| private int timeout; | ||||||||||
|
|
@@ -74,6 +81,18 @@ public static LocationService create(Context context, | |||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public List<String> getAvailableSources() { | ||||||||||
| return filterEnabledSources(locationManager.getAllProviders()).stream() | ||||||||||
| .filter(p -> !p.equals(LocationManager.PASSIVE_PROVIDER)) | ||||||||||
| .collect(Collectors.toList()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private List<String> filterEnabledSources(List<String> sources) { | ||||||||||
| return sources.stream() | ||||||||||
| .filter(locationManager::isProviderEnabled) | ||||||||||
| .collect(Collectors.toList()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private void callSequentialProviders(Iterator<ProviderRequest> providers, Consumer<LocationData> consumer) { | ||||||||||
| if (!providers.hasNext()) { | ||||||||||
| consumer.accept(null); // All providers invalid/failed | ||||||||||
|
|
@@ -123,49 +142,68 @@ private LocationData chooseBestLocation(List<LocationData> locations) { | |||||||||
| return best; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private List<ProviderRequest> createRequests(List<String> sources, LocationProvider provider) { | ||||||||||
| List<ProviderRequest> requests = new ArrayList<>(); | ||||||||||
|
|
||||||||||
| for (String source : filterEnabledSources(sources)) { | ||||||||||
| int timeout = timeoutForSource(source); | ||||||||||
| requests.add(new ProviderRequest(source, timeout, provider)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return requests; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private int timeoutForSource(String source) { | ||||||||||
| switch (source) { | ||||||||||
| case LocationManager.GPS_PROVIDER: | ||||||||||
| return GPS_TIMEOUT; | ||||||||||
| case LocationManager.NETWORK_PROVIDER: | ||||||||||
| return NETWORK_TIMEOUT; | ||||||||||
| default: | ||||||||||
| return DEFAULT_TIMEOUT; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @param accuracy Defines priority of accuracy vs battery use (GPS v.s. network v.s. cached location sources) | ||||||||||
| * @param consumer Callback for location data | ||||||||||
| * @throws SecurityException If we're missing location permissions | ||||||||||
| */ | ||||||||||
| public void getLocation(RequestedAccuracy accuracy, Consumer<LocationData> consumer) throws SecurityException { | ||||||||||
| public void getLocation(RequestedAccuracy accuracy, | ||||||||||
| List<String> sources, | ||||||||||
| Consumer<LocationData> consumer) throws SecurityException { | ||||||||||
| if (!permissionsManager.hasLocationPermissions()) { | ||||||||||
| throw new SecurityException("No location permissions"); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| List<ProviderRequest> providers = new ArrayList<>(); | ||||||||||
|
|
||||||||||
| if (optimisedProvider.isSupported()) { | ||||||||||
| /* Optimised provider will automatically use the location cache if available | ||||||||||
| * and return a fresh (< ~30s) location if available */ | ||||||||||
| log.d("Using optimised location manager"); | ||||||||||
|
|
||||||||||
| ProviderRequest gpsRequest = new ProviderRequest(LocationManager.GPS_PROVIDER, GPS_TIMEOUT, optimisedProvider); | ||||||||||
| ProviderRequest networkRequest = new ProviderRequest(LocationManager.NETWORK_PROVIDER, NETWORK_TIMEOUT, optimisedProvider); | ||||||||||
| List<ProviderRequest> requests = createRequests(sources, optimisedProvider); | ||||||||||
| log.d("Requesting location from providers: %s", Arrays.toString(requests.toArray())); | ||||||||||
|
|
||||||||||
| if (accuracy == RequestedAccuracy.HIGH) { | ||||||||||
| providers.addAll(Arrays.asList(gpsRequest, networkRequest)); | ||||||||||
| callParallelProviders(providers.iterator(), this::chooseBestLocation, consumer); | ||||||||||
| callParallelProviders(requests.iterator(), this::chooseBestLocation, consumer); | ||||||||||
| } else { | ||||||||||
| providers.addAll(Arrays.asList(networkRequest, gpsRequest)); | ||||||||||
| callSequentialProviders(providers.iterator(), consumer); | ||||||||||
| callSequentialProviders(requests.iterator(), consumer); | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| /* The legacy provider will directly request location from the hardware, | ||||||||||
| * so we've gotta implement our own cache checks */ | ||||||||||
| log.d("Using legacy location manager"); | ||||||||||
|
|
||||||||||
| ProviderRequest highGps = new ProviderRequest(LocationManager.GPS_PROVIDER, GPS_TIMEOUT, legacyHighAccuracyProvider); | ||||||||||
| ProviderRequest highNetwork = new ProviderRequest(LocationManager.NETWORK_PROVIDER, NETWORK_TIMEOUT, legacyHighAccuracyProvider); | ||||||||||
| ProviderRequest cachedGps = new ProviderRequest(LocationManager.GPS_PROVIDER, CACHE_TIMEOUT, legacyCachedProvider); | ||||||||||
| ProviderRequest cachedNetwork = new ProviderRequest(LocationManager.NETWORK_PROVIDER, CACHE_TIMEOUT, legacyCachedProvider); | ||||||||||
| List<ProviderRequest> cachedRequests = createRequests(sources, legacyCachedProvider); | ||||||||||
| List<ProviderRequest> highAccuracyRequests = createRequests(sources, legacyHighAccuracyProvider); | ||||||||||
| List<ProviderRequest> allRequests = Stream.concat(cachedRequests.stream(), highAccuracyRequests.stream()) | ||||||||||
| .collect(Collectors.toList()); | ||||||||||
|
Comment on lines
+199
to
+200
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||
| log.d("Requesting location from providers: %s", Arrays.toString(allRequests.toArray())); | ||||||||||
|
|
||||||||||
| if (accuracy == RequestedAccuracy.HIGH) { | ||||||||||
| providers.addAll(Arrays.asList(highGps, highNetwork, cachedGps, cachedNetwork)); | ||||||||||
| callParallelProviders(providers.iterator(), this::chooseBestLocation, consumer); | ||||||||||
| callParallelProviders(allRequests.iterator(), this::chooseBestLocation, consumer); | ||||||||||
| } else { | ||||||||||
| providers.addAll(Arrays.asList(cachedGps, cachedNetwork, highNetwork, highGps)); | ||||||||||
| callSequentialProviders(providers.iterator(), consumer); | ||||||||||
| callSequentialProviders(allRequests.iterator(), consumer); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
133 changes: 133 additions & 0 deletions
133
client/app/src/main/java/com/jackpf/locationhistory/client/ui/LocationProviderAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package com.jackpf.locationhistory.client.ui; | ||
|
|
||
| import android.annotation.SuppressLint; | ||
| import android.view.LayoutInflater; | ||
| import android.view.MotionEvent; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.widget.CheckBox; | ||
| import android.widget.ImageView; | ||
| import android.widget.TextView; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.recyclerview.widget.ItemTouchHelper; | ||
| import androidx.recyclerview.widget.RecyclerView; | ||
|
|
||
| import com.jackpf.locationhistory.client.R; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import lombok.Setter; | ||
|
|
||
| public class LocationProviderAdapter extends RecyclerView.Adapter<LocationProviderAdapter.ViewHolder> { | ||
|
|
||
| private final List<LocationProviderItem> providers; | ||
| @Setter | ||
| private ItemTouchHelper itemTouchHelper; | ||
|
|
||
| public LocationProviderAdapter(List<LocationProviderItem> providers) { | ||
| this.providers = providers; | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
| View view = LayoutInflater.from(parent.getContext()) | ||
| .inflate(R.layout.item_location_provider, parent, false); | ||
| return new ViewHolder(view); | ||
| } | ||
|
|
||
| @SuppressLint("ClickableViewAccessibility") | ||
| @Override | ||
| public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | ||
| LocationProviderItem item = providers.get(position); | ||
|
|
||
| holder.providerName.setText(item.getProviderName()); | ||
| holder.providerCheckbox.setChecked(item.isEnabled()); | ||
|
|
||
| holder.providerCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> { | ||
| item.setEnabled(isChecked); | ||
| }); | ||
|
|
||
| holder.dragHandle.setOnTouchListener((v, event) -> { | ||
| if (event.getActionMasked() == MotionEvent.ACTION_DOWN && itemTouchHelper != null) { | ||
| itemTouchHelper.startDrag(holder); | ||
| } | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public int getItemCount() { | ||
| return providers.size(); | ||
| } | ||
|
|
||
| public List<LocationProviderItem> getProviders() { | ||
| return providers; | ||
| } | ||
|
|
||
| public void onItemMove(int fromPosition, int toPosition) { | ||
| if (fromPosition < toPosition) { | ||
| for (int i = fromPosition; i < toPosition; i++) { | ||
| Collections.swap(providers, i, i + 1); | ||
| } | ||
| } else { | ||
| for (int i = fromPosition; i > toPosition; i--) { | ||
| Collections.swap(providers, i, i - 1); | ||
| } | ||
| } | ||
| notifyItemMoved(fromPosition, toPosition); | ||
| } | ||
|
|
||
| static class ViewHolder extends RecyclerView.ViewHolder { | ||
| final ImageView dragHandle; | ||
| final CheckBox providerCheckbox; | ||
| final TextView providerName; | ||
|
|
||
| ViewHolder(View itemView) { | ||
| super(itemView); | ||
| dragHandle = itemView.findViewById(R.id.dragHandle); | ||
| providerCheckbox = itemView.findViewById(R.id.providerCheckbox); | ||
| providerName = itemView.findViewById(R.id.providerName); | ||
| } | ||
| } | ||
|
|
||
| public static class DragCallback extends ItemTouchHelper.Callback { | ||
| private final LocationProviderAdapter adapter; | ||
|
|
||
| public DragCallback(LocationProviderAdapter adapter) { | ||
| this.adapter = adapter; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isLongPressDragEnabled() { | ||
| return false; // We use the drag handle instead | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isItemViewSwipeEnabled() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int getMovementFlags(@NonNull RecyclerView recyclerView, | ||
| @NonNull RecyclerView.ViewHolder viewHolder) { | ||
| int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; | ||
| return makeMovementFlags(dragFlags, 0); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onMove(@NonNull RecyclerView recyclerView, | ||
| @NonNull RecyclerView.ViewHolder viewHolder, | ||
| @NonNull RecyclerView.ViewHolder target) { | ||
| adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition()); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { | ||
| // Not used | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of double-brace initialization for creating and initializing the
HashMapis generally considered an anti-pattern. It creates an anonymous inner class, which can have a performance overhead and potential for memory leaks.A more standard and efficient approach is to use
Collections.singletonMap(), especially since only one entry is being added and the map is effectively immutable. Please also add the required import forjava.util.Collections.