mirror of https://github.com/ThmmyNoLife/mTHMMY
Apostolos Fanakis
8 years ago
13 changed files with 504 additions and 27 deletions
@ -0,0 +1,213 @@ |
|||
package gr.thmmy.mthmmy.activities.profile; |
|||
|
|||
import android.content.DialogInterface; |
|||
import android.content.Intent; |
|||
import android.content.SharedPreferences; |
|||
import android.os.AsyncTask; |
|||
import android.os.Bundle; |
|||
import android.support.design.widget.FloatingActionButton; |
|||
import android.support.v4.content.res.ResourcesCompat; |
|||
import android.support.v7.app.AlertDialog; |
|||
import android.support.v7.widget.Toolbar; |
|||
import android.text.Html; |
|||
import android.util.Log; |
|||
import android.view.View; |
|||
import android.widget.ImageView; |
|||
import android.widget.LinearLayout; |
|||
import android.widget.ProgressBar; |
|||
import android.widget.TextView; |
|||
import android.widget.Toast; |
|||
|
|||
import com.squareup.picasso.Picasso; |
|||
|
|||
import org.jsoup.Jsoup; |
|||
import org.jsoup.nodes.Document; |
|||
|
|||
import java.util.ArrayList; |
|||
|
|||
import javax.net.ssl.SSLHandshakeException; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.activities.BaseActivity; |
|||
import gr.thmmy.mthmmy.activities.LoginActivity; |
|||
import gr.thmmy.mthmmy.utils.CircleTransform; |
|||
import mthmmy.utils.Report; |
|||
import okhttp3.Request; |
|||
import okhttp3.Response; |
|||
|
|||
import static gr.thmmy.mthmmy.activities.profile.ProfileParser.NAME_INDEX; |
|||
import static gr.thmmy.mthmmy.activities.profile.ProfileParser.PERSONAL_TEXT_INDEX; |
|||
import static gr.thmmy.mthmmy.activities.profile.ProfileParser.THUMBNAIL_URL; |
|||
import static gr.thmmy.mthmmy.activities.profile.ProfileParser.parseProfile; |
|||
import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; |
|||
import static gr.thmmy.mthmmy.session.SessionManager.LOGIN_STATUS; |
|||
|
|||
public class ProfileActivity extends BaseActivity { |
|||
|
|||
//Graphic elements
|
|||
private ImageView userThumbnail; |
|||
private TextView userName; |
|||
private TextView personalText; |
|||
private LinearLayout mainContent; |
|||
private ProgressBar progressBar; |
|||
private FloatingActionButton replyFAB; |
|||
|
|||
//Other variables
|
|||
private ArrayList<String> parsedProfileData; |
|||
@SuppressWarnings("unused") |
|||
private static final String TAG = "ProfileActivity"; |
|||
static String PACKAGE_NAME; |
|||
private static final int THUMBNAIL_SIZE = 200; |
|||
|
|||
@Override |
|||
protected void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
setContentView(R.layout.activity_profile); |
|||
|
|||
PACKAGE_NAME = getApplicationContext().getPackageName(); |
|||
|
|||
Bundle extras = getIntent().getExtras(); |
|||
//username = getIntent().getExtras().getString("TOPIC_TITLE");
|
|||
|
|||
//Initialize toolbar, drawer and ProgressBar
|
|||
toolbar = (Toolbar) findViewById(R.id.toolbar); |
|||
toolbar.setTitle(null); |
|||
setSupportActionBar(toolbar); |
|||
getSupportActionBar().setDisplayShowTitleEnabled(false); |
|||
getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
|||
//getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|||
|
|||
createDrawer(); |
|||
|
|||
progressBar = (ProgressBar) findViewById(R.id.progressBar); |
|||
|
|||
userThumbnail = (ImageView) findViewById(R.id.user_thumbnail); |
|||
userName = (TextView) findViewById(R.id.profile_act_username); |
|||
personalText = (TextView) findViewById(R.id.profile_act_personal_text); |
|||
mainContent = (LinearLayout) findViewById(R.id.profile_act_content); |
|||
|
|||
replyFAB = (FloatingActionButton) findViewById(R.id.profile_fab); |
|||
replyFAB.setEnabled(false); |
|||
|
|||
replyFAB.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); |
|||
int tmp_curr_status = sharedPrefs.getInt(LOGIN_STATUS, -1); |
|||
if (tmp_curr_status == -1) { |
|||
new AlertDialog.Builder(ProfileActivity.this) |
|||
.setTitle("ERROR!") |
|||
.setMessage("An error occurred while trying to find your LOGIN_STATUS.\n" + |
|||
"Please sent below info to developers:\n" |
|||
+ getLocalClassName() + "." + "l" |
|||
+ Thread.currentThread().getStackTrace()[1].getLineNumber()) |
|||
.setNeutralButton("Dismiss", new DialogInterface.OnClickListener() { |
|||
@Override |
|||
public void onClick(DialogInterface dialogInterface, int i) { |
|||
//Todo
|
|||
//Maybe sent info back to developers?
|
|||
} |
|||
}) |
|||
.show(); |
|||
} else if (tmp_curr_status != LOGGED_IN) { |
|||
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(); |
|||
} else { |
|||
//TODO
|
|||
//PM
|
|||
} |
|||
} |
|||
}); |
|||
|
|||
new ProfileTask().execute(extras.getString("PROFILE_URL")); //Attempt data parsing
|
|||
} |
|||
|
|||
public class ProfileTask extends AsyncTask<String, Void, Boolean> { |
|||
//Class variables
|
|||
private static final String TAG = "TopicTask"; //Separate tag for AsyncTask
|
|||
|
|||
//Show a progress bar until done
|
|||
protected void onPreExecute() { |
|||
progressBar.setVisibility(ProgressBar.VISIBLE); |
|||
replyFAB.setEnabled(false); |
|||
} |
|||
|
|||
protected Boolean doInBackground(String... strings) { |
|||
Document document; |
|||
String pageUrl = strings[0]; //This page's url
|
|||
|
|||
|
|||
Request request = new Request.Builder() |
|||
.url(pageUrl) |
|||
.build(); |
|||
try { |
|||
Response response = client.newCall(request).execute(); |
|||
document = Jsoup.parse(response.body().string()); |
|||
//long parseStartTime = System.nanoTime();
|
|||
parsedProfileData = parseProfile(document); //Parse data
|
|||
//long parseEndTime = System.nanoTime();
|
|||
return true; |
|||
} catch (SSLHandshakeException e) { |
|||
Report.w(TAG, "Certificate problem (please switch to unsafe connection)."); |
|||
} catch (Exception e) { |
|||
Report.e("TAG", "ERROR", e); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
protected void onPostExecute(Boolean result) { |
|||
if (!result) { //Parse failed!
|
|||
//Should never happen
|
|||
Toast.makeText(getBaseContext() |
|||
, "Fatal error!\n Aborting...", Toast.LENGTH_LONG).show(); |
|||
finish(); |
|||
} |
|||
//Parse was successful
|
|||
progressBar.setVisibility(ProgressBar.INVISIBLE); //Hide progress bar
|
|||
populateLayout(); //Show parsed data
|
|||
} |
|||
} |
|||
|
|||
private void populateLayout() { |
|||
if (parsedProfileData.get(THUMBNAIL_URL) != null) |
|||
Picasso.with(this) |
|||
.load(parsedProfileData.get(THUMBNAIL_URL)) |
|||
.resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) |
|||
.centerCrop() |
|||
.error(ResourcesCompat.getDrawable(this.getResources() |
|||
, R.drawable.ic_default_user_thumbnail, null)) |
|||
.placeholder(ResourcesCompat.getDrawable(this.getResources() |
|||
, R.drawable.ic_default_user_thumbnail, null)) |
|||
.transform(new CircleTransform()) |
|||
.into(userThumbnail); |
|||
userName.setText(parsedProfileData.get(NAME_INDEX)); |
|||
if (parsedProfileData.get(PERSONAL_TEXT_INDEX) != null) { |
|||
personalText.setVisibility(View.VISIBLE); |
|||
personalText.setText(parsedProfileData.get(PERSONAL_TEXT_INDEX)); |
|||
} else { |
|||
personalText.setVisibility(View.GONE); |
|||
} |
|||
|
|||
for (int i = PERSONAL_TEXT_INDEX; i < parsedProfileData.size(); ++i) { |
|||
TextView entry = new TextView(this); |
|||
entry.setTextColor(getResources().getColor(R.color.primary_text)); |
|||
entry.setText(Html.fromHtml(parsedProfileData.get(i))); |
|||
mainContent.addView(entry); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,95 @@ |
|||
package gr.thmmy.mthmmy.activities.profile; |
|||
|
|||
import android.util.Log; |
|||
|
|||
import org.jsoup.nodes.Document; |
|||
import org.jsoup.nodes.Element; |
|||
import org.jsoup.select.Elements; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Objects; |
|||
|
|||
import mthmmy.utils.Report; |
|||
|
|||
class ProfileParser { |
|||
//Parsing variables
|
|||
private static String nameSelect; |
|||
private static String signatureSelect; |
|||
|
|||
//Other variables
|
|||
@SuppressWarnings("unused") |
|||
private static final String TAG = "ProfileParser"; |
|||
static final int THUMBNAIL_URL = 0; |
|||
static final int NAME_INDEX = 1; |
|||
static final int PERSONAL_TEXT_INDEX = 2; |
|||
|
|||
static ArrayList<String> parseProfile(Document doc) { |
|||
defineLanguage(doc); |
|||
|
|||
//Method's variables
|
|||
ArrayList<String> returnArray = new ArrayList<>(); |
|||
|
|||
//Contains all summary's rows
|
|||
Elements summaryRows = doc.select("td.windowbg:nth-child(1)"); |
|||
|
|||
{ //Find thumbnail url
|
|||
Element tmpEl = doc.select(".bordercolor img.avatar").first(); |
|||
if (tmpEl != null) |
|||
returnArray.add(THUMBNAIL_URL, tmpEl.attr("abs:src")); |
|||
else //User doesn't have an avatar
|
|||
returnArray.add(THUMBNAIL_URL, null); |
|||
} |
|||
|
|||
{ //Find username
|
|||
Element tmpEl = summaryRows.select("tr:contains(" + nameSelect + ")").first(); |
|||
if (tmpEl != null) { |
|||
returnArray.add(NAME_INDEX, tmpEl.select("td").get(1).text()); |
|||
} else { |
|||
//Should never get here!
|
|||
//Something is wrong.
|
|||
Report.e(TAG, "An error occurred while trying to find profile's username."); |
|||
} |
|||
} |
|||
|
|||
{ //Find personal text
|
|||
String tmpPersonalText = doc.select("td.windowbg:nth-child(2)").first().text().trim(); |
|||
returnArray.add(PERSONAL_TEXT_INDEX, tmpPersonalText); |
|||
} |
|||
|
|||
for (Element row : summaryRows.select("tr")) { |
|||
String rowText = row.text(), tmpHtml = ""; |
|||
|
|||
if (row.select("td").size() == 1) |
|||
tmpHtml = ""; |
|||
else if (rowText.contains(signatureSelect)) { |
|||
tmpHtml = row.html(); |
|||
} else if (!rowText.contains(nameSelect)) { |
|||
if (Objects.equals(row.select("td").get(1).text(), "")) |
|||
continue; |
|||
tmpHtml = "<b>" + row.select("td").first().text() + "</b> " |
|||
+ row.select("td").get(1).text(); |
|||
} |
|||
returnArray.add(tmpHtml); |
|||
} |
|||
return returnArray; |
|||
} |
|||
|
|||
private static void defineLanguage(Document doc) { |
|||
//English parsing variables
|
|||
final String en_nameSelect = "Name"; |
|||
final String en_signatureSelect = "Signature"; |
|||
|
|||
//Greek parsing variables
|
|||
final String gr_nameSelect = "Όνομα"; |
|||
final String gr_signatureSelect = "Υπογραφή"; |
|||
|
|||
if (doc.select("h3").text().contains("Καλώς ορίσατε")) { |
|||
nameSelect = gr_nameSelect; |
|||
signatureSelect = gr_signatureSelect; |
|||
|
|||
} else { //Default is english (eg. guest's language)
|
|||
nameSelect = en_nameSelect; |
|||
signatureSelect = en_signatureSelect; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,121 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<android.support.design.widget.CoordinatorLayout |
|||
xmlns:android="http://schemas.android.com/apk/res/android" |
|||
xmlns:app="http://schemas.android.com/apk/res-auto" |
|||
xmlns:tools="http://schemas.android.com/tools" |
|||
android:id="@+id/main_content" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:fitsSystemWindows="true" |
|||
tools:context=".activities.profile.ProfileActivity"> |
|||
|
|||
<android.support.design.widget.AppBarLayout |
|||
android:id="@+id/appbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:paddingTop="@dimen/appbar_padding_top" |
|||
android:theme="@style/ToolbarTheme"> |
|||
|
|||
<android.support.design.widget.CollapsingToolbarLayout |
|||
android:id="@+id/main.collapsing" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:fitsSystemWindows="true" |
|||
app:contentScrim="?attr/colorPrimary" |
|||
app:expandedTitleMarginEnd="64dp" |
|||
app:expandedTitleMarginStart="48dp" |
|||
app:layout_scrollFlags="scroll|exitUntilCollapsed"> |
|||
|
|||
<LinearLayout |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:layout_marginTop="5dp" |
|||
android:gravity="center" |
|||
android:orientation="vertical"> |
|||
|
|||
<ImageView |
|||
android:id="@+id/user_thumbnail" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="center" |
|||
android:adjustViewBounds="true" |
|||
android:contentDescription="@string/thumbnail" |
|||
android:fitsSystemWindows="true" |
|||
android:src="@drawable/ic_default_user_thumbnail" |
|||
android:transitionName="user_thumbnail" |
|||
app:layout_collapseMode="parallax"/> |
|||
|
|||
<TextView |
|||
android:id="@+id/profile_act_personal_text" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="center" |
|||
android:textColor="@color/primary_text" |
|||
android:visibility="gone"/> |
|||
</LinearLayout> |
|||
</android.support.design.widget.CollapsingToolbarLayout> |
|||
|
|||
<android.support.v7.widget.Toolbar |
|||
android:id="@+id/toolbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="?attr/actionBarSize" |
|||
android:background="?attr/colorPrimary" |
|||
android:gravity="center" |
|||
app:popupTheme="@style/ToolbarTheme"> |
|||
|
|||
<TextView |
|||
android:id="@+id/profile_act_username" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="center" |
|||
android:textColor="@color/accent" |
|||
android:textSize="25sp"/> |
|||
|
|||
</android.support.v7.widget.Toolbar> |
|||
|
|||
</android.support.design.widget.AppBarLayout> |
|||
|
|||
<ProgressBar |
|||
android:id="@+id/progressBar" |
|||
style="@style/Widget.AppCompat.ProgressBar.Horizontal" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:indeterminate="true" |
|||
android:theme="@style/AppTheme" |
|||
android:visibility="invisible" |
|||
app:layout_anchor="@id/appbar" |
|||
app:layout_anchorGravity="bottom|center"/> |
|||
|
|||
<android.support.v4.widget.NestedScrollView |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:layout_gravity="top|start" |
|||
android:background="@color/background" |
|||
android:paddingEnd="16dp" |
|||
android:paddingStart="16dp" |
|||
android:scrollbars="none" |
|||
app:layout_anchor="@id/appbar" |
|||
app:layout_anchorGravity="bottom|center" |
|||
app:layout_behavior="@string/appbar_scrolling_view_behavior"> |
|||
|
|||
<LinearLayout |
|||
android:id="@+id/profile_act_content" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:gravity="center" |
|||
android:orientation="vertical"> |
|||
</LinearLayout> |
|||
</android.support.v4.widget.NestedScrollView> |
|||
|
|||
<android.support.design.widget.FloatingActionButton |
|||
android:id="@+id/profile_fab" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="bottom|end" |
|||
android:layout_margin="@dimen/fab_margins" |
|||
android:transitionName="fab" |
|||
app:layout_behavior="gr.thmmy.mthmmy.utils.ScrollAwareFABBehavior" |
|||
app:srcCompat="@drawable/ic_add_fab"/> |
|||
</android.support.design.widget.CoordinatorLayout> |
|||
|
|||
|
Loading…
Reference in new issue