From 846aefc46b287c4a49e676cb91b08698ecc78dc5 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Fri, 20 Jan 2017 16:50:35 +0200 Subject: [PATCH] Fixes for file downloading, profile crashes, mixed views in topics, pageNav clicking. --- app/src/main/AndroidManifest.xml | 17 +++ .../profile/summary/SummaryFragment.java | 17 ++- .../activities/topic/TopicActivity.java | 131 ++++++++++++------ .../mthmmy/activities/topic/TopicAdapter.java | 21 ++- .../mthmmy/utils/FileManager/ThmmyFile.java | 29 ++-- app/src/main/res/xml/provider_paths.xml | 11 ++ 6 files changed, 156 insertions(+), 70 deletions(-) create mode 100644 app/src/main/res/xml/provider_paths.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 24b701c1..7b1efb2d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,6 +15,7 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java index 97d7f1e8..1236b5a0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java @@ -82,7 +82,7 @@ public class SummaryFragment extends Fragment { Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_summary, container, false); mainContent = (LinearLayout) rootView.findViewById(R.id.profile_activity_content); - if (!parsedProfileSummaryData.isEmpty()) + if (!parsedProfileSummaryData.isEmpty() && isAdded()) populateLayout(); return rootView; } @@ -126,7 +126,7 @@ public class SummaryFragment extends Fragment { } protected void onPostExecute(Void result) { - populateLayout(); + if (isAdded()) populateLayout(); } /** @@ -187,13 +187,12 @@ public class SummaryFragment extends Fragment { } TextView entry = new TextView(this.getContext()); - if (isAdded()) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) - entry.setTextColor(getResources().getColor(R.color.primary_text, null)); - else - //noinspection deprecation - entry.setTextColor(getResources().getColor(R.color.primary_text)); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + entry.setTextColor(getResources().getColor(R.color.primary_text, null)); + else + //noinspection deprecation + entry.setTextColor(getResources().getColor(R.color.primary_text)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { entry.setText(Html.fromHtml(profileSummaryRow, Html.FROM_HTML_MODE_LEGACY)); } else { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 1c13c834..7ccf2abd 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -1,16 +1,14 @@ package gr.thmmy.mthmmy.activities.topic; import android.Manifest; -import android.content.DialogInterface; -import android.content.Intent; import android.content.pm.PackageManager; +import android.graphics.Rect; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; -import android.support.v7.app.AlertDialog; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; @@ -30,7 +28,6 @@ import java.util.ArrayList; import java.util.Objects; import gr.thmmy.mthmmy.R; -import gr.thmmy.mthmmy.activities.LoginActivity; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.model.LinkTarget; import gr.thmmy.mthmmy.model.Post; @@ -237,6 +234,33 @@ public class TopicActivity extends BaseActivity { } //--------------------------------------BOTTOM NAV BAR METHODS---------------------------------- + + /** + * This class is used to implement the repetitive incrementPageRequestValue/decrementPageRequestValue + * of page value when long pressing one of the page navigation buttons. + */ + class RepetitiveUpdater implements Runnable { + private final int step; + + /** + * @param step number of pages to add/subtract on each repetition + */ + RepetitiveUpdater(int step) { + this.step = step; + } + + public void run() { + long REPEAT_DELAY = 250; + if (autoIncrement) { + incrementPageRequestValue(step); + repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); + } else if (autoDecrement) { + decrementPageRequestValue(step); + repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); + } + } + } + private void paginationEnabled(boolean enabled) { firstPage.setEnabled(enabled); previousPage.setEnabled(enabled); @@ -244,18 +268,38 @@ public class TopicActivity extends BaseActivity { lastPage.setEnabled(enabled); } + private void paginationEnabledExcept(boolean enabled, View exception) { + if (exception == firstPage) { + previousPage.setEnabled(enabled); + nextPage.setEnabled(enabled); + lastPage.setEnabled(enabled); + } else if (exception == previousPage) { + firstPage.setEnabled(enabled); + nextPage.setEnabled(enabled); + lastPage.setEnabled(enabled); + } else if (exception == nextPage) { + firstPage.setEnabled(enabled); + previousPage.setEnabled(enabled); + lastPage.setEnabled(enabled); + } else if (exception == lastPage) { + firstPage.setEnabled(enabled); + previousPage.setEnabled(enabled); + nextPage.setEnabled(enabled); + } else { + paginationEnabled(enabled); + } + } + private void initIncrementButton(ImageButton increment, final int step) { // Increment once for a click increment.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { - if (!autoIncrement && step == LARGE_STEP) { //If just clicked go to last page + if (!autoIncrement && step == LARGE_STEP) { changePage(numberOfPages - 1); - return; + } else if (!autoIncrement) { + incrementPageRequestValue(step); + changePage(pageRequestValue - 1); } - //Clicked and holden - autoIncrement = false; //Stop incrementing - incrementPageRequestValue(step); - changePage(pageRequestValue - 1); } }); @@ -263,6 +307,7 @@ public class TopicActivity extends BaseActivity { increment.setOnLongClickListener( new View.OnLongClickListener() { public boolean onLongClick(View arg0) { + paginationEnabledExcept(false, arg0); autoIncrement = true; repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), INITIAL_DELAY); return false; @@ -272,9 +317,21 @@ public class TopicActivity extends BaseActivity { // When the button is released increment.setOnTouchListener(new View.OnTouchListener() { + private Rect rect; + public boolean onTouch(View v, MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); + } else if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) { + autoIncrement = false; + paginationEnabled(true); changePage(pageRequestValue - 1); + } else if (event.getAction() == MotionEvent.ACTION_MOVE) { + if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { + autoIncrement = false; + decrementPageRequestValue(pageRequestValue - thisPage); + paginationEnabled(true); + } } return false; } @@ -285,22 +342,20 @@ public class TopicActivity extends BaseActivity { // Decrement once for a click decrement.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { - if (!autoDecrement && step == LARGE_STEP) { //If just clicked go to first page + if (!autoDecrement && step == LARGE_STEP) { changePage(0); - return; + } else if (!autoDecrement) { + decrementPageRequestValue(step); + changePage(pageRequestValue - 1); } - //Clicked and hold - autoDecrement = false; //Stop decrementing - decrementPageRequestValue(step); - changePage(pageRequestValue - 1); } }); - // Auto decrement for a long click decrement.setOnLongClickListener( new View.OnLongClickListener() { public boolean onLongClick(View arg0) { + paginationEnabledExcept(false, arg0); autoDecrement = true; repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), INITIAL_DELAY); return false; @@ -310,9 +365,21 @@ public class TopicActivity extends BaseActivity { // When the button is released decrement.setOnTouchListener(new View.OnTouchListener() { + private Rect rect; + public boolean onTouch(View v, MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); + } else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) { + autoDecrement = false; + paginationEnabled(true); changePage(pageRequestValue - 1); + } else if (event.getAction() == MotionEvent.ACTION_MOVE) { + if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { + autoIncrement = false; + incrementPageRequestValue(thisPage - pageRequestValue); + paginationEnabled(true); + } } return false; } @@ -496,30 +563,4 @@ public class TopicActivity extends BaseActivity { //postsList = TopicParser.parseTopic(topic, language); } } - - /** - * This class is used to implement the repetitive incrementPageRequestValue/decrementPageRequestValue - * of page value when long pressing one of the page navigation buttons. - */ - class RepetitiveUpdater implements Runnable { - private final int step; - - /** - * @param step number of pages to add/subtract on each repetition - */ - RepetitiveUpdater(int step) { - this.step = step; - } - - public void run() { - long REPEAT_DELAY = 250; - if (autoIncrement) { - incrementPageRequestValue(step); - repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); - } else if (autoDecrement) { - decrementPageRequestValue(step); - repeatUpdateHandler.postDelayed(new RepetitiveUpdater(step), REPEAT_DELAY); - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 711f7e55..8b4fa82d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -11,6 +11,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.v4.content.FileProvider; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; @@ -229,6 +230,7 @@ class TopicAdapter extends RecyclerView.Adapter { } else //noinspection deprecation filesTextColor = context.getResources().getColor(R.color.accent); + holder.postFooter.removeAllViews(); for (final ThmmyFile attachedFile : currentPost.getAttachedFiles()) { final TextView attached = new TextView(context); attached.setTextSize(10f); @@ -243,12 +245,12 @@ class TopicAdapter extends RecyclerView.Adapter { attached.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - if (((TopicActivity) context).requestPerms()) { - downloadTask = new DownloadTask(); - downloadTask.execute(attachedFile); - } else - Toast.makeText(context, "Persmissions missing!", Toast.LENGTH_SHORT) - .show(); + //if (((BaseApplication) context).requestPerms()) { + downloadTask = new DownloadTask(); + downloadTask.execute(attachedFile); + //} else + // Toast.makeText(context, "Persmissions missing!", Toast.LENGTH_SHORT) + // .show(); } }); @@ -658,7 +660,12 @@ class TopicAdapter extends RecyclerView.Adapter { Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); - intent.setDataAndType(Uri.fromFile(tempFile), mime); + //intent.setDataAndType(Uri.fromFile(tempFile), mime); + + intent.setDataAndType(FileProvider.getUriForFile(context, context. + getApplicationContext() + .getPackageName() + ".provider", tempFile), mime); + intent.setFlags(FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/FileManager/ThmmyFile.java b/app/src/main/java/gr/thmmy/mthmmy/utils/FileManager/ThmmyFile.java index eed84689..59d2816f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/FileManager/ThmmyFile.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/FileManager/ThmmyFile.java @@ -10,7 +10,6 @@ import android.os.Environment; import android.os.StatFs; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.util.Log; import android.webkit.MimeTypeMap; import android.widget.Toast; @@ -25,6 +24,7 @@ import mthmmy.utils.Report; import okhttp3.Request; import okhttp3.Response; +import static android.content.Context.MODE_PRIVATE; import static gr.thmmy.mthmmy.base.BaseActivity.getClient; /** @@ -41,6 +41,7 @@ public class ThmmyFile { private final String filename, fileInfo; private String extension, filePath; private File file; + private boolean isInternal; /** * This constructor only creates a empty ThmmyFile object and does not download the file. To download @@ -53,6 +54,7 @@ public class ThmmyFile { this.extension = null; this.filePath = null; this.file = null; + this.isInternal = false; } /** @@ -70,6 +72,7 @@ public class ThmmyFile { this.extension = null; this.filePath = null; this.file = null; + this.isInternal = false; } public URL getFileUrl() { @@ -122,6 +125,10 @@ public class ThmmyFile { this.filePath = filePath; } + public boolean isInternal() { + return isInternal; + } + /** * Used to download the file. If download is successful file's extension and path will be assigned * to object's fields and can be accessed using getter methods. @@ -143,12 +150,13 @@ public class ThmmyFile { else if (filename == null || Objects.equals(filename, "")) throw new IllegalStateException("Internal error!\nNo filename was provided."); - try { + return downloadWithoutManager(context, fileUrl); + /*try { downloadWithManager(context, fileUrl); } catch (IllegalStateException e) { return downloadWithoutManager(context, fileUrl); - } - return null; + }*/ + //return null; } private void downloadWithManager(Context context, @NonNull URL pFileUrl) throws IllegalStateException, IOException { @@ -191,7 +199,11 @@ public class ThmmyFile { if (file == null) { Report.d(TAG, "Error creating media file, check storage permissions!"); } else { - FileOutputStream fos = new FileOutputStream(file); + FileOutputStream fos; + if (isInternal) + fos = context.openFileOutput(filename, MODE_PRIVATE); + else + fos = new FileOutputStream(file); fos.write(response.body().bytes()); fos.close(); @@ -209,11 +221,11 @@ public class ThmmyFile { String extState = Environment.getExternalStorageState(); if (Environment.isExternalStorageRemovable() && Objects.equals(extState, Environment.MEDIA_MOUNTED)) { - mediaStorageDir = new File(Environment.getExternalStorageDirectory() - + "/Android/data/gr.thmmy.mthmmy/" - + "Downloads/"); + mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment + .DIRECTORY_DOWNLOADS), fileName); } else { mediaStorageDir = new File(context.getFilesDir(), "Downloads"); + isInternal = true; } //Creates the storage directory if it does not exist @@ -224,7 +236,6 @@ public class ThmmyFile { } } - if (fileInfo != null) { if (fileInfo.contains("KB")) { float fileSize = Float.parseFloat(fileInfo diff --git a/app/src/main/res/xml/provider_paths.xml b/app/src/main/res/xml/provider_paths.xml new file mode 100644 index 00000000..d056e2a0 --- /dev/null +++ b/app/src/main/res/xml/provider_paths.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file