Browse Source

Attachments and logo fix.

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
6112e981f4
  1. 2
      app/src/main/AndroidManifest.xml
  2. 85
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  3. 35
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  4. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  5. 8
      app/src/main/java/gr/thmmy/mthmmy/data/Post.java
  6. 14
      app/src/main/java/gr/thmmy/mthmmy/utils/FontManager.java
  7. BIN
      app/src/main/res/drawable/logo_animated.gif
  8. 7
      app/src/main/res/layout/activity_login.xml
  9. 10
      app/src/main/res/layout/activity_topic.xml

2
app/src/main/AndroidManifest.xml

@ -4,6 +4,8 @@
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

85
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -1,19 +1,24 @@
package gr.thmmy.mthmmy.activities.topic; package gr.thmmy.mthmmy.activities.topic;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
@ -22,6 +27,9 @@ import android.widget.Toast;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -33,9 +41,12 @@ import gr.thmmy.mthmmy.activities.BaseActivity;
import gr.thmmy.mthmmy.activities.LoginActivity; import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.data.Post; import gr.thmmy.mthmmy.data.Post;
import mthmmy.utils.Report; import mthmmy.utils.Report;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN;
import static gr.thmmy.mthmmy.session.SessionManager.LOGIN_STATUS; import static gr.thmmy.mthmmy.session.SessionManager.LOGIN_STATUS;
@ -79,12 +90,15 @@ public class TopicActivity extends BaseActivity {
private String parsedTitle; private String parsedTitle;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager; private RecyclerView.LayoutManager layoutManager;
static String PACKAGE_NAME;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic); setContentView(R.layout.activity_topic);
PACKAGE_NAME = getApplicationContext().getPackageName();
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
topicTitle = getIntent().getExtras().getString("TOPIC_TITLE"); topicTitle = getIntent().getExtras().getString("TOPIC_TITLE");
@ -452,4 +466,75 @@ public class TopicActivity extends BaseActivity {
} }
} }
//--------------------------------------REPETITIVE UPDATER END-------------------------------------- //--------------------------------------REPETITIVE UPDATER END--------------------------------------
//------------------------------METHODS FOR DOWNLOADING ATTACHED FILES------------------------------
/**
* Create a File
*/
static void downloadFileAsync(final String downloadUrl, final String fileName, final Context context) {
Request request = new Request.Builder().url(downloadUrl).build();
//final File[] tmpFile = new File[1];
getClient().newCall(request).enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Failed to download file: " + response);
}
File tmpFile = getOutputMediaFile(PACKAGE_NAME, fileName);
if (tmpFile == null) {
Report.d(TAG
, "Error creating media file, check storage permissions!");
} else {
FileOutputStream fos = new FileOutputStream(tmpFile);
fos.write(response.body().bytes());
fos.close();
String filePath = tmpFile.getAbsolutePath();
String extension = MimeTypeMap.getFileExtensionFromUrl(
filePath.substring(filePath.lastIndexOf("/")));
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(tmpFile), mime);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
});
}
/**
* Create a File
*/
private static File getOutputMediaFile(String packageName, String fileName) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ PACKAGE_NAME //TODO
+ "/Downloads");
// This location works best if you want the created files to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "problem!");
return null;
}
}
// Create a media file name
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + fileName);
return mediaFile;
}
//----------------------------METHODS FOR DOWNLOADING ATTACHED FILES END----------------------------
} }

35
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -5,15 +5,14 @@ import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.support.v4.content.res.ResourcesCompat; import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
@ -37,15 +36,14 @@ import java.util.Objects;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.data.Post; import gr.thmmy.mthmmy.data.Post;
import gr.thmmy.mthmmy.utils.CircleTransform; import gr.thmmy.mthmmy.utils.CircleTransform;
import gr.thmmy.mthmmy.utils.FontManager;
import mthmmy.utils.Report; import mthmmy.utils.Report;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.NO_POST_FOCUS; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.NO_POST_FOCUS;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.base_url; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.base_url;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.downloadFileAsync;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.postFocus; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.postFocus;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.toQuoteList; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.toQuoteList;
import static gr.thmmy.mthmmy.utils.FontManager.FONTAWESOME;
class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> { class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
private static final String TAG = "TopicAdapter"; private static final String TAG = "TopicAdapter";
@ -54,7 +52,7 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
private final Context context; private final Context context;
private final List<Post> postsList; private final List<Post> postsList;
private boolean foundPostFocus = false; private boolean foundPostFocus = false;
private ArrayList<boolean[]> viewProperties = new ArrayList<>(); private final ArrayList<boolean[]> viewProperties = new ArrayList<>();
private static final int isPostDateAndNumberVisibile = 0; private static final int isPostDateAndNumberVisibile = 0;
private static final int isUserExtraInfoVisibile = 1; private static final int isUserExtraInfoVisibile = 1;
private static final int isQuoteButtonChecked = 2; private static final int isQuoteButtonChecked = 2;
@ -195,13 +193,29 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
if (currentPost.getAttachedFiles().size() != 0) { if (currentPost.getAttachedFiles().size() != 0) {
holder.bodyFooterDivider.setVisibility(View.VISIBLE); holder.bodyFooterDivider.setVisibility(View.VISIBLE);
for (String attachedFile : currentPost.getAttachedFiles()) { String faFile = context.getResources().getString(R.string.fa_file);
TextView attached = new TextView(context); int filesTextColor = context.getResources().getColor(R.color.accent);
for (final String[] attachedFile : currentPost.getAttachedFiles()) {
final TextView attached = new TextView(context);
attached.setTextSize(10f); attached.setTextSize(10f);
attached.setClickable(true); attached.setClickable(true);
attached.setMovementMethod(LinkMovementMethod.getInstance()); attached.setTypeface(Typeface.createFromAsset(context.getAssets()
, "fonts/fontawesome-webfont.ttf"));
attached.setText(faFile + " " + attachedFile[1] + attachedFile[2]);
attached.setTextColor(filesTextColor);
attached.setPadding(0, 3, 0, 3);
attached.setText(Html.fromHtml(attachedFile)); attached.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
downloadFileAsync(attachedFile[0], attachedFile[1], context);
} catch (Exception e) {
e.printStackTrace();
}
}
});
holder.postFooter.addView(attached); holder.postFooter.addView(attached);
} }
@ -243,7 +257,8 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
holder.personalText.setVisibility(View.GONE); holder.personalText.setVisibility(View.GONE);
if (c_numberOfStars != 0) { if (c_numberOfStars != 0) {
holder.stars.setTypeface(FontManager.getTypeface(context, FONTAWESOME)); holder.stars.setTypeface(Typeface.createFromAsset(context.getAssets()
, "fonts/fontawesome-webfont.ttf"));
String aStar = context.getResources().getString(R.string.fa_icon_star); String aStar = context.getResources().getString(R.string.fa_icon_star);
String usersStars = ""; String usersStars = "";

24
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java

@ -1,7 +1,6 @@
package gr.thmmy.mthmmy.activities.topic; package gr.thmmy.mthmmy.activities.topic;
import android.graphics.Color; import android.graphics.Color;
import android.util.Log;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
@ -92,7 +91,7 @@ class TopicParser {
p_specialRank, p_gender, p_personalText, p_numberOfPosts; p_specialRank, p_gender, p_personalText, p_numberOfPosts;
int p_postNum, p_postIndex, p_numberOfStars, p_userColor; int p_postNum, p_postIndex, p_numberOfStars, p_userColor;
boolean p_isDeleted = false; boolean p_isDeleted = false;
ArrayList<String> p_attachedFiles; ArrayList<String[]> p_attachedFiles;
//Initialize variables //Initialize variables
p_rank = "Rank"; p_rank = "Rank";
@ -185,17 +184,24 @@ class TopicParser {
//Find attached file's urls, names and info, if present //Find attached file's urls, names and info, if present
Elements postAttachments = item.select("div:containsOwn(downloaded)"); Elements postAttachments = item.select("div:containsOwn(downloaded)");
if (postAttachments != null) { if (postAttachments != null) {
for (Element attached : postAttachments) { Elements attachedFiles = postAttachments.select("a");
String postAttachmentsText = postAttachments.text();
for(int i = 0; i<attachedFiles.size(); ++i){
String[] attachedArray = new String[3];
//Get file's url and filename //Get file's url and filename
Element tmpAttachedFileUrlAndName = attached.select("a").first(); Element tmpAttachedFileUrlAndName = attachedFiles.get(i);
tmpAttachedFileUrlAndName.select("img").remove().first(); attachedArray[0] = tmpAttachedFileUrlAndName.attr("href");
attachedArray[1] = tmpAttachedFileUrlAndName.text().substring(1);
//Get file's info (size and download count) //Get file's info (size and download count)
String tmpAttachedFileInfo = attached.text().trim(); String postAttachmentsTextSbstr = postAttachmentsText.substring(
tmpAttachedFileInfo = tmpAttachedFileInfo.substring( postAttachmentsText.indexOf(attachedArray[1]));
tmpAttachedFileInfo.indexOf("(")); attachedArray[2] = postAttachmentsTextSbstr.substring(attachedArray[1].length()
, postAttachmentsTextSbstr.indexOf("times.)"));
p_attachedFiles.add(tmpAttachedFileUrlAndName + " " + tmpAttachedFileInfo); p_attachedFiles.add(attachedArray);
} }
} }

8
app/src/main/java/gr/thmmy/mthmmy/data/Post.java

@ -21,13 +21,13 @@ public class Post {
private final String personalText; private final String personalText;
private final int numberOfStars; private final int numberOfStars;
private final int userColor; private final int userColor;
private final ArrayList<String> attachedFiles; private final ArrayList<String[]> attachedFiles;
public Post(String thumbnailUrl, String author, String subject, String content public Post(String thumbnailUrl, String author, String subject, String content
, int postIndex, int postNumber, String postDate, String rank , int postIndex, int postNumber, String postDate, String rank
, String special_rank, String gender, String numberOfPosts , String special_rank, String gender, String numberOfPosts
, String personalText, int numberOfStars, int userColor , String personalText, int numberOfStars, int userColor
, ArrayList<String> attachedFiles) { , ArrayList<String[]> attachedFiles) {
this.thumbnailUrl = thumbnailUrl; this.thumbnailUrl = thumbnailUrl;
this.author = author; this.author = author;
this.subject = subject; this.subject = subject;
@ -48,7 +48,7 @@ public class Post {
public Post(String thumbnailUrl, String author, String subject, String content public Post(String thumbnailUrl, String author, String subject, String content
, int postIndex, int postNumber, String postDate, int userColor , int postIndex, int postNumber, String postDate, int userColor
, ArrayList<String> attachedFiles) { , ArrayList<String[]> attachedFiles) {
this.thumbnailUrl = thumbnailUrl; this.thumbnailUrl = thumbnailUrl;
this.author = author; this.author = author;
this.subject = subject; this.subject = subject;
@ -128,7 +128,7 @@ public class Post {
return userColor; return userColor;
} }
public ArrayList<String> getAttachedFiles() { public ArrayList<String[]> getAttachedFiles() {
return attachedFiles; return attachedFiles;
} }
} }

14
app/src/main/java/gr/thmmy/mthmmy/utils/FontManager.java

@ -1,14 +0,0 @@
package gr.thmmy.mthmmy.utils;
import android.content.Context;
import android.graphics.Typeface;
public class FontManager {
public static final String ROOT = "fonts/",
FONTAWESOME = ROOT + "fontawesome-webfont.ttf";
public static Typeface getTypeface(Context context, String font) {
return Typeface.createFromAsset(context.getAssets(), font);
}
}

BIN
app/src/main/res/drawable/logo_animated.gif

Binary file not shown.

Before

Width:  |  Height:  |  Size: 514 KiB

After

Width:  |  Height:  |  Size: 561 KiB

7
app/src/main/res/layout/activity_login.xml

@ -3,8 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:fitsSystemWindows="true" android:background="@color/background"
android:background="@color/background"> android:fitsSystemWindows="true">
<ScrollView <ScrollView
android:id="@+id/inner_scroll_view" android:id="@+id/inner_scroll_view"
@ -25,11 +25,12 @@
android:layout_height="100dp"/> android:layout_height="100dp"/>
<pl.droidsonroids.gif.GifImageView <pl.droidsonroids.gif.GifImageView
android:id="@+id/logo"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:contentDescription="@string/thmmy_img_description"
android:src="@drawable/logo_animated" android:src="@drawable/logo_animated"
android:id="@+id/logo"
/> />
<Space <Space

10
app/src/main/res/layout/activity_topic.xml

@ -39,7 +39,7 @@
tools:context="gr.thmmy.mthmmy.activities.topic.TopicActivity"> tools:context="gr.thmmy.mthmmy.activities.topic.TopicActivity">
</android.support.v7.widget.RecyclerView> </android.support.v7.widget.RecyclerView>
<LinearLayout <android.support.v7.widget.FitWindowsLinearLayout
android:id="@+id/bottom_navigation_bar" android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
@ -91,16 +91,18 @@
android:layout_weight="0.8" android:layout_weight="0.8"
android:contentDescription="@string/text_last" android:contentDescription="@string/text_last"
app:srcCompat="@drawable/page_last"/> app:srcCompat="@drawable/page_last"/>
</LinearLayout> </android.support.v7.widget.FitWindowsLinearLayout>
<ProgressBar <ProgressBar
android:id="@+id/progressBar" android:id="@+id/progressBar"
android:layout_width="wrap_content" style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_gravity="center" android:layout_gravity="center"
android:background="@drawable/progress_bar_bg" android:indeterminate="true"
android:theme="@style/AppTheme"
android:visibility="invisible"/> android:visibility="invisible"/>
<android.support.design.widget.FloatingActionButton <android.support.design.widget.FloatingActionButton

Loading…
Cancel
Save