Skip to content
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

Convert tabs to spaces in all Java source files #610

Merged
merged 3 commits into from
Oct 1, 2020
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
3,628 changes: 1,814 additions & 1,814 deletions android/src/androidTest/java/io/ably/lib/test/android/AndroidPushTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,70 +26,70 @@
* Tests specific for Android
*/
public class AndroidSuite {
private static SessionHandlerNanoHTTPD server;

@BeforeClass
public static void setUp() throws IOException {
/* Create custom RouterNanoHTTPD class for getting session object */
server = new SessionHandlerNanoHTTPD(27333);
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, true);

while (!server.wasStarted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@AfterClass
public static void tearDown() {
server.stop();
}

@Test
public void android_http_header_test() {
try {
/* Init values for local server */
Setup.TestVars testVars = Setup.getTestVars();
ClientOptions opts = new ClientOptions(testVars.keys[0].keyStr);
opts.tls = false;
opts.port = server.getListeningPort();
opts.restHost = "localhost";
AblyRest ably = new AblyRest(opts);

ably.time();

Map<String, String> headers = server.getHeaders();

assertNotNull("Verify ably server was reached", headers);
String header = headers.get(Defaults.ABLY_LIB_HEADER.toLowerCase());
assertTrue("Verify correct library header was passed to the server", header != null && header.startsWith("android"));
}
catch (AblyException e) {
e.printStackTrace();
fail();
}

}

private static class SessionHandlerNanoHTTPD extends RouterNanoHTTPD {
public Map<String, String> headers;

public SessionHandlerNanoHTTPD(int port) {
super(port);
}

@Override
public Response serve(IHTTPSession session) {
headers = new HashMap<>(session.getHeaders());
return newFixedLengthResponse(String.format(Locale.US, "[%d]", System.currentTimeMillis()));
}

public Map<String, String> getHeaders() {
return headers;
}
}
private static SessionHandlerNanoHTTPD server;

@BeforeClass
public static void setUp() throws IOException {
/* Create custom RouterNanoHTTPD class for getting session object */
server = new SessionHandlerNanoHTTPD(27333);
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, true);

while (!server.wasStarted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@AfterClass
public static void tearDown() {
server.stop();
}

@Test
public void android_http_header_test() {
try {
/* Init values for local server */
Setup.TestVars testVars = Setup.getTestVars();
ClientOptions opts = new ClientOptions(testVars.keys[0].keyStr);
opts.tls = false;
opts.port = server.getListeningPort();
opts.restHost = "localhost";
AblyRest ably = new AblyRest(opts);

ably.time();

Map<String, String> headers = server.getHeaders();

assertNotNull("Verify ably server was reached", headers);
String header = headers.get(Defaults.ABLY_LIB_HEADER.toLowerCase());
assertTrue("Verify correct library header was passed to the server", header != null && header.startsWith("android"));
}
catch (AblyException e) {
e.printStackTrace();
fail();
}

}

private static class SessionHandlerNanoHTTPD extends RouterNanoHTTPD {
public Map<String, String> headers;

public SessionHandlerNanoHTTPD(int port) {
super(port);
}

@Override
public Response serve(IHTTPSession session) {
headers = new HashMap<>(session.getHeaders());
return newFixedLengthResponse(String.format(Locale.US, "[%d]", System.currentTimeMillis()));
}

public Map<String, String> getHeaders() {
return headers;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import android.support.test.InstrumentationRegistry;

public class ArgumentLoader {
public String getTestArgument(String name) {
Bundle arguments = InstrumentationRegistry.getArguments();
return arguments.getString(name);
}
public String getTestArgument(String name) {
Bundle arguments = InstrumentationRegistry.getArguments();
return arguments.getString(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
import java.io.IOException;

public class ResourceLoader {
public byte[] read(String resourceName) throws IOException {
InputStream is = null;
byte[] bytes = null;
try {
is = instrumentationCtx.getAssets().open(resourceName);
Log.v(TAG, "Reading " + is.available() + " bytes for resource " + resourceName);
bytes = new byte[is.available()];
is.read(bytes);
} catch(IOException ioe) {
Log.e(TAG, "Unexpected exception reading asset resource", ioe);
} finally {
if(is != null)
is.close();
return bytes;
}
}
public byte[] read(String resourceName) throws IOException {
InputStream is = null;
byte[] bytes = null;
try {
is = instrumentationCtx.getAssets().open(resourceName);
Log.v(TAG, "Reading " + is.available() + " bytes for resource " + resourceName);
bytes = new byte[is.available()];
is.read(bytes);
} catch(IOException ioe) {
Log.e(TAG, "Unexpected exception reading asset resource", ioe);
} finally {
if(is != null)
is.close();
return bytes;
}
}

private static final String TAG = ResourceLoader.class.getName();
private Context instrumentationCtx = InstrumentationRegistry.getContext();
private static final String TAG = ResourceLoader.class.getName();
private Context instrumentationCtx = InstrumentationRegistry.getContext();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,65 @@

public class AndroidNetworkConnectivity extends NetworkConnectivity {

AndroidNetworkConnectivity(Context applicationContext) {
this.applicationContext = applicationContext;
}
AndroidNetworkConnectivity(Context applicationContext) {
this.applicationContext = applicationContext;
}

public static AndroidNetworkConnectivity getNetworkConnectivity(Context applicationContext) {
AndroidNetworkConnectivity networkConnectivity;
synchronized (contexts) {
networkConnectivity = contexts.get(applicationContext);
if(networkConnectivity == null) {
contexts.put(applicationContext, (networkConnectivity = new AndroidNetworkConnectivity(applicationContext)));
}
}
return networkConnectivity;
}
public static AndroidNetworkConnectivity getNetworkConnectivity(Context applicationContext) {
AndroidNetworkConnectivity networkConnectivity;
synchronized (contexts) {
networkConnectivity = contexts.get(applicationContext);
if(networkConnectivity == null) {
contexts.put(applicationContext, (networkConnectivity = new AndroidNetworkConnectivity(applicationContext)));
}
}
return networkConnectivity;
}

protected void onNonempty() {
activate();
}
protected void onNonempty() {
activate();
}

protected void onEmpty() {
deactivate();
}
protected void onEmpty() {
deactivate();
}

private void activate() {
if(networkStateReceiver == null && applicationContext != null) {
networkStateReceiver = new NetworkStateReceiver();
applicationContext.registerReceiver(networkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
private void activate() {
if(networkStateReceiver == null && applicationContext != null) {
networkStateReceiver = new NetworkStateReceiver();
applicationContext.registerReceiver(networkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}

private void deactivate() {
if(networkStateReceiver != null) {
applicationContext.unregisterReceiver(networkStateReceiver);
networkStateReceiver = null;
}
}
private void deactivate() {
if(networkStateReceiver != null) {
applicationContext.unregisterReceiver(networkStateReceiver);
networkStateReceiver = null;
}
}

private class NetworkStateReceiver extends BroadcastReceiver {
public NetworkStateReceiver() {
}
private class NetworkStateReceiver extends BroadcastReceiver {
public NetworkStateReceiver() {
}

public void onReceive(Context context, Intent intent) {
if(intent == null || intent.getExtras() == null) {
return;
}
public void onReceive(Context context, Intent intent) {
if(intent == null || intent.getExtras() == null) {
return;
}

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();

if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
notifyNetworkAvailable();
} else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
notifyNetworkUnavailable(new ErrorInfo("No network connection available", 503, 80003));
}
}
}
if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
notifyNetworkAvailable();
} else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
notifyNetworkUnavailable(new ErrorInfo("No network connection available", 503, 80003));
}
}
}

private final Context applicationContext;
private NetworkStateReceiver networkStateReceiver;
private final Context applicationContext;
private NetworkStateReceiver networkStateReceiver;

private static WeakHashMap<Context, AndroidNetworkConnectivity> contexts = new WeakHashMap<Context, AndroidNetworkConnectivity>();
private static WeakHashMap<Context, AndroidNetworkConnectivity> contexts = new WeakHashMap<Context, AndroidNetworkConnectivity>();
}
72 changes: 36 additions & 36 deletions android/src/main/java/io/ably/lib/platform/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@
import java.util.WeakHashMap;

public class Platform {
public static final String name = "android";

public Platform() {}

public Context getApplicationContext() {
return applicationContext;
}
/**
* Set the Android Context for this instance
*/
public void setAndroidContext(Context context) throws AblyException {
context = context.getApplicationContext();
if(applicationContext != null) {
if(context == applicationContext) {
return;
}
throw AblyException.fromErrorInfo(new ErrorInfo("Incompatible application context set", 40000, 400));
}
applicationContext = context;
AndroidNetworkConnectivity.getNetworkConnectivity(context).addListener(this.networkConnectivity);
}

public boolean hasApplicationContext() {
return applicationContext != null;
}

/**
* Get the NetworkConnectivity tracker instance for this context
* @return
*/
public NetworkConnectivity getNetworkConnectivity() {
return networkConnectivity;
}

private Context applicationContext;
private final DelegatedNetworkConnectivity networkConnectivity = new DelegatedNetworkConnectivity();
public static final String name = "android";

public Platform() {}

public Context getApplicationContext() {
return applicationContext;
}
/**
* Set the Android Context for this instance
*/
public void setAndroidContext(Context context) throws AblyException {
context = context.getApplicationContext();
if(applicationContext != null) {
if(context == applicationContext) {
return;
}
throw AblyException.fromErrorInfo(new ErrorInfo("Incompatible application context set", 40000, 400));
}
applicationContext = context;
AndroidNetworkConnectivity.getNetworkConnectivity(context).addListener(this.networkConnectivity);
}

public boolean hasApplicationContext() {
return applicationContext != null;
}

/**
* Get the NetworkConnectivity tracker instance for this context
* @return
*/
public NetworkConnectivity getNetworkConnectivity() {
return networkConnectivity;
}

private Context applicationContext;
private final DelegatedNetworkConnectivity networkConnectivity = new DelegatedNetworkConnectivity();
}
Loading