improved robustness against shit data in dm

fixed several crashes
This commit is contained in:
Mariotaku Lee 2017-04-14 18:46:14 +08:00
parent b3090b93f5
commit c6e1862c14
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
6 changed files with 85 additions and 33 deletions

View File

@ -35,6 +35,7 @@ import com.hannesdorfmann.parcelableplease.annotation.ParcelableNoThanks;
import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease;
import org.mariotaku.commons.objectcursor.LoganSquareCursorFieldConverter;
import org.mariotaku.library.objectcursor.annotation.AfterCursorObjectCreated;
import org.mariotaku.library.objectcursor.annotation.BeforeWriteContentValues;
import org.mariotaku.library.objectcursor.annotation.CursorField;
import org.mariotaku.library.objectcursor.annotation.CursorObject;
@ -189,30 +190,6 @@ public class ParcelableMessageConversation implements Parcelable {
@ParcelableNoThanks
InternalExtras internalConversationExtras;
@OnPreJsonSerialize
void beforeJsonSerialize() {
internalMessageExtras = ParcelableMessage.InternalExtras.from(message_extras);
internalConversationExtras = InternalExtras.from(conversation_extras);
prepareParticipantKeys();
}
@OnJsonParseComplete
void onJsonParseComplete() {
if (internalMessageExtras != null) {
message_extras = internalMessageExtras.getExtras();
}
if (internalConversationExtras != null) {
conversation_extras = internalConversationExtras.getExtras();
}
}
@BeforeWriteContentValues
void beforeWriteContentValues(ContentValues values) throws IOException {
prepareParticipantKeys();
}
@Override
public String toString() {
return "ParcelableMessageConversation{" +
@ -244,6 +221,64 @@ public class ParcelableMessageConversation implements Parcelable {
'}';
}
@OnPreJsonSerialize
void beforeJsonSerialize() {
internalMessageExtras = ParcelableMessage.InternalExtras.from(message_extras);
internalConversationExtras = InternalExtras.from(conversation_extras);
prepareParticipantKeys();
}
@OnJsonParseComplete
void onJsonParseComplete() {
if (internalMessageExtras != null) {
message_extras = internalMessageExtras.getExtras();
}
if (internalConversationExtras != null) {
conversation_extras = internalConversationExtras.getExtras();
}
if (participants != null) {
participants = removeNullParticipants(participants);
}
}
@AfterCursorObjectCreated
void afterCursorObjectCreated() {
if (participants != null) {
participants = removeNullParticipants(participants);
}
}
@BeforeWriteContentValues
void beforeWriteContentValues(ContentValues values) throws IOException {
if (participants != null) {
participants = removeNullParticipants(participants);
}
prepareParticipantKeys();
}
private void onParcelableCreated() {
if (participants != null) {
participants = removeNullParticipants(participants);
}
}
private ParcelableUser[] removeNullParticipants(final ParcelableUser[] participants) {
int nullCount = 0;
for (final ParcelableUser user : participants) {
if (user == null) nullCount++;
}
if (nullCount == 0) return participants;
final int resultLength = participants.length - nullCount;
final ParcelableUser[] result = new ParcelableUser[resultLength];
for (int i = 0, j = 0, l = participants.length; i < l; i++) {
final ParcelableUser user = participants[i];
if (user == null) continue;
result[j++] = user;
}
return result;
}
private void prepareParticipantKeys() {
// Ensure keys are ordered
if (participants != null && participant_keys == null) {
@ -256,24 +291,24 @@ public class ParcelableMessageConversation implements Parcelable {
Arrays.sort(participant_keys);
}
}
@StringDef({ConversationType.ONE_TO_ONE, ConversationType.GROUP})
@Retention(RetentionPolicy.SOURCE)
public @interface ConversationType {
String ONE_TO_ONE = "one_to_one";
String GROUP = "group";
}
}
@StringDef({ExtrasType.DEFAULT, ExtrasType.TWITTER_OFFICIAL})
@Retention(RetentionPolicy.SOURCE)
public @interface ExtrasType {
String DEFAULT = "default";
String TWITTER_OFFICIAL = "twitter_official";
}
@JsonObject
static class InternalExtras {
@JsonField(name = "twitter_official")
TwitterOfficialConversationExtras twitterOfficial;
@ -287,13 +322,13 @@ public class ParcelableMessageConversation implements Parcelable {
}
return result;
}
public ConversationExtras getExtras() {
if (twitterOfficial != null) {
return twitterOfficial;
}
return null;
}
}
@Override
@ -310,6 +345,7 @@ public class ParcelableMessageConversation implements Parcelable {
public ParcelableMessageConversation createFromParcel(Parcel source) {
ParcelableMessageConversation target = new ParcelableMessageConversation();
ParcelableMessageConversationParcelablePlease.readFromParcel(target, source);
target.onParcelableCreated();
return target;
}

View File

@ -1 +1 @@
54042eb537a2d7133b28350a33773882de5fe472
495800fef7680a282e36ffa89ff57ae488aa1a98

View File

@ -192,6 +192,12 @@ class QuickSearchBarActivity : BaseActivity(), OnClickListener, LoaderCallbacks<
startActivityForResult(scanIntent, REQUEST_SCAN_QR)
} catch (e: ActivityNotFoundException) {
// Ignore
Toast.makeText(this, R.string.message_toast_qr_scanner_not_supported,
Toast.LENGTH_SHORT).show()
} catch (e: SecurityException) {
// Goddamned SAMSUNG again!!!
Toast.makeText(this, R.string.message_toast_qr_scanner_not_supported,
Toast.LENGTH_SHORT).show()
}
} else {
doSearch()

View File

@ -115,7 +115,11 @@ fun ParcelableMessageConversation.getSummaryText(context: Context, manager: User
fun ParcelableMessageConversation.addParticipants(users: Collection<ParcelableUser>) {
val participants = this.participants
if (participants == null) {
this.participants = arrayOf(user)
if (user != null) {
this.participants = arrayOf(user)
} else {
this.participants = emptyArray()
}
} else {
val addingUsers = ArrayList<ParcelableUser>()
users.forEach { user ->

View File

@ -238,6 +238,7 @@ class DraftsFragment : BaseFragment(), LoaderCallbacks<Cursor?>, OnItemClickList
val status = extras.status ?: return false
val intent = Intent(context, RetweetQuoteDialogActivity::class.java).apply {
putExtra(EXTRA_STATUS, status)
putExtra(EXTRA_STATUS_ID, status.id)
putExtra(EXTRA_ACCOUNT_KEY, draft.account_keys?.singleOrNull())
putExtra(EXTRA_TEXT, draft.text)
}

View File

@ -38,8 +38,13 @@ class JsonCache(val cacheDir: File) {
}
fun <T> getList(key: String, cls: Class<T>): List<T>? {
return cache?.get(key)?.getFile(0)?.inputStream()?.use {
JsonSerializer.parseList(it, cls)
val value = cache?.get(key) ?: return null
try {
return value.getFile(0)?.inputStream()?.use {
JsonSerializer.parseList(it, cls)
}
} catch (e: IOException) {
return null
}
}