Browse Source

complete task for sending PMs

pms
oogee 6 years ago
parent
commit
27b2a04809
  1. 4
      app/src/main/AndroidManifest.xml
  2. 57
      app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/CreatePMActivity.java
  3. 87
      app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/SendPMTask.java
  4. 65
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java

4
app/src/main/AndroidManifest.xml

@ -173,7 +173,9 @@
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity" /> android:value=".activities.main.MainActivity" />
</activity> </activity>
<activity android:name=".activities.create_pm.CreatePMActivity"></activity> <activity android:name=".activities.create_pm.CreatePMActivity"
android:configChanges="orientation|screenSize"
android:theme="@style/AppTheme.NoActionBar" />
</application> </application>
</manifest> </manifest>

57
app/src/main/java/gr/thmmy/mthmmy/activities/create_pm/CreatePMActivity.java

@ -1,30 +1,45 @@
package gr.thmmy.mthmmy.activities.create_pm; package gr.thmmy.mthmmy.activities.create_pm;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType; import android.text.InputType;
import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
import gr.thmmy.mthmmy.R; 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.base.BaseActivity;
import gr.thmmy.mthmmy.editorview.EditorView; import gr.thmmy.mthmmy.editorview.EditorView;
import gr.thmmy.mthmmy.editorview.EmojiKeyboard; 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 me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import timber.log.Timber;
public class CreatePMActivity extends BaseActivity { public class CreatePMActivity extends BaseActivity implements ExternalAsyncTask.OnTaskStartedListener, ExternalAsyncTask.OnTaskFinishedListener<Boolean> {
private MaterialProgressBar progressBar; private MaterialProgressBar progressBar;
private EditorView contentEditor; private EditorView contentEditor;
private TextInputLayout subjectInput; private TextInputLayout subjectInput;
private EmojiKeyboard emojiKeyboard; private EmojiKeyboard emojiKeyboard;
private String username, sendPmUrl;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_pm); 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 //Initialize toolbar
toolbar = findViewById(R.id.toolbar); toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Create topic"); toolbar.setTitle("Create topic");
@ -46,7 +61,27 @@ public class CreatePMActivity extends BaseActivity {
contentEditor.setEmojiKeyboard(emojiKeyboard); contentEditor.setEmojiKeyboard(emojiKeyboard);
emojiKeyboard.registerEmojiInputField(contentEditor); emojiKeyboard.registerEmojiInputField(contentEditor);
contentEditor.setOnSubmitListener(v -> { 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();
}
}
} }

87
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<String, Boolean> {
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;
}
}
}

65
app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java

@ -1,5 +1,6 @@
package gr.thmmy.mthmmy.activities.profile; package gr.thmmy.mthmmy.activities.profile;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
@ -31,6 +32,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -38,6 +40,8 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter; import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager.widget.ViewPager;
import gr.thmmy.mthmmy.R; 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.latestPosts.LatestPostsFragment;
import gr.thmmy.mthmmy.activities.profile.stats.StatsFragment; import gr.thmmy.mthmmy.activities.profile.stats.StatsFragment;
import gr.thmmy.mthmmy.activities.profile.summary.SummaryFragment; 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. * 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_PROFILE_USERNAME = "USERNAME";
public static final String BUNDLE_SEND_PM_URL = "send-pm-url";
private TextView usernameView; private TextView usernameView;
private ImageView avatarView; private ImageView avatarView;
@ -92,6 +97,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
private String profileUrl; private String profileUrl;
private String avatarUrl; private String avatarUrl;
private String username; private String username;
private String sendPmUrl;
private int tabSelect; private int tabSelect;
//Fix for vector drawables on android <21 //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); viewPager = findViewById(R.id.profile_tab_container);
pmFAB = findViewById(R.id.profile_fab); pmFAB = findViewById(R.id.profile_fab);
pmFAB.setEnabled(false); if (!sessionManager.isLoggedIn()) pmFAB.hide();
pmFAB.hide();
/*if (!sessionManager.isLoggedIn()) pmFAB.hide();
else { else {
pmFAB.setOnClickListener(new View.OnClickListener() { pmFAB.setOnClickListener(view -> {
@Override if (sessionManager.isLoggedIn()) {
public void onClick(View view) { Intent sendPMIntent = new Intent(ProfileActivity.this, CreatePMActivity.class);
if (sessionManager.isLoggedIn()) { sendPMIntent.putExtra(BUNDLE_PROFILE_USERNAME, username);
//TODO PM sendPMIntent.putExtra(BUNDLE_SEND_PM_URL, sendPmUrl);
} else { startActivity(sendPMIntent);
new AlertDialog.Builder(ProfileActivity.this) } else {
.setMessage("You need to be logged in to sent a personal message!") new AlertDialog.Builder(ProfileActivity.this)
.setPositiveButton("Login", new DialogInterface.OnClickListener() { .setMessage("You need to be logged in to sent a personal message!")
@Override .setPositiveButton("Login", (dialogInterface, i) -> {
public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(ProfileActivity.this, LoginActivity.class);
Intent intent = new Intent(ProfileActivity.this, LoginActivity.class); startActivity(intent);
startActivity(intent); finish();
finish(); overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); })
} .setNegativeButton("Cancel", (dialogInterface, i) -> {
}) })
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { .show();
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.show();
}
} }
}); });
}*/ }
ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(Uri.parse(profileUrl)); ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(Uri.parse(profileUrl));
if (!target.is(ThmmyPage.PageCategory.PROFILE)) { 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); if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(false);
} }
private void loadAvatar(){ private void loadAvatar() {
Picasso.with(this) Picasso.with(this)
.load(avatarUrl) .load(avatarUrl)
.fit() .fit()
@ -224,7 +222,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
.into(avatarView); .into(avatarView);
} }
private void loadDefaultAvatar(){ private void loadDefaultAvatar() {
Picasso.with(this) Picasso.with(this)
.load(R.drawable.ic_default_user_avatar) .load(R.drawable.ic_default_user_avatar)
.fit() .fit()
@ -296,6 +294,13 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A")) usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A"))
, 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); , 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; return null;
} }

Loading…
Cancel
Save