This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update connectState before invoking connectionCallbacks * Rm unnecessary semicolon * Extract all connection related logic into testable connection manager class * Add tests coverage for remaining FusedLocationProviderApiImpl methods * Test coverage for FusedLocationServiceConnectionManager * Update state in connection manager before invoking callbacks * Remove unnecessary parameter from disconnect method
- Loading branch information
1 parent
edc3cea
commit 944656f
Showing
7 changed files
with
449 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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
95 changes: 95 additions & 0 deletions
95
...src/main/java/com/mapzen/android/lost/internal/FusedLocationServiceConnectionManager.java
This file contains 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,95 @@ | ||
package com.mapzen.android.lost.internal; | ||
|
||
import com.mapzen.android.lost.api.LostApiClient; | ||
import com.mapzen.android.lost.api.LostApiClient.ConnectionCallbacks; | ||
|
||
import android.content.Context; | ||
import android.os.IBinder; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class FusedLocationServiceConnectionManager { | ||
|
||
public interface EventCallbacks { | ||
void onConnect(Context context); | ||
void onServiceConnected(IBinder binder); | ||
void onDisconnect(LostApiClient client, boolean disconnectService); | ||
} | ||
|
||
private enum ConnectState { IDLE, CONNECTING, CONNECTED } | ||
|
||
private EventCallbacks eventCallbacks; | ||
private ConnectState connectState; | ||
Set<ConnectionCallbacks> connectionCallbacks; | ||
|
||
public FusedLocationServiceConnectionManager() { | ||
connectionCallbacks = new HashSet<>(); | ||
connectState = ConnectState.IDLE; | ||
} | ||
|
||
public void setEventCallbacks(EventCallbacks callbacks) { | ||
eventCallbacks = callbacks; | ||
} | ||
|
||
public void addCallbacks(ConnectionCallbacks callbacks) { | ||
if (callbacks != null) { | ||
connectionCallbacks.add(callbacks); | ||
} | ||
} | ||
|
||
public boolean isConnected() { | ||
return connectState == ConnectState.CONNECTED; | ||
} | ||
|
||
public boolean isConnecting() { | ||
return connectState == ConnectState.CONNECTING; | ||
} | ||
|
||
public void connect(Context context, ConnectionCallbacks callbacks) { | ||
if (connectState == ConnectState.IDLE) { | ||
connectState = ConnectState.CONNECTING; | ||
|
||
if (eventCallbacks != null) { | ||
eventCallbacks.onConnect(context); | ||
} | ||
} | ||
addCallbacks(callbacks); | ||
} | ||
|
||
public void disconnect(LostApiClient client) { | ||
if (connectState != ConnectState.IDLE) { | ||
boolean disconnectService = (connectState == ConnectState.CONNECTED); | ||
connectState = ConnectState.IDLE; | ||
if (eventCallbacks != null) { | ||
eventCallbacks.onDisconnect(client, disconnectService); | ||
} | ||
} | ||
} | ||
|
||
public void onServiceConnected(IBinder binder) { | ||
if (connectState != ConnectState.IDLE) { | ||
connectState = ConnectState.CONNECTED; | ||
if (eventCallbacks != null) { | ||
eventCallbacks.onServiceConnected(binder); | ||
} | ||
|
||
if (!connectionCallbacks.isEmpty()) { | ||
for (LostApiClient.ConnectionCallbacks callbacks : connectionCallbacks) { | ||
callbacks.onConnected(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void onServiceDisconnected() { | ||
if (connectState != ConnectState.IDLE) { | ||
connectState = ConnectState.IDLE; | ||
if (!connectionCallbacks.isEmpty()) { | ||
for (LostApiClient.ConnectionCallbacks callbacks : connectionCallbacks) { | ||
callbacks.onConnectionSuspended(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.