Skip to content

Commit

Permalink
Reversed the chat view to be bottom instead of top posting.
Browse files Browse the repository at this point in the history
Added a few FCM notification classes, nothing functional yet.
  • Loading branch information
zond committed Dec 28, 2016
1 parent aa2bd03 commit ffe2d2c
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 103 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.google.firebase:firebase-crash:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_otto" />

<activity
android:name=".MainActivity"
Expand Down Expand Up @@ -48,12 +51,26 @@
</activity>
<activity
android:name=".game.PressActivity"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="se.oort.diplicity.MainActivity" />
</activity>

<service
android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>

</manifest>
15 changes: 15 additions & 0 deletions app/src/main/java/se/oort/diplicity/FirebaseMessageIDService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package se.oort.diplicity;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class FirebaseMessageIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("Diplicity", "Refreshed IID token: " + refreshedToken);
}
}
70 changes: 70 additions & 0 deletions app/src/main/java/se/oort/diplicity/FirebaseMessagingService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package se.oort.diplicity;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]

// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
Log.d("Diplicity", "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d("Diplicity", "Message data payload: " + remoteMessage.getData());
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d("Diplicity", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]

/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_otto)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
95 changes: 34 additions & 61 deletions app/src/main/java/se/oort/diplicity/game/PressActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import android.preference.EditTextPreference;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
Expand All @@ -18,7 +20,10 @@
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
Expand Down Expand Up @@ -51,60 +56,6 @@ public class PressActivity extends RetrofitActivity {
public Member member;
public Game game;

private MessageListAdapter messages = new MessageListAdapter();

private class MessageListAdapter extends BaseAdapter {
private List<Message> messages = new ArrayList<>();

public void add(Message message) {
this.messages.add(message);
notifyDataSetChanged();
}

public void replace(List<Message> messages) {
this.messages = messages;
notifyDataSetChanged();
}

@Override
public int getCount() {
return messages.size();
}

@Override
public Object getItem(int i) {
return messages.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row = view;
if (row == null) {
row = getLayoutInflater().inflate(R.layout.message_list_row, viewGroup, false);
}

String url = null;
for (Member member : game.Members) {
if (member.Nation.equals(messages.get(i).Sender)) {
url = member.User.Picture;
}
}

((TextView) row.findViewById(R.id.body)).setText(messages.get(i).Body);
((TextView) row.findViewById(R.id.at)).setText(messages.get(i).Age.deadlineAt().toString());
((TextView) row.findViewById(R.id.sender)).setText(getResources().getString(R.string.x_, messages.get(i).Sender));
if (url != null) {
PressActivity.this.populateImage((ImageView) row.findViewById(R.id.avatar), url);
}
return row;
}
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Expand All @@ -125,8 +76,6 @@ protected void onPostCreate(Bundle savedInstanceState) {

setTitle(TextUtils.join(", ", channel.Members));

((ListView) findViewById(R.id.press_messages)).setAdapter(messages);

if (member == null) {
findViewById(R.id.send_message_button).setVisibility(View.GONE);
findViewById(R.id.body).setVisibility(View.GONE);
Expand Down Expand Up @@ -164,12 +113,36 @@ private void loadMessages() {
new Sendable<MultiContainer<Message>>() {
@Override
public void send(final MultiContainer<Message> messageMultiContainer) {
final List<Message> newMessages= new ArrayList<>();
for (SingleContainer<Message> messageSingleContainer: messageMultiContainer.Properties) {
newMessages.add(messageSingleContainer.Properties);
Log.d("Diplicity", "Got " + messageSingleContainer.Properties.Body);
((LinearLayout) findViewById(R.id.press_messages)).removeAllViews();
for (int i = 0; i < messageMultiContainer.Properties.size(); i++) {
Message message = messageMultiContainer.Properties.get(messageMultiContainer.Properties.size() - i - 1).Properties;
View row = getLayoutInflater().inflate(R.layout.message_list_row, (ViewGroup) findViewById(R.id.press_layout), false);
String url = null;
for (Member member : game.Members) {
if (member.Nation.equals(message.Sender)) {
url = member.User.Picture;
}
}

((TextView) row.findViewById(R.id.body)).setText(message.Body);
((TextView) row.findViewById(R.id.at)).setText(message.Age.deadlineAt().toString());
((TextView) row.findViewById(R.id.sender)).setText(getResources().getString(R.string.x_, message.Sender));
if (url != null) {
PressActivity.this.populateImage((ImageView) row.findViewById(R.id.avatar), url);
}

((LinearLayout) findViewById(R.id.press_messages)).addView(row);
}
messages.replace(newMessages);
findViewById(R.id.press_layout).invalidate();
findViewById(R.id.press_messages).invalidate();
final NestedScrollView pressScroll = (NestedScrollView) findViewById(R.id.press_scroll);
pressScroll.post(new Runnable() {
@Override
public void run() {
Log.d("diplicity", " *** scrolling!");
pressScroll.fullScroll(View.FOCUS_DOWN);
}
});
}
}, getResources().getString(R.string.loading_messages));
}
Expand Down
90 changes: 48 additions & 42 deletions app/src/main/res/layout/content_press.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/press_scroll"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
Expand All @@ -9,54 +11,58 @@
tools:context="se.oort.diplicity.game.PressActivity"
tools:showIn="@layout/activity_press">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
app:elevation="6dp"
app:borderWidth="0dp"
app:fabSize="mini"
app:rippleColor="@color/colorPrimary"
app:useCompatPadding="true"
android:tint="@android:color/white"
android:id="@+id/send_message_button"
android:src="@drawable/ic_send_black_24dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<android.support.design.widget.TextInputLayout
android:id="@+id/message_layout"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/send_message_button"
android:layout_toLeftOf="@id/send_message_button"
android:layout_width="wrap_content"
<RelativeLayout
android:id="@+id/press_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.TextInputEditText
android:id="@+id/new_message_body"
android:hint="@string/message"
android:inputType="textCapSentences|textAutoComplete|textAutoCorrect|textMultiLine"
<LinearLayout
android:orientation="vertical"
android:id="@+id/press_messages"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</LinearLayout>

<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
app:elevation="6dp"
app:borderWidth="0dp"
app:fabSize="mini"
app:rippleColor="@color/colorPrimary"
app:useCompatPadding="true"
android:tint="@android:color/white"
android:id="@+id/send_message_button"
android:src="@drawable/ic_send_black_24dp"
android:layout_below="@id/press_messages"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/message_layout"
android:layout_below="@id/press_messages"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/send_message_button"
android:layout_toLeftOf="@id/send_message_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ListView
android:id="@+id/press_messages"
android:layout_below="@id/message_layout"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/new_message_body"
android:hint="@string/message"
android:inputType="textCapSentences|textAutoComplete|textAutoCorrect|textMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</android.support.design.widget.TextInputLayout>

</ListView>

</RelativeLayout>
</RelativeLayout>

</android.support.v4.widget.NestedScrollView>

0 comments on commit ffe2d2c

Please sign in to comment.