Skip to content

Commit

Permalink
Basic functions working:
Browse files Browse the repository at this point in the history
 - Connect / disconnect button
 - SSL toggle (effect on next connect)
 - Logs (not updating screen correctly though)
  • Loading branch information
mdpye committed Nov 12, 2013
1 parent e145210 commit b43bda2
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 43 deletions.
41 changes: 22 additions & 19 deletions PusherTestApp/PusherTestApp.iml
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@
<output url="file://$MODULE_DIR$/build/classes/debug" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/source/r/debug" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/debug" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/debug" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/debug" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/debug" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/build/source/r/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/r/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/aidl/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/rs/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/source/buildConfig/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/res/rs/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/instrumentTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/instrumentTest/assets" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/instrumentTest/java" isTestSource="true" />
Expand All @@ -71,7 +71,10 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-v4-18.0.0" level="project" />
<orderEntry type="library" exported="" name="ComAndroidSupportAppcompatV71800.aar" level="project" />
<orderEntry type="library" exported="" name="slf4j-api-1.7.5" level="project" />
<orderEntry type="library" exported="" name="gson-2.2.2" level="project" />
<orderEntry type="library" exported="" name="ComAndroidSupportGridlayoutV71800.aar" level="project" />
<orderEntry type="library" exported="" name="pusher-java-client-0.2.0-beta" level="project" />
</component>
</module>

3 changes: 3 additions & 0 deletions PusherTestApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
android:minSdkVersion="14"
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
Expand Down
170 changes: 160 additions & 10 deletions PusherTestApp/src/main/java/com/pusher/testapp/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,187 @@
package com.pusher.testapp;

import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends ActionBarActivity {
import com.pusher.client.Pusher;
import com.pusher.client.PusherOptions;
import com.pusher.client.channel.ChannelEventListener;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionState;
import com.pusher.client.connection.ConnectionStateChange;

public class MainActivity extends ActionBarActivity
implements ConnectionEventListener, ChannelEventListener {

private static final String TAG = "PUSHER";
private static final String API_KEY = "157a2f34672776fc4a73";
private static final String CHANNEL_NAME = "test-channel";

private Pusher pusher;
private PusherOptions options = new PusherOptions();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}

pusher = createPusher();
}

private Pusher createPusher() {
return new Pusher(API_KEY, options);
}

private PusherOptions createOptionsFromUiState() {
ToggleButton ssl = (ToggleButton) findViewById(R.id.toggle_ssl);

PusherOptions options = new PusherOptions()
.setEncrypted(ssl.isChecked());

return options;
}

/*
* PUSHER CALLBACKS
*/

@Override
public void onSubscriptionSucceeded(String s) {
log("Subscription success: " + s);
}

@Override
public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
Log.i(TAG, "Got connection state change from [" + connectionStateChange.getPreviousState()
+ "] to [" + connectionStateChange.getCurrentState() + "]");

log("State is now: " + connectionStateChange.getCurrentState().name());

updateConnectionIndicator(R.id.pusher_status, connectionStateChange.getCurrentState() == ConnectionState.CONNECTED);

updateConnectButton(connectionStateChange.getCurrentState());
}

@Override
public void onError(String s, String s2, Exception e) {
log("Error: " + s);
}

@Override
public void onEvent(String s, String s2, String s3) {
log("New event: " + s);
}


/*
* UI callbacks
*/

public void onClick_Connect(final View btnConnect) {
Log.i(TAG, "Clicked connect button");

if (pusher.getConnection().getState() == ConnectionState.DISCONNECTED) {
pusher.connect(this);
}
else if (pusher.getConnection().getState() == ConnectionState.CONNECTED) {
pusher.disconnect();
}
// Ignore presses in other states, button should be disabled
}

public void onClick_Ssl(final ToggleButton sslToggle) {
options = createOptionsFromUiState();
}

/**
* @param target resource ID, must be instance of TextArea
* @param state true for green, false for red
*/
private void updateConnectionIndicator(final int target, final boolean state) {

final Drawable bg = state ? getResources().getDrawable(R.drawable.rect_green)
: getResources().getDrawable(R.drawable.rect_red);

final CharSequence text = state ? getResources().getText(R.string.connected)
: getResources().getText(R.string.disconnected);

runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Updating connection indicator");
TextView view = (TextView) findViewById(target);
view.setBackground(bg);
view.setText(text);
view.invalidate();
}
});
}

private void updateConnectButton(final ConnectionState state) {
final boolean enabled;
final CharSequence text;

switch (state) {
case CONNECTED:
enabled = true;
text = getResources().getString(R.string.disconnect);
break;
case DISCONNECTED:
enabled = true;
text = getResources().getString(R.string.connect);
break;
case CONNECTING:
case DISCONNECTING:
enabled = false;
text = getResources().getString(R.string.connect);
break;
default:
throw new RuntimeException("Notified of switch to unknown state [" + state + "]");
}

runOnUiThread(new Runnable() {
@Override
public void run() {
Button connectBtn = (Button) findViewById(R.id.btn_connect);
connectBtn.setText(text);
connectBtn.setEnabled(enabled);
connectBtn.invalidate();
}
});
}

private void log(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView view = (TextView) findViewById(R.id.log_view);
CharSequence currentLogs = view.getText();
view.setText(currentLogs + "\n" + text);
view.invalidate();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
Expand All @@ -49,10 +202,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}
public static class MainFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand Down
31 changes: 20 additions & 11 deletions PusherTestApp/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
android:text="@string/label_pusher_status" />

<TextView
android:id="@+id/pusher_status"
android:background="@drawable/rect_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@string/text_pusher_status" />
android:text="@string/disconnected" />

</LinearLayout>

Expand All @@ -52,11 +53,12 @@
android:text="@string/label_net_status" />

<TextView
android:background="@drawable/rect_green"
android:id="@+id/net_status"
android:background="@drawable/rect_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@string/text_net_status" />
android:text="@string/disconnected" />

</LinearLayout>

Expand All @@ -68,36 +70,41 @@
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">

<TextView
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:text="@string/label_ssl" />

<Switch
<ToggleButton
android:id="@+id/toggle_ssl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:onClick="onClick_Ssl" />

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">

<TextView
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:text="@string/label_reconnect" />

<Switch
<ToggleButton
android:id="@+id/toggle_reconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Expand All @@ -110,10 +117,12 @@
android:layout_height="wrap_content">

<Button
android:id="@+id/btn_connect"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/text_btn_connect" />
android:text="@string/text_btn_connect"
android:onClick="onClick_Connect" />

<Button
android:layout_width="0dp"
Expand Down
7 changes: 5 additions & 2 deletions PusherTestApp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
<string name="app_name">Pusher Test App</string>
<string name="action_settings">Settings</string>
<string name="label_pusher_status">Pusher</string>
<string name="text_pusher_status">Disconnected</string>
<string name="label_net_status">Network</string>
<string name="text_net_status">Disconnected</string>
<string name="label_ssl">SSL</string>
<string name="label_reconnect">Auto-reconnect</string>
<string name="text_btn_connect">Connect</string>
<string name="text_btn_event">Trigger event</string>
<string name="text_btn_clear_log">Clear logs</string>
<string name="text_btn_email_log">Email Support</string>

<string name="connected">Connected</string>
<string name="disconnected">Disconnected</string>
<string name="connect">Connect</string>
<string name="disconnect">Disconnect</string>

</resources>
4 changes: 3 additions & 1 deletion pusher-test-android.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
Expand Down

0 comments on commit b43bda2

Please sign in to comment.