-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a ConnectivityReceiver/ConnectivityListener pair (#276)
* add a ConnectivityReceiver/ConnectivityListener pair and a ConnectivityActivity in the test app * typos
- Loading branch information
Showing
7 changed files
with
302 additions
and
0 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
98 changes: 98 additions & 0 deletions
98
.../src/main/java/com/mapbox/services/android/testapp/connectivity/ConnectivityActivity.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,98 @@ | ||
package com.mapbox.services.android.testapp.connectivity; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.TextView; | ||
|
||
import com.mapbox.services.android.telemetry.connectivity.ConnectivityListener; | ||
import com.mapbox.services.android.telemetry.connectivity.ConnectivityReceiver; | ||
import com.mapbox.services.android.testapp.R; | ||
|
||
public class ConnectivityActivity extends AppCompatActivity | ||
implements ConnectivityListener { | ||
|
||
private static final String LOG_TAG = ConnectivityActivity.class.getSimpleName(); | ||
private ConnectivityReceiver connectivityReceiver; | ||
|
||
private Button buttonAuto; | ||
private Button buttonYes; | ||
private Button buttonNo; | ||
private TextView textUpdate; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_connectivity); | ||
textUpdate = (TextView) findViewById(R.id.text_update); | ||
setupButtons(); | ||
|
||
// Set up the ConnectivityReceiver | ||
Log.d(LOG_TAG, "Setting up ConnectivityReceiver."); | ||
connectivityReceiver = new ConnectivityReceiver(getApplicationContext()); | ||
connectivityReceiver.addConnectivityListener(this); | ||
setInitialState(); | ||
} | ||
|
||
private void setupButtons() { | ||
buttonAuto = (Button) findViewById(R.id.button_auto); | ||
buttonAuto.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
setInitialState(); | ||
} | ||
}); | ||
|
||
buttonYes = (Button) findViewById(R.id.button_yes); | ||
buttonYes.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
buttonAuto.setEnabled(true); | ||
buttonYes.setEnabled(false); | ||
buttonNo.setEnabled(true); | ||
connectivityReceiver.setConnectedFlag(true); | ||
} | ||
}); | ||
|
||
buttonNo = (Button) findViewById(R.id.button_no); | ||
buttonNo.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
buttonAuto.setEnabled(true); | ||
buttonYes.setEnabled(true); | ||
buttonNo.setEnabled(false); | ||
connectivityReceiver.setConnectedFlag(false); | ||
} | ||
}); | ||
} | ||
|
||
private void setInitialState() { | ||
buttonAuto.setEnabled(false); | ||
buttonYes.setEnabled(true); | ||
buttonNo.setEnabled(true); | ||
connectivityReceiver.setConnectedFlag(null); | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
|
||
Log.d(LOG_TAG, "Requesting connectivity updates."); | ||
connectivityReceiver.requestConnectivityUpdates(); | ||
} | ||
|
||
@Override | ||
protected void onStop() { | ||
super.onStop(); | ||
|
||
Log.d(LOG_TAG, "Removing connectivity updates."); | ||
connectivityReceiver.removeConnectivityUpdates(); | ||
} | ||
|
||
@Override | ||
public void onConnectivityChanged(boolean connected) { | ||
textUpdate.setText(String.format("Connectivity changed to %b.", connected)); | ||
} | ||
} |
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,47 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/activity_connectivity" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
tools:context="com.mapbox.services.android.testapp.connectivity.ConnectivityActivity"> | ||
|
||
<LinearLayout | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<Button | ||
android:id="@+id/button_auto" | ||
android:text="Auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"/> | ||
|
||
<Button | ||
android:id="@+id/button_yes" | ||
android:text="Always connected" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"/> | ||
|
||
<Button | ||
android:id="@+id/button_no" | ||
android:text="Always disconnected" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"/> | ||
|
||
</LinearLayout> | ||
|
||
<TextView | ||
android:id="@+id/text_update" | ||
android:text="Waiting for a connectivity update." | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"/> | ||
|
||
</LinearLayout> |
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
11 changes: 11 additions & 0 deletions
11
...rc/main/java/com/mapbox/services/android/telemetry/connectivity/ConnectivityListener.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,11 @@ | ||
package com.mapbox.services.android.telemetry.connectivity; | ||
|
||
/** | ||
* Callback to use with the ConnectivityReceiver | ||
*/ | ||
|
||
public interface ConnectivityListener { | ||
|
||
void onConnectivityChanged(boolean connected); | ||
|
||
} |
131 changes: 131 additions & 0 deletions
131
...rc/main/java/com/mapbox/services/android/telemetry/connectivity/ConnectivityReceiver.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,131 @@ | ||
package com.mapbox.services.android.telemetry.connectivity; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.net.ConnectivityManager; | ||
import android.net.NetworkInfo; | ||
|
||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
/** | ||
* ConnectivityReceiver is a BroadcastReceiver that helps you keep track of the connectivity | ||
* status. When used statically (getSystemConnectivity) the ConnectivityReceiver will always | ||
* return the connectivity status as reported by the Android system. | ||
* | ||
* When instantiating ConnectivityReceiver, you have the option to set a connectedFlag. You can | ||
* override the connectivity value reported by the system by setting this flag to true or false. If | ||
* left in its default value (null), ConnectivityReceiver will report the system value. | ||
* | ||
* ConnectivityReceiver also lets you subscribe to connecitivity changes using a | ||
* ConnectivityListener. | ||
*/ | ||
public class ConnectivityReceiver extends BroadcastReceiver { | ||
|
||
private Context context; | ||
private CopyOnWriteArrayList<ConnectivityListener> connectivityListeners; | ||
private Boolean connectedFlag; | ||
|
||
/** | ||
* ConnectivityReceiver constructor | ||
* | ||
* @param context Android context. To avoid memory leaks, you might want to pass the application | ||
* context and make sure you call removeConnectivityUpdates() when you don't need | ||
* further updates (https://github.com/mapbox/mapbox-gl-native/issues/7176) | ||
*/ | ||
public ConnectivityReceiver(Context context) { | ||
this.context = context; | ||
connectivityListeners = new CopyOnWriteArrayList<>(); | ||
connectedFlag = null; | ||
} | ||
|
||
/** | ||
* Get the connectivity state as reported by the Android system | ||
* | ||
* @param context Android context | ||
* @return the connectivity state as reported by the Android system | ||
*/ | ||
private static boolean getSystemConnectivity(Context context) { | ||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | ||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | ||
return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); | ||
} | ||
|
||
/** | ||
* Get the connectivity state. This can be overriden using the connectedFlag. | ||
* | ||
* @return the connectivity state | ||
*/ | ||
private boolean getManagedConnectivity() { | ||
if (connectedFlag == null) { | ||
return getSystemConnectivity(context); | ||
} | ||
|
||
return connectedFlag; | ||
} | ||
|
||
/** | ||
* Get the connectivity state as reported by the Android system | ||
* | ||
* @param context Android context | ||
* @return the connectivity state as reported by the Android system | ||
*/ | ||
public static boolean isConnected(Context context) { | ||
return getSystemConnectivity(context); | ||
} | ||
|
||
/** | ||
* Get the connectivity state. This can be overriden using the connectedFlag. | ||
* | ||
* @return the connectivity state | ||
*/ | ||
public boolean isConnected() { | ||
return getManagedConnectivity(); | ||
} | ||
|
||
/** | ||
* Get the connectedFlag value | ||
* | ||
* @return the connectedFlag value, true/false if the connectivity state has ben overriden, | ||
* null otherwise. | ||
*/ | ||
public Boolean getConnectedFlag() { | ||
return connectedFlag; | ||
} | ||
|
||
/** | ||
* Set the connectedFlag value | ||
* | ||
* @param connectedFlag Set it to true/false to override the connectivity state | ||
*/ | ||
public void setConnectedFlag(Boolean connectedFlag) { | ||
this.connectedFlag = connectedFlag; | ||
} | ||
|
||
public void addConnectivityListener(ConnectivityListener listener) { | ||
if (!connectivityListeners.contains(listener)) { | ||
connectivityListeners.add(listener); | ||
} | ||
} | ||
|
||
public boolean removeConnectivityListener(ConnectivityListener listener) { | ||
return connectivityListeners.remove(listener); | ||
} | ||
|
||
public void requestConnectivityUpdates() { | ||
context.registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); | ||
} | ||
|
||
public void removeConnectivityUpdates() { | ||
context.unregisterReceiver(this); | ||
} | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
boolean connected = getManagedConnectivity(); | ||
for (ConnectivityListener listener : connectivityListeners) { | ||
listener.onConnectivityChanged(connected); | ||
} | ||
} | ||
} |