Skip to content

Commit

Permalink
Working app.
Browse files Browse the repository at this point in the history
  • Loading branch information
joemoore committed Sep 28, 2011
1 parent 09a595d commit 30d39f0
Show file tree
Hide file tree
Showing 22 changed files with 722 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .idea/libraries/Android_2_3_3_Google_API_libs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion AndroidManifest.xml
Expand Up @@ -20,7 +20,7 @@

<application
android:label="@string/app_name"
android:icon="@drawable/icon"
android:icon="@drawable/ic_launcher"
android:name=".C2DeMoApplication">
<activity android:name=".HomeActivity"
android:label="@string/app_name">
Expand Down
Binary file added res/drawable-hdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-ldpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
144 changes: 122 additions & 22 deletions res/layout/home.xml
@@ -1,27 +1,127 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/title"
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="20dip"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dip"
android:text="C2DeMo"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/registerButton"
android:text="Register"
/>
android:layout_height="fill_parent"
android:background="@android:color/white"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="20dip"
android:layout_gravity="center_horizontal"
android:layout_margin="10dip"
android:text="Test C2DM Notifications"
/>
<LinearLayout android:id="@+id/authSection"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:padding="5dip"
android:background="#DDD">

<TextView
android:id="@+id/authTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="15dip"
android:layout_gravity="center_horizontal"
android:layout_margin="10dip"
android:text="@string/auth_title"
android:singleLine="true"
/>

<EditText android:id="@+id/senderEmail"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Google Account Email"
android:singleLine="true"
android:inputType="textEmailAddress"
android:text="c2dm@pivot13.com"
/>

<EditText android:id="@+id/senderPassword"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Google Account Password"
android:password="true"
android:text="c2dmpushforandroid"
/>

<Button
android:id="@+id/authWithGoogleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Authenticate with Google"
/>
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:padding="5dip"
android:background="#DDD">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="15dip"
android:layout_gravity="center_horizontal"
android:layout_margin="10dip"
android:text="@string/register_title"
android:singleLine="true"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/registerButton"
android:text="Register This Device"
/>
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:padding="5dip"
android:background="#DDD">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="15dip"
android:layout_gravity="center_horizontal"
android:layout_margin="10dip"
android:text="@string/send_title"
android:singleLine="true"
/>
<EditText android:id="@+id/notificationText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Notification Text"
android:singleLine="true"
/>
<Button
android:id="@+id/sendNotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send Notification"
/>

</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>

4 changes: 4 additions & 0 deletions res/values/strings.xml
@@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">C2DeMo</string>
<string name="register_button">"Register This Device"</string>
<string name="auth_title"><b>Register to Send</b> C2DM Notifications</string>
<string name="register_title"><b>Register to Receive</b> C2DM Notifications</string>
<string name="send_title">Send Yourself a Notification</string>
</resources>
23 changes: 13 additions & 10 deletions src/com/pivot13/C2DMReceiver.java
Expand Up @@ -14,6 +14,7 @@
import com.google.android.c2dm.C2DMBaseReceiver;

import java.io.IOException;
import java.util.Random;

/**
* NOTE: C2DM is supported in API v.2.2 and above but is safely ignored in lower versions.
Expand All @@ -26,7 +27,6 @@
*/
public class C2DMReceiver extends C2DMBaseReceiver {

public static final int MESSAGE_KEY = 1;
public static final String REG_ID = "regId";
public static final CharSequence REGISTRATION_SUCCESS_MESSAGE = "C2DM Registration Successful!";
public static final CharSequence REGISTRATION_ERROR_MESSAGE = "C2DM Registration Error: ";
Expand Down Expand Up @@ -59,19 +59,22 @@ public C2DMReceiver() {
protected void onMessage(Context context, Intent intent) {

String message = intent.getStringExtra("message");
String moreData = intent.getStringExtra("moreData");
String notificationText = new StringBuilder()
.append("message: ").append(message).append("; ")
.append("moreData: ").append(moreData).toString();
String notificationText = new StringBuilder().append(message).toString();

Notification notification = new Notification(
android.R.drawable.ic_dialog_info,
"New Message: " + notificationText,
SystemClock.currentThreadTimeMillis());

Notification notification = new Notification(android.R.drawable.ic_dialog_alert, "Ticker Ticker ticker!! " + notificationText, SystemClock.currentThreadTimeMillis());

notification.setLatestEventInfo(context, "C2DeMo Message", notificationText, PendingIntent.getBroadcast(context, 0, new Intent(), 0));
notification.setLatestEventInfo(
context,
"C2DeMo Message",
notificationText,
PendingIntent.getBroadcast(context, 0, new Intent(), 0));

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(MESSAGE_KEY, notification);
notificationManager.notify(new Random().nextInt(), notification);
Log.v("pivot13", "************ about to notify!!!!!!!!!!!!!!!!!!!!!!!!");
}

Expand All @@ -93,7 +96,7 @@ protected void onMessage(Context context, Intent intent) {
public void onRegistered(final Context context, String registrationId) throws IOException {
super.onRegistered(context, registrationId);
SharedPreferences.Editor editor = getSharedPreferences("C2DM", 0).edit();
editor.putString(REG_ID, registrationId);
editor.putString(REG_ID, registrationId.trim());
editor.commit();
Log.v("pivot13", registrationId);

Expand Down
7 changes: 7 additions & 0 deletions src/com/pivot13/C2DeMoApplication.java
Expand Up @@ -5,6 +5,9 @@
import roboguice.config.AbstractAndroidModule;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class C2DeMoApplication extends RoboInjectableApplication {
public static final String C2DM_SENDER_KEY = "c2dm@pivot13.com";
Expand All @@ -23,6 +26,10 @@ public void setModule(Module module) {
public static class ApplicationModule extends AbstractAndroidModule {
@Override
protected void configure() {
final ExecutorService executorService = Executors.newFixedThreadPool(5);
bind(Executor.class).toInstance(executorService);
bind(ExecutorService.class).toInstance(executorService);

/*Samples of injection binding*/
// bind(FooBar.class).in(Scopes.SINGLETON);
// bind(Date.class).toProvider(FakeDateProvider.class);
Expand Down
107 changes: 106 additions & 1 deletion src/com/pivot13/HomeActivity.java
@@ -1,29 +1,93 @@
package com.pivot13;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.c2dm.C2DMessaging;
import com.google.inject.Inject;
import com.pivot13.util.CurrentTime;
import com.pivotallabs.api.Http;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import roboguice.util.Strings;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

public class HomeActivity extends RoboActivity {
@InjectView(R.id.registerButton)
Button registerButton;

@InjectView(R.id.authWithGoogleButton)
Button authWithGoogleButton;

@InjectView(R.id.sendNotification)
Button sendNotification;

@InjectView(R.id.senderEmail)
EditText senderEmail;

@InjectView(R.id.notificationText)
EditText notificationText;

@InjectView(R.id.senderPassword)
EditText senderPassword;

@Inject
private CurrentTime currentTime;

@Inject
private ExecutorService executorService;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);

registerButton.setOnClickListener(new RegisterOnClickListener());
authWithGoogleButton.setOnClickListener(new AuthenticateWithGoogleListener());
sendNotification.setOnClickListener(new SendNotificationListener());
}

private class AuthenticateWithGoogleListener implements View.OnClickListener {
@Override
public void onClick(View v) {

List<String> strings = Arrays.asList(
"Email=" + senderEmail.getText().toString(),
"Passwd=" + senderPassword.getText().toString(),
"accountType=HOSTED_OR_GOOGLE",
"service=ac2dm");

String paramsAsBody = Strings.join("&", strings);
Map<String, String> headers = new HashMap<String, String>();
Http.Response response = doPost(paramsAsBody, headers, "https://www.google.com/accounts/ClientLogin");

String responseBody = response.getResponseBody();
String[] responsePieces = responseBody.split("Auth=");
if (responsePieces.length == 2) {
String authToken = responsePieces[1];
saveAuthToken(authToken);
toast("Authorization Success!");
Log.v("pivot13", "Auth Token is =" + authToken);
} else {
toast("Auth Failed: " + responseBody);
}
}

private void saveAuthToken(String authToken) {
SharedPreferences.Editor editor = getSharedPreferences("C2DM", 0).edit();
editor.putString("AuthToken", authToken.trim());
editor.commit();
}
}

private class RegisterOnClickListener implements View.OnClickListener {
Expand All @@ -32,4 +96,45 @@ public void onClick(View v) {
C2DMessaging.register(HomeActivity.this, C2DeMoApplication.C2DM_SENDER_KEY);
}
}

private class SendNotificationListener implements View.OnClickListener {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("C2DM", 0);
String authToken = sharedPreferences.getString("AuthToken", "");
String regId = sharedPreferences.getString(C2DMReceiver.REG_ID, "");

List<String> strings = Arrays.asList(
"data.message=" + notificationText.getText().toString(),
"registration_id=" + regId,
"collapse_key=something");

String paramsAsBody = Strings.join("&", strings);

Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "GoogleLogin auth=" + authToken);
Http.Response response = doPost(paramsAsBody, headers, "https://android.apis.google.com/c2dm/send");
if(response.getStatusCode() != 200) {
toast("Notification failed: " + response.getResponseBody());
}
}
}

private Http.Response doPost(String paramsAsBody, Map<String, String> headers, String url) {
Http.Response post = null;
try {
headers.put("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
post = new Http().post(url, headers, paramsAsBody);
Log.v("pivot13", "response for " + url + " was \n" + post.getResponseBody());
} catch (IOException e) {
toast("Exception: " + e.getMessage());
} catch (URISyntaxException e) {
toast("Exception: " + e.getMessage());
}
return post;
}

private void toast(String text) {
Toast.makeText(HomeActivity.this, text, Toast.LENGTH_LONG).show();
}
}

0 comments on commit 30d39f0

Please sign in to comment.