Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Review] Minor performance tweaks #74

Merged
merged 4 commits into from
Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change from Serializable to Parcelable
  • Loading branch information
pt2121 committed Aug 13, 2015
commit fda8a6757c5f51cc37d6065844d59949ef815ec2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.rockerhieu.emojicon;

import java.util.Arrays;
import com.rockerhieu.emojicon.emoji.Emojicon;
import com.rockerhieu.emojicon.emoji.People;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand All @@ -25,8 +27,6 @@
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import com.rockerhieu.emojicon.emoji.Emojicon;
import com.rockerhieu.emojicon.emoji.People;

/**
* @author Hieu Rocker (rockerhieu@gmail.com)
Expand All @@ -38,6 +38,7 @@ public class EmojiconGridFragment extends Fragment implements AdapterView.OnItem
private boolean mUseSystemDefault = false;

private static final String USE_SYSTEM_DEFAULT_KEY = "useSystemDefaults";
private static final String EMOJICONS_KEY = "emojicons";

protected static EmojiconGridFragment newInstance(Emojicon[] emojicons, EmojiconRecents recents) {
return newInstance(emojicons, recents, false);
Expand All @@ -46,7 +47,7 @@ protected static EmojiconGridFragment newInstance(Emojicon[] emojicons, Emojicon
protected static EmojiconGridFragment newInstance(Emojicon[] emojicons, EmojiconRecents recents, boolean useSystemDefault) {
EmojiconGridFragment emojiGridFragment = new EmojiconGridFragment();
Bundle args = new Bundle();
args.putSerializable("emojicons", emojicons);
args.putParcelableArray(EMOJICONS_KEY, emojicons);
args.putBoolean(USE_SYSTEM_DEFAULT_KEY, useSystemDefault);
emojiGridFragment.setArguments(args);
emojiGridFragment.setRecents(recents);
Expand All @@ -66,8 +67,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
mData = People.DATA;
mUseSystemDefault = false;
} else {
Object[] o = (Object[]) getArguments().getSerializable("emojicons");
mData = Arrays.asList(o).toArray(new Emojicon[o.length]);
mData = (Emojicon[]) getArguments().getParcelableArray(EMOJICONS_KEY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this throws an exception

Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.rockerhieu.emojicon.emoji.Emojicon[]

it can be fixed as described here: http://stackoverflow.com/questions/3647432/how-to-pass-an-array-of-address-objects-to-an-other-acitvity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uwemaurer Interesting. It worked fine when I tested it. Does it crash your app or something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we see this exception in our exception reporting. (reported on Android 4.1.2 and 4.4.2 so far)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uwemaurer Maybe I am missing something but I can't reproduce this on emulators API 19 and 23. Anyway, I've sent a pull request (#79). Thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reproduce it on my phone when I enable 'Don't keep activities' in the developer options. I can confirm that your pull request fixes it.

I got this stacktrace (maybe it has to do with the support library):

E/AndroidRuntime(26523): Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.rockerhieu.emojicon.emoji.Emojicon[]
E/AndroidRuntime(26523): at com.rockerhieu.emojicon.EmojiconGridFragment.onViewCreated(EmojiconGridFragment.java:70)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1179)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1991)
E/AndroidRuntime(26523): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1976)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1179)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1991)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165)
E/AndroidRuntime(26523): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:507)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uwemaurer Ok Thanks. It's good to learn something new every day!

mUseSystemDefault = bundle.getBoolean(USE_SYSTEM_DEFAULT_KEY);
}
gridView.setAdapter(new EmojiAdapter(view.getContext(), mData, mUseSystemDefault));
Expand All @@ -77,7 +77,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("emojicons", mData);
outState.putParcelableArray(EMOJICONS_KEY, mData);
}

@Override
Expand Down
64 changes: 52 additions & 12 deletions library/src/main/java/com/rockerhieu/emojicon/emoji/Emojicon.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,51 @@

package com.rockerhieu.emojicon.emoji;

import java.io.Serializable;
import android.os.Parcel;
import android.os.Parcelable;

/**
* @author Hieu Rocker (rockerhieu@gmail.com)
*/
public class Emojicon implements Serializable {
private static final long serialVersionUID = 1L;
public class Emojicon implements Parcelable {

public static final Creator<Emojicon> CREATOR = new Creator<Emojicon>() {
@Override
public Emojicon createFromParcel(Parcel in) {
return new Emojicon(in);
}

@Override
public Emojicon[] newArray(int size) {
return new Emojicon[size];
}
};

private int icon;

private char value;

private String emoji;

public Emojicon(int icon, char value, String emoji) {
this.icon = icon;
this.value = value;
this.emoji = emoji;
}

public Emojicon(Parcel in) {
this.icon = in.readInt();
this.value = (char) in.readInt();
this.emoji = in.readString();
}

private Emojicon() {
}

public Emojicon(String emoji) {
this.emoji = emoji;
}

public static Emojicon fromResource(int icon, int value) {
Emojicon emoji = new Emojicon();
emoji.icon = icon;
Expand All @@ -55,8 +86,24 @@ public static Emojicon fromChars(String chars) {
return emoji;
}

public Emojicon(String emoji) {
this.emoji = emoji;
public static final String newString(int codePoint) {
if (Character.charCount(codePoint) == 1) {
return String.valueOf(codePoint);
} else {
return new String(Character.toChars(codePoint));
}
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(icon);
dest.writeInt(value);
dest.writeString(emoji);
}

public char getValue() {
Expand All @@ -81,11 +128,4 @@ public int hashCode() {
return emoji.hashCode();
}

public static final String newString(int codePoint) {
if (Character.charCount(codePoint) == 1) {
return String.valueOf(codePoint);
} else {
return new String(Character.toChars(codePoint));
}
}
}