From 27b2a04809ad6d3dce114f078af4d57b1cc4e654 Mon Sep 17 00:00:00 2001 From: oogee Date: Wed, 23 Jan 2019 11:50:17 +0200 Subject: [PATCH] complete task for sending PMs --- app/src/main/AndroidManifest.xml | 4 +- .../create_pm/CreatePMActivity.java | 57 +++++++++++- .../activities/create_pm/SendPMTask.java | 87 +++++++++++++++++++ .../activities/profile/ProfileActivity.java | 65 +++++++------- 4 files changed, 180 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/SendPMTask.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 28662173..80f6e4b8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -173,7 +173,9 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".activities.main.MainActivity" /> - + \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/CreatePMActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/CreatePMActivity.java index 54eec9ea..e690023b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/CreatePMActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/CreatePMActivity.java @@ -1,30 +1,45 @@ package gr.thmmy.mthmmy.activities.create_pm; +import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.text.InputType; +import android.text.TextUtils; import android.view.View; import android.view.inputmethod.EditorInfo; +import android.widget.Toast; import com.google.android.material.textfield.TextInputLayout; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.activities.profile.ProfileActivity; +import gr.thmmy.mthmmy.activities.settings.SettingsActivity; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.editorview.EditorView; import gr.thmmy.mthmmy.editorview.EmojiKeyboard; +import gr.thmmy.mthmmy.session.SessionManager; +import gr.thmmy.mthmmy.utils.ExternalAsyncTask; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; +import timber.log.Timber; -public class CreatePMActivity extends BaseActivity { +public class CreatePMActivity extends BaseActivity implements ExternalAsyncTask.OnTaskStartedListener, ExternalAsyncTask.OnTaskFinishedListener { private MaterialProgressBar progressBar; private EditorView contentEditor; private TextInputLayout subjectInput; private EmojiKeyboard emojiKeyboard; + private String username, sendPmUrl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create_pm); + Intent callingIntent = getIntent(); + username = callingIntent.getStringExtra(ProfileActivity.BUNDLE_PROFILE_USERNAME); + sendPmUrl = callingIntent.getStringExtra(ProfileActivity.BUNDLE_SEND_PM_URL); + //Initialize toolbar toolbar = findViewById(R.id.toolbar); toolbar.setTitle("Create topic"); @@ -46,7 +61,27 @@ public class CreatePMActivity extends BaseActivity { contentEditor.setEmojiKeyboard(emojiKeyboard); emojiKeyboard.registerEmojiInputField(contentEditor); contentEditor.setOnSubmitListener(v -> { - // TODO: send pm + if (TextUtils.isEmpty(subjectInput.getEditText().getText())) { + subjectInput.setError("Required"); + return; + } + if (TextUtils.isEmpty(contentEditor.getText())) { + contentEditor.setError("Required"); + return; + } + + boolean includeAppSignature = true; + SessionManager sessionManager = BaseActivity.getSessionManager(); + if (sessionManager.isLoggedIn()) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + includeAppSignature = prefs.getBoolean(SettingsActivity.POSTING_APP_SIGNATURE_ENABLE_KEY, true); + } + + SendPMTask sendPMTask = new SendPMTask(includeAppSignature); + sendPMTask.setOnTaskStartedListener(this); + sendPMTask.setOnTaskFinishedListener(this); + sendPMTask.execute(sendPmUrl, username, subjectInput.getEditText().getText().toString(), + contentEditor.getText().toString()); }); } @@ -60,4 +95,22 @@ public class CreatePMActivity extends BaseActivity { } + @Override + public void onTaskStarted() { + Timber.i("New pm started being sent"); + progressBar.setVisibility(View.VISIBLE); + } + + @Override + public void onTaskFinished(Boolean success) { + progressBar.setVisibility(View.INVISIBLE); + if (success) { + Timber.i("New pm sent successfully"); + Toast.makeText(this, "Personal message sent successfully", Toast.LENGTH_SHORT).show(); + finish(); + } else { + Timber.w("Failed to send pm"); + Toast.makeText(getBaseContext(), "Failed to send PM. Check your connection", Toast.LENGTH_LONG).show(); + } + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/SendPMTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/SendPMTask.java new file mode 100644 index 00000000..875504dc --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/SendPMTask.java @@ -0,0 +1,87 @@ +package gr.thmmy.mthmmy.activities.create_pm; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + +import java.io.IOException; + +import gr.thmmy.mthmmy.base.BaseApplication; +import gr.thmmy.mthmmy.utils.ExternalAsyncTask; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import timber.log.Timber; + +import static gr.thmmy.mthmmy.activities.topic.Posting.replyStatus; + +public class SendPMTask extends ExternalAsyncTask { + + private boolean includeAppSignature; + + public SendPMTask(boolean includeAppSignature) { + this.includeAppSignature = includeAppSignature; + } + + @Override + protected Boolean doInBackground(String... strings) { + Request request = new Request.Builder() + .url(strings[0] + ";wap2") + .build(); + + OkHttpClient client = BaseApplication.getInstance().getClient(); + + Document document; + String seqnum, sc, outbox, createTopicUrl, replied_to, folder; + try { + Response response = client.newCall(request).execute(); + document = Jsoup.parse(response.body().string()); + + seqnum = document.select("input[name=seqnum]").first().attr("value"); + sc = document.select("input[name=sc]").first().attr("value"); + outbox = document.select("input[name=outbox]").first().attr("value"); + replied_to = document.select("input[name=replied_to]").first().attr("value"); + folder = document.select("input[name=folder]").first().attr("value"); + createTopicUrl = document.select("form").first().attr("action"); + + final String appSignature = "\n[right][size=7pt][i]sent from [url=https://play.google.com/store/apps/" + + "details?id=gr.thmmy.mthmmy]mTHMMY[/url] [/i][/size][/right]"; + + RequestBody postBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("message", strings[3] + (includeAppSignature ? appSignature : "")) + .addFormDataPart("seqnum", seqnum) + .addFormDataPart("sc", sc) + .addFormDataPart("u", strings[1]) // recipient + .addFormDataPart("subject", strings[2]) + .addFormDataPart("outbox", outbox) + .addFormDataPart("replied_to", replied_to) + .addFormDataPart("folder", folder) + .build(); + + Request pmRequest = new Request.Builder() + .url(createTopicUrl) + .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36") + .post(postBody) + .build(); + + try { + client.newCall(pmRequest).execute(); + Response response2 = client.newCall(pmRequest).execute(); + switch (replyStatus(response2)) { + case SUCCESSFUL: + BaseApplication.getInstance().logFirebaseAnalyticsEvent("new_topic_creation", null); + return true; + default: + Timber.e("Malformed pmRequest. Request string: %s", pmRequest.toString()); + return false; + } + } catch (IOException e) { + return false; + } + } catch (IOException e) { + return false; + } + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java index 247365c8..b54f9484 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java @@ -1,5 +1,6 @@ package gr.thmmy.mthmmy.activities.profile; +import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; @@ -31,6 +32,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; @@ -38,6 +40,8 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; import androidx.viewpager.widget.ViewPager; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.activities.LoginActivity; +import gr.thmmy.mthmmy.activities.create_pm.CreatePMActivity; import gr.thmmy.mthmmy.activities.profile.latestPosts.LatestPostsFragment; import gr.thmmy.mthmmy.activities.profile.stats.StatsFragment; import gr.thmmy.mthmmy.activities.profile.summary.SummaryFragment; @@ -79,6 +83,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment * If username is not available put an empty string or leave it null. */ public static final String BUNDLE_PROFILE_USERNAME = "USERNAME"; + public static final String BUNDLE_SEND_PM_URL = "send-pm-url"; private TextView usernameView; private ImageView avatarView; @@ -92,6 +97,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment private String profileUrl; private String avatarUrl; private String username; + private String sendPmUrl; private int tabSelect; //Fix for vector drawables on android <21 @@ -140,37 +146,29 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment viewPager = findViewById(R.id.profile_tab_container); pmFAB = findViewById(R.id.profile_fab); - pmFAB.setEnabled(false); - pmFAB.hide(); - /*if (!sessionManager.isLoggedIn()) pmFAB.hide(); + if (!sessionManager.isLoggedIn()) pmFAB.hide(); else { - pmFAB.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (sessionManager.isLoggedIn()) { - //TODO PM - } else { - new AlertDialog.Builder(ProfileActivity.this) - .setMessage("You need to be logged in to sent a personal message!") - .setPositiveButton("Login", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Intent intent = new Intent(ProfileActivity.this, LoginActivity.class); - startActivity(intent); - finish(); - overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); - } - }) - .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - } - }) - .show(); - } + pmFAB.setOnClickListener(view -> { + if (sessionManager.isLoggedIn()) { + Intent sendPMIntent = new Intent(ProfileActivity.this, CreatePMActivity.class); + sendPMIntent.putExtra(BUNDLE_PROFILE_USERNAME, username); + sendPMIntent.putExtra(BUNDLE_SEND_PM_URL, sendPmUrl); + startActivity(sendPMIntent); + } else { + new AlertDialog.Builder(ProfileActivity.this) + .setMessage("You need to be logged in to sent a personal message!") + .setPositiveButton("Login", (dialogInterface, i) -> { + Intent intent = new Intent(ProfileActivity.this, LoginActivity.class); + startActivity(intent); + finish(); + overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); + }) + .setNegativeButton("Cancel", (dialogInterface, i) -> { + }) + .show(); } }); - }*/ + } ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(Uri.parse(profileUrl)); if (!target.is(ThmmyPage.PageCategory.PROFILE)) { @@ -211,7 +209,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(false); } - private void loadAvatar(){ + private void loadAvatar() { Picasso.with(this) .load(avatarUrl) .fit() @@ -224,7 +222,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment .into(avatarView); } - private void loadDefaultAvatar(){ + private void loadDefaultAvatar() { Picasso.with(this) .load(R.drawable.ic_default_user_avatar) .fit() @@ -296,6 +294,13 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A")) , 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } + // Url needed to send to send pm + Elements urllinks; + urllinks = profilePage.select("a:contains(Send this member a personal message.)"); + if (urllinks.size() == 0) { + urllinks = profilePage.select("a:contains(Αποστολή προσωπικού μηνύματος σε αυτό το μέλος.)"); + } + sendPmUrl = urllinks.first().attr("href"); return null; }