Skip to content

Commit

Permalink
Merge pull request #32 from jogrimst/master
Browse files Browse the repository at this point in the history
#31 Create StudentUpdateReceiver
  • Loading branch information
literacyapp authored Dec 11, 2016
2 parents 97f1883 + f906d74 commit 867e060
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name=".receiver.StudentUpdateReceiver"
android:enabled="true"
android:exported="true">

<intent-filter>
<action android:name="literacyapp.intent.action.STUDENT_UPDATED" />
</intent-filter>
</receiver>
</application>
</manifest>

</manifest>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Message> {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.literacyapp.chat.receiver;

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 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) {
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();
}
}
}

0 comments on commit 867e060

Please sign in to comment.