From 957573ea836b7668024276f9636109b7b3b6c3cb Mon Sep 17 00:00:00 2001 From: Gregor Santner Date: Mon, 21 May 2018 14:57:41 +0200 Subject: [PATCH] Update opoc --- README.md | 6 +- .../data/DiasporaPodList.java | 2 +- .../data/DiasporaUserProfile.java | 2 +- .../DiasporaUserProfileChangedListener.java | 2 +- .../diaspora_android/util/AppSettings.java | 2 +- .../opoc/preference/PropertyBackend.java | 1 - .../SharedPreferencesPropertyBackend.java | 16 +++ .../net/gsantner/opoc/util/ActivityUtils.java | 11 ++ .../net/gsantner/opoc/util/FileUtils.java | 103 ++++++++++++++++++ .../net/gsantner/opoc/util/ShareUtil.java | 22 +++- build.gradle | 2 +- metadata/en-US/full_description.txt | 4 +- 12 files changed, 161 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 15296ffe..57ba4cc3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.org/gsantner/dandelion.svg?branch=master)](https://travis-ci.org/gsantner/dandelion) [![Translate - with Stringlate](https://img.shields.io/badge/stringlate-translate-green.svg)](https://lonamiwebs.github.io/stringlate/translate?git=https%3A%2F%2Fgithub.com%2Fgsantner%2Fdandelion.git&mail=gro.xobliam@@rentnasg) [![Chat - Matrix](https://img.shields.io/badge/chat-on%20matrix-blue.svg)](https://matrix.to/#/#dandelion:matrix.org) [![Chat - FreeNode IRC](https://img.shields.io/badge/chat-on%20irc-blue.svg)](https://kiwiirc.com/client/irc.freenode.net/?nick=dandelion-anon|?##dandelion) -[![Donate Bitcoin](https://img.shields.io/badge/support-project-orange.svg)](https://gsantner.net/supportme/?project=dandelion&source=readme) +[![Donate](https://img.shields.io/badge/donate-appreciation-orange.svg)](https://gsantner.net/supportme/?project=dandelion&source=readme) [![Donate LiberaPay](https://img.shields.io/badge/donate-liberapay-orange.svg)](https://liberapay.com/gsantner/donate) # dandelion\* @@ -39,7 +39,7 @@ dandelion\* requires access to the Internet and to external storage to be able t ## Contributions The project is always open for contributions and accepts pull requests. -The project uses [AOSP Java Code Style](https://source.android.com/source/code-style#follow-field-naming-conventions), with one exception: private members are `_camelCase` instead of `mBigCamel`. You may use Android Studios _auto reformat feature_ before sending a PR. See [gsantner's android contribution guide](https://gsantner.net/android-contribution-guide/?packageid=com.github.dfa.diaspora_android&name=dandelion&web=https://github.com/gsantner/dandelion#logcat) for more information. +The project uses [AOSP Java Code Style](https://source.android.com/source/code-style#follow-field-naming-conventions), with one exception: private members are `_camelCase` instead of `mBigCamel`. You may use Android Studios _auto reformat feature_ before sending a PR. See [gsantner's android contribution guide](https://gsantner.net/android-contribution-guide/?packageid=com.github.dfa.diaspora_android&name=dandelion&web=https://github.com/gsantner/dandelion&source=readme#logcat) for more information. Translations can be contributed on GitHub or via [E-Mail](https://gsantner.net/#contact). You can use Stringlate ([![Translate - with Stringlate](https://img.shields.io/badge/stringlate-translate-green.svg)](https://lonamiwebs.github.io/stringlate/translate?git=https%3A%2F%2Fgithub.com%2Fgsantner%2Fdandelion.git)) to translate the project directly on your Android phone. It allows you to export as E-Mail attachement and to post on GitHub. @@ -79,6 +79,6 @@ For more licensing informations, see [`3rd party licenses`](/app/src/main/res/ra ### Notice #### Maintainers - gsantner ([GitHub](https://github.com/gsantner), [Web](https://gsantner.net/supportme/?project=dandelion&source=readme), [diaspora*](https://pod.geraspora.de/people/d1cbdd70095301341e834860008dbc6c)) - - Bitcoin: [1B9ZyYdQoY9BxMe9dRUEKaZbJWsbQqfXU5](https://gsantner.net/donate/#donate) + - Bitcoin: [1B9ZyYdQoY9BxMe9dRUEKaZbJWsbQqfXU5](https://gsantner.net/supportme/?project=dandelion&source=readme) - vanitasvitae ([GitHub](https://github.com/vanitasvitae), [diaspora*](https://pod.geraspora.de/people/bbd7af90fbec013213e34860008dbc6c)) - Bitcoin: 1Ao3W6NaQv3xKppviB7RSFKjHo6PGd8RTy diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java index 3c56cef3..e0fa9955 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java @@ -13,7 +13,7 @@ import java.util.List; /** - * Created by gsantner (http://gsantner.net/ on 30.09.16. + * Created by gsantner (https://gsantner.net/ on 30.09.16. * DiasporaPodList - List container for DiasporaPod's, with methods to merge with other DiasporaPodLists * DiasporaPod - Data container for a Pod, can include N DiasporaPodUrl's * DiasporaPodUrl - A Url of an DiasporaPod diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaUserProfile.java b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaUserProfile.java index 863b7c6f..e54b351a 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaUserProfile.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaUserProfile.java @@ -32,7 +32,7 @@ import org.json.JSONObject; /** * User profile - * Created by gsantner (http://gsantner.net/) on 24.03.16. Part of dandelion*. + * Created by gsantner (https://gsantner.net/) on 24.03.16. Part of dandelion*. */ public class DiasporaUserProfile { private static final int MINIMUM_USERPROFILE_LOAD_TIMEDIFF = 5000; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/listener/DiasporaUserProfileChangedListener.java b/app/src/main/java/com/github/dfa/diaspora_android/listener/DiasporaUserProfileChangedListener.java index aff25a2a..644612c4 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/listener/DiasporaUserProfileChangedListener.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/listener/DiasporaUserProfileChangedListener.java @@ -21,7 +21,7 @@ package com.github.dfa.diaspora_android.listener; import com.github.dfa.diaspora_android.data.DiasporaUserProfile; /** - * Created by gsantner (http://gsantner.net/) on 26.03.16. + * Created by gsantner (https://gsantner.net/) on 26.03.16. * Interface that needs to be implemented by classes that listen for Profile related changes */ public interface DiasporaUserProfileChangedListener { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java b/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java index 79953d7a..9cbf7049 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java @@ -37,7 +37,7 @@ import java.util.List; /** * Settings - * Created by gsantner (http://gsantner.net/) on 20.03.16. Part of dandelion*. + * Created by gsantner (https://gsantner.net/) on 20.03.16. Part of dandelion*. */ @SuppressWarnings("ConstantConditions") public class AppSettings extends SharedPreferencesPropertyBackend { diff --git a/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java b/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java index c27d9bce..f134122b 100644 --- a/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java +++ b/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java @@ -45,5 +45,4 @@ public interface PropertyBackend { TTHIS setIntList(TKEY key, List value); TTHIS setStringList(TKEY key, List value); - } diff --git a/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java b/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java index 275d88a5..bfcce37b 100644 --- a/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java +++ b/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java @@ -136,6 +136,22 @@ public class SharedPreferencesPropertyBackend implements PropertyBackend 0 ? pref[0] : _prefApp); } + + public static void limitListTo(final List list, int maxSize, boolean removeDuplicates) { + Object o; + int pos; + + for (int i = 0; removeDuplicates && i < list.size(); i++) { + o = list.get(i); + while ((pos = list.lastIndexOf(o)) != i && pos >= 0) { + list.remove(pos); + } + } + while ((pos = list.size()) > maxSize && pos > 0) { + list.remove(list.size() - 1); + } + } + // // Getter for resources // diff --git a/app/src/main/java/net/gsantner/opoc/util/ActivityUtils.java b/app/src/main/java/net/gsantner/opoc/util/ActivityUtils.java index 53a86cc1..7c72b724 100644 --- a/app/src/main/java/net/gsantner/opoc/util/ActivityUtils.java +++ b/app/src/main/java/net/gsantner/opoc/util/ActivityUtils.java @@ -18,6 +18,7 @@ import android.net.Uri; import android.os.Build; import android.support.annotation.StringRes; import android.support.design.widget.Snackbar; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.AppCompatTextView; import android.text.Html; @@ -163,4 +164,14 @@ public class ActivityUtils extends net.gsantner.opoc.util.ContextUtils { Uri.parse("http://play.google.com/store/apps/" + pkgId))); } } + + public void setStatusbarColor(int color, boolean... fromRes) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + if (fromRes != null && fromRes.length > 0 && fromRes[0]) { + color = ContextCompat.getColor(_context, color); + } + + _activity.getWindow().setStatusBarColor(color); + } + } } diff --git a/app/src/main/java/net/gsantner/opoc/util/FileUtils.java b/app/src/main/java/net/gsantner/opoc/util/FileUtils.java index 13cb66d5..15a417a1 100644 --- a/app/src/main/java/net/gsantner/opoc/util/FileUtils.java +++ b/app/src/main/java/net/gsantner/opoc/util/FileUtils.java @@ -20,15 +20,19 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.net.URLConnection; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Pattern; @SuppressWarnings({"WeakerAccess", "unused", "SameParameterValue", "SpellCheckingInspection", "deprecation"}) @@ -36,6 +40,15 @@ public class FileUtils { // Used on methods like copyFile(src, dst) private static final int BUFFER_SIZE = 4096; + public static String readTextFileFast(final File file) { + try { + return new String(readCloseBinaryStream(new FileInputStream(file))); + } catch (FileNotFoundException e) { + System.err.println("readTextFileFast: File " + file + " not found."); + } + return ""; + } + public static String readTextFile(final File file) { try { return readCloseTextStream(new FileInputStream(file)); @@ -337,4 +350,94 @@ public class FileUtils { return null; } } + + /** + * Try to detect MimeType by backwards compatible methods + */ + public static String getMimeType(File file) { + String guess = null; + if (file != null && file.exists() && file.isFile()) { + InputStream is = null; + try { + is = new BufferedInputStream(new FileInputStream(file)); + guess = URLConnection.guessContentTypeFromStream(is); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ignored) { + } + } + } + + if (guess == null || guess.isEmpty()) { + guess = "*/*"; + int dot = file.getName().lastIndexOf(".") + 1; + if (dot > 0 && dot < file.getName().length()) { + switch (file.getName().substring(dot)) { + case "md": + case "markdown": + case "mkd": + case "mdown": + case "mkdn": + case "mdwn": + case "rmd": + guess = "text/markdown"; + break; + case "txt": + guess = "text/plain"; + break; + } + } + } + } + return guess; + } + + public static boolean isTextFile(File file) { + return getMimeType(file).startsWith("text/"); + } + + /** + * Analyze given textfile and retrieve multiple information from it + * Information is written back to the {@link AtomicInteger} parameters + */ + public static void retrieveTextFileSummary(File file, AtomicInteger numCharacters, AtomicInteger numLines) { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String line; + while ((line = br.readLine()) != null) { + numLines.getAndIncrement(); + numCharacters.getAndSet(numCharacters.get() + line.length()); + } + } catch (Exception e) { + e.printStackTrace(); + numCharacters.set(-1); + numLines.set(-1); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ignored) { + } + } + } + } + + /** + * Format filesize to human readable format + * Get size in bytes e.g. from {@link File} using {@code File#length()} + */ + public static String getReadableFileSize(long size, boolean abbreviation) { + if (size <= 0) { + return "0B"; + } + String[] units = abbreviation ? new String[]{"B", "kB", "MB", "GB", "TB"} : new String[]{"Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes"}; + int unit = (int) (Math.log10(size) / Math.log10(1024)); + return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, unit)) + + " " + units[unit]; + } } diff --git a/app/src/main/java/net/gsantner/opoc/util/ShareUtil.java b/app/src/main/java/net/gsantner/opoc/util/ShareUtil.java index 46ae484d..d85abc6c 100644 --- a/app/src/main/java/net/gsantner/opoc/util/ShareUtil.java +++ b/app/src/main/java/net/gsantner/opoc/util/ShareUtil.java @@ -182,6 +182,22 @@ public class ShareUtil { showChooser(intent, null); } + /** + * Open a View intent for given file + * + * @param file The file to share + */ + public void viewFileInOtherApp(File file, @Nullable String type) { + Uri fileUri = FileProvider.getUriForFile(_context, getFileProviderAuthority(), file); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.putExtra(Intent.EXTRA_STREAM, fileUri); + intent.setData(fileUri); + intent.putExtra(EXTRA_FILEPATH, file.getAbsolutePath()); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + intent.setDataAndType(fileUri, type); + showChooser(intent, null); + } + /** * Share the given bitmap with given format * @@ -397,7 +413,7 @@ public class ShareUtil { String tmps; String fileStr; - if ((Intent.ACTION_VIEW.equals(action) || Intent.ACTION_EDIT.equals(action))) { + if ((Intent.ACTION_VIEW.equals(action) || Intent.ACTION_EDIT.equals(action)) || Intent.ACTION_SEND.equals(action)) { // Markor, S.M.T FileManager if (receivingIntent.hasExtra((tmps = EXTRA_FILEPATH))) { return new File(receivingIntent.getStringExtra(tmps)); @@ -450,6 +466,10 @@ public class ShareUtil { } } } + fileUri = receivingIntent.getParcelableExtra(Intent.EXTRA_STREAM); + if (fileUri != null && !TextUtils.isEmpty(tmps = fileUri.getPath()) && tmps.startsWith("/") && (tmpf = new File(tmps)).exists()) { + return tmpf; + } } return null; } diff --git a/build.gradle b/build.gradle index 399c39ba..5c5bfd78 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { // https://github.com/guardianproject/NetCipher/releases ext.version_library_netcipher = "2.0.0-alpha1" // https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin#LookAtCentral - ext.version_plugin_kotlin = "1.2.41" + ext.version_plugin_kotlin = "1.2.21" ext.enable_plugin_kotlin = false repositories { diff --git a/metadata/en-US/full_description.txt b/metadata/en-US/full_description.txt index e1c1f084..0486a692 100644 --- a/metadata/en-US/full_description.txt +++ b/metadata/en-US/full_description.txt @@ -14,5 +14,5 @@ It adds useful features to your networking experience: ✋ Translate using StringlateJoin discussion on MatrixMore information about contributions - ✋ Android Contribution Guide (gsantner blog) - ✋ Support main developer + ✋ Android Contribution Guide (gsantner blog) + ✋ Support main developer