Skip to content
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
7 changes: 7 additions & 0 deletions mapbox/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
<activity
android:name=".connectivity.ConnectivityActivity"
android:label="@string/title_connectivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>

<!--
Service to asynchronously fetch a location address using a Geocoder. Setting the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.mapbox.services.android.BuildConfig;
import com.mapbox.services.android.telemetry.permissions.PermissionsListener;
import com.mapbox.services.android.telemetry.permissions.PermissionsManager;
import com.mapbox.services.android.testapp.connectivity.ConnectivityActivity;
import com.mapbox.services.android.testapp.directions.DirectionsV5Activity;
import com.mapbox.services.android.testapp.directions.RouteUtilsV5Activity;
import com.mapbox.services.android.testapp.distance.DistanceActivity;
Expand Down Expand Up @@ -150,6 +151,11 @@ protected void onCreate(Bundle savedInstanceState) {
getString(R.string.title_off_route_detection),
getString(R.string.description_off_route_detection),
OffRouteDetectionActivity.class
),
new SampleItem(
getString(R.string.title_connectivity),
getString(R.string.description_connectivity),
ConnectivityActivity.class
)
));

Expand Down
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));
}
}
47 changes: 47 additions & 0 deletions mapbox/app/src/main/res/layout/activity_connectivity.xml
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>
2 changes: 2 additions & 0 deletions mapbox/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<string name="title_turf_inside">Turf inside</string>
<string name="title_turf_midpoint">Turf midpoint</string>
<string name="title_off_route_detection">Off route detection</string>
<string name="title_connectivity">Connectivity state</string>

<!-- Descriptions -->
<string name="description_location">Shows how to obtain location from different engines.</string>
Expand All @@ -63,6 +64,7 @@
<string name="description_turf_inside">Use Turf to determine if something is within a polygon.</string>
<string name="description_turf_midpoint">Use Turf to get a midpoint.</string>
<string name="description_off_route_detection">Uses the Route Utils class to determine if a users off route.</string>
<string name="description_connectivity">Keep track of the connectivity status.</string>

<string-array name="turf_calculation_items">
<item>Select calculation</item>
Expand Down
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);

}
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);
}
}
}