From f906d748091357b6079c0e72db8ca540dd3658c5 Mon Sep 17 00:00:00 2001 From: Jo Grimstad Date: Sun, 11 Dec 2016 23:14:45 +0300 Subject: [PATCH] #31 Create StudentUpdateReceiver --- app/src/main/AndroidManifest.xml | 14 ++++++++- .../chat/MessageListArrayAdapter.java | 17 +++++++++++ .../chat/receiver/StudentUpdateReceiver.java | 29 +++++++++++++++---- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9780610..2a7e9ab 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,8 +14,20 @@ android:label="@string/app_name"> + + + + + + + + - + + \ No newline at end of file diff --git a/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java b/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java index 3dfef55..610dfe6 100644 --- a/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java +++ b/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java @@ -1,6 +1,11 @@ package org.literacyapp.chat; import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.preference.PreferenceManager; +import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -10,7 +15,9 @@ import android.widget.TextView; import org.literacyapp.chat.model.Message; +import org.literacyapp.chat.receiver.StudentUpdateReceiver; +import java.io.File; import java.util.List; public class MessageListArrayAdapter extends ArrayAdapter { @@ -51,6 +58,16 @@ public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = (ViewHolder) listItem.getTag(); viewHolder.textViewListItem.setText(message.getText()); + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + String studentAvatar = sharedPreferences.getString(StudentUpdateReceiver.PREF_STUDENT_AVATAR, null); + if (!TextUtils.isEmpty(studentAvatar)) { + File file = new File(studentAvatar); + if (file.exists()) { + Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); + viewHolder.imageViewAvatar.setImageBitmap(bitmap); + } + } + return listItem; } } diff --git a/app/src/main/java/org/literacyapp/chat/receiver/StudentUpdateReceiver.java b/app/src/main/java/org/literacyapp/chat/receiver/StudentUpdateReceiver.java index 5d69af0..37cd822 100644 --- a/app/src/main/java/org/literacyapp/chat/receiver/StudentUpdateReceiver.java +++ b/app/src/main/java/org/literacyapp/chat/receiver/StudentUpdateReceiver.java @@ -3,15 +3,34 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.text.TextUtils; +import android.util.Log; public class StudentUpdateReceiver extends BroadcastReceiver { - public StudentUpdateReceiver() { - } + + public static final String PREF_STUDENT_ID = "pref_student_id"; + public static final String PREF_STUDENT_AVATAR = "pref_student_avatar"; @Override public void onReceive(Context context, Intent intent) { - // TODO: This method is called when the BroadcastReceiver is receiving - // an Intent broadcast. - throw new UnsupportedOperationException("Not yet implemented"); + Log.i(getClass().getName(), "onReceive"); + + String studentId = intent.getStringExtra("studentId"); + Log.i(getClass().getName(), "studentId: " + studentId); + + String studentAvatar = intent.getStringExtra("studentAvatar"); + Log.i(getClass().getName(), "studentAvatar: " + studentAvatar); + + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + + if (!TextUtils.isEmpty(studentId)) { + sharedPreferences.edit().putString(PREF_STUDENT_ID, studentId).commit(); + } + + if (!TextUtils.isEmpty(studentAvatar)) { + sharedPreferences.edit().putString(PREF_STUDENT_AVATAR, studentAvatar).commit(); + } } }