Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2b7a7583 authored by Enming Xie's avatar Enming Xie
Browse files

Fix Draft SMS is not saved in some context

parent 8350086f
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -99,7 +99,6 @@ public enum QKPreference {
    CONVERSATION_THEME("conversation_theme"),

    // Storage
    COMPOSE_DRAFT("compose_draft", ""),
    LAST_AUTO_DELETE_CHECK("last_auto_delete_check", 0);

    private String mKey;
+28 −0
Original line number Diff line number Diff line
@@ -4,8 +4,13 @@ import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.ViewGroup;

import com.moez.QKSMS.R;
import com.moez.QKSMS.mmssms.Utils;
import com.moez.QKSMS.ui.base.QKSwipeBackActivity;
import com.moez.QKSMS.ui.dialog.DefaultSmsHelper;
import com.moez.QKSMS.ui.dialog.QKDialog;

public class ComposeActivity extends QKSwipeBackActivity {

@@ -32,4 +37,27 @@ public class ComposeActivity extends QKSwipeBackActivity {
        new MenuInflater(this).inflate(R.menu.compose, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {
        // Check if we're not the default SMS app
        if (!Utils.isDefaultSmsApp(this)) {
            // Ask to become the default SMS app
            new DefaultSmsHelper(this, R.string.not_default_send)
                    .showIfNotDefault((ViewGroup)getWindow().getDecorView().getRootView());
        } else if (mComposeFragment != null && !mComposeFragment.isReplyTextEmpty()
                && mComposeFragment.getRecipientAddresses().length == 0) {
            // If there is Draft message and no recipients are set
            new QKDialog()
                    .setContext(this)
                    .setMessage(R.string.discard_message_reason)
                    .setPositiveButton(R.string.yes, v -> {
                        super.onBackPressed();
                    })
                    .setNegativeButton(R.string.cancel, null)
                    .show();
        } else {
            super.onBackPressed();
        }
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -105,4 +105,11 @@ public class ComposeFragment extends QKFragment implements ActivityLauncher, Rec
        mStarredContactsView.collapse();
        mComposeView.requestReplyTextFocus();
    }

    public boolean isReplyTextEmpty() {
        if (mComposeView != null) {
            return mComposeView.isReplyTextEmpty();
        }
        return true;
    }
}
+32 −10
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -748,13 +749,34 @@ public class ComposeView extends LinearLayout implements View.OnClickListener {
                    mConversationLegacy.saveDraft(draft);
                }
            } else {
                String oldDraft = mPrefs.getString(QKPreference.COMPOSE_DRAFT.getKey(), "");
                if (!draft.equals(oldDraft)) {
                    mPrefs.edit().putString(QKPreference.COMPOSE_DRAFT.getKey(), draft).apply();

                // Only show the draft if we saved text, not if we just cleared some
                if (!TextUtils.isEmpty(draft)) {
                        Toast.makeText(mContext, R.string.toast_draft, Toast.LENGTH_SHORT).show();
                    if (mRecipientProvider != null) {
                        String[] addresses = mRecipientProvider.getRecipientAddresses();

                        if (addresses != null && addresses.length > 0) {
                            // save the message for each of the addresses
                            for (int i = 0; i < addresses.length; i++) {
                                ContentValues values = new ContentValues();
                                values.put("address", addresses[i]);
                                values.put("date", System.currentTimeMillis());
                                values.put("read", 1);
                                values.put("type", 4);

                                // attempt to create correct thread id
                                long threadId = Utils.getOrCreateThreadId(mContext, addresses[i]);

                                Log.v(TAG, "saving message with thread id: " + threadId);

                                values.put("thread_id", threadId);
                                Uri messageUri = mContext.getContentResolver().insert(Uri.parse("content://sms/draft"), values);

                                Log.v(TAG, "inserted to uri: " + messageUri);

                                ConversationLegacy mConversationLegacy = new ConversationLegacy(mContext, threadId);
                                mConversationLegacy.saveDraft(draft);
                            }
                        }
                    }
                }
            }
@@ -780,10 +802,6 @@ public class ComposeView extends LinearLayout implements View.OnClickListener {
                mReplyText.setText("");
                clearAttachment();
            }
        } else {
            String draft = mPrefs.getString(QKPreference.COMPOSE_DRAFT.getKey(), "");
            mReplyText.setText(draft);
            mReplyText.setSelection(draft.length());
        }
    }

@@ -977,4 +995,8 @@ public class ComposeView extends LinearLayout implements View.OnClickListener {
                        ThemeManager.getTextOnColorPrimary() : ThemeManager.getTextOnColorSecondary(),
                PorterDuff.Mode.SRC_ATOP);
    }

    public boolean isReplyTextEmpty() {
        return TextUtils.isEmpty(mReplyText.getText());
    }
}