diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
new file mode 100644
index 000000000..ea2fb6af3
--- /dev/null
+++ b/.github/FUNDING.yml
@@ -0,0 +1,12 @@
+# These are supported funding model platforms
+
+github: LucasGGamerM
+custom: ["https://liberapay.com/LucasGGamerM/donate", liberapay.com]
+patreon: # mastodon
+open_collective: # Replace with a single Open Collective username e.g., user1
+tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
+community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
+liberapay: # Replace with a single Liberapay username e.g., user1
+issuehunt: # Replace with a single IssueHunt username e.g., user1
+otechie: # Replace with a single Otechie username e.g., user1
+custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md
new file mode 100644
index 000000000..9f1bb06e9
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug_report.md
@@ -0,0 +1,40 @@
+---
+name: Bug report
+about: Create a report to help us improve
+title: ''
+labels: bug
+assignees: ''
+
+---
+
+**Describe the bug**
+
+A clear and concise description of what the bug is.
+
+**To reproduce**
+
+Steps to reproduce the behavior:
+1. Go to '...'
+2. Click on '....'
+3. Scroll down to '....'
+4. See error
+
+**Does this happen in the official app?**
+
+Does this issue also occur with the respective upstream release?
+> No / Yes
+
+> In case it does, please consider filing an [upstream bug report](https://github.com/mastodon/mastodon-android/issues) instead.
+> If this bug is seriously impacting your usage or you think I might want to try to fix it for Megalodon, feel free to still create this issue!
+
+**Screenshots and screen recordings**
+
+If applicable, add screenshots (and screen recordings, if possible) to help explain your problem.
+
+**Version**
+
+Moshidon version: [e.g. v1.1.4+fork.#]
+
+**Crash log**
+
+If you know your way around Android development tools, please consider attaching a crash log, if possible.
diff --git a/.github/ISSUE_TEMPLATE/feature-ui-request.md b/.github/ISSUE_TEMPLATE/feature-ui-request.md
new file mode 100644
index 000000000..7a56c359a
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/feature-ui-request.md
@@ -0,0 +1,20 @@
+---
+name: Feature/UI request
+about: Suggest an idea for this project
+title: ''
+labels: feature
+assignees: ''
+
+---
+
+**Is your feature request related to a problem? Please describe.**
+A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
+
+**Describe the solution you'd like**
+A clear and concise description of what you want to happen.
+
+**Describe alternatives you've considered**
+If applicable: a clear and concise description of any alternative solutions or features you've considered.
+
+**Additional context**
+Add any other context or screenshots about the feature request here.
diff --git a/.github/ISSUE_TEMPLATE/something-else.md b/.github/ISSUE_TEMPLATE/something-else.md
new file mode 100644
index 000000000..995600f1e
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/something-else.md
@@ -0,0 +1,10 @@
+---
+name: It's something else…
+about: Issues that can't be categorized as feature requests or bug reports
+title: ''
+labels: ''
+assignees: ''
+
+---
+
+
diff --git a/.github/workflows/nightly-builds.yml b/.github/workflows/nightly-builds.yml
new file mode 100644
index 000000000..45e64bc97
--- /dev/null
+++ b/.github/workflows/nightly-builds.yml
@@ -0,0 +1,71 @@
+name: Nightly builds
+
+on:
+ push:
+ branches: [ "master" ]
+ workflow_dispatch:
+
+jobs:
+ build:
+
+ runs-on: ubuntu-latest
+
+ steps:
+# - name: Checkout Appkit Repo
+# uses: actions/checkout@v3
+# with:
+# repository: grishka/appkit
+#
+# - name: set up JDK 17
+# uses: actions/setup-java@v3
+# with:
+# java-version: '17'
+# distribution: 'corretto'
+# cache: gradle
+#
+# - name: Comment out signing config in appkits gradle file
+# run: |
+# sed -i 's/sign publishing\.publications\.release/\/\/ sign publishing.publications.release/' appkit/maven-push.gradle
+#
+# - name: Grant execute permission for gradlew for Appkit
+# run: chmod +x gradlew
+#
+# - name: Compile appkit
+# run: ./gradlew publishToMavenLocal
+
+ - uses: actions/checkout@v3
+ - name: set up JDK 17
+ uses: actions/setup-java@v3
+ with:
+ java-version: '17'
+ distribution: 'corretto'
+ cache: gradle
+
+ - name: Get current date
+ id: date
+ run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
+
+ - name: Grant execute permission for gradlew
+ run: chmod +x gradlew
+
+ - name: Decode Keystore
+ id: decode_keystore
+ uses: timheuer/base64-to-file@v1
+ with:
+ fileName: 'nightly_keystore.jks'
+ fileDir: './mastodon/keystore/'
+ encodedString: ${{ secrets.KEYSTORE }}
+
+ - name: Build with Gradle
+ run: ./gradlew assembleNightly
+ env:
+ SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
+ SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
+ SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
+ CURRENT_DATE: ${{ steps.date.outputs.date }}
+
+ - name: Upload a Build Artifact
+ uses: actions/upload-artifact@v3.1.2
+ with:
+ name: moshidon-nightly.apk
+ path: ./mastodon/build/outputs/apk/nightly/moshidon-nightly.apk
diff --git a/.github/workflows/validate-gradle-wrapper.yml b/.github/workflows/validate-gradle-wrapper.yml
new file mode 100644
index 000000000..b5f0b31ac
--- /dev/null
+++ b/.github/workflows/validate-gradle-wrapper.yml
@@ -0,0 +1,11 @@
+name: Validate Gradle Wrapper
+
+on: [pull_request, push]
+
+jobs:
+ validation:
+ name: Validation
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v3
+ - uses: gradle/wrapper-validation-action@v1
diff --git a/.gitignore b/.gitignore
index 593af090d..97a0829e2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,5 @@
.cxx
local.properties
*.jks
+*.keystore
+/mastodon/keystore/nightly_keystore.keystore
diff --git a/FAQ.md b/FAQ.md
new file mode 100644
index 000000000..f18a7ca59
--- /dev/null
+++ b/FAQ.md
@@ -0,0 +1,9 @@
+## F.A.Q
+
+Q: What are the main differences between Moshidon and Megalodon?
+
+A: There are many, but the most outstanding differences are: the ability to have other server's local timeline inside the app. It can be acessed in the "Add community" option in the top right corner of the Edit timelines screen. Other outstanding features that Moshidon has are some quality of life improvements, such as notification actions and allowing for unlisted replies by default. Most other features are pretty minor, such as profile notes directly available in the person's profile. Other features are quite minor usability and visibility improvements. All of which can be found in the settings page.
+
+Q: Will there ever be a version of Moshidon for iOS?
+
+A: No. As android and iOS apps do not share code, it is incredibly hard to port.
diff --git a/README.md b/README.md
index 2f062e8fd..dbde9f559 100644
--- a/README.md
+++ b/README.md
@@ -1,24 +1,183 @@
-Mastodon for Android
-======================
+
-[](https://crowdin.com/project/mastodon-for-android)
+# Moshidon, the material you mastodon client!
-This is the repository for the official Android app for Mastodon.
+> A fork of [megalodon](https://github.com/sk22/megalodon) which is a fork of [official Mastodon Android app](https://github.com/mastodon/mastodon-android) adding important features that are missing in the official app and possibly won’t ever be implemented, such as the federated timeline, unlisted posting, bookmarks and an image description viewer.
-[](https://f-droid.org/packages/org.joinmastodon.android/)
-[](https://play.google.com/store/apps/details?id=org.joinmastodon.android)
-Or get the APK from the [The Releases Section](https://github.com/mastodon/mastodon-android/releases/latest).
+[](https://github.com/LucasGGamerM/moshidon/releases/latest/download/moshidon.apk)
-## Contributing
+[](https://github.com/LucasGGamerM/moshidon-nightly/releases/latest/download/moshidon-nightly.apk)
-Our goal is delivering a polished, professionally designed and user-friendly app. We proceed according to wireframes provided by a professional UX designer that works with Mastodon gGmbH. This means that any outside contributions that change the app visually must first be coordinated with the UX designer. *This can take time.* Furthermore, we work off of an internal roadmap and aim for feature-parity and consistency with our iOS app. The iOS app is designated as the "primary" between the two, therefore, if you want to request features, please do so in the [Mastodon for iOS](https://github.com/mastodon/mastodon-ios) repository, as you are requesting a feature to be both in iOS and Android (exceptions being system integrations specific to Android). On the other hand, any contributions that improve existing functionality, performance, or accessibility should not have any roadblocks to being merged.
-If you would like to help translate the app into your language, please go to [Crowdin](https://crowdin.com/project/mastodon-for-android). If your language is not listed in the Crowdin project, please create an issue and we will add it. Please do not create pull requests that modify `strings.xml` files for languages other than English.
+[](https://translate.codeberg.org/engage/moshidon/)
+
+[](https://github.com/LucasGGamerM/moshidon/actions/workflows/nightly-builds.yml)
+
+
+
+
+
+
+
+## Help out the project by donating at: https://github.com/sponsors/LucasGGamerM!
+### We also support LiberaPay at: https://liberapay.com/LucasGGamerM/donate (Currently broken)
+
+### You can also donate some Monero through this wallet address as well:
+4886mdarcyB6Yf8Qc6vDJBK1fz6ibHFLZUmHb4GZZz9yLGNhcG3XC64e5UZ8dVQYTLZb82W6P9WhteowW4STJEec97Gf22j
+
+---
+
+## Key features
+
+### **The ability to add other server's local timeline to your timelines**
+
+It can be accessed in the "Edit timelines" menu, where you can add a new "Community" to see other server's local posts!
+
+### **View remote profiles**
+
+You can now see all of a profile follows and followers, by directly loading them from the profile's home instance. In case of a failed lookup, the app will automatically fall back to the older method.
+
+### **Translate posts easily**
+
+Allows you to easily translate posts in another language with a translate button! Your instance must support translation, otherwise it will not work.
+
+### **Show posts filtered with a warning**
+
+Allows you to have filtered posts collapsed with a warning! As shown in the screenshots:
+
+Before | After
+:-------------------------:|:-------------------------:
+ | 
+
+
+### **Color themes**
+
+Allows you to change theme within the app. Supports Material You, purple, pink, green, blue, red, orange, yellow and Nord!
+
+### **Unlisted posting**
+
+**Allows you to post publicly without having your post show up in trends, hashtags or public timelines (i.e., in the tabs “Local”, “Community” and “Posts”).**
+
+When posting with Unlisted visibility, your posts will still be publicly accessible in your profile. They will also be shown in people’s Home timelines, but only if they follow you or someone they follow reposted/replied to your post.
+
+The Mastodon documentation has some more information about [Unlisted posting](https://docs.joinmastodon.org/user/posting/#unlisted) and [Public timelines](https://docs.joinmastodon.org/user/network/#timelines).
+
+### **Federated timeline**
+
+**This allows you to chronologically see all Public posts from people on all other Fediverse neighborhoods your home instance is connected to.**
+
+Despite being one of the main features of federated social media, the Federated timeline wasn’t included in the official Mastodon app – supposedly, because this conflicts with Google’s safety requirements for apps on the Play Store.
+
+That’s one of the reasons why choosing a small, **well-moderated instance is important**. Instance admins and moderators should always make sure to ban abusive users and stop federating with instances who platform them. On well-moderated instances, the Federated timeline can be a welcoming place to meet new people!
+
+### **Image description viewer**
+
+**Allows you to quickly check whether an image or video has an alternative text attached to it.**
+
+This is important to **ensure the content you’re sharing is as accessible as possible** to people who can’t see the images and rely on software to read back the provided content descriptions. Thankfully, it’s quite common for people on the Fediverse to provide such alt texts, and hopefully things stay this way!
+
+### **Reminder to add alt text to attached media**
+
+By default, Moshidon will show a warning to add alt text if your post has any attachments without any alt text. This is for better accessibility, and it can easily be bypassed and disabled in settings.
+
+### **Pinning posts**
+
+**This lets you can highlight important posts on your profile. A dedicated “Pinned” tab in people’s profiles shows all the posts they pinned.**
+
+On the Fediverse, it’s quite common for people to pin posts they want others to read before following them. You can pin/unpin posts yourself by clicking the `⋯` button in the top right corner of your posts.
+
+### **Bookmarks**
+
+**They allow for quickly saving posts and viewing them through the Bookmarks button on the top right of your profile.**
+
+To bookmark a post, press the button between the Favorite and Share buttons on the bottom of the post. Bookmarks are saved privately, so the post authors won’t know you saved their post – the list of bookmarked posts is only visible to you.
+
+## Installation
+
+**Press the download button above to download the APK. Open the downloaded file on your Android device to install it. Moshidon will automatically notify you about new updates inside the app.**
+
+To install this app on your Android device, download the [latest release from GitHub](https://github.com/LucasGGamerM/moshidon/releases/latest/download/moshidon.apk) and open it. You might have to accept installing APK files from your browser when trying to install it. You can also take a look at all releases on the [Releases](https://github.com/LucasGGamerM/moshidon/releases) page.
+
+Moshidon makes use of [Mastodon for Android](https://github.com/mastodon/mastodon-android)’s automatic update checker. Moshidon will check for new updates available on GitHub and offer to download and install them. You can also manually press “Check for updates” at the bottom of the settings page!
+
+Moshidon is also available in [IzzyOnDroid repo](https://apt.izzysoft.de/fdroid/index/apk/org.joinmastodon.android.moshinda), compatible with all F-Droid clients. The APK provided here is the same as the one included in the Releases.
+
+## Release variants
+
+### Stable variant
+
+All stable version downloads can be found on the [Releases](https://github.com/LucasGGamerM/moshidon/releases) page.
+
+**`moshidon.apk`**
+
+Variant with an integrated updater. If you download Moshidon from here (and not from an app store), just download the regular `moshidon.apk`.
+
+### Nightly variant
+
+All nightly builds can be downloaded at [Nightly Releases](https://github.com/LucasGGamerM/moshidon-nightly/releases) page.
+
+**`moshidon-nightly.apk`**
+
+Unstable variant with an integrated updater. It's for development and testing purposes. If you find any bugs with it, please file a bug report at our [issues](https://github.com/LucasGGamerM/moshidon/issues) page.
+
+---
+
+
+## Detailed changes
+
+### Features
+
+* [Adding the ability to view other server's local timelines](https://github.com/LucasGGamerM/moshidon/tree/feature/local-timelines)
+* [Adding the ability to load followers and following from remote instance](https://github.com/LucasGGamerM/moshidon/tree/feature/remote-followers)
+* [Adding the ability to have filtered posts show with a warning](https://github.com/LucasGGamerM/moshidon/tree/feature/filters_again)
+* [Add “Unlisted” as a post visibility option](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/enable-unlisted)
+ ([Pull request](https://github.com/mastodon/mastodon-android/pull/103))
+* Adding a useful private profile note box
+* Auto hiding the compose button on scroll
+* Adding the ability to remind yourself to add alt text to images
+* An indicator for if an image has alt text or not
+* Adding the ability to have drafts
+* Also adding the ability to view announcements from your instance
+* Adding the ability to post for local timeline only (Only on instances that support it!)
+* [Add image description button and viewer](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/display-alt-text) ([Pull request](https://github.com/mastodon/mastodon-android/pull/129))
+* [Implement pinning posts and displaying pinned posts](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/pin-posts) ([Pull request](https://github.com/mastodon/mastodon-android/pull/140))
+* [Implement deleting and re-drafting](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/delete-redraft) ([Closes issue](https://github.com/mastodon/mastodon-android/issues/21))
+* [Implement a bookmark button and list](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/bookmarks) ([Closes issue](https://github.com/mastodon/mastodon-android/issues/22))
+* [Add “Check for update” button in addition to integrated update checker](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/check-for-update-button)
+* [Add “Mark media as sensitive” option](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/mark-media-as-sensitive)
+* [Add settings to hide replies and reposts from the timeline](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/filter-home-timeline) ([Pull request](https://github.com/mastodon/mastodon-android/pull/317))
+* [Follow and unfollow hashtags](https://github.com/sk22/megalodon/commit/7d38f031f197aa6cefaf53e39d929538689c1e4e) ([Closes issue](https://github.com/mastodon/mastodon-android/issues/233))
+* [Notification bell for posts](https://github.com/sk22/megalodon/commit/b166ca705eb9169025ef32bbe6315b42491b57ea) ([Closes issue](https://github.com/mastodon/mastodon-android/issues/81))
+* [Viewing lists and adding/removing users from lists](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:list-timeline-views) based on [@obstsalatschuessel](https://github.com/obstsalatschuessel)'s [Pull request](https://github.com/mastodon/mastodon-android/pull/286)
+* [List favorited posts](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/favs-list)
+* [Accept/reject follow requests](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/follow-requests)
+* [Display content warning title above text](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/cw-above-text)
+* [Add notifications tab for posts](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/posts-notifications-tab)
+* [Show visibility of original post when replying](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/display-reply-visibility)
+* [Clickable reply/boost line above posts](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:clickable-boost-reply-line)
+* [Clickable reply line while replying to open original post](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/clickable-reply-line-compose)
+
+
+### Behavior
+
+* Allow for confirmation before reblogging
+* Adding a bottom option for the publish button, allowing for easier use on larger screens!
+* [Make back button return to the home tab before exiting the app](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/back-returns-home) ([Closes issue](https://github.com/mastodon/mastodon-android/issues/118))
+* [Always preserve content warnings when replying](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/always-preserve-cw) ([Closes issue](https://github.com/mastodon/mastodon-android/issues/113))
+* [Display full image when adding image description](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/compose-image-description-full-image) ([Pull request](https://github.com/mastodon/mastodon-android/pull/182))
+* [Set spoiler height independently to content height](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:spoiler-height-independent) ([Closes issue](https://github.com/mastodon/mastodon-android/issues/166))
+* [Option to hide interaction numbers](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:settings/hide-interaction-numbers)
+* [Option to always reveal content warnings](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:feature/cw-above-text)
+* [Option to disable scrolling title bars](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:settings/disable-marquee)
+
+
+### Visual
+
+* [Custom extended footer redesign](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:compact-extended-footer)
+* [Improvements to the true black mode](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:true-black-improvements)
+* [Profile header tweaks](https://github.com/mastodon/mastodon-android/compare/master...sk22:megalodon:ui/profile-header-tweaks)
+
## Building
@@ -32,4 +191,14 @@ As this app is using Java 17 features, you need JDK 17 or newer to build it. Oth
This project is released under the [GPL-3 License](./LICENSE).
-The Mastodon name and logo are trademarks of Mastodon gGmbH. If you intend to redistribute a modified version of this app, use a unique name and icon for your app that does not mistakenly imply any official connection with or endorsement by Mastodon gGmbH.
+## Links
+
+[F.A.Q](FAQ.md)
+
+[Official matrix chatroom:](https://matrix.to/#/#moshidon:floss.social) https://matrix.to/#/#moshidon:floss.social
+
+[Moshidon roadmap](https://github.com/users/LucasGGamerM/projects/1)
+
+@moshidon@floss.social
+
+---
diff --git a/_config.yml b/_config.yml
new file mode 100644
index 000000000..402335c59
--- /dev/null
+++ b/_config.yml
@@ -0,0 +1,2 @@
+title: Moshidon
+layout: default
diff --git a/_layouts/default.html b/_layouts/default.html
new file mode 100644
index 000000000..1007430d6
--- /dev/null
+++ b/_layouts/default.html
@@ -0,0 +1,17 @@
+
+
+
+ * To customize chipifying with this class you can provide a custom {@link ChipTokenizer} by calling {@link #setChipTokenizer(ChipTokenizer)}.
+ * By default the {@link SpanChipTokenizer} is used.
+ *
Chip Terminators
+ * To set which characters trigger the creation of a chip, call {@link #addChipTerminator(char, int)} or {@link #setChipTerminators(Map)}.
+ * For example if tapping enter should cause all unchipped text to become chipped, call
+ * {@code chipSuggestionTextView.addChipTerminator('\n', ChipTerminatorHandler.BEHAVIOR_CHIPIFY_ALL);}
+ * To completely customize how chips are created when text is entered in this text view you can provide a custom {@link ChipTerminatorHandler}
+ * through {@link #setChipTerminatorHandler(ChipTerminatorHandler)}
+ *
Illegal Characters
+ * To prevent a character from being typed you can call {@link #setIllegalCharacterIdentifier(IllegalCharacterIdentifier)}} to identify characters
+ * that should be considered illegal.
+ *
Suggestions
+ * To provide suggestions you must provide an {@link android.widget.Adapter} by calling {@link #setAdapter(ListAdapter)}
+ *
UI Customization
+ * This view defines six custom attributes (all of which are optional):
+ *
+ *
chipHorizontalSpacing - the horizontal space between chips
+ *
chipBackground - the background color of the chip
+ *
chipCornerRadius - the corner radius of the chip background
+ *
chipTextColor - the color of the chip text
+ *
chipTextSize - the font size of the chip text
+ *
chipHeight - the height of a single chip
+ *
chipVerticalSpacing - the vertical space between chips on consecutive lines
+ *
+ *
Note: chipVerticalSpacing is only used if a chipHeight is also set
+ *
+ *
+ *
+ * The values of these attributes will be passed to the ChipTokenizer through {@link ChipTokenizer#applyConfiguration(Editable, ChipConfiguration)}
+ *
Validation
+ * This class can perform validation when certain events occur (such as losing focus). When the validation occurs is decided by
+ * {@link AutoCompleteTextView}. To perform validation, set a {@link NachoValidator}:
+ *
+ * Note: The NachoValidator will be ignored if a ChipTokenizer is not set. To perform validation without a ChipTokenizer you can use
+ * {@link AutoCompleteTextView}'s built-in {@link AutoCompleteTextView.Validator Validator} through {@link #setValidator(Validator)}
+ *
Editing Chips
+ * This class also supports editing chips on touch. To enable this behavior call {@link #enableEditChipOnTouch(boolean, boolean)}. To disable this
+ * behavior you can call {@link #disableEditChipOnTouch()}
+ *
Example Setup:
+ * A standard setup for this class could look something like the following:
+ *
+ *
+ * @see SpanChipTokenizer
+ * @see DefaultChipTerminatorHandler
+ * @see ChipifyingNachoValidator
+ */
+public class NachoTextView extends MultiAutoCompleteTextView implements TextWatcher, AdapterView.OnItemClickListener {
+
+ // UI Attributes
+ private int mChipHorizontalSpacing = -1;
+ private ColorStateList mChipBackground = null;
+ private int mChipCornerRadius = -1;
+ private int mChipTextColor = Color.TRANSPARENT;
+ private int mChipTextSize = -1;
+ private int mChipHeight = -1;
+ private int mChipVerticalSpacing = -1;
+
+ private int mDefaultPaddingTop = 0;
+ private int mDefaultPaddingBottom = 0;
+ /**
+ * Flag to keep track of the padding state so we only update the padding when necessary
+ */
+ private boolean mUsingDefaultPadding = true;
+
+ // Touch events
+ @Nullable
+ private OnChipClickListener mOnChipClickListener;
+ private GestureDetector singleTapDetector;
+ private boolean mEditChipOnTouchEnabled;
+ private boolean mMoveChipToEndOnEdit;
+ private boolean mChipifyUnterminatedTokensOnEdit;
+
+ // Text entry
+ @Nullable
+ private ChipTokenizer mChipTokenizer;
+ @Nullable
+ private ChipTerminatorHandler mChipTerminatorHandler;
+ @Nullable
+ private NachoValidator mNachoValidator;
+ @Nullable
+ private IllegalCharacterIdentifier illegalCharacterIdentifier;
+
+ @Nullable
+ private OnChipRemoveListener mOnChipRemoveListener;
+ private List mChipsToRemove = new ArrayList<>();
+ private boolean mIgnoreTextChangedEvents;
+ private int mTextChangedStart;
+ private int mTextChangedEnd;
+ private boolean mIsPasteEvent;
+
+ // Measurement
+ private boolean mMeasured;
+
+ // Layout
+ private boolean mLayoutComplete;
+
+ public NachoTextView(Context context) {
+ super(context);
+ init(null);
+ }
+
+ public NachoTextView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ init(attrs);
+ }
+
+ public NachoTextView(Context context, AttributeSet attrs, int defStyleAttr) {
+ super(context, attrs, defStyleAttr);
+ init(attrs);
+ }
+
+ private void init(@Nullable AttributeSet attrs) {
+ Context context = getContext();
+
+ if (attrs != null) {
+ TypedArray attributes = context.getTheme().obtainStyledAttributes(
+ attrs,
+ R.styleable.NachoTextView,
+ 0,
+ R.style.DefaultChipSuggestionTextView);
+
+ try {
+ mChipHorizontalSpacing = attributes.getDimensionPixelSize(R.styleable.NachoTextView_chipHorizontalSpacing, -1);
+ mChipBackground = attributes.getColorStateList(R.styleable.NachoTextView_chipBackground);
+ mChipCornerRadius = attributes.getDimensionPixelSize(R.styleable.NachoTextView_chipCornerRadius, -1);
+ mChipTextColor = attributes.getColor(R.styleable.NachoTextView_chipTextColor, Color.TRANSPARENT);
+ mChipTextSize = attributes.getDimensionPixelSize(R.styleable.NachoTextView_chipTextSize, -1);
+ mChipHeight = attributes.getDimensionPixelSize(R.styleable.NachoTextView_chipHeight, -1);
+ mChipVerticalSpacing = attributes.getDimensionPixelSize(R.styleable.NachoTextView_chipVerticalSpacing, -1);
+ } finally {
+ attributes.recycle();
+ }
+ }
+
+ mDefaultPaddingTop = getPaddingTop();
+ mDefaultPaddingBottom = getPaddingBottom();
+
+ singleTapDetector = new GestureDetector(getContext(), new SingleTapListener());
+
+ setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
+ addTextChangedListener(this);
+ setChipTokenizer(new SpanChipTokenizer<>(context, new ChipSpanChipCreator(), ChipSpan.class));
+ setChipTerminatorHandler(new DefaultChipTerminatorHandler());
+ setOnItemClickListener(this);
+
+ updatePadding();
+ }
+
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+
+ if (!mMeasured && getWidth() > 0) {
+ // Refresh the tokenizer for width changes
+ invalidateChips();
+ mMeasured = true;
+ }
+ }
+
+ @Override
+ protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+ super.onLayout(changed, left, top, right, bottom);
+
+ if (!mLayoutComplete) {
+ invalidateChips();
+ mLayoutComplete = true;
+ }
+ }
+
+ /**
+ * Updates the padding based on whether or not any chips are present to avoid the view from changing heights when chips are inserted/deleted.
+ * Extra padding is added when there are no chips. When there are chips the padding is reverted to its defaults. This only affects top and bottom
+ * padding because the chips only affect the height of the view.
+ */
+ private void updatePadding() {
+ if (mChipHeight != -1) {
+ boolean chipsArePresent = !getAllChips().isEmpty();
+ if (!chipsArePresent && mUsingDefaultPadding) {
+ mUsingDefaultPadding = false;
+ Paint paint = getPaint();
+ Paint.FontMetricsInt fm = paint.getFontMetricsInt();
+ int textHeight = fm.descent - fm.ascent;
+ // Calculate how tall the view should be if there were chips
+ int newTextHeight = mChipHeight + (mChipVerticalSpacing != -1 ? mChipVerticalSpacing : 0);
+ // We need to add half our missing height above and below the text by increasing top and bottom padding
+ int paddingAdjustment = (newTextHeight - textHeight) / 2;
+ super.setPadding(getPaddingLeft(), mDefaultPaddingTop + paddingAdjustment, getPaddingRight(), mDefaultPaddingBottom + paddingAdjustment);
+ } else if (chipsArePresent && !mUsingDefaultPadding) {
+ // If there are chips we can revert to default padding
+ mUsingDefaultPadding = true;
+ super.setPadding(getPaddingLeft(), mDefaultPaddingTop, getPaddingRight(), mDefaultPaddingBottom);
+ }
+ }
+ }
+
+ /**
+ * Sets the padding on this View. The left and right padding will be handled as they normally would in a TextView. The top and bottom padding passed
+ * here will be the padding that is used when there are one or more chips in the text view. When there are no chips present, the padding will be
+ * increased to make sure the overall height of the text view stays the same, since chips take up more vertical space than plain text.
+ *
+ * @param left the left padding in pixels
+ * @param top the top padding in pixels
+ * @param right the right padding in pixels
+ * @param bottom the bottom padding in pixels
+ */
+ @Override
+ public void setPadding(int left, int top, int right, int bottom) {
+ // Call the super method so that left and right padding are updated
+ // top and bottom padding will be handled in updatePadding()
+ super.setPadding(left, top, right, bottom);
+ mDefaultPaddingTop = top;
+ mDefaultPaddingBottom = bottom;
+ updatePadding();
+ }
+
+ public int getChipHorizontalSpacing() {
+ return mChipHorizontalSpacing;
+ }
+
+ public void setChipHorizontalSpacing(@DimenRes int chipHorizontalSpacingResId) {
+ mChipHorizontalSpacing = getContext().getResources().getDimensionPixelSize(chipHorizontalSpacingResId);
+ invalidateChips();
+ }
+
+ public ColorStateList getChipBackground() {
+ return mChipBackground;
+ }
+
+ public void setChipBackgroundResource(@ColorRes int chipBackgroundResId) {
+ setChipBackground(getContext().getColorStateList(chipBackgroundResId));
+ }
+
+ public void setChipBackground(ColorStateList chipBackground) {
+ mChipBackground = chipBackground;
+ invalidateChips();
+ }
+
+ /**
+ * @return The chip background corner radius value, in pixels.
+ */
+ @Dimension
+ public int getChipCornerRadius() {
+ return mChipCornerRadius;
+ }
+
+ /**
+ * Sets the chip background corner radius.
+ *
+ * @param chipCornerRadiusResId The dimension resource with the corner radius value.
+ */
+ public void setChipCornerRadiusResource(@DimenRes int chipCornerRadiusResId) {
+ setChipCornerRadius(getContext().getResources().getDimensionPixelSize(chipCornerRadiusResId));
+ }
+
+ /**
+ * Sets the chip background corner radius.
+ *
+ * @param chipCornerRadius The corner radius value, in pixels.
+ */
+ public void setChipCornerRadius(@Dimension int chipCornerRadius) {
+ mChipCornerRadius = chipCornerRadius;
+ invalidateChips();
+ }
+
+
+ public int getChipTextColor() {
+ return mChipTextColor;
+ }
+
+ public void setChipTextColorResource(@ColorRes int chipTextColorResId) {
+ setChipTextColor(getContext().getColor(chipTextColorResId));
+ }
+
+ public void setChipTextColor(@ColorInt int chipTextColor) {
+ mChipTextColor = chipTextColor;
+ invalidateChips();
+ }
+
+ public int getChipTextSize() {
+ return mChipTextSize;
+ }
+
+ public void setChipTextSize(@DimenRes int chipTextSizeResId) {
+ mChipTextSize = getContext().getResources().getDimensionPixelSize(chipTextSizeResId);
+ invalidateChips();
+ }
+
+ public int getChipHeight() {
+ return mChipHeight;
+ }
+
+ public void setChipHeight(@DimenRes int chipHeightResId) {
+ mChipHeight = getContext().getResources().getDimensionPixelSize(chipHeightResId);
+ invalidateChips();
+ }
+
+ public int getChipVerticalSpacing() {
+ return mChipVerticalSpacing;
+ }
+
+ public void setChipVerticalSpacing(@DimenRes int chipVerticalSpacingResId) {
+ mChipVerticalSpacing = getContext().getResources().getDimensionPixelSize(chipVerticalSpacingResId);
+ invalidateChips();
+ }
+
+ @Nullable
+ public ChipTokenizer getChipTokenizer() {
+ return mChipTokenizer;
+ }
+
+ /**
+ * Sets the {@link ChipTokenizer} to be used by this ChipSuggestionTextView.
+ * Note that a Tokenizer set here will override any Tokenizer set by {@link #setTokenizer(Tokenizer)}
+ *
+ * @param chipTokenizer the {@link ChipTokenizer} to set
+ */
+ public void setChipTokenizer(@Nullable ChipTokenizer chipTokenizer) {
+ mChipTokenizer = chipTokenizer;
+ if (mChipTokenizer != null) {
+ setTokenizer(new ChipTokenizerWrapper(mChipTokenizer));
+ } else {
+ setTokenizer(null);
+ }
+ invalidateChips();
+ }
+
+ public void setOnChipClickListener(@Nullable OnChipClickListener onChipClickListener) {
+ mOnChipClickListener = onChipClickListener;
+ }
+
+ public void setOnChipRemoveListener(@Nullable OnChipRemoveListener onChipRemoveListener) {
+ mOnChipRemoveListener = onChipRemoveListener;
+ }
+
+ public void setChipTerminatorHandler(@Nullable ChipTerminatorHandler chipTerminatorHandler) {
+ mChipTerminatorHandler = chipTerminatorHandler;
+ }
+
+ public void setNachoValidator(@Nullable NachoValidator nachoValidator) {
+ mNachoValidator = nachoValidator;
+ }
+
+ /**
+ * @see ChipTerminatorHandler#setChipTerminators(Map)
+ */
+ public void setChipTerminators(@Nullable Map chipTerminators) {
+ if (mChipTerminatorHandler != null) {
+ mChipTerminatorHandler.setChipTerminators(chipTerminators);
+ }
+ }
+
+ /**
+ * @see ChipTerminatorHandler#addChipTerminator(char, int)
+ */
+ public void addChipTerminator(char character, int behavior) {
+ if (mChipTerminatorHandler != null) {
+ mChipTerminatorHandler.addChipTerminator(character, behavior);
+ }
+ }
+
+ /**
+ * @see ChipTerminatorHandler#setPasteBehavior(int)
+ */
+ public void setPasteBehavior(int pasteBehavior) {
+ if (mChipTerminatorHandler != null) {
+ mChipTerminatorHandler.setPasteBehavior(pasteBehavior);
+ }
+ }
+
+ /**
+ * Sets the {@link IllegalCharacterIdentifier} that will identify characters that should
+ * not show up in the field when typed (i.e. they will be deleted as soon as they are entered).
+ * If a character is listed as both a chip terminator character and an illegal character,
+ * it will be treated as an illegal character.
+ *
+ * @param illegalCharacterIdentifier the identifier to use
+ */
+ public void setIllegalCharacterIdentifier(@Nullable IllegalCharacterIdentifier illegalCharacterIdentifier) {
+ this.illegalCharacterIdentifier = illegalCharacterIdentifier;
+ }
+
+ /**
+ * Applies any updated configuration parameters to any existing chips and all future chips in the text view.
+ *
+ * @see ChipTokenizer#applyConfiguration(Editable, ChipConfiguration)
+ */
+ public void invalidateChips() {
+ beginUnwatchedTextChange();
+
+ if (mChipTokenizer != null) {
+ Editable text = getText();
+ int availableWidth = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
+ ChipConfiguration configuration = new ChipConfiguration(
+ mChipHorizontalSpacing,
+ mChipBackground,
+ mChipCornerRadius,
+ mChipTextColor,
+ mChipTextSize,
+ mChipHeight,
+ mChipVerticalSpacing,
+ availableWidth);
+
+ mChipTokenizer.applyConfiguration(text, configuration);
+ }
+
+ endUnwatchedTextChange();
+ }
+
+ /**
+ * Enables editing chips on touch events. When a touch event occurs, the touched chip will be put in editing mode. To later disable this behavior
+ * call {@link #disableEditChipOnTouch()}.
+ *
+ * Note: If an {@link OnChipClickListener} is set it's behavior will override the behavior described here if it's
+ * {@link OnChipClickListener#onChipClick(Chip, MotionEvent)} method returns true. If that method returns false, the touched chip will be put
+ * in editing mode as expected.
+ *
+ *
+ * @param moveChipToEnd if true, the chip will also be moved to the end of the text when it is put in editing mode
+ * @param chipifyUnterminatedTokens if true, all unterminated tokens will be chipified before the touched chip is put in editing mode
+ * @see #disableEditChipOnTouch()
+ */
+ public void enableEditChipOnTouch(boolean moveChipToEnd, boolean chipifyUnterminatedTokens) {
+ mEditChipOnTouchEnabled = true;
+ mMoveChipToEndOnEdit = moveChipToEnd;
+ mChipifyUnterminatedTokensOnEdit = chipifyUnterminatedTokens;
+ }
+
+ /**
+ * Disables editing chips on touch events. To re-enable this behavior call {@link #enableEditChipOnTouch(boolean, boolean)}.
+ *
+ * @see #enableEditChipOnTouch(boolean, boolean)
+ */
+ public void disableEditChipOnTouch() {
+ mEditChipOnTouchEnabled = false;
+ }
+
+ /**
+ * Puts the provided Chip in editing mode (i.e. reverts it to an unchipified token whose text can be edited).
+ *
+ * @param chip the chip to edit
+ * @param moveChipToEnd if true, the chip will also be moved to the end of the text
+ */
+ public void setEditingChip(Chip chip, boolean moveChipToEnd) {
+ if (mChipTokenizer == null) {
+ return;
+ }
+
+ beginUnwatchedTextChange();
+
+ Editable text = getText();
+ if (moveChipToEnd) {
+ // Move the chip text to the end of the text
+ text.append(chip.getText());
+ // Delete the existing chip
+ mChipTokenizer.deleteChipAndPadding(chip, text);
+ // Move the cursor to the end of the text
+ setSelection(text.length());
+ } else {
+ int chipStart = mChipTokenizer.findChipStart(chip, text);
+ mChipTokenizer.revertChipToToken(chip, text);
+ setSelection(mChipTokenizer.findTokenEnd(text, chipStart));
+ }
+
+ endUnwatchedTextChange();
+ }
+
+ @Override
+ public boolean onTouchEvent(@NonNull MotionEvent event) {
+ boolean wasHandled = false;
+ clearChipStates();
+ Chip touchedChip = findTouchedChip(event);
+ if (touchedChip != null && isFocused() && singleTapDetector.onTouchEvent(event)) {
+ touchedChip.setState(View.PRESSED_SELECTED_STATE_SET);
+ if (onChipClicked(touchedChip)) {
+ wasHandled = true;
+ }
+ if (mOnChipClickListener != null) {
+ mOnChipClickListener.onChipClick(touchedChip, event);
+ }
+ }
+
+ // Getting NullPointerException inside Editor.updateFloatingToolbarVisibility (Editor.java:1520)
+ // primarily seen in Samsung Nougat devices.
+ boolean superOnTouch = false;
+ try {
+ superOnTouch = super.onTouchEvent(event);
+ } catch (NullPointerException e) {
+ Log.w("Nacho", String.format("Error during touch event of type [%d]", event.getAction()), e);
+ // can't handle or reproduce, but will monitor the error
+ }
+
+ return wasHandled || superOnTouch;
+ }
+
+ @Nullable
+ private Chip findTouchedChip(MotionEvent event) {
+ if (mChipTokenizer == null) {
+ return null;
+ }
+
+ Editable text = getText();
+ int offset = getOffsetForPosition(event.getX(), event.getY());
+ List chips = getAllChips();
+ for (Chip chip : chips) {
+ int chipStart = mChipTokenizer.findChipStart(chip, text);
+ int chipEnd = mChipTokenizer.findChipEnd(chip, text); // This is actually the index of the character just past the end of the chip
+ // When a touch event occurs getOffsetForPosition will either return the index of the first character of the span or the index of the
+ // character one past the end of the span
+ // This matches up perfectly with chipStart and chipEnd so we can just directly compare them...
+ if (chipStart <= offset && offset <= chipEnd) {
+ float startX = getXForIndex(chipStart);
+ float endX = getXForIndex(chipEnd - 1);
+ float eventX = event.getX();
+ // ... however, when comparing the x coordinate we need to use (chipEnd - 1) because chipEnd will give us the x coordinate of the
+ // beginning of the next span since that is actually what chipEnd holds. We want the x coordinate of the end of the current span so
+ // we use (chipEnd - 1)
+ if (startX <= eventX && eventX <= endX) {
+ return chip;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Implement this method to handle chip clicked events.
+ *
+ * @param chip the chip that was clicked
+ * @return true if the event was handled, otherwise false
+ */
+ public boolean onChipClicked(Chip chip) {
+ boolean wasHandled = false;
+ if (mEditChipOnTouchEnabled) {
+ if (mChipifyUnterminatedTokensOnEdit) {
+ chipifyAllUnterminatedTokens();
+ }
+ setEditingChip(chip, mMoveChipToEndOnEdit);
+ wasHandled = true;
+ }
+ return wasHandled;
+ }
+
+ private float getXForIndex(int index) {
+ Layout layout = getLayout();
+ return layout.getPrimaryHorizontal(index);
+ }
+
+ private void clearChipStates() {
+ for (Chip chip : getAllChips()) {
+ chip.setState(View.EMPTY_STATE_SET);
+ }
+ }
+
+ @Override
+ public boolean onTextContextMenuItem(int id) {
+ int start = getSelectionStart();
+ int end = getSelectionEnd();
+ switch (id) {
+ case android.R.id.cut:
+ try {
+ setClipboardData(ClipData.newPlainText(null, getTextWithPlainTextSpans(start, end)));
+ } catch (StringIndexOutOfBoundsException e) {
+ throw new StringIndexOutOfBoundsException(
+ String.format(
+ "%s \nError cutting text index [%s, %s] for text [%s] and substring [%s]",
+ e.getMessage(),
+ start,
+ end,
+ getText().toString(),
+ getText().subSequence(start, end)));
+ }
+ getText().delete(getSelectionStart(), getSelectionEnd());
+ return true;
+ case android.R.id.copy:
+ try {
+ setClipboardData(ClipData.newPlainText(null, getTextWithPlainTextSpans(start, end)));
+ } catch (StringIndexOutOfBoundsException e) {
+ throw new StringIndexOutOfBoundsException(
+ String.format(
+ "%s \nError copying text index [%s, %s] for text [%s] and substring [%s]",
+ e.getMessage(),
+ start,
+ end,
+ getText().toString(),
+ getText().subSequence(start, end)));
+ }
+ return true;
+ case android.R.id.paste:
+ mIsPasteEvent = true;
+ boolean returnValue = super.onTextContextMenuItem(id);
+ mIsPasteEvent = false;
+ return returnValue;
+ default:
+ return super.onTextContextMenuItem(id);
+ }
+ }
+
+ private void setClipboardData(ClipData clip) {
+ ClipboardManager clipboard = (ClipboardManager) getContext().
+ getSystemService(Context.CLIPBOARD_SERVICE);
+ clipboard.setPrimaryClip(clip);
+ }
+
+ /**
+ * If a {@link android.widget.AutoCompleteTextView.Validator Validator} was set, this method will validate the entire text.
+ * (Overrides the superclass method which only validates the current token)
+ */
+ @Override
+ public void performValidation() {
+ if (mNachoValidator == null || mChipTokenizer == null) {
+ super.performValidation();
+ return;
+ }
+
+ CharSequence text = getText();
+ if (!TextUtils.isEmpty(text) && !mNachoValidator.isValid(mChipTokenizer, text)) {
+ setRawText(mNachoValidator.fixText(mChipTokenizer, text));
+ }
+ }
+
+ /**
+ * From the point this method is called to when {@link #endUnwatchedTextChange()} is called, all TextChanged events will be ignored
+ */
+ private void beginUnwatchedTextChange() {
+ mIgnoreTextChangedEvents = true;
+ }
+
+ /**
+ * After this method is called TextChanged events will resume being handled.
+ * This method also calls {@link #updatePadding()} in case the unwatched changed created/destroyed chips
+ */
+ private void endUnwatchedTextChange() {
+ updatePadding();
+ mIgnoreTextChangedEvents = false;
+ }
+
+ /**
+ * Sets the contents of this text view without performing any processing (nothing will be chipified, no characters will be removed etc.)
+ *
+ * @param text the text to set
+ */
+ private void setRawText(CharSequence text) {
+ beginUnwatchedTextChange();
+ super.setText(text);
+ endUnwatchedTextChange();
+ }
+
+ /**
+ * Sets the contents of this text view to contain the provided list of strings. The text view will be cleared then each string in the list will
+ * be chipified and appended to the text.
+ *
+ * @param chipValues the list of strings to chipify and set as the contents of the text view or null to clear the text view
+ */
+ public void setText(@Nullable List chipValues) {
+ if (mChipTokenizer == null) {
+ return;
+ }
+ beginUnwatchedTextChange();
+
+ Editable text = getText();
+ text.clear();
+
+ if (chipValues != null) {
+ for (String chipValue : chipValues) {
+ CharSequence chippedText = mChipTokenizer.terminateToken(chipValue, null);
+ text.append(chippedText);
+ }
+ }
+ setSelection(text.length());
+
+ endUnwatchedTextChange();
+ }
+
+ public void setTextWithChips(@Nullable List chips) {
+ if (mChipTokenizer == null) {
+ return;
+ }
+ beginUnwatchedTextChange();
+
+ Editable text = getText();
+ text.clear();
+
+ if (chips != null) {
+ for (ChipInfo chipInfo : chips) {
+ CharSequence chippedText = mChipTokenizer.terminateToken(chipInfo.getText(), chipInfo.getData());
+ text.append(chippedText);
+ }
+ }
+ setSelection(text.length());
+ endUnwatchedTextChange();
+ }
+
+ @Override
+ public void onItemClick(AdapterView> adapterView, View view, int position, long id) {
+ if (mChipTokenizer == null) {
+ return;
+ }
+ Adapter adapter = getAdapter();
+ if (adapter == null) {
+ return;
+ }
+ beginUnwatchedTextChange();
+
+ Object data = getDataForSuggestion(adapter, position);
+ CharSequence text = getFilter().convertResultToString(adapter.getItem(position));
+
+ clearComposingText();
+ int end = getSelectionEnd();
+ Editable editable = getText();
+ int start = mChipTokenizer.findTokenStart(editable, end);
+
+ // guard against java.lang.StringIndexOutOfBoundsException
+ start = Math.min(Math.max(0, start), editable.length());
+ end = Math.min(Math.max(0, end), editable.length());
+ if (end < start) {
+ end = start;
+ }
+
+ editable.replace(start, end, mChipTokenizer.terminateToken(text, data));
+
+ endUnwatchedTextChange();
+ }
+
+ /**
+ * Returns a object that will be associated with a chip that is about to be created for the item at {@code position} in {@code adapter} because that
+ * item was just tapped.
+ *
+ * @param adapter the adapter supplying the suggestions
+ * @param position the position of the suggestion that was tapped
+ * @return the data object
+ */
+ protected Object getDataForSuggestion(@NonNull Adapter adapter, int position) {
+ return adapter.getItem(position);
+ }
+
+ /**
+ * If there is a ChipTokenizer set, this method will do nothing. Instead we wait until the OnItemClickListener is triggered to actually perform
+ * the text replacement so we can also associate the suggestion data with it.
+ *
+ * If there is no ChipTokenizer set, we call through to the super method.
+ *
+ * @param text the text to be chipified
+ */
+ @Override
+ protected void replaceText(CharSequence text) {
+ // If we have a ChipTokenizer, this will be handled by our OnItemClickListener so we can do nothing here.
+ // If we don't have a ChipTokenizer, we'll use the default behavior
+ if (mChipTokenizer == null) {
+ super.replaceText(text);
+ }
+ }
+
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+ if (mIgnoreTextChangedEvents) {
+ return;
+ }
+
+ mTextChangedStart = start;
+ mTextChangedEnd = start + after;
+
+ // Check for backspace
+ if (mChipTokenizer != null) {
+ if (count > 0 && after < count) {
+ int end = start + count;
+ Editable message = getText();
+ Chip[] chips = mChipTokenizer.findAllChips(start, end, message);
+
+ for (Chip chip : chips) {
+ int spanStart = mChipTokenizer.findChipStart(chip, message);
+ int spanEnd = mChipTokenizer.findChipEnd(chip, message);
+ if ((spanStart < end) && (spanEnd > start)) {
+ // Add to remove list
+ mChipsToRemove.add(chip);
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void onTextChanged(@NonNull CharSequence textChanged, int start, int before, int count) {
+ }
+
+ @Override
+ public void afterTextChanged(Editable message) {
+ if (mIgnoreTextChangedEvents) {
+ return;
+ }
+
+ // Avoid triggering text changed events from changes we make in this method
+ beginUnwatchedTextChange();
+
+ // Handle backspace key
+ if (mChipTokenizer != null) {
+ Iterator iterator = mChipsToRemove.iterator();
+ while (iterator.hasNext()) {
+ Chip chip = iterator.next();
+ iterator.remove();
+ mChipTokenizer.deleteChip(chip, message);
+ if (mOnChipRemoveListener != null) {
+ mOnChipRemoveListener.onChipRemove(chip);
+ }
+ }
+ }
+
+ // Handle an illegal or chip terminator character
+ if (message.length() >= mTextChangedEnd && message.length() >= mTextChangedStart) {
+ handleTextChanged(mTextChangedStart, mTextChangedEnd);
+ }
+
+ endUnwatchedTextChange();
+ }
+
+ private void handleTextChanged(int start, int end) {
+ if (start == end) {
+ // If start and end are the same there was text deleted, so this type of event can be ignored
+ return;
+ }
+
+ // First remove any illegal characters
+ Editable text = getText();
+ CharSequence subText = text.subSequence(start, end);
+ CharSequence withoutIllegalCharacters = removeIllegalCharacters(subText);
+
+ // Check if illegal characters were found
+ if (withoutIllegalCharacters.length() < subText.length()) {
+ text.replace(start, end, withoutIllegalCharacters);
+ end = start + withoutIllegalCharacters.length();
+ clearComposingText();
+ }
+
+ if (start == end) {
+ // If start and end are the same here, it means only illegal characters were inserted so there's nothing left to do
+ return;
+ }
+
+ // Then handle chip terminator characters
+ if (mChipTokenizer != null && mChipTerminatorHandler != null) {
+ int newSelectionIndex = mChipTerminatorHandler.findAndHandleChipTerminators(mChipTokenizer, getText(), start, end, mIsPasteEvent);
+ if (newSelectionIndex > 0) {
+ setSelection(newSelectionIndex);
+ }
+ }
+ }
+
+ private CharSequence removeIllegalCharacters(CharSequence text) {
+ StringBuilder newText = new StringBuilder();
+
+ for (int i = 0; i < text.length(); i++) {
+ char theChar = text.charAt(i);
+ if (!isIllegalCharacter(theChar)) {
+ newText.append(theChar);
+ }
+ }
+
+ return newText;
+ }
+
+ private boolean isIllegalCharacter(char character) {
+ if (illegalCharacterIdentifier != null) {
+ return illegalCharacterIdentifier.isCharacterIllegal(character);
+ }
+ return false;
+ }
+
+ /**
+ * Chipifies all existing plain text in the field
+ */
+ public void chipifyAllUnterminatedTokens() {
+ beginUnwatchedTextChange();
+ chipifyAllUnterminatedTokens(getText());
+ endUnwatchedTextChange();
+ }
+
+ private void chipifyAllUnterminatedTokens(Editable text) {
+ if (mChipTokenizer != null) {
+ mChipTokenizer.terminateAllTokens(text);
+ }
+ }
+
+ /**
+ * Replaces the text from start (inclusive) to end (exclusive) with a chip
+ * containing the same text
+ *
+ * @param start the index of the first character to replace
+ * @param end one more than the index of the last character to replace
+ */
+ public void chipify(int start, int end) {
+ beginUnwatchedTextChange();
+ chipify(start, end, getText(), null);
+ endUnwatchedTextChange();
+ }
+
+ private void chipify(int start, int end, Editable text, Object data) {
+ if (mChipTokenizer != null) {
+ CharSequence textToChip = text.subSequence(start, end);
+ CharSequence chippedText = mChipTokenizer.terminateToken(textToChip, data);
+ text.replace(start, end, chippedText);
+ }
+ }
+
+ private CharSequence getTextWithPlainTextSpans(int start, int end) {
+ Editable editable = getText();
+ String selectedText = editable.subSequence(start, end).toString();
+
+ if (mChipTokenizer != null) {
+ List chips = Arrays.asList(mChipTokenizer.findAllChips(start, end, editable));
+ Collections.reverse(chips);
+ for (Chip chip : chips) {
+ String chipText = chip.getText().toString();
+ int chipStart = mChipTokenizer.findChipStart(chip, editable) - start;
+ int chipEnd = mChipTokenizer.findChipEnd(chip, editable) - start;
+ selectedText = selectedText.substring(0, chipStart) + chipText + selectedText.substring(chipEnd, selectedText.length());
+ }
+ }
+ return selectedText;
+ }
+
+ /**
+ * @return all of the chips currently in the text view - this does not include any unchipped text
+ */
+ @NonNull
+ public List getAllChips() {
+ Editable text = getText();
+ return mChipTokenizer != null ? Arrays.asList(mChipTokenizer.findAllChips(0, text.length(), text)) : new ArrayList();
+ }
+
+ /**
+ * Returns a List of the string values of all the chips in the text (obtained through {@link Chip#getText()}).
+ * This does not include the text of any unterminated tokens.
+ *
+ * @return the List of chip values
+ */
+ @NonNull
+ public List getChipValues() {
+ List chipValues = new ArrayList<>();
+
+ List chips = getAllChips();
+ for (Chip chip : chips) {
+ chipValues.add(chip.getText().toString());
+ }
+
+ return chipValues;
+ }
+
+ /**
+ * Returns a List of the string values of all the tokens (unchipped text) in the text
+ * (obtained through {@link ChipTokenizer#findAllTokens(CharSequence)}). This does not include any chipped text.
+ *
+ * @return the List of token values
+ */
+ @NonNull
+ public List getTokenValues() {
+ List tokenValues = new ArrayList<>();
+
+ if (mChipTokenizer != null) {
+ Editable text = getText();
+ List> unterminatedTokenIndexes = mChipTokenizer.findAllTokens(text);
+ for (Pair indexes : unterminatedTokenIndexes) {
+ String tokenValue = text.subSequence(indexes.first, indexes.second).toString();
+ tokenValues.add(tokenValue);
+ }
+ }
+
+ return tokenValues;
+ }
+
+ /**
+ * Returns a combination of the chip values and token values in the text.
+ *
+ * @return the List of all chip and token values
+ * @see #getChipValues()
+ * @see #getTokenValues()
+ */
+ @NonNull
+ public List getChipAndTokenValues() {
+ List chipAndTokenValues = new ArrayList<>();
+ chipAndTokenValues.addAll(getChipValues());
+ chipAndTokenValues.addAll(getTokenValues());
+ return chipAndTokenValues;
+ }
+
+ @Override
+ public String toString() {
+ try {
+ return getTextWithPlainTextSpans(0, getText().length()).toString();
+ } catch (ClassCastException ex) { // Exception is thrown by cast in getText() on some LG devices
+ return super.toString();
+ } catch (StringIndexOutOfBoundsException e) {
+ throw new StringIndexOutOfBoundsException(String.format("%s \nError converting toString() [%s]", e.getMessage(), getText().toString()));
+ }
+ }
+
+ private class ChipTokenizerWrapper implements Tokenizer {
+
+ @NonNull
+ private ChipTokenizer mChipTokenizer;
+
+ public ChipTokenizerWrapper(@NonNull ChipTokenizer chipTokenizer) {
+ mChipTokenizer = chipTokenizer;
+ }
+
+ @Override
+ public int findTokenStart(CharSequence text, int cursor) {
+ return mChipTokenizer.findTokenStart(text, cursor);
+ }
+
+ @Override
+ public int findTokenEnd(CharSequence text, int cursor) {
+ return mChipTokenizer.findTokenEnd(text, cursor);
+ }
+
+ @Override
+ public CharSequence terminateToken(CharSequence text) {
+ return mChipTokenizer.terminateToken(text, null);
+ }
+ }
+
+ public interface OnChipClickListener {
+
+ /**
+ * Called when a chip in this TextView is touched. This callback is triggered by the {@link MotionEvent#ACTION_UP} event.
+ *
+ * @param chip the {@link Chip} that was touched
+ * @param event the {@link MotionEvent} that caused the touch
+ */
+ void onChipClick(Chip chip, MotionEvent event);
+ }
+
+ public interface OnChipRemoveListener {
+
+ /**
+ * Called when a chip in this TextView is removed
+ *
+ * @param chip the {@link Chip} that was removed
+ */
+ void onChipRemove(Chip chip);
+ }
+
+ private class SingleTapListener extends GestureDetector.SimpleOnGestureListener {
+
+ /**
+ * @param e the {@link MotionEvent} passed to the GestureDetector
+ * @return true if singleTapUp (click) was detected
+ */
+ @Override
+ public boolean onSingleTapUp(MotionEvent e) {
+ return true;
+ }
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/chip/Chip.java b/mastodon/src/main/java/com/hootsuite/nachos/chip/Chip.java
new file mode 100644
index 000000000..f07707cd3
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/chip/Chip.java
@@ -0,0 +1,29 @@
+package com.hootsuite.nachos.chip;
+
+import androidx.annotation.Nullable;
+
+public interface Chip {
+
+ /**
+ * @return the text represented by this Chip
+ */
+ CharSequence getText();
+
+ /**
+ * @return the data associated with this Chip or null if no data is associated with it
+ */
+ @Nullable
+ Object getData();
+
+ /**
+ * @return the width of the Chip or -1 if the Chip hasn't been given the chance to calculate its width
+ */
+ int getWidth();
+
+ /**
+ * Sets the UI state.
+ *
+ * @param stateSet one of the state constants in {@link android.view.View}
+ */
+ void setState(int[] stateSet);
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipCreator.java b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipCreator.java
new file mode 100644
index 000000000..c38f5cff1
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipCreator.java
@@ -0,0 +1,44 @@
+package com.hootsuite.nachos.chip;
+
+import android.content.Context;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.hootsuite.nachos.ChipConfiguration;
+
+/**
+ * Interface to allow the creation and configuration of chips
+ *
+ * @param The type of {@link Chip} that the implementation will create/configure
+ */
+public interface ChipCreator {
+
+ /**
+ * Creates a chip from the given context and text. Use this method when creating a brand new chip from a piece of text.
+ *
+ * @param context the {@link Context} to use to initialize the chip
+ * @param text the text the Chip should represent
+ * @param data the data to associate with the Chip, or null to associate no data
+ * @return the created chip
+ */
+ C createChip(@NonNull Context context, @NonNull CharSequence text, @Nullable Object data);
+
+ /**
+ * Creates a chip from the given context and existing chip. Use this method when recreating a chip from an existing one.
+ *
+ * @param context the {@link Context} to use to initialize the chip
+ * @param existingChip the chip that the created chip should be based on
+ * @return the created chip
+ */
+ C createChip(@NonNull Context context, @NonNull C existingChip);
+
+ /**
+ * Applies the given {@link ChipConfiguration} to the given {@link Chip}. Use this method to customize the appearance/behavior of a chip before
+ * adding it to the text.
+ *
+ * @param chip the chip to configure
+ * @param chipConfiguration the configuration to apply to the chip
+ */
+ void configureChip(@NonNull C chip, @NonNull ChipConfiguration chipConfiguration);
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipInfo.java b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipInfo.java
new file mode 100644
index 000000000..69b8ea4c4
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipInfo.java
@@ -0,0 +1,20 @@
+package com.hootsuite.nachos.chip;
+
+public class ChipInfo {
+
+ private final CharSequence mText;
+ private final Object mData;
+
+ public ChipInfo(CharSequence text, Object data) {
+ this.mText = text;
+ this.mData = data;
+ }
+
+ public CharSequence getText() {
+ return mText;
+ }
+
+ public Object getData() {
+ return mData;
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipSpan.java b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipSpan.java
new file mode 100644
index 000000000..e3cc57922
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipSpan.java
@@ -0,0 +1,510 @@
+package com.hootsuite.nachos.chip;
+
+import android.content.Context;
+import android.content.res.ColorStateList;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.Rect;
+import android.graphics.RectF;
+import android.graphics.drawable.Drawable;
+import android.text.style.ImageSpan;
+
+import androidx.annotation.Dimension;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import org.joinmastodon.android.R;
+
+/**
+ * A Span that displays text and an optional icon inside of a material design chip. The chip's dimensions, colors etc. can be extensively customized
+ * through the various setter methods available in this class.
+ * The basic structure of the chip is the following:
+ * For chips with the icon on right:
+ *
+ */
+public class ChipSpan extends ImageSpan implements Chip {
+
+ private static final float SCALE_PERCENT_OF_CHIP_HEIGHT = 0.70f;
+ private static final boolean ICON_ON_LEFT_DEFAULT = true;
+
+ private int[] mStateSet = new int[]{};
+
+ private String mEllipsis;
+
+ private ColorStateList mDefaultBackgroundColor;
+ private ColorStateList mBackgroundColor;
+ private int mTextColor;
+ private int mCornerRadius = -1;
+ private int mIconBackgroundColor;
+
+ private int mTextSize = -1;
+ private int mPaddingEdgePx;
+ private int mPaddingBetweenImagePx;
+ private int mLeftMarginPx;
+ private int mRightMarginPx;
+ private int mMaxAvailableWidth = -1;
+
+ private CharSequence mText;
+ private String mTextToDraw;
+
+ private Drawable mIcon;
+ private boolean mShowIconOnLeft = ICON_ON_LEFT_DEFAULT;
+
+ private int mChipVerticalSpacing = 0;
+ private int mChipHeight = -1;
+ private int mChipWidth = -1;
+ private int mIconWidth;
+
+ private int mCachedSize = -1;
+
+ private Object mData;
+
+ /**
+ * Constructs a new ChipSpan.
+ *
+ * @param context a {@link Context} that will be used to retrieve default configurations from resource files
+ * @param text the text for the ChipSpan to display
+ * @param icon an optional icon (can be {@code null}) for the ChipSpan to display
+ */
+ public ChipSpan(@NonNull Context context, @NonNull CharSequence text, @Nullable Drawable icon, Object data) {
+ super(icon);
+ mIcon = icon;
+ mText = text;
+ mTextToDraw = mText.toString();
+
+ mEllipsis = context.getString(R.string.chip_ellipsis);
+
+ mDefaultBackgroundColor = context.getColorStateList(R.color.chip_material_background);
+ mBackgroundColor = mDefaultBackgroundColor;
+
+ mTextColor = context.getColor(R.color.chip_default_text_color);
+ mIconBackgroundColor = context.getColor(R.color.chip_default_icon_background_color);
+
+ Resources resources = context.getResources();
+ mPaddingEdgePx = resources.getDimensionPixelSize(R.dimen.chip_default_padding_edge);
+ mPaddingBetweenImagePx = resources.getDimensionPixelSize(R.dimen.chip_default_padding_between_image);
+ mLeftMarginPx = resources.getDimensionPixelSize(R.dimen.chip_default_left_margin);
+ mRightMarginPx = resources.getDimensionPixelSize(R.dimen.chip_default_right_margin);
+
+ mData = data;
+ }
+
+ /**
+ * Copy constructor to recreate a ChipSpan from an existing one
+ *
+ * @param context a {@link Context} that will be used to retrieve default configurations from resource files
+ * @param chipSpan the ChipSpan to copy
+ */
+ public ChipSpan(@NonNull Context context, @NonNull ChipSpan chipSpan) {
+ this(context, chipSpan.getText(), chipSpan.getDrawable(), chipSpan.getData());
+
+ mDefaultBackgroundColor = chipSpan.mDefaultBackgroundColor;
+ mTextColor = chipSpan.mTextColor;
+ mIconBackgroundColor = chipSpan.mIconBackgroundColor;
+ mCornerRadius = chipSpan.mCornerRadius;
+
+ mTextSize = chipSpan.mTextSize;
+ mPaddingEdgePx = chipSpan.mPaddingEdgePx;
+ mPaddingBetweenImagePx = chipSpan.mPaddingBetweenImagePx;
+ mLeftMarginPx = chipSpan.mLeftMarginPx;
+ mRightMarginPx = chipSpan.mRightMarginPx;
+ mMaxAvailableWidth = chipSpan.mMaxAvailableWidth;
+
+ mShowIconOnLeft = chipSpan.mShowIconOnLeft;
+
+ mChipVerticalSpacing = chipSpan.mChipVerticalSpacing;
+ mChipHeight = chipSpan.mChipHeight;
+
+ mStateSet = chipSpan.mStateSet;
+ }
+
+ @Override
+ public Object getData() {
+ return mData;
+ }
+
+ /**
+ * Sets the height of the chip. This height should not include any extra spacing (for extra vertical spacing call {@link #setChipVerticalSpacing(int)}).
+ * The background of the chip will fill the full height provided here. If this method is never called, the chip will have the height of one full line
+ * of text by default. If {@code -1} is passed here, the chip will revert to this default behavior.
+ *
+ * @param chipHeight the height to set in pixels
+ */
+ public void setChipHeight(int chipHeight) {
+ mChipHeight = chipHeight;
+ }
+
+ /**
+ * Sets the vertical spacing to include in between chips. Half of the value set here will be placed as empty space above the chip and half the value
+ * will be placed as empty space below the chip. Therefore chips on consecutive lines will have the full value as vertical space in between them.
+ * This spacing is achieved by adjusting the font metrics used by the text view containing these chips; however it does not come into effect until
+ * at least one chip is created. Note that vertical spacing is dependent on having a fixed chip height (set in {@link #setChipHeight(int)}). If a
+ * height is not specified in that method, the value set here will be ignored.
+ *
+ * @param chipVerticalSpacing the vertical spacing to set in pixels
+ */
+ public void setChipVerticalSpacing(int chipVerticalSpacing) {
+ mChipVerticalSpacing = chipVerticalSpacing;
+ }
+
+ /**
+ * Sets the font size for the chip's text. If this method is never called, the chip text will have the same font size as the text in the TextView
+ * containing this chip by default. If {@code -1} is passed here, the chip will revert to this default behavior.
+ *
+ * @param size the font size to set in pixels
+ */
+ public void setTextSize(int size) {
+ mTextSize = size;
+ invalidateCachedSize();
+ }
+
+ /**
+ * Sets the color for the chip's text.
+ *
+ * @param color the color to set (as a hexadecimal number in the form 0xAARRGGBB)
+ */
+ public void setTextColor(int color) {
+ mTextColor = color;
+ }
+
+ /**
+ * Sets where the icon (if an icon was provided in the constructor) will appear.
+ *
+ * @param showIconOnLeft if true, the icon will appear on the left, otherwise the icon will appear on the right
+ */
+ public void setShowIconOnLeft(boolean showIconOnLeft) {
+ this.mShowIconOnLeft = showIconOnLeft;
+ invalidateCachedSize();
+ }
+
+ /**
+ * Sets the left margin. This margin will appear as empty space (it will not share the chip's background color) to the left of the chip.
+ *
+ * @param leftMarginPx the left margin to set in pixels
+ */
+ public void setLeftMargin(int leftMarginPx) {
+ mLeftMarginPx = leftMarginPx;
+ invalidateCachedSize();
+ }
+
+ /**
+ * Sets the right margin. This margin will appear as empty space (it will not share the chip's background color) to the right of the chip.
+ *
+ * @param rightMarginPx the right margin to set in pixels
+ */
+ public void setRightMargin(int rightMarginPx) {
+ this.mRightMarginPx = rightMarginPx;
+ invalidateCachedSize();
+ }
+
+ /**
+ * Sets the background color. To configure which color in the {@link ColorStateList} is shown you can call {@link #setState(int[])}.
+ * Passing {@code null} here will cause the chip to revert to it's default background.
+ *
+ * @param backgroundColor a {@link ColorStateList} containing backgrounds for different states.
+ * @see #setState(int[])
+ */
+ public void setBackgroundColor(@Nullable ColorStateList backgroundColor) {
+ mBackgroundColor = backgroundColor != null ? backgroundColor : mDefaultBackgroundColor;
+ }
+
+ /**
+ * Sets the chip background corner radius.
+ *
+ * @param cornerRadius The corner radius value, in pixels.
+ */
+ public void setCornerRadius(@Dimension int cornerRadius) {
+ mCornerRadius = cornerRadius;
+ }
+
+ /**
+ * Sets the icon background color. This is the color of the circle that gets drawn behind the icon passed to the
+ * {@link #ChipSpan(Context, CharSequence, Drawable, Object)} constructor}
+ *
+ * @param iconBackgroundColor the icon background color to set (as a hexadecimal number in the form 0xAARRGGBB)
+ */
+ public void setIconBackgroundColor(int iconBackgroundColor) {
+ mIconBackgroundColor = iconBackgroundColor;
+ }
+
+ public void setMaxAvailableWidth(int maxAvailableWidth) {
+ mMaxAvailableWidth = maxAvailableWidth;
+ invalidateCachedSize();
+ }
+
+ /**
+ * Sets the UI state. This state will be reflected in the background color drawn for the chip.
+ *
+ * @param stateSet one of the state constants in {@link android.view.View}
+ * @see #setBackgroundColor(ColorStateList)
+ */
+ @Override
+ public void setState(int[] stateSet) {
+ this.mStateSet = stateSet != null ? stateSet : new int[]{};
+ }
+
+ @Override
+ public CharSequence getText() {
+ return mText;
+ }
+
+ @Override
+ public int getWidth() {
+ // If we haven't actually calculated a chip width yet just return -1, otherwise return the chip width + margins
+ return mChipWidth != -1 ? (mLeftMarginPx + mChipWidth + mRightMarginPx) : -1;
+ }
+
+ @Override
+ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
+ boolean usingFontMetrics = (fm != null);
+
+ // Adjust the font metrics regardless of whether or not there is a cached size so that the text view can maintain its height
+ if (usingFontMetrics) {
+ adjustFontMetrics(paint, fm);
+ }
+
+ if (mCachedSize == -1 && usingFontMetrics) {
+ mIconWidth = (mIcon != null) ? calculateChipHeight(fm.top, fm.bottom) : 0;
+
+ int actualWidth = calculateActualWidth(paint);
+ mCachedSize = actualWidth;
+
+ if (mMaxAvailableWidth != -1) {
+ int maxAvailableWidthMinusMargins = mMaxAvailableWidth - mLeftMarginPx - mRightMarginPx;
+ if (actualWidth > maxAvailableWidthMinusMargins) {
+ mTextToDraw = mText + mEllipsis;
+
+ while ((calculateActualWidth(paint) > maxAvailableWidthMinusMargins) && mTextToDraw.length() > 0) {
+ int lastCharacterIndex = mTextToDraw.length() - mEllipsis.length() - 1;
+ if (lastCharacterIndex < 0) {
+ break;
+ }
+ mTextToDraw = mTextToDraw.substring(0, lastCharacterIndex) + mEllipsis;
+ }
+
+ // Avoid a negative width
+ mChipWidth = Math.max(0, maxAvailableWidthMinusMargins);
+ mCachedSize = mMaxAvailableWidth;
+ }
+ }
+ }
+
+ return mCachedSize;
+ }
+
+ private int calculateActualWidth(Paint paint) {
+ // Only change the text size if a text size was set
+ if (mTextSize != -1) {
+ paint.setTextSize(mTextSize);
+ }
+
+ int totalPadding = mPaddingEdgePx;
+
+ // Find text width
+ Rect bounds = new Rect();
+ paint.getTextBounds(mTextToDraw, 0, mTextToDraw.length(), bounds);
+ int textWidth = bounds.width();
+
+ if (mIcon != null) {
+ totalPadding += mPaddingBetweenImagePx;
+ } else {
+ totalPadding += mPaddingEdgePx;
+ }
+
+ mChipWidth = totalPadding + textWidth + mIconWidth;
+ return getWidth();
+ }
+
+ public void invalidateCachedSize() {
+ mCachedSize = -1;
+ }
+
+ /**
+ * Adjusts the provided font metrics to make it seem like the font takes up {@code mChipHeight + mChipVerticalSpacing} pixels in height.
+ * This effectively ensures that the TextView will have a height equal to {@code mChipHeight + mChipVerticalSpacing} + whatever padding it has set.
+ * In {@link #draw(Canvas, CharSequence, int, int, float, int, int, int, Paint)} the chip itself is drawn to that it is vertically centered with
+ * {@code mChipVerticalSpacing / 2} pixels of space above and below it
+ *
+ * @param paint the paint whose font metrics should be adjusted
+ * @param fm the font metrics object to populate through {@link Paint#getFontMetricsInt(Paint.FontMetricsInt)}
+ */
+ private void adjustFontMetrics(Paint paint, Paint.FontMetricsInt fm) {
+ // Only actually adjust font metrics if we have a chip height set
+ if (mChipHeight != -1) {
+ paint.getFontMetricsInt(fm);
+ int textHeight = fm.descent - fm.ascent;
+ // Break up the vertical spacing in half because half will go above the chip, half will go below the chip
+ int halfSpacing = mChipVerticalSpacing / 2;
+
+ // Given that the text is centered vertically within the chip, the amount of space above or below the text (inbetween the text and chip)
+ // is half their difference in height:
+ int spaceBetweenChipAndText = (mChipHeight - textHeight) / 2;
+
+ int textTop = fm.top;
+ int chipTop = fm.top - spaceBetweenChipAndText;
+
+ int textBottom = fm.bottom;
+ int chipBottom = fm.bottom + spaceBetweenChipAndText;
+
+ // The text may have been taller to begin with so we take the most negative coordinate (highest up) to be the top of the content
+ int topOfContent = Math.min(textTop, chipTop);
+ // Same as above but we want the largest positive coordinate (lowest down) to be the bottom of the content
+ int bottomOfContent = Math.max(textBottom, chipBottom);
+
+ // Shift the top up by halfSpacing and the bottom down by halfSpacing
+ int topOfContentWithSpacing = topOfContent - halfSpacing;
+ int bottomOfContentWithSpacing = bottomOfContent + halfSpacing;
+
+ // Change the font metrics so that the TextView thinks the font takes up the vertical space of a chip + spacing
+ fm.ascent = topOfContentWithSpacing;
+ fm.descent = bottomOfContentWithSpacing;
+ fm.top = topOfContentWithSpacing;
+ fm.bottom = bottomOfContentWithSpacing;
+ }
+ }
+
+ private int calculateChipHeight(int top, int bottom) {
+ // If a chip height was set we can return that, otherwise calculate it from top and bottom
+ return mChipHeight != -1 ? mChipHeight : bottom - top;
+ }
+
+ @Override
+ public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
+ // Shift everything mLeftMarginPx to the left to create an empty space on the left (creating the margin)
+ x += mLeftMarginPx;
+ if (mChipHeight != -1) {
+ // If we set a chip height, adjust to vertically center chip in the line
+ // Adding (bottom - top) / 2 shifts the chip down so the top of it will be centered vertically
+ // Subtracting (mChipHeight / 2) shifts the chip back up so that the center of it will be centered vertically (as desired)
+ top += ((bottom - top) / 2) - (mChipHeight / 2);
+ bottom = top + mChipHeight;
+ }
+
+ // Perform actual drawing
+ drawBackground(canvas, x, top, bottom, paint);
+ drawText(canvas, x, top, bottom, paint, mTextToDraw);
+ if (mIcon != null) {
+ drawIcon(canvas, x, top, bottom, paint);
+ }
+ }
+
+ private void drawBackground(Canvas canvas, float x, int top, int bottom, Paint paint) {
+ int backgroundColor = mBackgroundColor.getColorForState(mStateSet, mBackgroundColor.getDefaultColor());
+ paint.setColor(backgroundColor);
+ int height = calculateChipHeight(top, bottom);
+ RectF rect = new RectF(x, top, x + mChipWidth, bottom);
+ int cornerRadius = (mCornerRadius != -1) ? mCornerRadius : height / 2;
+ canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint);
+ paint.setColor(mTextColor);
+ }
+
+ private void drawText(Canvas canvas, float x, int top, int bottom, Paint paint, CharSequence text) {
+ if (mTextSize != -1) {
+ paint.setTextSize(mTextSize);
+ }
+ int height = calculateChipHeight(top, bottom);
+ Paint.FontMetrics fm = paint.getFontMetrics();
+
+ // The top value provided here is the y coordinate for the very top of the chip
+ // The y coordinate we are calculating is where the baseline of the text will be drawn
+ // Our objective is to have the midpoint between the top and baseline of the text be in line with the vertical center of the chip
+ // First we add height / 2 which will put the baseline at the vertical center of the chip
+ // Then we add half the height of the text which will lower baseline so that the midpoint is at the vertical center of the chip as desired
+ float adjustedY = top + ((height / 2) + ((-fm.top - fm.bottom) / 2));
+
+ // The x coordinate provided here is the left-most edge of the chip
+ // If there is no icon or the icon is on the right, then the text will start at the left-most edge, but indented with the edge padding, so we
+ // add mPaddingEdgePx
+ // If there is an icon and it's on the left, the text will start at the left-most edge, but indented by the combined width of the icon and
+ // the padding between the icon and text, so we add (mIconWidth + mPaddingBetweenImagePx)
+ float adjustedX = x + ((mIcon == null || !mShowIconOnLeft) ? mPaddingEdgePx : (mIconWidth + mPaddingBetweenImagePx));
+
+ canvas.drawText(text, 0, text.length(), adjustedX, adjustedY, paint);
+ }
+
+ private void drawIcon(Canvas canvas, float x, int top, int bottom, Paint paint) {
+ drawIconBackground(canvas, x, top, bottom, paint);
+ drawIconBitmap(canvas, x, top, bottom, paint);
+ }
+
+ private void drawIconBackground(Canvas canvas, float x, int top, int bottom, Paint paint) {
+ int height = calculateChipHeight(top, bottom);
+
+ paint.setColor(mIconBackgroundColor);
+
+ // Since it's a circle the diameter is equal to the height, so the radius == diameter / 2 == height / 2
+ int radius = height / 2;
+ // The coordinates that get passed to drawCircle are for the center of the circle
+ // x is the left edge of the chip, (x + mChipWidth) is the right edge of the chip
+ // So the center of the circle is one radius distance from either the left or right edge (depending on which side the icon is being drawn on)
+ float circleX = mShowIconOnLeft ? (x + radius) : (x + mChipWidth - radius);
+ // The y coordinate is always just one radius distance from the top
+ canvas.drawCircle(circleX, top + radius, radius, paint);
+
+ paint.setColor(mTextColor);
+ }
+
+ private void drawIconBitmap(Canvas canvas, float x, int top, int bottom, Paint paint) {
+ int height = calculateChipHeight(top, bottom);
+
+ // Create a scaled down version of the bitmap to fit within the circle (whose diameter == height)
+ Bitmap iconBitmap = Bitmap.createBitmap(mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
+ Bitmap scaledIconBitMap = scaleDown(iconBitmap, (float) height * SCALE_PERCENT_OF_CHIP_HEIGHT, true);
+ iconBitmap.recycle();
+ Canvas bitmapCanvas = new Canvas(scaledIconBitMap);
+ mIcon.setBounds(0, 0, bitmapCanvas.getWidth(), bitmapCanvas.getHeight());
+ mIcon.draw(bitmapCanvas);
+
+ // We are drawing a square icon inside of a circle
+ // The coordinates we pass to canvas.drawBitmap have to be for the top-left corner of the bitmap
+ // The bitmap should be inset by half of (circle width - bitmap width)
+ // Since it's a circle, the circle's width is equal to it's height which is equal to the chip height
+ float xInsetWithinCircle = (height - bitmapCanvas.getWidth()) / 2;
+
+ // The icon x coordinate is going to be insetWithinCircle pixels away from the left edge of the circle
+ // If the icon is on the left, the left edge of the circle is just x
+ // If the icon is on the right, the left edge of the circle is x + mChipWidth - height
+ float iconX = mShowIconOnLeft ? (x + xInsetWithinCircle) : (x + mChipWidth - height + xInsetWithinCircle);
+
+ // The y coordinate works the same way (only it's always from the top edge)
+ float yInsetWithinCircle = (height - bitmapCanvas.getHeight()) / 2;
+ float iconY = top + yInsetWithinCircle;
+
+ canvas.drawBitmap(scaledIconBitMap, iconX, iconY, paint);
+ }
+
+ private Bitmap scaleDown(Bitmap realImage, float maxImageSize, boolean filter) {
+ float ratio = Math.min(maxImageSize / realImage.getWidth(), maxImageSize / realImage.getHeight());
+ int width = Math.round(ratio * realImage.getWidth());
+ int height = Math.round(ratio * realImage.getHeight());
+ return Bitmap.createScaledBitmap(realImage, width, height, filter);
+ }
+
+ @Override
+ public String toString() {
+ return mText.toString();
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipSpanChipCreator.java b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipSpanChipCreator.java
new file mode 100644
index 000000000..8e0278f0c
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/chip/ChipSpanChipCreator.java
@@ -0,0 +1,60 @@
+package com.hootsuite.nachos.chip;
+
+import android.content.Context;
+import android.content.res.ColorStateList;
+import android.graphics.Color;
+
+import androidx.annotation.NonNull;
+
+import com.hootsuite.nachos.ChipConfiguration;
+
+public class ChipSpanChipCreator implements ChipCreator {
+
+ @Override
+ public ChipSpan createChip(@NonNull Context context, @NonNull CharSequence text, Object data) {
+ return new ChipSpan(context, text, null, data);
+ }
+
+ @Override
+ public ChipSpan createChip(@NonNull Context context, @NonNull ChipSpan existingChip) {
+ return new ChipSpan(context, existingChip);
+ }
+
+ @Override
+ public void configureChip(@NonNull ChipSpan chip, @NonNull ChipConfiguration chipConfiguration) {
+ int chipHorizontalSpacing = chipConfiguration.getChipHorizontalSpacing();
+ ColorStateList chipBackground = chipConfiguration.getChipBackground();
+ int chipCornerRadius = chipConfiguration.getChipCornerRadius();
+ int chipTextColor = chipConfiguration.getChipTextColor();
+ int chipTextSize = chipConfiguration.getChipTextSize();
+ int chipHeight = chipConfiguration.getChipHeight();
+ int chipVerticalSpacing = chipConfiguration.getChipVerticalSpacing();
+ int maxAvailableWidth = chipConfiguration.getMaxAvailableWidth();
+
+ if (chipHorizontalSpacing != -1) {
+ chip.setLeftMargin(chipHorizontalSpacing / 2);
+ chip.setRightMargin(chipHorizontalSpacing / 2);
+ }
+ if (chipBackground != null) {
+ chip.setBackgroundColor(chipBackground);
+ }
+ if (chipCornerRadius != -1) {
+ chip.setCornerRadius(chipCornerRadius);
+ }
+ if (chipTextColor != Color.TRANSPARENT) {
+ chip.setTextColor(chipTextColor);
+ }
+ if (chipTextSize != -1) {
+ chip.setTextSize(chipTextSize);
+ }
+ if (chipHeight != -1) {
+ chip.setChipHeight(chipHeight);
+ }
+ if (chipVerticalSpacing != -1) {
+ chip.setChipVerticalSpacing(chipVerticalSpacing);
+ }
+ if (maxAvailableWidth != -1) {
+ chip.setMaxAvailableWidth(maxAvailableWidth);
+ }
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/terminator/ChipTerminatorHandler.java b/mastodon/src/main/java/com/hootsuite/nachos/terminator/ChipTerminatorHandler.java
new file mode 100644
index 000000000..f97d430ea
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/terminator/ChipTerminatorHandler.java
@@ -0,0 +1,95 @@
+package com.hootsuite.nachos.terminator;
+
+import android.text.Editable;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.hootsuite.nachos.tokenizer.ChipTokenizer;
+
+import java.util.Map;
+
+/**
+ * This interface is used to handle the management of characters that should trigger the creation of chips in a text view.
+ *
+ * @see ChipTokenizer
+ */
+public interface ChipTerminatorHandler {
+
+ /**
+ * When a chip terminator character is encountered in newly inserted text, all tokens in the whole text view will be chipified
+ */
+ int BEHAVIOR_CHIPIFY_ALL = 0;
+
+ /**
+ * When a chip terminator character is encountered in newly inserted text, only the current token (that in which the chip terminator character
+ * was found) will be chipified. This token may extend beyond where the chip terminator character was located.
+ */
+ int BEHAVIOR_CHIPIFY_CURRENT_TOKEN = 1;
+
+ /**
+ * When a chip terminator character is encountered in newly inserted text, only the text from the previous chip up until the chip terminator
+ * character will be chipified. This may not be an entire token.
+ */
+ int BEHAVIOR_CHIPIFY_TO_TERMINATOR = 2;
+
+ /**
+ * Constant for use with {@link #setPasteBehavior(int)}. Use this if a paste should behave the same as a standard text input (the chip temrinators
+ * will all behave according to their pre-determined behavior set through {@link #addChipTerminator(char, int)} or {@link #setChipTerminators(Map)}).
+ */
+ int PASTE_BEHAVIOR_USE_DEFAULT = -1;
+
+ /**
+ * Sets all the characters that will be marked as chip terminators. This will replace any previously set chip terminators.
+ *
+ * @param chipTerminators a map of characters to be marked as chip terminators to behaviors that describe how to respond to the characters, or null
+ * to remove all chip terminators
+ */
+ void setChipTerminators(@Nullable Map chipTerminators);
+
+ /**
+ * Adds a character as a chip terminator. When the provided character is encountered in entered text, the nearby text will be chipified according
+ * to the behavior provided here.
+ * {@code behavior} Must be one of:
+ *
+ *
{@link #BEHAVIOR_CHIPIFY_ALL}
+ *
{@link #BEHAVIOR_CHIPIFY_CURRENT_TOKEN}
+ *
{@link #BEHAVIOR_CHIPIFY_TO_TERMINATOR}
+ *
+ *
+ * @param character the character to mark as a chip terminator
+ * @param behavior the behavior describing how to respond to the chip terminator
+ */
+ void addChipTerminator(char character, int behavior);
+
+ /**
+ * Customizes the way paste events are handled.
+ * If one of:
+ *
+ *
{@link #BEHAVIOR_CHIPIFY_ALL}
+ *
{@link #BEHAVIOR_CHIPIFY_CURRENT_TOKEN}
+ *
{@link #BEHAVIOR_CHIPIFY_TO_TERMINATOR}
+ *
+ * is passed, all chip terminators will be handled with that behavior when a paste event occurs.
+ * If {@link #PASTE_BEHAVIOR_USE_DEFAULT} is passed, whatever behavior is configured for a particular chip terminator
+ * (through {@link #setChipTerminators(Map)} or {@link #addChipTerminator(char, int)} will be used for that chip terminator
+ *
+ * @param pasteBehavior the behavior to use on a paste event
+ */
+ void setPasteBehavior(int pasteBehavior);
+
+ /**
+ * Parses the provided text looking for characters marked as chip terminators through {@link #addChipTerminator(char, int)} and {@link #setChipTerminators(Map)}.
+ * The provided {@link Editable} will be modified if chip terminators are encountered.
+ *
+ * @param tokenizer the {@link ChipTokenizer} to use to identify and chipify tokens in the text
+ * @param text the text in which to search for chip terminators tokens to be chipped
+ * @param start the index at which to begin looking for chip terminators (inclusive)
+ * @param end the index at which to end looking for chip terminators (exclusive)
+ * @param isPasteEvent true if this handling is for a paste event in which case the behavior set in {@link #setPasteBehavior(int)} will be used,
+ * otherwise false
+ * @return an non-negative integer indicating the index where the cursor (selection) should be placed once the handling is complete,
+ * or a negative integer indicating that the cursor should not be moved.
+ */
+ int findAndHandleChipTerminators(@NonNull ChipTokenizer tokenizer, @NonNull Editable text, int start, int end, boolean isPasteEvent);
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/terminator/DefaultChipTerminatorHandler.java b/mastodon/src/main/java/com/hootsuite/nachos/terminator/DefaultChipTerminatorHandler.java
new file mode 100644
index 000000000..75cb9b0dc
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/terminator/DefaultChipTerminatorHandler.java
@@ -0,0 +1,115 @@
+package com.hootsuite.nachos.terminator;
+
+import android.text.Editable;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.hootsuite.nachos.tokenizer.ChipTokenizer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class DefaultChipTerminatorHandler implements ChipTerminatorHandler {
+
+ @Nullable
+ private Map mChipTerminators;
+ private int mPasteBehavior = BEHAVIOR_CHIPIFY_TO_TERMINATOR;
+
+ @Override
+ public void setChipTerminators(@Nullable Map chipTerminators) {
+ mChipTerminators = chipTerminators;
+ }
+
+ @Override
+ public void addChipTerminator(char character, int behavior) {
+ if (mChipTerminators == null) {
+ mChipTerminators = new HashMap<>();
+ }
+
+ mChipTerminators.put(character, behavior);
+ }
+
+ @Override
+ public void setPasteBehavior(int pasteBehavior) {
+ mPasteBehavior = pasteBehavior;
+ }
+
+ @Override
+ public int findAndHandleChipTerminators(@NonNull ChipTokenizer tokenizer, @NonNull Editable text, int start, int end, boolean isPasteEvent) {
+ // If we don't have a tokenizer or any chip terminators, there's nothing to look for
+ if (mChipTerminators == null) {
+ return -1;
+ }
+
+ TextIterator textIterator = new TextIterator(text, start, end);
+ int selectionIndex = -1;
+
+ characterLoop:
+ while (textIterator.hasNextCharacter()) {
+ char theChar = textIterator.nextCharacter();
+ if (isChipTerminator(theChar)) {
+ int behavior = (isPasteEvent && mPasteBehavior != PASTE_BEHAVIOR_USE_DEFAULT) ? mPasteBehavior : mChipTerminators.get(theChar);
+ int newSelection = -1;
+ switch (behavior) {
+ case BEHAVIOR_CHIPIFY_ALL:
+ selectionIndex = handleChipifyAll(textIterator, tokenizer);
+ break characterLoop;
+ case BEHAVIOR_CHIPIFY_CURRENT_TOKEN:
+ newSelection = handleChipifyCurrentToken(textIterator, tokenizer);
+ break;
+ case BEHAVIOR_CHIPIFY_TO_TERMINATOR:
+ newSelection = handleChipifyToTerminator(textIterator, tokenizer);
+ break;
+ }
+
+ if (newSelection != -1) {
+ selectionIndex = newSelection;
+ }
+ }
+ }
+
+ return selectionIndex;
+ }
+
+ private int handleChipifyAll(TextIterator textIterator, ChipTokenizer tokenizer) {
+ textIterator.deleteCharacter(true);
+ tokenizer.terminateAllTokens(textIterator.getText());
+ return textIterator.totalLength();
+ }
+
+ private int handleChipifyCurrentToken(TextIterator textIterator, ChipTokenizer tokenizer) {
+ textIterator.deleteCharacter(true);
+ Editable text = textIterator.getText();
+ int index = textIterator.getIndex();
+ int tokenStart = tokenizer.findTokenStart(text, index);
+ int tokenEnd = tokenizer.findTokenEnd(text, index);
+ if (tokenStart < tokenEnd) {
+ CharSequence chippedText = tokenizer.terminateToken(text.subSequence(tokenStart, tokenEnd), null);
+ textIterator.replace(tokenStart, tokenEnd, chippedText);
+ return tokenStart + chippedText.length();
+ }
+ return -1;
+ }
+
+ private int handleChipifyToTerminator(TextIterator textIterator, ChipTokenizer tokenizer) {
+ Editable text = textIterator.getText();
+ int index = textIterator.getIndex();
+ if (index > 0) {
+ int tokenStart = tokenizer.findTokenStart(text, index);
+ if (tokenStart < index) {
+ CharSequence chippedText = tokenizer.terminateToken(text.subSequence(tokenStart, index), null);
+ textIterator.replace(tokenStart, index + 1, chippedText);
+ } else {
+ textIterator.deleteCharacter(false);
+ }
+ } else {
+ textIterator.deleteCharacter(false);
+ }
+ return -1;
+ }
+
+ private boolean isChipTerminator(char character) {
+ return mChipTerminators != null && mChipTerminators.keySet().contains(character);
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/terminator/TextIterator.java b/mastodon/src/main/java/com/hootsuite/nachos/terminator/TextIterator.java
new file mode 100644
index 000000000..7c25d4c9f
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/terminator/TextIterator.java
@@ -0,0 +1,63 @@
+package com.hootsuite.nachos.terminator;
+
+import android.text.Editable;
+
+public class TextIterator {
+
+ private Editable mText;
+ private int mStart;
+ private int mEnd;
+
+ private int mIndex;
+
+ public TextIterator(Editable text, int start, int end) {
+ mText = text;
+ mStart = start;
+ mEnd = end;
+
+ mIndex = mStart - 1; // Subtract 1 so that the first call to nextCharacter() will return the first character
+ }
+
+ public int totalLength() {
+ return mText.length();
+ }
+
+ public int windowLength() {
+ return mEnd - mStart;
+ }
+
+ public Editable getText() {
+ return mText;
+ }
+
+ public int getIndex() {
+ return mIndex;
+ }
+
+ public boolean hasNextCharacter() {
+ return (mIndex + 1) < mEnd;
+ }
+
+ public char nextCharacter() {
+ mIndex++;
+ return mText.charAt(mIndex);
+ }
+
+ public void deleteCharacter(boolean maintainIndex) {
+ mText.replace(mIndex, mIndex + 1, "");
+ if (!maintainIndex) {
+ mIndex--;
+ }
+ mEnd--;
+ }
+
+ public void replace(int replaceStart, int replaceEnd, CharSequence chippedText) {
+ mText.replace(replaceStart, replaceEnd, chippedText);
+
+ // Update indexes
+ int newLength = chippedText.length();
+ int oldLength = replaceEnd - replaceStart;
+ mIndex = replaceStart + newLength - 1;
+ mEnd += newLength - oldLength;
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/BaseChipTokenizer.java b/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/BaseChipTokenizer.java
new file mode 100644
index 000000000..ba946b5b0
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/BaseChipTokenizer.java
@@ -0,0 +1,89 @@
+package com.hootsuite.nachos.tokenizer;
+
+import android.text.Editable;
+import android.text.Spanned;
+import android.util.Pair;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.hootsuite.nachos.ChipConfiguration;
+import com.hootsuite.nachos.chip.Chip;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Base implementation of the {@link ChipTokenizer} interface that performs no actions and returns default values.
+ * This class allows for the easy creation of a ChipTokenizer that only implements some of the methods of the interface.
+ */
+public abstract class BaseChipTokenizer implements ChipTokenizer {
+
+ @Override
+ public void applyConfiguration(Editable text, ChipConfiguration chipConfiguration) {
+ // Do nothing
+ }
+
+ @Override
+ public int findTokenStart(CharSequence charSequence, int i) {
+ // Do nothing
+ return 0;
+ }
+
+ @Override
+ public int findTokenEnd(CharSequence charSequence, int i) {
+ // Do nothing
+ return 0;
+ }
+
+ @NonNull
+ @Override
+ public List> findAllTokens(CharSequence text) {
+ // Do nothing
+ return new ArrayList<>();
+ }
+
+ @Override
+ public CharSequence terminateToken(CharSequence charSequence, @Nullable Object data) {
+ // Do nothing
+ return charSequence;
+ }
+
+ @Override
+ public void terminateAllTokens(Editable text) {
+ // Do nothing
+ }
+
+ @Override
+ public int findChipStart(Chip chip, Spanned text) {
+ // Do nothing
+ return 0;
+ }
+
+ @Override
+ public int findChipEnd(Chip chip, Spanned text) {
+ // Do nothing
+ return 0;
+ }
+
+ @NonNull
+ @Override
+ public Chip[] findAllChips(int start, int end, Spanned text) {
+ return new Chip[]{};
+ }
+
+ @Override
+ public void revertChipToToken(Chip chip, Editable text) {
+ // Do nothing
+ }
+
+ @Override
+ public void deleteChip(Chip chip, Editable text) {
+ // Do nothing
+ }
+
+ @Override
+ public void deleteChipAndPadding(Chip chip, Editable text) {
+ // Do nothing
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/ChipTokenizer.java b/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/ChipTokenizer.java
new file mode 100644
index 000000000..32d8f0614
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/ChipTokenizer.java
@@ -0,0 +1,134 @@
+package com.hootsuite.nachos.tokenizer;
+
+import android.text.Editable;
+import android.text.Spanned;
+import android.util.Pair;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.hootsuite.nachos.ChipConfiguration;
+import com.hootsuite.nachos.chip.Chip;
+
+import java.util.List;
+
+/**
+ * An extension of {@link android.widget.MultiAutoCompleteTextView.Tokenizer Tokenizer} that provides extra support
+ * for chipification.
+ *
+ * In the context of this interface, a token is considered to be plain (non-chipped) text. Once a token is terminated it becomes or contains a chip.
+ *
+ *
+ * The CharSequences passed to the ChipTokenizer methods may contain both chipped text
+ * and plain text so the tokenizer must have some method of distinguishing between the two (e.g. using a delimeter character.
+ * The {@link #terminateToken(CharSequence, Object)} method is where a chip can be formed and returned to replace the plain text.
+ * Whatever class the implementation deems to represent a chip, must implement the {@link Chip} interface.
+ *
+ *
+ * @see SpanChipTokenizer
+ */
+public interface ChipTokenizer {
+
+ /**
+ * Configures this ChipTokenizer to produce chips with the provided attributes. For each of these attributes, {@code -1} or {@code null} may be
+ * passed to indicate that the attribute may be ignored.
+ *
+ * This will also apply the provided {@link ChipConfiguration} to any existing chips in the provided text.
+ *
+ *
+ * @param text the text in which to search for existing chips to apply the configuration to
+ * @param chipConfiguration a {@link ChipConfiguration} containing customizations for the chips produced by this class
+ */
+ void applyConfiguration(Editable text, ChipConfiguration chipConfiguration);
+
+ /**
+ * Returns the start of the token that ends at offset
+ * cursor within text.
+ */
+ int findTokenStart(CharSequence text, int cursor);
+
+ /**
+ * Returns the end of the token (minus trailing punctuation)
+ * that begins at offset cursor within text.
+ */
+ int findTokenEnd(CharSequence text, int cursor);
+
+ /**
+ * Searches through {@code text} for any tokens.
+ *
+ * @param text the text in which to search for un-terminated tokens
+ * @return a list of {@link Pair}s of the form (startIndex, endIndex) containing the locations of all
+ * unterminated tokens
+ */
+ @NonNull
+ List> findAllTokens(CharSequence text);
+
+ /**
+ * Returns text, modified, if necessary, to ensure that
+ * it ends with a token terminator (for example a space or comma).
+ */
+ CharSequence terminateToken(CharSequence text, @Nullable Object data);
+
+ /**
+ * Terminates (converts from token into chip) all unterminated tokens in the provided text.
+ * This method CAN alter the provided text.
+ *
+ * @param text the text in which to terminate all tokens
+ */
+ void terminateAllTokens(Editable text);
+
+ /**
+ * Finds the index of the first character in {@code text} that is a part of {@code chip}
+ *
+ * @param chip the chip whose start should be found
+ * @param text the text in which to search for the start of {@code chip}
+ * @return the start index of the chip
+ */
+ int findChipStart(Chip chip, Spanned text);
+
+ /**
+ * Finds the index of the character after the last character in {@code text} that is a part of {@code chip}
+ *
+ * @param chip the chip whose end should be found
+ * @param text the text in which to search for the end of {@code chip}
+ * @return the end index of the chip
+ */
+ int findChipEnd(Chip chip, Spanned text);
+
+ /**
+ * Searches through {@code text} for any chips
+ *
+ * @param start index to start looking for terminated tokens (inclusive)
+ * @param end index to end looking for terminated tokens (exclusive)
+ * @param text the text in which to search for terminated tokens
+ * @return a list of objects implementing the {@link Chip} interface to represent the terminated tokens
+ */
+ @NonNull
+ Chip[] findAllChips(int start, int end, Spanned text);
+
+ /**
+ * Effectively does the opposite of {@link #terminateToken(CharSequence, Object)} by reverting the provided chip back into a token.
+ * This method CAN alter the provided text.
+ *
+ * @param chip the chip to revert into a token
+ * @param text the text in which the chip resides
+ */
+ void revertChipToToken(Chip chip, Editable text);
+
+ /**
+ * Removes a chip and any text it encompasses from {@code text}. This method CAN alter the provided text.
+ *
+ * @param chip the chip to remove
+ * @param text the text to remove the chip from
+ */
+ void deleteChip(Chip chip, Editable text);
+
+ /**
+ * Removes a chip, any text it encompasses AND any padding text (such as spaces) that may have been inserted when the chip was created in
+ * {@link #terminateToken(CharSequence, Object)} or after. This method CAN alter the provided text.
+ *
+ * @param chip the chip to remove
+ * @param text the text to remove the chip and padding from
+ */
+ void deleteChipAndPadding(Chip chip, Editable text);
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/SpanChipTokenizer.java b/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/SpanChipTokenizer.java
new file mode 100644
index 000000000..511bdb9bb
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/tokenizer/SpanChipTokenizer.java
@@ -0,0 +1,246 @@
+package com.hootsuite.nachos.tokenizer;
+
+import android.content.Context;
+import android.text.Editable;
+import android.text.SpannableString;
+import android.text.Spanned;
+import android.util.Pair;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.hootsuite.nachos.ChipConfiguration;
+import com.hootsuite.nachos.chip.Chip;
+import com.hootsuite.nachos.chip.ChipCreator;
+import com.hootsuite.nachos.chip.ChipSpan;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * A default implementation of {@link ChipTokenizer}.
+ * This implementation does the following:
+ *
+ *
Surrounds each token with a space and the Unit Separator ASCII control character (31) - See the diagram below
+ *
+ *
The spaces are included so that android keyboards can distinguish the chips as different words and provide accurate
+ * autocorrect suggestions
+ *
+ *
+ *
Replaces each token with a {@link ChipSpan} containing the same text, once the token terminates
+ *
Uses the values passed to {@link #applyConfiguration(Editable, ChipConfiguration)} to configure any ChipSpans that get created
+ *
+ * Each terminated token will therefore look like the following (this is what will be returned from {@link #terminateToken(CharSequence, Object)}):
+ *
+ *
+ * @see ChipSpan
+ */
+public class SpanChipTokenizer implements ChipTokenizer {
+
+ /**
+ * The character used to separate chips internally is the US (Unit Separator) ASCII control character.
+ * This character is used because it's untypable so we have complete control over when chips are created.
+ */
+ public static final char CHIP_SPAN_SEPARATOR = 31;
+ public static final char AUTOCORRECT_SEPARATOR = ' ';
+
+ private Context mContext;
+
+ @Nullable
+ private ChipConfiguration mChipConfiguration;
+ @NonNull
+ private ChipCreator mChipCreator;
+ @NonNull
+ private Class mChipClass;
+
+ private Comparator> mReverseTokenIndexesSorter = new Comparator>() {
+ @Override
+ public int compare(Pair lhs, Pair rhs) {
+ return rhs.first - lhs.first;
+ }
+ };
+
+ public SpanChipTokenizer(Context context, @NonNull ChipCreator chipCreator, @NonNull Class chipClass) {
+ mContext = context;
+ mChipCreator = chipCreator;
+ mChipClass = chipClass;
+ }
+
+ @Override
+ public void applyConfiguration(Editable text, ChipConfiguration chipConfiguration) {
+ mChipConfiguration = chipConfiguration;
+
+ for (C chip : findAllChips(0, text.length(), text)) {
+ // Recreate the chips with the new configuration
+ int chipStart = findChipStart(chip, text);
+ deleteChip(chip, text);
+ text.insert(chipStart, terminateToken(mChipCreator.createChip(mContext, chip)));
+ }
+ }
+
+ @Override
+ public int findTokenStart(CharSequence text, int cursor) {
+ int i = cursor;
+
+ // Work backwards until we find a CHIP_SPAN_SEPARATOR
+ while (i > 0 && text.charAt(i - 1) != CHIP_SPAN_SEPARATOR) {
+ i--;
+ }
+ // Work forwards to skip over any extra whitespace at the beginning of the token
+ while (i > 0 && i < text.length() && Character.isWhitespace(text.charAt(i))) {
+ i++;
+ }
+ return i;
+ }
+
+ @Override
+ public int findTokenEnd(CharSequence text, int cursor) {
+ int i = cursor;
+ int len = text.length();
+
+ // Work forwards till we find a CHIP_SPAN_SEPARATOR
+ while (i < len) {
+ if (text.charAt(i) == CHIP_SPAN_SEPARATOR) {
+ return (i - 1); // subtract one because the CHIP_SPAN_SEPARATOR will be preceded by a space
+ } else {
+ i++;
+ }
+ }
+ return len;
+ }
+
+ @NonNull
+ @Override
+ public List> findAllTokens(CharSequence text) {
+ List> unterminatedTokens = new ArrayList<>();
+
+ boolean insideChip = false;
+ // Iterate backwards through the text (to avoid messing up indexes)
+ for (int index = text.length() - 1; index >= 0; index--) {
+ char theCharacter = text.charAt(index);
+
+ // Every time we hit a CHIP_SPAN_SEPARATOR character we switch from being inside to outside
+ // or outside to inside a chip
+ // This check must happen before the whitespace check because CHIP_SPAN_SEPARATOR is considered a whitespace character
+ if (theCharacter == CHIP_SPAN_SEPARATOR) {
+ insideChip = !insideChip;
+ continue;
+ }
+
+ // Completely skip over whitespace
+ if (Character.isWhitespace(theCharacter)) {
+ continue;
+ }
+
+ // If we're ever outside a chip, see if the text we're in is a viable token for chipification
+ if (!insideChip) {
+ int tokenStart = findTokenStart(text, index);
+ int tokenEnd = findTokenEnd(text, index);
+
+ // Can only actually be chipified if there's at least one character between them
+ if (tokenEnd - tokenStart >= 1) {
+ unterminatedTokens.add(new Pair<>(tokenStart, tokenEnd));
+ index = tokenStart;
+ }
+ }
+ }
+ return unterminatedTokens;
+ }
+
+ @Override
+ public CharSequence terminateToken(CharSequence text, @Nullable Object data) {
+ // Remove leading/trailing whitespace
+ CharSequence trimmedText = text.toString().trim();
+ return terminateToken(mChipCreator.createChip(mContext, trimmedText, data));
+ }
+
+ private CharSequence terminateToken(C chip) {
+ // Surround the text with CHIP_SPAN_SEPARATOR and spaces
+ // The spaces allow autocorrect to correctly identify words
+ String chipSeparator = Character.toString(CHIP_SPAN_SEPARATOR);
+ String autoCorrectSeparator = Character.toString(AUTOCORRECT_SEPARATOR);
+ CharSequence textWithSeparator = autoCorrectSeparator + chipSeparator + chip.getText() + chipSeparator + autoCorrectSeparator;
+
+ // Build the container object to house the ChipSpan and space
+ SpannableString spannableString = new SpannableString(textWithSeparator);
+
+ // Attach the ChipSpan
+ if (mChipConfiguration != null) {
+ mChipCreator.configureChip(chip, mChipConfiguration);
+ }
+ spannableString.setSpan(chip, 0, textWithSeparator.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+
+ return spannableString;
+ }
+
+ @Override
+ public void terminateAllTokens(Editable text) {
+ List> unterminatedTokens = findAllTokens(text);
+ // Sort in reverse order (so index changes don't affect anything)
+ Collections.sort(unterminatedTokens, mReverseTokenIndexesSorter);
+ for (Pair indexes : unterminatedTokens) {
+ int start = indexes.first;
+ int end = indexes.second;
+ CharSequence textToChip = text.subSequence(start, end);
+ CharSequence chippedText = terminateToken(textToChip, null);
+ text.replace(start, end, chippedText);
+ }
+ }
+
+ @Override
+ public int findChipStart(Chip chip, Spanned text) {
+ return text.getSpanStart(chip);
+ }
+
+ @Override
+ public int findChipEnd(Chip chip, Spanned text) {
+ return text.getSpanEnd(chip);
+ }
+
+ @SuppressWarnings("unchecked")
+ @NonNull
+ @Override
+ public C[] findAllChips(int start, int end, Spanned text) {
+ C[] spansArray = text.getSpans(start, end, mChipClass);
+ return (spansArray != null) ? spansArray : (C[]) Array.newInstance(mChipClass, 0);
+ }
+
+ @Override
+ public void revertChipToToken(Chip chip, Editable text) {
+ int chipStart = findChipStart(chip, text);
+ int chipEnd = findChipEnd(chip, text);
+ text.removeSpan(chip);
+ text.replace(chipStart, chipEnd, chip.getText());
+ }
+
+ @Override
+ public void deleteChip(Chip chip, Editable text) {
+ int chipStart = findChipStart(chip, text);
+ int chipEnd = findChipEnd(chip, text);
+ text.removeSpan(chip);
+ // On the emulator for some reason the text automatically gets deleted and chipStart and chipEnd end up both being -1, so in that case we
+ // don't need to call text.delete(...)
+ if (chipStart != chipEnd) {
+ text.delete(chipStart, chipEnd);
+ }
+ }
+
+ @Override
+ public void deleteChipAndPadding(Chip chip, Editable text) {
+ // This implementation does not add any extra padding outside of the span so we can just delete the chip normally
+ deleteChip(chip, text);
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/validator/ChipifyingNachoValidator.java b/mastodon/src/main/java/com/hootsuite/nachos/validator/ChipifyingNachoValidator.java
new file mode 100644
index 000000000..fa1aa5717
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/validator/ChipifyingNachoValidator.java
@@ -0,0 +1,32 @@
+package com.hootsuite.nachos.validator;
+
+import android.text.SpannableStringBuilder;
+import android.util.Pair;
+
+import androidx.annotation.NonNull;
+
+import com.hootsuite.nachos.tokenizer.ChipTokenizer;
+
+import java.util.List;
+
+/**
+ * A {@link NachoValidator} that deems text to be invalid if it contains
+ * unterminated tokens and fixes the text by chipifying all the unterminated tokens.
+ */
+public class ChipifyingNachoValidator implements NachoValidator {
+
+ @Override
+ public boolean isValid(@NonNull ChipTokenizer chipTokenizer, CharSequence text) {
+
+ // The text is considered valid if there are no unterminated tokens (everything is a chip)
+ List> unterminatedTokens = chipTokenizer.findAllTokens(text);
+ return unterminatedTokens.isEmpty();
+ }
+
+ @Override
+ public CharSequence fixText(@NonNull ChipTokenizer chipTokenizer, CharSequence invalidText) {
+ SpannableStringBuilder newText = new SpannableStringBuilder(invalidText);
+ chipTokenizer.terminateAllTokens(newText);
+ return newText;
+ }
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/validator/IllegalCharacterIdentifier.java b/mastodon/src/main/java/com/hootsuite/nachos/validator/IllegalCharacterIdentifier.java
new file mode 100644
index 000000000..76f3f858f
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/validator/IllegalCharacterIdentifier.java
@@ -0,0 +1,5 @@
+package com.hootsuite.nachos.validator;
+
+public interface IllegalCharacterIdentifier {
+ boolean isCharacterIllegal(Character c);
+}
diff --git a/mastodon/src/main/java/com/hootsuite/nachos/validator/NachoValidator.java b/mastodon/src/main/java/com/hootsuite/nachos/validator/NachoValidator.java
new file mode 100644
index 000000000..70438c155
--- /dev/null
+++ b/mastodon/src/main/java/com/hootsuite/nachos/validator/NachoValidator.java
@@ -0,0 +1,29 @@
+package com.hootsuite.nachos.validator;
+
+import androidx.annotation.NonNull;
+
+import com.hootsuite.nachos.tokenizer.ChipTokenizer;
+
+/**
+ * Interface used to ensure that a given CharSequence complies to a particular format.
+ */
+public interface NachoValidator {
+
+ /**
+ * Validates the specified text.
+ *
+ * @return true If the text currently in the text editor is valid.
+ * @see #fixText(ChipTokenizer, CharSequence)
+ */
+ boolean isValid(@NonNull ChipTokenizer chipTokenizer, CharSequence text);
+
+ /**
+ * Corrects the specified text to make it valid.
+ *
+ * @param invalidText A string that doesn't pass validation: isValid(invalidText)
+ * returns false
+ * @return A string based on invalidText such as invoking isValid() on it returns true.
+ * @see #isValid(ChipTokenizer, CharSequence)
+ */
+ CharSequence fixText(@NonNull ChipTokenizer chipTokenizer, CharSequence invalidText);
+}
diff --git a/mastodon/src/main/java/name/fraser/neil/plaintext/LICENSE b/mastodon/src/main/java/name/fraser/neil/plaintext/LICENSE
new file mode 100644
index 000000000..d64569567
--- /dev/null
+++ b/mastodon/src/main/java/name/fraser/neil/plaintext/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/mastodon/src/main/java/name/fraser/neil/plaintext/diff_match_patch.java b/mastodon/src/main/java/name/fraser/neil/plaintext/diff_match_patch.java
new file mode 100644
index 000000000..9d07867de
--- /dev/null
+++ b/mastodon/src/main/java/name/fraser/neil/plaintext/diff_match_patch.java
@@ -0,0 +1,2471 @@
+/*
+ * Diff Match and Patch
+ * Copyright 2018 The diff-match-patch Authors.
+ * https://github.com/google/diff-match-patch
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package name.fraser.neil.plaintext;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/*
+ * Functions for diff, match and patch.
+ * Computes the difference between two texts to create a patch.
+ * Applies the patch onto another text, allowing for errors.
+ *
+ * @author fraser@google.com (Neil Fraser)
+ */
+
+/**
+ * Class containing the diff, match and patch methods.
+ * Also contains the behaviour settings.
+ */
+public class diff_match_patch {
+
+ // Defaults.
+ // Set these on your diff_match_patch instance to override the defaults.
+
+ /**
+ * Number of seconds to map a diff before giving up (0 for infinity).
+ */
+ public float Diff_Timeout = 1.0f;
+ /**
+ * Cost of an empty edit operation in terms of edit characters.
+ */
+ public short Diff_EditCost = 4;
+ /**
+ * At what point is no match declared (0.0 = perfection, 1.0 = very loose).
+ */
+ public float Match_Threshold = 0.5f;
+ /**
+ * How far to search for a match (0 = exact location, 1000+ = broad match).
+ * A match this many characters away from the expected location will add
+ * 1.0 to the score (0.0 is a perfect match).
+ */
+ public int Match_Distance = 1000;
+ /**
+ * When deleting a large block of text (over ~64 characters), how close do
+ * the contents have to be to match the expected contents. (0.0 = perfection,
+ * 1.0 = very loose). Note that Match_Threshold controls how closely the
+ * end points of a delete need to match.
+ */
+ public float Patch_DeleteThreshold = 0.5f;
+ /**
+ * Chunk size for context length.
+ */
+ public short Patch_Margin = 4;
+
+ /**
+ * The number of bits in an int.
+ */
+ private short Match_MaxBits = 32;
+
+ /**
+ * Internal class for returning results from diff_linesToChars().
+ * Other less paranoid languages just use a three-element array.
+ */
+ protected static class LinesToCharsResult {
+ protected String chars1;
+ protected String chars2;
+ protected List lineArray;
+
+ protected LinesToCharsResult(String chars1, String chars2,
+ List lineArray) {
+ this.chars1 = chars1;
+ this.chars2 = chars2;
+ this.lineArray = lineArray;
+ }
+ }
+
+
+ // DIFF FUNCTIONS
+
+
+ /**
+ * The data structure representing a diff is a Linked list of Diff objects:
+ * {Diff(Operation.DELETE, "Hello"), Diff(Operation.INSERT, "Goodbye"),
+ * Diff(Operation.EQUAL, " world.")}
+ * which means: delete "Hello", add "Goodbye" and keep " world."
+ */
+ public enum Operation {
+ DELETE, INSERT, EQUAL
+ }
+
+ /**
+ * Find the differences between two texts.
+ * Run a faster, slightly less optimal diff.
+ * This method allows the 'checklines' of diff_main() to be optional.
+ * Most of the time checklines is wanted, so default to true.
+ * @param text1 Old string to be diffed.
+ * @param text2 New string to be diffed.
+ * @return Linked List of Diff objects.
+ */
+ public LinkedList diff_main(String text1, String text2) {
+ return diff_main(text1, text2, true);
+ }
+
+ /**
+ * Find the differences between two texts.
+ * @param text1 Old string to be diffed.
+ * @param text2 New string to be diffed.
+ * @param checklines Speedup flag. If false, then don't run a
+ * line-level diff first to identify the changed areas.
+ * If true, then run a faster slightly less optimal diff.
+ * @return Linked List of Diff objects.
+ */
+ public LinkedList diff_main(String text1, String text2,
+ boolean checklines) {
+ // Set a deadline by which time the diff must be complete.
+ long deadline;
+ if (Diff_Timeout <= 0) {
+ deadline = Long.MAX_VALUE;
+ } else {
+ deadline = System.currentTimeMillis() + (long) (Diff_Timeout * 1000);
+ }
+ return diff_main(text1, text2, checklines, deadline);
+ }
+
+ /**
+ * Find the differences between two texts. Simplifies the problem by
+ * stripping any common prefix or suffix off the texts before diffing.
+ * @param text1 Old string to be diffed.
+ * @param text2 New string to be diffed.
+ * @param checklines Speedup flag. If false, then don't run a
+ * line-level diff first to identify the changed areas.
+ * If true, then run a faster slightly less optimal diff.
+ * @param deadline Time when the diff should be complete by. Used
+ * internally for recursive calls. Users should set DiffTimeout instead.
+ * @return Linked List of Diff objects.
+ */
+ private LinkedList diff_main(String text1, String text2,
+ boolean checklines, long deadline) {
+ // Check for null inputs.
+ if (text1 == null || text2 == null) {
+ throw new IllegalArgumentException("Null inputs. (diff_main)");
+ }
+
+ // Check for equality (speedup).
+ LinkedList diffs;
+ if (text1.equals(text2)) {
+ diffs = new LinkedList();
+ if (text1.length() != 0) {
+ diffs.add(new Diff(Operation.EQUAL, text1));
+ }
+ return diffs;
+ }
+
+ // Trim off common prefix (speedup).
+ int commonlength = diff_commonPrefix(text1, text2);
+ String commonprefix = text1.substring(0, commonlength);
+ text1 = text1.substring(commonlength);
+ text2 = text2.substring(commonlength);
+
+ // Trim off common suffix (speedup).
+ commonlength = diff_commonSuffix(text1, text2);
+ String commonsuffix = text1.substring(text1.length() - commonlength);
+ text1 = text1.substring(0, text1.length() - commonlength);
+ text2 = text2.substring(0, text2.length() - commonlength);
+
+ // Compute the diff on the middle block.
+ diffs = diff_compute(text1, text2, checklines, deadline);
+
+ // Restore the prefix and suffix.
+ if (commonprefix.length() != 0) {
+ diffs.addFirst(new Diff(Operation.EQUAL, commonprefix));
+ }
+ if (commonsuffix.length() != 0) {
+ diffs.addLast(new Diff(Operation.EQUAL, commonsuffix));
+ }
+
+ diff_cleanupMerge(diffs);
+ return diffs;
+ }
+
+ /**
+ * Find the differences between two texts. Assumes that the texts do not
+ * have any common prefix or suffix.
+ * @param text1 Old string to be diffed.
+ * @param text2 New string to be diffed.
+ * @param checklines Speedup flag. If false, then don't run a
+ * line-level diff first to identify the changed areas.
+ * If true, then run a faster slightly less optimal diff.
+ * @param deadline Time when the diff should be complete by.
+ * @return Linked List of Diff objects.
+ */
+ private LinkedList diff_compute(String text1, String text2,
+ boolean checklines, long deadline) {
+ LinkedList diffs = new LinkedList();
+
+ if (text1.length() == 0) {
+ // Just add some text (speedup).
+ diffs.add(new Diff(Operation.INSERT, text2));
+ return diffs;
+ }
+
+ if (text2.length() == 0) {
+ // Just delete some text (speedup).
+ diffs.add(new Diff(Operation.DELETE, text1));
+ return diffs;
+ }
+
+ String longtext = text1.length() > text2.length() ? text1 : text2;
+ String shorttext = text1.length() > text2.length() ? text2 : text1;
+ int i = longtext.indexOf(shorttext);
+ if (i != -1) {
+ // Shorter text is inside the longer text (speedup).
+ Operation op = (text1.length() > text2.length()) ?
+ Operation.DELETE : Operation.INSERT;
+ diffs.add(new Diff(op, longtext.substring(0, i)));
+ diffs.add(new Diff(Operation.EQUAL, shorttext));
+ diffs.add(new Diff(op, longtext.substring(i + shorttext.length())));
+ return diffs;
+ }
+
+ if (shorttext.length() == 1) {
+ // Single character string.
+ // After the previous speedup, the character can't be an equality.
+ diffs.add(new Diff(Operation.DELETE, text1));
+ diffs.add(new Diff(Operation.INSERT, text2));
+ return diffs;
+ }
+
+ // Check to see if the problem can be split in two.
+ String[] hm = diff_halfMatch(text1, text2);
+ if (hm != null) {
+ // A half-match was found, sort out the return data.
+ String text1_a = hm[0];
+ String text1_b = hm[1];
+ String text2_a = hm[2];
+ String text2_b = hm[3];
+ String mid_common = hm[4];
+ // Send both pairs off for separate processing.
+ LinkedList diffs_a = diff_main(text1_a, text2_a,
+ checklines, deadline);
+ LinkedList diffs_b = diff_main(text1_b, text2_b,
+ checklines, deadline);
+ // Merge the results.
+ diffs = diffs_a;
+ diffs.add(new Diff(Operation.EQUAL, mid_common));
+ diffs.addAll(diffs_b);
+ return diffs;
+ }
+
+ if (checklines && text1.length() > 100 && text2.length() > 100) {
+ return diff_lineMode(text1, text2, deadline);
+ }
+
+ return diff_bisect(text1, text2, deadline);
+ }
+
+ /**
+ * Do a quick line-level diff on both strings, then rediff the parts for
+ * greater accuracy.
+ * This speedup can produce non-minimal diffs.
+ * @param text1 Old string to be diffed.
+ * @param text2 New string to be diffed.
+ * @param deadline Time when the diff should be complete by.
+ * @return Linked List of Diff objects.
+ */
+ private LinkedList diff_lineMode(String text1, String text2,
+ long deadline) {
+ // Scan the text on a line-by-line basis first.
+ LinesToCharsResult a = diff_linesToChars(text1, text2);
+ text1 = a.chars1;
+ text2 = a.chars2;
+ List linearray = a.lineArray;
+
+ LinkedList diffs = diff_main(text1, text2, false, deadline);
+
+ // Convert the diff back to original text.
+ diff_charsToLines(diffs, linearray);
+ // Eliminate freak matches (e.g. blank lines)
+ diff_cleanupSemantic(diffs);
+
+ // Rediff any replacement blocks, this time character-by-character.
+ // Add a dummy entry at the end.
+ diffs.add(new Diff(Operation.EQUAL, ""));
+ int count_delete = 0;
+ int count_insert = 0;
+ String text_delete = "";
+ String text_insert = "";
+ ListIterator pointer = diffs.listIterator();
+ Diff thisDiff = pointer.next();
+ while (thisDiff != null) {
+ switch (thisDiff.operation) {
+ case INSERT:
+ count_insert++;
+ text_insert += thisDiff.text;
+ break;
+ case DELETE:
+ count_delete++;
+ text_delete += thisDiff.text;
+ break;
+ case EQUAL:
+ // Upon reaching an equality, check for prior redundancies.
+ if (count_delete >= 1 && count_insert >= 1) {
+ // Delete the offending records and add the merged ones.
+ pointer.previous();
+ for (int j = 0; j < count_delete + count_insert; j++) {
+ pointer.previous();
+ pointer.remove();
+ }
+ for (Diff subDiff : diff_main(text_delete, text_insert, false,
+ deadline)) {
+ pointer.add(subDiff);
+ }
+ }
+ count_insert = 0;
+ count_delete = 0;
+ text_delete = "";
+ text_insert = "";
+ break;
+ }
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+ diffs.removeLast(); // Remove the dummy entry at the end.
+
+ return diffs;
+ }
+
+ /**
+ * Find the 'middle snake' of a diff, split the problem in two
+ * and return the recursively constructed diff.
+ * See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
+ * @param text1 Old string to be diffed.
+ * @param text2 New string to be diffed.
+ * @param deadline Time at which to bail if not yet complete.
+ * @return LinkedList of Diff objects.
+ */
+ protected LinkedList diff_bisect(String text1, String text2,
+ long deadline) {
+ // Cache the text lengths to prevent multiple calls.
+ int text1_length = text1.length();
+ int text2_length = text2.length();
+ int max_d = (text1_length + text2_length + 1) / 2;
+ int v_offset = max_d;
+ int v_length = 2 * max_d;
+ int[] v1 = new int[v_length];
+ int[] v2 = new int[v_length];
+ for (int x = 0; x < v_length; x++) {
+ v1[x] = -1;
+ v2[x] = -1;
+ }
+ v1[v_offset + 1] = 0;
+ v2[v_offset + 1] = 0;
+ int delta = text1_length - text2_length;
+ // If the total number of characters is odd, then the front path will
+ // collide with the reverse path.
+ boolean front = (delta % 2 != 0);
+ // Offsets for start and end of k loop.
+ // Prevents mapping of space beyond the grid.
+ int k1start = 0;
+ int k1end = 0;
+ int k2start = 0;
+ int k2end = 0;
+ for (int d = 0; d < max_d; d++) {
+ // Bail out if deadline is reached.
+ if (System.currentTimeMillis() > deadline) {
+ break;
+ }
+
+ // Walk the front path one step.
+ for (int k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
+ int k1_offset = v_offset + k1;
+ int x1;
+ if (k1 == -d || (k1 != d && v1[k1_offset - 1] < v1[k1_offset + 1])) {
+ x1 = v1[k1_offset + 1];
+ } else {
+ x1 = v1[k1_offset - 1] + 1;
+ }
+ int y1 = x1 - k1;
+ while (x1 < text1_length && y1 < text2_length
+ && text1.charAt(x1) == text2.charAt(y1)) {
+ x1++;
+ y1++;
+ }
+ v1[k1_offset] = x1;
+ if (x1 > text1_length) {
+ // Ran off the right of the graph.
+ k1end += 2;
+ } else if (y1 > text2_length) {
+ // Ran off the bottom of the graph.
+ k1start += 2;
+ } else if (front) {
+ int k2_offset = v_offset + delta - k1;
+ if (k2_offset >= 0 && k2_offset < v_length && v2[k2_offset] != -1) {
+ // Mirror x2 onto top-left coordinate system.
+ int x2 = text1_length - v2[k2_offset];
+ if (x1 >= x2) {
+ // Overlap detected.
+ return diff_bisectSplit(text1, text2, x1, y1, deadline);
+ }
+ }
+ }
+ }
+
+ // Walk the reverse path one step.
+ for (int k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
+ int k2_offset = v_offset + k2;
+ int x2;
+ if (k2 == -d || (k2 != d && v2[k2_offset - 1] < v2[k2_offset + 1])) {
+ x2 = v2[k2_offset + 1];
+ } else {
+ x2 = v2[k2_offset - 1] + 1;
+ }
+ int y2 = x2 - k2;
+ while (x2 < text1_length && y2 < text2_length
+ && text1.charAt(text1_length - x2 - 1)
+ == text2.charAt(text2_length - y2 - 1)) {
+ x2++;
+ y2++;
+ }
+ v2[k2_offset] = x2;
+ if (x2 > text1_length) {
+ // Ran off the left of the graph.
+ k2end += 2;
+ } else if (y2 > text2_length) {
+ // Ran off the top of the graph.
+ k2start += 2;
+ } else if (!front) {
+ int k1_offset = v_offset + delta - k2;
+ if (k1_offset >= 0 && k1_offset < v_length && v1[k1_offset] != -1) {
+ int x1 = v1[k1_offset];
+ int y1 = v_offset + x1 - k1_offset;
+ // Mirror x2 onto top-left coordinate system.
+ x2 = text1_length - x2;
+ if (x1 >= x2) {
+ // Overlap detected.
+ return diff_bisectSplit(text1, text2, x1, y1, deadline);
+ }
+ }
+ }
+ }
+ }
+ // Diff took too long and hit the deadline or
+ // number of diffs equals number of characters, no commonality at all.
+ LinkedList diffs = new LinkedList();
+ diffs.add(new Diff(Operation.DELETE, text1));
+ diffs.add(new Diff(Operation.INSERT, text2));
+ return diffs;
+ }
+
+ /**
+ * Given the location of the 'middle snake', split the diff in two parts
+ * and recurse.
+ * @param text1 Old string to be diffed.
+ * @param text2 New string to be diffed.
+ * @param x Index of split point in text1.
+ * @param y Index of split point in text2.
+ * @param deadline Time at which to bail if not yet complete.
+ * @return LinkedList of Diff objects.
+ */
+ private LinkedList diff_bisectSplit(String text1, String text2,
+ int x, int y, long deadline) {
+ String text1a = text1.substring(0, x);
+ String text2a = text2.substring(0, y);
+ String text1b = text1.substring(x);
+ String text2b = text2.substring(y);
+
+ // Compute both diffs serially.
+ LinkedList diffs = diff_main(text1a, text2a, false, deadline);
+ LinkedList diffsb = diff_main(text1b, text2b, false, deadline);
+
+ diffs.addAll(diffsb);
+ return diffs;
+ }
+
+ /**
+ * Split two texts into a list of strings. Reduce the texts to a string of
+ * hashes where each Unicode character represents one line.
+ * @param text1 First string.
+ * @param text2 Second string.
+ * @return An object containing the encoded text1, the encoded text2 and
+ * the List of unique strings. The zeroth element of the List of
+ * unique strings is intentionally blank.
+ */
+ protected LinesToCharsResult diff_linesToChars(String text1, String text2) {
+ List lineArray = new ArrayList();
+ Map lineHash = new HashMap();
+ // e.g. linearray[4] == "Hello\n"
+ // e.g. linehash.get("Hello\n") == 4
+
+ // "\x00" is a valid character, but various debuggers don't like it.
+ // So we'll insert a junk entry to avoid generating a null character.
+ lineArray.add("");
+
+ // Allocate 2/3rds of the space for text1, the rest for text2.
+ String chars1 = diff_linesToCharsMunge(text1, lineArray, lineHash, 40000);
+ String chars2 = diff_linesToCharsMunge(text2, lineArray, lineHash, 65535);
+ return new LinesToCharsResult(chars1, chars2, lineArray);
+ }
+
+ /**
+ * Split a text into a list of strings. Reduce the texts to a string of
+ * hashes where each Unicode character represents one line.
+ * @param text String to encode.
+ * @param lineArray List of unique strings.
+ * @param lineHash Map of strings to indices.
+ * @param maxLines Maximum length of lineArray.
+ * @return Encoded string.
+ */
+ private String diff_linesToCharsMunge(String text, List lineArray,
+ Map lineHash, int maxLines) {
+ int lineStart = 0;
+ int lineEnd = -1;
+ String line;
+ StringBuilder chars = new StringBuilder();
+ // Walk the text, pulling out a substring for each line.
+ // text.split('\n') would would temporarily double our memory footprint.
+ // Modifying text would create many large strings to garbage collect.
+ while (lineEnd < text.length() - 1) {
+ lineEnd = text.indexOf('\n', lineStart);
+ if (lineEnd == -1) {
+ lineEnd = text.length() - 1;
+ }
+ line = text.substring(lineStart, lineEnd + 1);
+
+ if (lineHash.containsKey(line)) {
+ chars.append(String.valueOf((char) (int) lineHash.get(line)));
+ } else {
+ if (lineArray.size() == maxLines) {
+ // Bail out at 65535 because
+ // String.valueOf((char) 65536).equals(String.valueOf(((char) 0)))
+ line = text.substring(lineStart);
+ lineEnd = text.length();
+ }
+ lineArray.add(line);
+ lineHash.put(line, lineArray.size() - 1);
+ chars.append(String.valueOf((char) (lineArray.size() - 1)));
+ }
+ lineStart = lineEnd + 1;
+ }
+ return chars.toString();
+ }
+
+ /**
+ * Rehydrate the text in a diff from a string of line hashes to real lines of
+ * text.
+ * @param diffs List of Diff objects.
+ * @param lineArray List of unique strings.
+ */
+ protected void diff_charsToLines(List diffs,
+ List lineArray) {
+ StringBuilder text;
+ for (Diff diff : diffs) {
+ text = new StringBuilder();
+ for (int j = 0; j < diff.text.length(); j++) {
+ text.append(lineArray.get(diff.text.charAt(j)));
+ }
+ diff.text = text.toString();
+ }
+ }
+
+ /**
+ * Determine the common prefix of two strings
+ * @param text1 First string.
+ * @param text2 Second string.
+ * @return The number of characters common to the start of each string.
+ */
+ public int diff_commonPrefix(String text1, String text2) {
+ // Performance analysis: https://neil.fraser.name/news/2007/10/09/
+ int n = Math.min(text1.length(), text2.length());
+ for (int i = 0; i < n; i++) {
+ if (text1.charAt(i) != text2.charAt(i)) {
+ return i;
+ }
+ }
+ return n;
+ }
+
+ /**
+ * Determine the common suffix of two strings
+ * @param text1 First string.
+ * @param text2 Second string.
+ * @return The number of characters common to the end of each string.
+ */
+ public int diff_commonSuffix(String text1, String text2) {
+ // Performance analysis: https://neil.fraser.name/news/2007/10/09/
+ int text1_length = text1.length();
+ int text2_length = text2.length();
+ int n = Math.min(text1_length, text2_length);
+ for (int i = 1; i <= n; i++) {
+ if (text1.charAt(text1_length - i) != text2.charAt(text2_length - i)) {
+ return i - 1;
+ }
+ }
+ return n;
+ }
+
+ /**
+ * Determine if the suffix of one string is the prefix of another.
+ * @param text1 First string.
+ * @param text2 Second string.
+ * @return The number of characters common to the end of the first
+ * string and the start of the second string.
+ */
+ protected int diff_commonOverlap(String text1, String text2) {
+ // Cache the text lengths to prevent multiple calls.
+ int text1_length = text1.length();
+ int text2_length = text2.length();
+ // Eliminate the null case.
+ if (text1_length == 0 || text2_length == 0) {
+ return 0;
+ }
+ // Truncate the longer string.
+ if (text1_length > text2_length) {
+ text1 = text1.substring(text1_length - text2_length);
+ } else if (text1_length < text2_length) {
+ text2 = text2.substring(0, text1_length);
+ }
+ int text_length = Math.min(text1_length, text2_length);
+ // Quick check for the worst case.
+ if (text1.equals(text2)) {
+ return text_length;
+ }
+
+ // Start by looking for a single character match
+ // and increase length until no match is found.
+ // Performance analysis: https://neil.fraser.name/news/2010/11/04/
+ int best = 0;
+ int length = 1;
+ while (true) {
+ String pattern = text1.substring(text_length - length);
+ int found = text2.indexOf(pattern);
+ if (found == -1) {
+ return best;
+ }
+ length += found;
+ if (found == 0 || text1.substring(text_length - length).equals(
+ text2.substring(0, length))) {
+ best = length;
+ length++;
+ }
+ }
+ }
+
+ /**
+ * Do the two texts share a substring which is at least half the length of
+ * the longer text?
+ * This speedup can produce non-minimal diffs.
+ * @param text1 First string.
+ * @param text2 Second string.
+ * @return Five element String array, containing the prefix of text1, the
+ * suffix of text1, the prefix of text2, the suffix of text2 and the
+ * common middle. Or null if there was no match.
+ */
+ protected String[] diff_halfMatch(String text1, String text2) {
+ if (Diff_Timeout <= 0) {
+ // Don't risk returning a non-optimal diff if we have unlimited time.
+ return null;
+ }
+ String longtext = text1.length() > text2.length() ? text1 : text2;
+ String shorttext = text1.length() > text2.length() ? text2 : text1;
+ if (longtext.length() < 4 || shorttext.length() * 2 < longtext.length()) {
+ return null; // Pointless.
+ }
+
+ // First check if the second quarter is the seed for a half-match.
+ String[] hm1 = diff_halfMatchI(longtext, shorttext,
+ (longtext.length() + 3) / 4);
+ // Check again based on the third quarter.
+ String[] hm2 = diff_halfMatchI(longtext, shorttext,
+ (longtext.length() + 1) / 2);
+ String[] hm;
+ if (hm1 == null && hm2 == null) {
+ return null;
+ } else if (hm2 == null) {
+ hm = hm1;
+ } else if (hm1 == null) {
+ hm = hm2;
+ } else {
+ // Both matched. Select the longest.
+ hm = hm1[4].length() > hm2[4].length() ? hm1 : hm2;
+ }
+
+ // A half-match was found, sort out the return data.
+ if (text1.length() > text2.length()) {
+ return hm;
+ //return new String[]{hm[0], hm[1], hm[2], hm[3], hm[4]};
+ } else {
+ return new String[]{hm[2], hm[3], hm[0], hm[1], hm[4]};
+ }
+ }
+
+ /**
+ * Does a substring of shorttext exist within longtext such that the
+ * substring is at least half the length of longtext?
+ * @param longtext Longer string.
+ * @param shorttext Shorter string.
+ * @param i Start index of quarter length substring within longtext.
+ * @return Five element String array, containing the prefix of longtext, the
+ * suffix of longtext, the prefix of shorttext, the suffix of shorttext
+ * and the common middle. Or null if there was no match.
+ */
+ private String[] diff_halfMatchI(String longtext, String shorttext, int i) {
+ // Start with a 1/4 length substring at position i as a seed.
+ String seed = longtext.substring(i, i + longtext.length() / 4);
+ int j = -1;
+ String best_common = "";
+ String best_longtext_a = "", best_longtext_b = "";
+ String best_shorttext_a = "", best_shorttext_b = "";
+ while ((j = shorttext.indexOf(seed, j + 1)) != -1) {
+ int prefixLength = diff_commonPrefix(longtext.substring(i),
+ shorttext.substring(j));
+ int suffixLength = diff_commonSuffix(longtext.substring(0, i),
+ shorttext.substring(0, j));
+ if (best_common.length() < suffixLength + prefixLength) {
+ best_common = shorttext.substring(j - suffixLength, j)
+ + shorttext.substring(j, j + prefixLength);
+ best_longtext_a = longtext.substring(0, i - suffixLength);
+ best_longtext_b = longtext.substring(i + prefixLength);
+ best_shorttext_a = shorttext.substring(0, j - suffixLength);
+ best_shorttext_b = shorttext.substring(j + prefixLength);
+ }
+ }
+ if (best_common.length() * 2 >= longtext.length()) {
+ return new String[]{best_longtext_a, best_longtext_b,
+ best_shorttext_a, best_shorttext_b, best_common};
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Reduce the number of edits by eliminating semantically trivial equalities.
+ * @param diffs LinkedList of Diff objects.
+ */
+ public void diff_cleanupSemantic(LinkedList diffs) {
+ if (diffs.isEmpty()) {
+ return;
+ }
+ boolean changes = false;
+ Deque equalities = new ArrayDeque(); // Double-ended queue of qualities.
+ String lastEquality = null; // Always equal to equalities.peek().text
+ ListIterator pointer = diffs.listIterator();
+ // Number of characters that changed prior to the equality.
+ int length_insertions1 = 0;
+ int length_deletions1 = 0;
+ // Number of characters that changed after the equality.
+ int length_insertions2 = 0;
+ int length_deletions2 = 0;
+ Diff thisDiff = pointer.next();
+ while (thisDiff != null) {
+ if (thisDiff.operation == Operation.EQUAL) {
+ // Equality found.
+ equalities.push(thisDiff);
+ length_insertions1 = length_insertions2;
+ length_deletions1 = length_deletions2;
+ length_insertions2 = 0;
+ length_deletions2 = 0;
+ lastEquality = thisDiff.text;
+ } else {
+ // An insertion or deletion.
+ if (thisDiff.operation == Operation.INSERT) {
+ length_insertions2 += thisDiff.text.length();
+ } else {
+ length_deletions2 += thisDiff.text.length();
+ }
+ // Eliminate an equality that is smaller or equal to the edits on both
+ // sides of it.
+ if (lastEquality != null && (lastEquality.length()
+ <= Math.max(length_insertions1, length_deletions1))
+ && (lastEquality.length()
+ <= Math.max(length_insertions2, length_deletions2))) {
+ //System.out.println("Splitting: '" + lastEquality + "'");
+ // Walk back to offending equality.
+ while (thisDiff != equalities.peek()) {
+ thisDiff = pointer.previous();
+ }
+ pointer.next();
+
+ // Replace equality with a delete.
+ pointer.set(new Diff(Operation.DELETE, lastEquality));
+ // Insert a corresponding an insert.
+ pointer.add(new Diff(Operation.INSERT, lastEquality));
+
+ equalities.pop(); // Throw away the equality we just deleted.
+ if (!equalities.isEmpty()) {
+ // Throw away the previous equality (it needs to be reevaluated).
+ equalities.pop();
+ }
+ if (equalities.isEmpty()) {
+ // There are no previous equalities, walk back to the start.
+ while (pointer.hasPrevious()) {
+ pointer.previous();
+ }
+ } else {
+ // There is a safe equality we can fall back to.
+ thisDiff = equalities.peek();
+ while (thisDiff != pointer.previous()) {
+ // Intentionally empty loop.
+ }
+ }
+
+ length_insertions1 = 0; // Reset the counters.
+ length_insertions2 = 0;
+ length_deletions1 = 0;
+ length_deletions2 = 0;
+ lastEquality = null;
+ changes = true;
+ }
+ }
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+
+ // Normalize the diff.
+ if (changes) {
+ diff_cleanupMerge(diffs);
+ }
+ diff_cleanupSemanticLossless(diffs);
+
+ // Find any overlaps between deletions and insertions.
+ // e.g: abcxxxxxxdef
+ // -> abcxxxdef
+ // e.g: xxxabcdefxxx
+ // -> defxxxabc
+ // Only extract an overlap if it is as big as the edit ahead or behind it.
+ pointer = diffs.listIterator();
+ Diff prevDiff = null;
+ thisDiff = null;
+ if (pointer.hasNext()) {
+ prevDiff = pointer.next();
+ if (pointer.hasNext()) {
+ thisDiff = pointer.next();
+ }
+ }
+ while (thisDiff != null) {
+ if (prevDiff.operation == Operation.DELETE &&
+ thisDiff.operation == Operation.INSERT) {
+ String deletion = prevDiff.text;
+ String insertion = thisDiff.text;
+ int overlap_length1 = this.diff_commonOverlap(deletion, insertion);
+ int overlap_length2 = this.diff_commonOverlap(insertion, deletion);
+ if (overlap_length1 >= overlap_length2) {
+ if (overlap_length1 >= deletion.length() / 2.0 ||
+ overlap_length1 >= insertion.length() / 2.0) {
+ // Overlap found. Insert an equality and trim the surrounding edits.
+ pointer.previous();
+ pointer.add(new Diff(Operation.EQUAL,
+ insertion.substring(0, overlap_length1)));
+ prevDiff.text =
+ deletion.substring(0, deletion.length() - overlap_length1);
+ thisDiff.text = insertion.substring(overlap_length1);
+ // pointer.add inserts the element before the cursor, so there is
+ // no need to step past the new element.
+ }
+ } else {
+ if (overlap_length2 >= deletion.length() / 2.0 ||
+ overlap_length2 >= insertion.length() / 2.0) {
+ // Reverse overlap found.
+ // Insert an equality and swap and trim the surrounding edits.
+ pointer.previous();
+ pointer.add(new Diff(Operation.EQUAL,
+ deletion.substring(0, overlap_length2)));
+ prevDiff.operation = Operation.INSERT;
+ prevDiff.text =
+ insertion.substring(0, insertion.length() - overlap_length2);
+ thisDiff.operation = Operation.DELETE;
+ thisDiff.text = deletion.substring(overlap_length2);
+ // pointer.add inserts the element before the cursor, so there is
+ // no need to step past the new element.
+ }
+ }
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+ prevDiff = thisDiff;
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+ }
+
+ /**
+ * Look for single edits surrounded on both sides by equalities
+ * which can be shifted sideways to align the edit to a word boundary.
+ * e.g: The cat came. -> The cat came.
+ * @param diffs LinkedList of Diff objects.
+ */
+ public void diff_cleanupSemanticLossless(LinkedList diffs) {
+ String equality1, edit, equality2;
+ String commonString;
+ int commonOffset;
+ int score, bestScore;
+ String bestEquality1, bestEdit, bestEquality2;
+ // Create a new iterator at the start.
+ ListIterator pointer = diffs.listIterator();
+ Diff prevDiff = pointer.hasNext() ? pointer.next() : null;
+ Diff thisDiff = pointer.hasNext() ? pointer.next() : null;
+ Diff nextDiff = pointer.hasNext() ? pointer.next() : null;
+ // Intentionally ignore the first and last element (don't need checking).
+ while (nextDiff != null) {
+ if (prevDiff.operation == Operation.EQUAL &&
+ nextDiff.operation == Operation.EQUAL) {
+ // This is a single edit surrounded by equalities.
+ equality1 = prevDiff.text;
+ edit = thisDiff.text;
+ equality2 = nextDiff.text;
+
+ // First, shift the edit as far left as possible.
+ commonOffset = diff_commonSuffix(equality1, edit);
+ if (commonOffset != 0) {
+ commonString = edit.substring(edit.length() - commonOffset);
+ equality1 = equality1.substring(0, equality1.length() - commonOffset);
+ edit = commonString + edit.substring(0, edit.length() - commonOffset);
+ equality2 = commonString + equality2;
+ }
+
+ // Second, step character by character right, looking for the best fit.
+ bestEquality1 = equality1;
+ bestEdit = edit;
+ bestEquality2 = equality2;
+ bestScore = diff_cleanupSemanticScore(equality1, edit)
+ + diff_cleanupSemanticScore(edit, equality2);
+ while (edit.length() != 0 && equality2.length() != 0
+ && edit.charAt(0) == equality2.charAt(0)) {
+ equality1 += edit.charAt(0);
+ edit = edit.substring(1) + equality2.charAt(0);
+ equality2 = equality2.substring(1);
+ score = diff_cleanupSemanticScore(equality1, edit)
+ + diff_cleanupSemanticScore(edit, equality2);
+ // The >= encourages trailing rather than leading whitespace on edits.
+ if (score >= bestScore) {
+ bestScore = score;
+ bestEquality1 = equality1;
+ bestEdit = edit;
+ bestEquality2 = equality2;
+ }
+ }
+
+ if (!prevDiff.text.equals(bestEquality1)) {
+ // We have an improvement, save it back to the diff.
+ if (bestEquality1.length() != 0) {
+ prevDiff.text = bestEquality1;
+ } else {
+ pointer.previous(); // Walk past nextDiff.
+ pointer.previous(); // Walk past thisDiff.
+ pointer.previous(); // Walk past prevDiff.
+ pointer.remove(); // Delete prevDiff.
+ pointer.next(); // Walk past thisDiff.
+ pointer.next(); // Walk past nextDiff.
+ }
+ thisDiff.text = bestEdit;
+ if (bestEquality2.length() != 0) {
+ nextDiff.text = bestEquality2;
+ } else {
+ pointer.remove(); // Delete nextDiff.
+ nextDiff = thisDiff;
+ thisDiff = prevDiff;
+ }
+ }
+ }
+ prevDiff = thisDiff;
+ thisDiff = nextDiff;
+ nextDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+ }
+
+ /**
+ * Given two strings, compute a score representing whether the internal
+ * boundary falls on logical boundaries.
+ * Scores range from 6 (best) to 0 (worst).
+ * @param one First string.
+ * @param two Second string.
+ * @return The score.
+ */
+ private int diff_cleanupSemanticScore(String one, String two) {
+ if (one.length() == 0 || two.length() == 0) {
+ // Edges are the best.
+ return 6;
+ }
+
+ // Each port of this function behaves slightly differently due to
+ // subtle differences in each language's definition of things like
+ // 'whitespace'. Since this function's purpose is largely cosmetic,
+ // the choice has been made to use each language's native features
+ // rather than force total conformity.
+ char char1 = one.charAt(one.length() - 1);
+ char char2 = two.charAt(0);
+ boolean nonAlphaNumeric1 = !Character.isLetterOrDigit(char1);
+ boolean nonAlphaNumeric2 = !Character.isLetterOrDigit(char2);
+ boolean whitespace1 = nonAlphaNumeric1 && Character.isWhitespace(char1);
+ boolean whitespace2 = nonAlphaNumeric2 && Character.isWhitespace(char2);
+ boolean lineBreak1 = whitespace1
+ && Character.getType(char1) == Character.CONTROL;
+ boolean lineBreak2 = whitespace2
+ && Character.getType(char2) == Character.CONTROL;
+ boolean blankLine1 = lineBreak1 && BLANKLINEEND.matcher(one).find();
+ boolean blankLine2 = lineBreak2 && BLANKLINESTART.matcher(two).find();
+
+ if (blankLine1 || blankLine2) {
+ // Five points for blank lines.
+ return 5;
+ } else if (lineBreak1 || lineBreak2) {
+ // Four points for line breaks.
+ return 4;
+ } else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
+ // Three points for end of sentences.
+ return 3;
+ } else if (whitespace1 || whitespace2) {
+ // Two points for whitespace.
+ return 2;
+ } else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
+ // One point for non-alphanumeric.
+ return 1;
+ }
+ return 0;
+ }
+
+ // Define some regex patterns for matching boundaries.
+ private Pattern BLANKLINEEND
+ = Pattern.compile("\\n\\r?\\n\\Z", Pattern.DOTALL);
+ private Pattern BLANKLINESTART
+ = Pattern.compile("\\A\\r?\\n\\r?\\n", Pattern.DOTALL);
+
+ /**
+ * Reduce the number of edits by eliminating operationally trivial equalities.
+ * @param diffs LinkedList of Diff objects.
+ */
+ public void diff_cleanupEfficiency(LinkedList diffs) {
+ if (diffs.isEmpty()) {
+ return;
+ }
+ boolean changes = false;
+ Deque equalities = new ArrayDeque(); // Double-ended queue of equalities.
+ String lastEquality = null; // Always equal to equalities.peek().text
+ ListIterator pointer = diffs.listIterator();
+ // Is there an insertion operation before the last equality.
+ boolean pre_ins = false;
+ // Is there a deletion operation before the last equality.
+ boolean pre_del = false;
+ // Is there an insertion operation after the last equality.
+ boolean post_ins = false;
+ // Is there a deletion operation after the last equality.
+ boolean post_del = false;
+ Diff thisDiff = pointer.next();
+ Diff safeDiff = thisDiff; // The last Diff that is known to be unsplittable.
+ while (thisDiff != null) {
+ if (thisDiff.operation == Operation.EQUAL) {
+ // Equality found.
+ if (thisDiff.text.length() < Diff_EditCost && (post_ins || post_del)) {
+ // Candidate found.
+ equalities.push(thisDiff);
+ pre_ins = post_ins;
+ pre_del = post_del;
+ lastEquality = thisDiff.text;
+ } else {
+ // Not a candidate, and can never become one.
+ equalities.clear();
+ lastEquality = null;
+ safeDiff = thisDiff;
+ }
+ post_ins = post_del = false;
+ } else {
+ // An insertion or deletion.
+ if (thisDiff.operation == Operation.DELETE) {
+ post_del = true;
+ } else {
+ post_ins = true;
+ }
+ /*
+ * Five types to be split:
+ * ABXYCD
+ * AXCD
+ * ABXC
+ * AXCD
+ * ABXC
+ */
+ if (lastEquality != null
+ && ((pre_ins && pre_del && post_ins && post_del)
+ || ((lastEquality.length() < Diff_EditCost / 2)
+ && ((pre_ins ? 1 : 0) + (pre_del ? 1 : 0)
+ + (post_ins ? 1 : 0) + (post_del ? 1 : 0)) == 3))) {
+ //System.out.println("Splitting: '" + lastEquality + "'");
+ // Walk back to offending equality.
+ while (thisDiff != equalities.peek()) {
+ thisDiff = pointer.previous();
+ }
+ pointer.next();
+
+ // Replace equality with a delete.
+ pointer.set(new Diff(Operation.DELETE, lastEquality));
+ // Insert a corresponding an insert.
+ pointer.add(thisDiff = new Diff(Operation.INSERT, lastEquality));
+
+ equalities.pop(); // Throw away the equality we just deleted.
+ lastEquality = null;
+ if (pre_ins && pre_del) {
+ // No changes made which could affect previous entry, keep going.
+ post_ins = post_del = true;
+ equalities.clear();
+ safeDiff = thisDiff;
+ } else {
+ if (!equalities.isEmpty()) {
+ // Throw away the previous equality (it needs to be reevaluated).
+ equalities.pop();
+ }
+ if (equalities.isEmpty()) {
+ // There are no previous questionable equalities,
+ // walk back to the last known safe diff.
+ thisDiff = safeDiff;
+ } else {
+ // There is an equality we can fall back to.
+ thisDiff = equalities.peek();
+ }
+ while (thisDiff != pointer.previous()) {
+ // Intentionally empty loop.
+ }
+ post_ins = post_del = false;
+ }
+
+ changes = true;
+ }
+ }
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+
+ if (changes) {
+ diff_cleanupMerge(diffs);
+ }
+ }
+
+ /**
+ * Reorder and merge like edit sections. Merge equalities.
+ * Any edit section can move as long as it doesn't cross an equality.
+ * @param diffs LinkedList of Diff objects.
+ */
+ public void diff_cleanupMerge(LinkedList diffs) {
+ diffs.add(new Diff(Operation.EQUAL, "")); // Add a dummy entry at the end.
+ ListIterator pointer = diffs.listIterator();
+ int count_delete = 0;
+ int count_insert = 0;
+ String text_delete = "";
+ String text_insert = "";
+ Diff thisDiff = pointer.next();
+ Diff prevEqual = null;
+ int commonlength;
+ while (thisDiff != null) {
+ switch (thisDiff.operation) {
+ case INSERT:
+ count_insert++;
+ text_insert += thisDiff.text;
+ prevEqual = null;
+ break;
+ case DELETE:
+ count_delete++;
+ text_delete += thisDiff.text;
+ prevEqual = null;
+ break;
+ case EQUAL:
+ if (count_delete + count_insert > 1) {
+ boolean both_types = count_delete != 0 && count_insert != 0;
+ // Delete the offending records.
+ pointer.previous(); // Reverse direction.
+ while (count_delete-- > 0) {
+ pointer.previous();
+ pointer.remove();
+ }
+ while (count_insert-- > 0) {
+ pointer.previous();
+ pointer.remove();
+ }
+ if (both_types) {
+ // Factor out any common prefixies.
+ commonlength = diff_commonPrefix(text_insert, text_delete);
+ if (commonlength != 0) {
+ if (pointer.hasPrevious()) {
+ thisDiff = pointer.previous();
+ assert thisDiff.operation == Operation.EQUAL
+ : "Previous diff should have been an equality.";
+ thisDiff.text += text_insert.substring(0, commonlength);
+ pointer.next();
+ } else {
+ pointer.add(new Diff(Operation.EQUAL,
+ text_insert.substring(0, commonlength)));
+ }
+ text_insert = text_insert.substring(commonlength);
+ text_delete = text_delete.substring(commonlength);
+ }
+ // Factor out any common suffixies.
+ commonlength = diff_commonSuffix(text_insert, text_delete);
+ if (commonlength != 0) {
+ thisDiff = pointer.next();
+ thisDiff.text = text_insert.substring(text_insert.length()
+ - commonlength) + thisDiff.text;
+ text_insert = text_insert.substring(0, text_insert.length()
+ - commonlength);
+ text_delete = text_delete.substring(0, text_delete.length()
+ - commonlength);
+ pointer.previous();
+ }
+ }
+ // Insert the merged records.
+ if (text_delete.length() != 0) {
+ pointer.add(new Diff(Operation.DELETE, text_delete));
+ }
+ if (text_insert.length() != 0) {
+ pointer.add(new Diff(Operation.INSERT, text_insert));
+ }
+ // Step forward to the equality.
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ } else if (prevEqual != null) {
+ // Merge this equality with the previous one.
+ prevEqual.text += thisDiff.text;
+ pointer.remove();
+ thisDiff = pointer.previous();
+ pointer.next(); // Forward direction
+ }
+ count_insert = 0;
+ count_delete = 0;
+ text_delete = "";
+ text_insert = "";
+ prevEqual = thisDiff;
+ break;
+ }
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+ if (diffs.getLast().text.length() == 0) {
+ diffs.removeLast(); // Remove the dummy entry at the end.
+ }
+
+ /*
+ * Second pass: look for single edits surrounded on both sides by equalities
+ * which can be shifted sideways to eliminate an equality.
+ * e.g: ABAC -> ABAC
+ */
+ boolean changes = false;
+ // Create a new iterator at the start.
+ // (As opposed to walking the current one back.)
+ pointer = diffs.listIterator();
+ Diff prevDiff = pointer.hasNext() ? pointer.next() : null;
+ thisDiff = pointer.hasNext() ? pointer.next() : null;
+ Diff nextDiff = pointer.hasNext() ? pointer.next() : null;
+ // Intentionally ignore the first and last element (don't need checking).
+ while (nextDiff != null) {
+ if (prevDiff.operation == Operation.EQUAL &&
+ nextDiff.operation == Operation.EQUAL) {
+ // This is a single edit surrounded by equalities.
+ if (thisDiff.text.endsWith(prevDiff.text)) {
+ // Shift the edit over the previous equality.
+ thisDiff.text = prevDiff.text
+ + thisDiff.text.substring(0, thisDiff.text.length()
+ - prevDiff.text.length());
+ nextDiff.text = prevDiff.text + nextDiff.text;
+ pointer.previous(); // Walk past nextDiff.
+ pointer.previous(); // Walk past thisDiff.
+ pointer.previous(); // Walk past prevDiff.
+ pointer.remove(); // Delete prevDiff.
+ pointer.next(); // Walk past thisDiff.
+ thisDiff = pointer.next(); // Walk past nextDiff.
+ nextDiff = pointer.hasNext() ? pointer.next() : null;
+ changes = true;
+ } else if (thisDiff.text.startsWith(nextDiff.text)) {
+ // Shift the edit over the next equality.
+ prevDiff.text += nextDiff.text;
+ thisDiff.text = thisDiff.text.substring(nextDiff.text.length())
+ + nextDiff.text;
+ pointer.remove(); // Delete nextDiff.
+ nextDiff = pointer.hasNext() ? pointer.next() : null;
+ changes = true;
+ }
+ }
+ prevDiff = thisDiff;
+ thisDiff = nextDiff;
+ nextDiff = pointer.hasNext() ? pointer.next() : null;
+ }
+ // If shifts were made, the diff needs reordering and another shift sweep.
+ if (changes) {
+ diff_cleanupMerge(diffs);
+ }
+ }
+
+ /**
+ * loc is a location in text1, compute and return the equivalent location in
+ * text2.
+ * e.g. "The cat" vs "The big cat", 1->1, 5->8
+ * @param diffs List of Diff objects.
+ * @param loc Location within text1.
+ * @return Location within text2.
+ */
+ public int diff_xIndex(List diffs, int loc) {
+ int chars1 = 0;
+ int chars2 = 0;
+ int last_chars1 = 0;
+ int last_chars2 = 0;
+ Diff lastDiff = null;
+ for (Diff aDiff : diffs) {
+ if (aDiff.operation != Operation.INSERT) {
+ // Equality or deletion.
+ chars1 += aDiff.text.length();
+ }
+ if (aDiff.operation != Operation.DELETE) {
+ // Equality or insertion.
+ chars2 += aDiff.text.length();
+ }
+ if (chars1 > loc) {
+ // Overshot the location.
+ lastDiff = aDiff;
+ break;
+ }
+ last_chars1 = chars1;
+ last_chars2 = chars2;
+ }
+ if (lastDiff != null && lastDiff.operation == Operation.DELETE) {
+ // The location was deleted.
+ return last_chars2;
+ }
+ // Add the remaining character length.
+ return last_chars2 + (loc - last_chars1);
+ }
+
+ /**
+ * Convert a Diff list into a pretty HTML report.
+ * @param diffs List of Diff objects.
+ * @return HTML representation.
+ */
+ public String diff_prettyHtml(List diffs) {
+ StringBuilder html = new StringBuilder();
+ for (Diff aDiff : diffs) {
+ String text = aDiff.text.replace("&", "&").replace("<", "<")
+ .replace(">", ">").replace("\n", "¶ ");
+ switch (aDiff.operation) {
+ case INSERT:
+ html.append("").append(text)
+ .append("");
+ break;
+ case DELETE:
+ html.append("").append(text)
+ .append("");
+ break;
+ case EQUAL:
+ html.append("").append(text).append("");
+ break;
+ }
+ }
+ return html.toString();
+ }
+
+ /**
+ * Compute and return the source text (all equalities and deletions).
+ * @param diffs List of Diff objects.
+ * @return Source text.
+ */
+ public String diff_text1(List diffs) {
+ StringBuilder text = new StringBuilder();
+ for (Diff aDiff : diffs) {
+ if (aDiff.operation != Operation.INSERT) {
+ text.append(aDiff.text);
+ }
+ }
+ return text.toString();
+ }
+
+ /**
+ * Compute and return the destination text (all equalities and insertions).
+ * @param diffs List of Diff objects.
+ * @return Destination text.
+ */
+ public String diff_text2(List diffs) {
+ StringBuilder text = new StringBuilder();
+ for (Diff aDiff : diffs) {
+ if (aDiff.operation != Operation.DELETE) {
+ text.append(aDiff.text);
+ }
+ }
+ return text.toString();
+ }
+
+ /**
+ * Compute the Levenshtein distance; the number of inserted, deleted or
+ * substituted characters.
+ * @param diffs List of Diff objects.
+ * @return Number of changes.
+ */
+ public int diff_levenshtein(List diffs) {
+ int levenshtein = 0;
+ int insertions = 0;
+ int deletions = 0;
+ for (Diff aDiff : diffs) {
+ switch (aDiff.operation) {
+ case INSERT:
+ insertions += aDiff.text.length();
+ break;
+ case DELETE:
+ deletions += aDiff.text.length();
+ break;
+ case EQUAL:
+ // A deletion and an insertion is one substitution.
+ levenshtein += Math.max(insertions, deletions);
+ insertions = 0;
+ deletions = 0;
+ break;
+ }
+ }
+ levenshtein += Math.max(insertions, deletions);
+ return levenshtein;
+ }
+
+ /**
+ * Crush the diff into an encoded string which describes the operations
+ * required to transform text1 into text2.
+ * E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
+ * Operations are tab-separated. Inserted text is escaped using %xx notation.
+ * @param diffs List of Diff objects.
+ * @return Delta text.
+ */
+ public String diff_toDelta(List diffs) {
+ StringBuilder text = new StringBuilder();
+ for (Diff aDiff : diffs) {
+ switch (aDiff.operation) {
+ case INSERT:
+ try {
+ text.append("+").append(URLEncoder.encode(aDiff.text, "UTF-8")
+ .replace('+', ' ')).append("\t");
+ } catch (UnsupportedEncodingException e) {
+ // Not likely on modern system.
+ throw new Error("This system does not support UTF-8.", e);
+ }
+ break;
+ case DELETE:
+ text.append("-").append(aDiff.text.length()).append("\t");
+ break;
+ case EQUAL:
+ text.append("=").append(aDiff.text.length()).append("\t");
+ break;
+ }
+ }
+ String delta = text.toString();
+ if (delta.length() != 0) {
+ // Strip off trailing tab character.
+ delta = delta.substring(0, delta.length() - 1);
+ delta = unescapeForEncodeUriCompatability(delta);
+ }
+ return delta;
+ }
+
+ /**
+ * Given the original text1, and an encoded string which describes the
+ * operations required to transform text1 into text2, compute the full diff.
+ * @param text1 Source string for the diff.
+ * @param delta Delta text.
+ * @return Array of Diff objects or null if invalid.
+ * @throws IllegalArgumentException If invalid input.
+ */
+ public LinkedList diff_fromDelta(String text1, String delta)
+ throws IllegalArgumentException {
+ LinkedList diffs = new LinkedList();
+ int pointer = 0; // Cursor in text1
+ String[] tokens = delta.split("\t");
+ for (String token : tokens) {
+ if (token.length() == 0) {
+ // Blank tokens are ok (from a trailing \t).
+ continue;
+ }
+ // Each token begins with a one character parameter which specifies the
+ // operation of this token (delete, insert, equality).
+ String param = token.substring(1);
+ switch (token.charAt(0)) {
+ case '+':
+ // decode would change all "+" to " "
+ param = param.replace("+", "%2B");
+ try {
+ param = URLDecoder.decode(param, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // Not likely on modern system.
+ throw new Error("This system does not support UTF-8.", e);
+ } catch (IllegalArgumentException e) {
+ // Malformed URI sequence.
+ throw new IllegalArgumentException(
+ "Illegal escape in diff_fromDelta: " + param, e);
+ }
+ diffs.add(new Diff(Operation.INSERT, param));
+ break;
+ case '-':
+ // Fall through.
+ case '=':
+ int n;
+ try {
+ n = Integer.parseInt(param);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(
+ "Invalid number in diff_fromDelta: " + param, e);
+ }
+ if (n < 0) {
+ throw new IllegalArgumentException(
+ "Negative number in diff_fromDelta: " + param);
+ }
+ String text;
+ try {
+ text = text1.substring(pointer, pointer += n);
+ } catch (StringIndexOutOfBoundsException e) {
+ throw new IllegalArgumentException("Delta length (" + pointer
+ + ") larger than source text length (" + text1.length()
+ + ").", e);
+ }
+ if (token.charAt(0) == '=') {
+ diffs.add(new Diff(Operation.EQUAL, text));
+ } else {
+ diffs.add(new Diff(Operation.DELETE, text));
+ }
+ break;
+ default:
+ // Anything else is an error.
+ throw new IllegalArgumentException(
+ "Invalid diff operation in diff_fromDelta: " + token.charAt(0));
+ }
+ }
+ if (pointer != text1.length()) {
+ throw new IllegalArgumentException("Delta length (" + pointer
+ + ") smaller than source text length (" + text1.length() + ").");
+ }
+ return diffs;
+ }
+
+
+ // MATCH FUNCTIONS
+
+
+ /**
+ * Locate the best instance of 'pattern' in 'text' near 'loc'.
+ * Returns -1 if no match found.
+ * @param text The text to search.
+ * @param pattern The pattern to search for.
+ * @param loc The location to search around.
+ * @return Best match index or -1.
+ */
+ public int match_main(String text, String pattern, int loc) {
+ // Check for null inputs.
+ if (text == null || pattern == null) {
+ throw new IllegalArgumentException("Null inputs. (match_main)");
+ }
+
+ loc = Math.max(0, Math.min(loc, text.length()));
+ if (text.equals(pattern)) {
+ // Shortcut (potentially not guaranteed by the algorithm)
+ return 0;
+ } else if (text.length() == 0) {
+ // Nothing to match.
+ return -1;
+ } else if (loc + pattern.length() <= text.length()
+ && text.substring(loc, loc + pattern.length()).equals(pattern)) {
+ // Perfect match at the perfect spot! (Includes case of null pattern)
+ return loc;
+ } else {
+ // Do a fuzzy compare.
+ return match_bitap(text, pattern, loc);
+ }
+ }
+
+ /**
+ * Locate the best instance of 'pattern' in 'text' near 'loc' using the
+ * Bitap algorithm. Returns -1 if no match found.
+ * @param text The text to search.
+ * @param pattern The pattern to search for.
+ * @param loc The location to search around.
+ * @return Best match index or -1.
+ */
+ protected int match_bitap(String text, String pattern, int loc) {
+ assert (Match_MaxBits == 0 || pattern.length() <= Match_MaxBits)
+ : "Pattern too long for this application.";
+
+ // Initialise the alphabet.
+ Map s = match_alphabet(pattern);
+
+ // Highest score beyond which we give up.
+ double score_threshold = Match_Threshold;
+ // Is there a nearby exact match? (speedup)
+ int best_loc = text.indexOf(pattern, loc);
+ if (best_loc != -1) {
+ score_threshold = Math.min(match_bitapScore(0, best_loc, loc, pattern),
+ score_threshold);
+ // What about in the other direction? (speedup)
+ best_loc = text.lastIndexOf(pattern, loc + pattern.length());
+ if (best_loc != -1) {
+ score_threshold = Math.min(match_bitapScore(0, best_loc, loc, pattern),
+ score_threshold);
+ }
+ }
+
+ // Initialise the bit arrays.
+ int matchmask = 1 << (pattern.length() - 1);
+ best_loc = -1;
+
+ int bin_min, bin_mid;
+ int bin_max = pattern.length() + text.length();
+ // Empty initialization added to appease Java compiler.
+ int[] last_rd = new int[0];
+ for (int d = 0; d < pattern.length(); d++) {
+ // Scan for the best match; each iteration allows for one more error.
+ // Run a binary search to determine how far from 'loc' we can stray at
+ // this error level.
+ bin_min = 0;
+ bin_mid = bin_max;
+ while (bin_min < bin_mid) {
+ if (match_bitapScore(d, loc + bin_mid, loc, pattern)
+ <= score_threshold) {
+ bin_min = bin_mid;
+ } else {
+ bin_max = bin_mid;
+ }
+ bin_mid = (bin_max - bin_min) / 2 + bin_min;
+ }
+ // Use the result from this iteration as the maximum for the next.
+ bin_max = bin_mid;
+ int start = Math.max(1, loc - bin_mid + 1);
+ int finish = Math.min(loc + bin_mid, text.length()) + pattern.length();
+
+ int[] rd = new int[finish + 2];
+ rd[finish + 1] = (1 << d) - 1;
+ for (int j = finish; j >= start; j--) {
+ int charMatch;
+ if (text.length() <= j - 1 || !s.containsKey(text.charAt(j - 1))) {
+ // Out of range.
+ charMatch = 0;
+ } else {
+ charMatch = s.get(text.charAt(j - 1));
+ }
+ if (d == 0) {
+ // First pass: exact match.
+ rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
+ } else {
+ // Subsequent passes: fuzzy match.
+ rd[j] = (((rd[j + 1] << 1) | 1) & charMatch)
+ | (((last_rd[j + 1] | last_rd[j]) << 1) | 1) | last_rd[j + 1];
+ }
+ if ((rd[j] & matchmask) != 0) {
+ double score = match_bitapScore(d, j - 1, loc, pattern);
+ // This match will almost certainly be better than any existing
+ // match. But check anyway.
+ if (score <= score_threshold) {
+ // Told you so.
+ score_threshold = score;
+ best_loc = j - 1;
+ if (best_loc > loc) {
+ // When passing loc, don't exceed our current distance from loc.
+ start = Math.max(1, 2 * loc - best_loc);
+ } else {
+ // Already passed loc, downhill from here on in.
+ break;
+ }
+ }
+ }
+ }
+ if (match_bitapScore(d + 1, loc, loc, pattern) > score_threshold) {
+ // No hope for a (better) match at greater error levels.
+ break;
+ }
+ last_rd = rd;
+ }
+ return best_loc;
+ }
+
+ /**
+ * Compute and return the score for a match with e errors and x location.
+ * @param e Number of errors in match.
+ * @param x Location of match.
+ * @param loc Expected location of match.
+ * @param pattern Pattern being sought.
+ * @return Overall score for match (0.0 = good, 1.0 = bad).
+ */
+ private double match_bitapScore(int e, int x, int loc, String pattern) {
+ float accuracy = (float) e / pattern.length();
+ int proximity = Math.abs(loc - x);
+ if (Match_Distance == 0) {
+ // Dodge divide by zero error.
+ return proximity == 0 ? accuracy : 1.0;
+ }
+ return accuracy + (proximity / (float) Match_Distance);
+ }
+
+ /**
+ * Initialise the alphabet for the Bitap algorithm.
+ * @param pattern The text to encode.
+ * @return Hash of character locations.
+ */
+ protected Map match_alphabet(String pattern) {
+ Map s = new HashMap();
+ char[] char_pattern = pattern.toCharArray();
+ for (char c : char_pattern) {
+ s.put(c, 0);
+ }
+ int i = 0;
+ for (char c : char_pattern) {
+ s.put(c, s.get(c) | (1 << (pattern.length() - i - 1)));
+ i++;
+ }
+ return s;
+ }
+
+
+ // PATCH FUNCTIONS
+
+
+ /**
+ * Increase the context until it is unique,
+ * but don't let the pattern expand beyond Match_MaxBits.
+ * @param patch The patch to grow.
+ * @param text Source text.
+ */
+ protected void patch_addContext(Patch patch, String text) {
+ if (text.length() == 0) {
+ return;
+ }
+ String pattern = text.substring(patch.start2, patch.start2 + patch.length1);
+ int padding = 0;
+
+ // Look for the first and last matches of pattern in text. If two different
+ // matches are found, increase the pattern length.
+ while (text.indexOf(pattern) != text.lastIndexOf(pattern)
+ && pattern.length() < Match_MaxBits - Patch_Margin - Patch_Margin) {
+ padding += Patch_Margin;
+ pattern = text.substring(Math.max(0, patch.start2 - padding),
+ Math.min(text.length(), patch.start2 + patch.length1 + padding));
+ }
+ // Add one chunk for good luck.
+ padding += Patch_Margin;
+
+ // Add the prefix.
+ String prefix = text.substring(Math.max(0, patch.start2 - padding),
+ patch.start2);
+ if (prefix.length() != 0) {
+ patch.diffs.addFirst(new Diff(Operation.EQUAL, prefix));
+ }
+ // Add the suffix.
+ String suffix = text.substring(patch.start2 + patch.length1,
+ Math.min(text.length(), patch.start2 + patch.length1 + padding));
+ if (suffix.length() != 0) {
+ patch.diffs.addLast(new Diff(Operation.EQUAL, suffix));
+ }
+
+ // Roll back the start points.
+ patch.start1 -= prefix.length();
+ patch.start2 -= prefix.length();
+ // Extend the lengths.
+ patch.length1 += prefix.length() + suffix.length();
+ patch.length2 += prefix.length() + suffix.length();
+ }
+
+ /**
+ * Compute a list of patches to turn text1 into text2.
+ * A set of diffs will be computed.
+ * @param text1 Old text.
+ * @param text2 New text.
+ * @return LinkedList of Patch objects.
+ */
+ public LinkedList patch_make(String text1, String text2) {
+ if (text1 == null || text2 == null) {
+ throw new IllegalArgumentException("Null inputs. (patch_make)");
+ }
+ // No diffs provided, compute our own.
+ LinkedList diffs = diff_main(text1, text2, true);
+ if (diffs.size() > 2) {
+ diff_cleanupSemantic(diffs);
+ diff_cleanupEfficiency(diffs);
+ }
+ return patch_make(text1, diffs);
+ }
+
+ /**
+ * Compute a list of patches to turn text1 into text2.
+ * text1 will be derived from the provided diffs.
+ * @param diffs Array of Diff objects for text1 to text2.
+ * @return LinkedList of Patch objects.
+ */
+ public LinkedList patch_make(LinkedList diffs) {
+ if (diffs == null) {
+ throw new IllegalArgumentException("Null inputs. (patch_make)");
+ }
+ // No origin string provided, compute our own.
+ String text1 = diff_text1(diffs);
+ return patch_make(text1, diffs);
+ }
+
+ /**
+ * Compute a list of patches to turn text1 into text2.
+ * text2 is ignored, diffs are the delta between text1 and text2.
+ * @param text1 Old text
+ * @param text2 Ignored.
+ * @param diffs Array of Diff objects for text1 to text2.
+ * @return LinkedList of Patch objects.
+ * @deprecated Prefer patch_make(String text1, LinkedList diffs).
+ */
+ @Deprecated public LinkedList patch_make(String text1, String text2,
+ LinkedList diffs) {
+ return patch_make(text1, diffs);
+ }
+
+ /**
+ * Compute a list of patches to turn text1 into text2.
+ * text2 is not provided, diffs are the delta between text1 and text2.
+ * @param text1 Old text.
+ * @param diffs Array of Diff objects for text1 to text2.
+ * @return LinkedList of Patch objects.
+ */
+ public LinkedList patch_make(String text1, LinkedList diffs) {
+ if (text1 == null || diffs == null) {
+ throw new IllegalArgumentException("Null inputs. (patch_make)");
+ }
+
+ LinkedList patches = new LinkedList();
+ if (diffs.isEmpty()) {
+ return patches; // Get rid of the null case.
+ }
+ Patch patch = new Patch();
+ int char_count1 = 0; // Number of characters into the text1 string.
+ int char_count2 = 0; // Number of characters into the text2 string.
+ // Start with text1 (prepatch_text) and apply the diffs until we arrive at
+ // text2 (postpatch_text). We recreate the patches one by one to determine
+ // context info.
+ String prepatch_text = text1;
+ String postpatch_text = text1;
+ for (Diff aDiff : diffs) {
+ if (patch.diffs.isEmpty() && aDiff.operation != Operation.EQUAL) {
+ // A new patch starts here.
+ patch.start1 = char_count1;
+ patch.start2 = char_count2;
+ }
+
+ switch (aDiff.operation) {
+ case INSERT:
+ patch.diffs.add(aDiff);
+ patch.length2 += aDiff.text.length();
+ postpatch_text = postpatch_text.substring(0, char_count2)
+ + aDiff.text + postpatch_text.substring(char_count2);
+ break;
+ case DELETE:
+ patch.length1 += aDiff.text.length();
+ patch.diffs.add(aDiff);
+ postpatch_text = postpatch_text.substring(0, char_count2)
+ + postpatch_text.substring(char_count2 + aDiff.text.length());
+ break;
+ case EQUAL:
+ if (aDiff.text.length() <= 2 * Patch_Margin
+ && !patch.diffs.isEmpty() && aDiff != diffs.getLast()) {
+ // Small equality inside a patch.
+ patch.diffs.add(aDiff);
+ patch.length1 += aDiff.text.length();
+ patch.length2 += aDiff.text.length();
+ }
+
+ if (aDiff.text.length() >= 2 * Patch_Margin && !patch.diffs.isEmpty()) {
+ // Time for a new patch.
+ if (!patch.diffs.isEmpty()) {
+ patch_addContext(patch, prepatch_text);
+ patches.add(patch);
+ patch = new Patch();
+ // Unlike Unidiff, our patch lists have a rolling context.
+ // https://github.com/google/diff-match-patch/wiki/Unidiff
+ // Update prepatch text & pos to reflect the application of the
+ // just completed patch.
+ prepatch_text = postpatch_text;
+ char_count1 = char_count2;
+ }
+ }
+ break;
+ }
+
+ // Update the current character count.
+ if (aDiff.operation != Operation.INSERT) {
+ char_count1 += aDiff.text.length();
+ }
+ if (aDiff.operation != Operation.DELETE) {
+ char_count2 += aDiff.text.length();
+ }
+ }
+ // Pick up the leftover patch if not empty.
+ if (!patch.diffs.isEmpty()) {
+ patch_addContext(patch, prepatch_text);
+ patches.add(patch);
+ }
+
+ return patches;
+ }
+
+ /**
+ * Given an array of patches, return another array that is identical.
+ * @param patches Array of Patch objects.
+ * @return Array of Patch objects.
+ */
+ public LinkedList patch_deepCopy(LinkedList patches) {
+ LinkedList patchesCopy = new LinkedList();
+ for (Patch aPatch : patches) {
+ Patch patchCopy = new Patch();
+ for (Diff aDiff : aPatch.diffs) {
+ Diff diffCopy = new Diff(aDiff.operation, aDiff.text);
+ patchCopy.diffs.add(diffCopy);
+ }
+ patchCopy.start1 = aPatch.start1;
+ patchCopy.start2 = aPatch.start2;
+ patchCopy.length1 = aPatch.length1;
+ patchCopy.length2 = aPatch.length2;
+ patchesCopy.add(patchCopy);
+ }
+ return patchesCopy;
+ }
+
+ /**
+ * Merge a set of patches onto the text. Return a patched text, as well
+ * as an array of true/false values indicating which patches were applied.
+ * @param patches Array of Patch objects
+ * @param text Old text.
+ * @return Two element Object array, containing the new text and an array of
+ * boolean values.
+ */
+ public Object[] patch_apply(LinkedList patches, String text) {
+ if (patches.isEmpty()) {
+ return new Object[]{text, new boolean[0]};
+ }
+
+ // Deep copy the patches so that no changes are made to originals.
+ patches = patch_deepCopy(patches);
+
+ String nullPadding = patch_addPadding(patches);
+ text = nullPadding + text + nullPadding;
+ patch_splitMax(patches);
+
+ int x = 0;
+ // delta keeps track of the offset between the expected and actual location
+ // of the previous patch. If there are patches expected at positions 10 and
+ // 20, but the first patch was found at 12, delta is 2 and the second patch
+ // has an effective expected position of 22.
+ int delta = 0;
+ boolean[] results = new boolean[patches.size()];
+ for (Patch aPatch : patches) {
+ int expected_loc = aPatch.start2 + delta;
+ String text1 = diff_text1(aPatch.diffs);
+ int start_loc;
+ int end_loc = -1;
+ if (text1.length() > this.Match_MaxBits) {
+ // patch_splitMax will only provide an oversized pattern in the case of
+ // a monster delete.
+ start_loc = match_main(text,
+ text1.substring(0, this.Match_MaxBits), expected_loc);
+ if (start_loc != -1) {
+ end_loc = match_main(text,
+ text1.substring(text1.length() - this.Match_MaxBits),
+ expected_loc + text1.length() - this.Match_MaxBits);
+ if (end_loc == -1 || start_loc >= end_loc) {
+ // Can't find valid trailing context. Drop this patch.
+ start_loc = -1;
+ }
+ }
+ } else {
+ start_loc = match_main(text, text1, expected_loc);
+ }
+ if (start_loc == -1) {
+ // No match found. :(
+ results[x] = false;
+ // Subtract the delta for this failed patch from subsequent patches.
+ delta -= aPatch.length2 - aPatch.length1;
+ } else {
+ // Found a match. :)
+ results[x] = true;
+ delta = start_loc - expected_loc;
+ String text2;
+ if (end_loc == -1) {
+ text2 = text.substring(start_loc,
+ Math.min(start_loc + text1.length(), text.length()));
+ } else {
+ text2 = text.substring(start_loc,
+ Math.min(end_loc + this.Match_MaxBits, text.length()));
+ }
+ if (text1.equals(text2)) {
+ // Perfect match, just shove the replacement text in.
+ text = text.substring(0, start_loc) + diff_text2(aPatch.diffs)
+ + text.substring(start_loc + text1.length());
+ } else {
+ // Imperfect match. Run a diff to get a framework of equivalent
+ // indices.
+ LinkedList diffs = diff_main(text1, text2, false);
+ if (text1.length() > this.Match_MaxBits
+ && diff_levenshtein(diffs) / (float) text1.length()
+ > this.Patch_DeleteThreshold) {
+ // The end points match, but the content is unacceptably bad.
+ results[x] = false;
+ } else {
+ diff_cleanupSemanticLossless(diffs);
+ int index1 = 0;
+ for (Diff aDiff : aPatch.diffs) {
+ if (aDiff.operation != Operation.EQUAL) {
+ int index2 = diff_xIndex(diffs, index1);
+ if (aDiff.operation == Operation.INSERT) {
+ // Insertion
+ text = text.substring(0, start_loc + index2) + aDiff.text
+ + text.substring(start_loc + index2);
+ } else if (aDiff.operation == Operation.DELETE) {
+ // Deletion
+ text = text.substring(0, start_loc + index2)
+ + text.substring(start_loc + diff_xIndex(diffs,
+ index1 + aDiff.text.length()));
+ }
+ }
+ if (aDiff.operation != Operation.DELETE) {
+ index1 += aDiff.text.length();
+ }
+ }
+ }
+ }
+ }
+ x++;
+ }
+ // Strip the padding off.
+ text = text.substring(nullPadding.length(), text.length()
+ - nullPadding.length());
+ return new Object[]{text, results};
+ }
+
+ /**
+ * Add some padding on text start and end so that edges can match something.
+ * Intended to be called only from within patch_apply.
+ * @param patches Array of Patch objects.
+ * @return The padding string added to each side.
+ */
+ public String patch_addPadding(LinkedList patches) {
+ short paddingLength = this.Patch_Margin;
+ String nullPadding = "";
+ for (short x = 1; x <= paddingLength; x++) {
+ nullPadding += String.valueOf((char) x);
+ }
+
+ // Bump all the patches forward.
+ for (Patch aPatch : patches) {
+ aPatch.start1 += paddingLength;
+ aPatch.start2 += paddingLength;
+ }
+
+ // Add some padding on start of first diff.
+ Patch patch = patches.getFirst();
+ LinkedList diffs = patch.diffs;
+ if (diffs.isEmpty() || diffs.getFirst().operation != Operation.EQUAL) {
+ // Add nullPadding equality.
+ diffs.addFirst(new Diff(Operation.EQUAL, nullPadding));
+ patch.start1 -= paddingLength; // Should be 0.
+ patch.start2 -= paddingLength; // Should be 0.
+ patch.length1 += paddingLength;
+ patch.length2 += paddingLength;
+ } else if (paddingLength > diffs.getFirst().text.length()) {
+ // Grow first equality.
+ Diff firstDiff = diffs.getFirst();
+ int extraLength = paddingLength - firstDiff.text.length();
+ firstDiff.text = nullPadding.substring(firstDiff.text.length())
+ + firstDiff.text;
+ patch.start1 -= extraLength;
+ patch.start2 -= extraLength;
+ patch.length1 += extraLength;
+ patch.length2 += extraLength;
+ }
+
+ // Add some padding on end of last diff.
+ patch = patches.getLast();
+ diffs = patch.diffs;
+ if (diffs.isEmpty() || diffs.getLast().operation != Operation.EQUAL) {
+ // Add nullPadding equality.
+ diffs.addLast(new Diff(Operation.EQUAL, nullPadding));
+ patch.length1 += paddingLength;
+ patch.length2 += paddingLength;
+ } else if (paddingLength > diffs.getLast().text.length()) {
+ // Grow last equality.
+ Diff lastDiff = diffs.getLast();
+ int extraLength = paddingLength - lastDiff.text.length();
+ lastDiff.text += nullPadding.substring(0, extraLength);
+ patch.length1 += extraLength;
+ patch.length2 += extraLength;
+ }
+
+ return nullPadding;
+ }
+
+ /**
+ * Look through the patches and break up any which are longer than the
+ * maximum limit of the match algorithm.
+ * Intended to be called only from within patch_apply.
+ * @param patches LinkedList of Patch objects.
+ */
+ public void patch_splitMax(LinkedList patches) {
+ short patch_size = Match_MaxBits;
+ String precontext, postcontext;
+ Patch patch;
+ int start1, start2;
+ boolean empty;
+ Operation diff_type;
+ String diff_text;
+ ListIterator pointer = patches.listIterator();
+ Patch bigpatch = pointer.hasNext() ? pointer.next() : null;
+ while (bigpatch != null) {
+ if (bigpatch.length1 <= Match_MaxBits) {
+ bigpatch = pointer.hasNext() ? pointer.next() : null;
+ continue;
+ }
+ // Remove the big old patch.
+ pointer.remove();
+ start1 = bigpatch.start1;
+ start2 = bigpatch.start2;
+ precontext = "";
+ while (!bigpatch.diffs.isEmpty()) {
+ // Create one of several smaller patches.
+ patch = new Patch();
+ empty = true;
+ patch.start1 = start1 - precontext.length();
+ patch.start2 = start2 - precontext.length();
+ if (precontext.length() != 0) {
+ patch.length1 = patch.length2 = precontext.length();
+ patch.diffs.add(new Diff(Operation.EQUAL, precontext));
+ }
+ while (!bigpatch.diffs.isEmpty()
+ && patch.length1 < patch_size - Patch_Margin) {
+ diff_type = bigpatch.diffs.getFirst().operation;
+ diff_text = bigpatch.diffs.getFirst().text;
+ if (diff_type == Operation.INSERT) {
+ // Insertions are harmless.
+ patch.length2 += diff_text.length();
+ start2 += diff_text.length();
+ patch.diffs.addLast(bigpatch.diffs.removeFirst());
+ empty = false;
+ } else if (diff_type == Operation.DELETE && patch.diffs.size() == 1
+ && patch.diffs.getFirst().operation == Operation.EQUAL
+ && diff_text.length() > 2 * patch_size) {
+ // This is a large deletion. Let it pass in one chunk.
+ patch.length1 += diff_text.length();
+ start1 += diff_text.length();
+ empty = false;
+ patch.diffs.add(new Diff(diff_type, diff_text));
+ bigpatch.diffs.removeFirst();
+ } else {
+ // Deletion or equality. Only take as much as we can stomach.
+ diff_text = diff_text.substring(0, Math.min(diff_text.length(),
+ patch_size - patch.length1 - Patch_Margin));
+ patch.length1 += diff_text.length();
+ start1 += diff_text.length();
+ if (diff_type == Operation.EQUAL) {
+ patch.length2 += diff_text.length();
+ start2 += diff_text.length();
+ } else {
+ empty = false;
+ }
+ patch.diffs.add(new Diff(diff_type, diff_text));
+ if (diff_text.equals(bigpatch.diffs.getFirst().text)) {
+ bigpatch.diffs.removeFirst();
+ } else {
+ bigpatch.diffs.getFirst().text = bigpatch.diffs.getFirst().text
+ .substring(diff_text.length());
+ }
+ }
+ }
+ // Compute the head context for the next patch.
+ precontext = diff_text2(patch.diffs);
+ precontext = precontext.substring(Math.max(0, precontext.length()
+ - Patch_Margin));
+ // Append the end context for this patch.
+ if (diff_text1(bigpatch.diffs).length() > Patch_Margin) {
+ postcontext = diff_text1(bigpatch.diffs).substring(0, Patch_Margin);
+ } else {
+ postcontext = diff_text1(bigpatch.diffs);
+ }
+ if (postcontext.length() != 0) {
+ patch.length1 += postcontext.length();
+ patch.length2 += postcontext.length();
+ if (!patch.diffs.isEmpty()
+ && patch.diffs.getLast().operation == Operation.EQUAL) {
+ patch.diffs.getLast().text += postcontext;
+ } else {
+ patch.diffs.add(new Diff(Operation.EQUAL, postcontext));
+ }
+ }
+ if (!empty) {
+ pointer.add(patch);
+ }
+ }
+ bigpatch = pointer.hasNext() ? pointer.next() : null;
+ }
+ }
+
+ /**
+ * Take a list of patches and return a textual representation.
+ * @param patches List of Patch objects.
+ * @return Text representation of patches.
+ */
+ public String patch_toText(List patches) {
+ StringBuilder text = new StringBuilder();
+ for (Patch aPatch : patches) {
+ text.append(aPatch);
+ }
+ return text.toString();
+ }
+
+ /**
+ * Parse a textual representation of patches and return a List of Patch
+ * objects.
+ * @param textline Text representation of patches.
+ * @return List of Patch objects.
+ * @throws IllegalArgumentException If invalid input.
+ */
+ public List patch_fromText(String textline)
+ throws IllegalArgumentException {
+ List patches = new LinkedList();
+ if (textline.length() == 0) {
+ return patches;
+ }
+ List textList = Arrays.asList(textline.split("\n"));
+ LinkedList text = new LinkedList(textList);
+ Patch patch;
+ Pattern patchHeader
+ = Pattern.compile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$");
+ Matcher m;
+ char sign;
+ String line;
+ while (!text.isEmpty()) {
+ m = patchHeader.matcher(text.getFirst());
+ if (!m.matches()) {
+ throw new IllegalArgumentException(
+ "Invalid patch string: " + text.getFirst());
+ }
+ patch = new Patch();
+ patches.add(patch);
+ patch.start1 = Integer.parseInt(m.group(1));
+ if (m.group(2).length() == 0) {
+ patch.start1--;
+ patch.length1 = 1;
+ } else if (m.group(2).equals("0")) {
+ patch.length1 = 0;
+ } else {
+ patch.start1--;
+ patch.length1 = Integer.parseInt(m.group(2));
+ }
+
+ patch.start2 = Integer.parseInt(m.group(3));
+ if (m.group(4).length() == 0) {
+ patch.start2--;
+ patch.length2 = 1;
+ } else if (m.group(4).equals("0")) {
+ patch.length2 = 0;
+ } else {
+ patch.start2--;
+ patch.length2 = Integer.parseInt(m.group(4));
+ }
+ text.removeFirst();
+
+ while (!text.isEmpty()) {
+ try {
+ sign = text.getFirst().charAt(0);
+ } catch (IndexOutOfBoundsException e) {
+ // Blank line? Whatever.
+ text.removeFirst();
+ continue;
+ }
+ line = text.getFirst().substring(1);
+ line = line.replace("+", "%2B"); // decode would change all "+" to " "
+ try {
+ line = URLDecoder.decode(line, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // Not likely on modern system.
+ throw new Error("This system does not support UTF-8.", e);
+ } catch (IllegalArgumentException e) {
+ // Malformed URI sequence.
+ throw new IllegalArgumentException(
+ "Illegal escape in patch_fromText: " + line, e);
+ }
+ if (sign == '-') {
+ // Deletion.
+ patch.diffs.add(new Diff(Operation.DELETE, line));
+ } else if (sign == '+') {
+ // Insertion.
+ patch.diffs.add(new Diff(Operation.INSERT, line));
+ } else if (sign == ' ') {
+ // Minor equality.
+ patch.diffs.add(new Diff(Operation.EQUAL, line));
+ } else if (sign == '@') {
+ // Start of next patch.
+ break;
+ } else {
+ // WTF?
+ throw new IllegalArgumentException(
+ "Invalid patch mode '" + sign + "' in: " + line);
+ }
+ text.removeFirst();
+ }
+ }
+ return patches;
+ }
+
+
+ /**
+ * Class representing one diff operation.
+ */
+ public static class Diff {
+ /**
+ * One of: INSERT, DELETE or EQUAL.
+ */
+ public Operation operation;
+ /**
+ * The text associated with this diff operation.
+ */
+ public String text;
+
+ /**
+ * Constructor. Initializes the diff with the provided values.
+ * @param operation One of INSERT, DELETE or EQUAL.
+ * @param text The text being applied.
+ */
+ public Diff(Operation operation, String text) {
+ // Construct a diff with the specified operation and text.
+ this.operation = operation;
+ this.text = text;
+ }
+
+ /**
+ * Display a human-readable version of this Diff.
+ * @return text version.
+ */
+ public String toString() {
+ String prettyText = this.text.replace('\n', '\u00b6');
+ return "Diff(" + this.operation + ",\"" + prettyText + "\")";
+ }
+
+ /**
+ * Create a numeric hash value for a Diff.
+ * This function is not used by DMP.
+ * @return Hash value.
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = (operation == null) ? 0 : operation.hashCode();
+ result += prime * ((text == null) ? 0 : text.hashCode());
+ return result;
+ }
+
+ /**
+ * Is this Diff equivalent to another Diff?
+ * @param obj Another Diff to compare against.
+ * @return true or false.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ Diff other = (Diff) obj;
+ if (operation != other.operation) {
+ return false;
+ }
+ if (text == null) {
+ if (other.text != null) {
+ return false;
+ }
+ } else if (!text.equals(other.text)) {
+ return false;
+ }
+ return true;
+ }
+ }
+
+
+ /**
+ * Class representing one patch operation.
+ */
+ public static class Patch {
+ public LinkedList diffs;
+ public int start1;
+ public int start2;
+ public int length1;
+ public int length2;
+
+ /**
+ * Constructor. Initializes with an empty list of diffs.
+ */
+ public Patch() {
+ this.diffs = new LinkedList();
+ }
+
+ /**
+ * Emulate GNU diff's format.
+ * Header: @@ -382,8 +481,9 @@
+ * Indices are printed as 1-based, not 0-based.
+ * @return The GNU diff string.
+ */
+ public String toString() {
+ String coords1, coords2;
+ if (this.length1 == 0) {
+ coords1 = this.start1 + ",0";
+ } else if (this.length1 == 1) {
+ coords1 = Integer.toString(this.start1 + 1);
+ } else {
+ coords1 = (this.start1 + 1) + "," + this.length1;
+ }
+ if (this.length2 == 0) {
+ coords2 = this.start2 + ",0";
+ } else if (this.length2 == 1) {
+ coords2 = Integer.toString(this.start2 + 1);
+ } else {
+ coords2 = (this.start2 + 1) + "," + this.length2;
+ }
+ StringBuilder text = new StringBuilder();
+ text.append("@@ -").append(coords1).append(" +").append(coords2)
+ .append(" @@\n");
+ // Escape the body of the patch with %xx notation.
+ for (Diff aDiff : this.diffs) {
+ switch (aDiff.operation) {
+ case INSERT:
+ text.append('+');
+ break;
+ case DELETE:
+ text.append('-');
+ break;
+ case EQUAL:
+ text.append(' ');
+ break;
+ }
+ try {
+ text.append(URLEncoder.encode(aDiff.text, "UTF-8").replace('+', ' '))
+ .append("\n");
+ } catch (UnsupportedEncodingException e) {
+ // Not likely on modern system.
+ throw new Error("This system does not support UTF-8.", e);
+ }
+ }
+ return unescapeForEncodeUriCompatability(text.toString());
+ }
+ }
+
+ /**
+ * Unescape selected chars for compatability with JavaScript's encodeURI.
+ * In speed critical applications this could be dropped since the
+ * receiving application will certainly decode these fine.
+ * Note that this function is case-sensitive. Thus "%3f" would not be
+ * unescaped. But this is ok because it is only called with the output of
+ * URLEncoder.encode which returns uppercase hex.
+ *
+ * Example: "%3F" -> "?", "%24" -> "$", etc.
+ *
+ * @param str The string to escape.
+ * @return The escaped string.
+ */
+ private static String unescapeForEncodeUriCompatability(String str) {
+ return str.replace("%21", "!").replace("%7E", "~")
+ .replace("%27", "'").replace("%28", "(").replace("%29", ")")
+ .replace("%3B", ";").replace("%2F", "/").replace("%3F", "?")
+ .replace("%3A", ":").replace("%40", "@").replace("%26", "&")
+ .replace("%3D", "=").replace("%2B", "+").replace("%24", "$")
+ .replace("%2C", ",").replace("%23", "#");
+ }
+}
diff --git a/mastodon/src/main/java/org/joinmastodon/android/AudioPlayerService.java b/mastodon/src/main/java/org/joinmastodon/android/AudioPlayerService.java
index 4a01c13f0..067446d3e 100644
--- a/mastodon/src/main/java/org/joinmastodon/android/AudioPlayerService.java
+++ b/mastodon/src/main/java/org/joinmastodon/android/AudioPlayerService.java
@@ -169,7 +169,8 @@ public class AudioPlayerService extends Service{
}
updateNotification(false, false);
- getSystemService(AudioManager.class).requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
+ int audiofocus = GlobalUserPreferences.overlayMedia ? AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK : AudioManager.AUDIOFOCUS_GAIN;
+ getSystemService(AudioManager.class).requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, audiofocus);
player=new MediaPlayer();
player.setOnPreparedListener(this::onPlayerPrepared);
@@ -265,7 +266,7 @@ public class AudioPlayerService extends Service{
private void updateNotification(boolean dismissable, boolean removeNotification){
Notification.Builder bldr=new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_ntf_logo)
- .setContentTitle(status.account.displayName)
+ .setContentTitle(status.account.getDisplayName())
.setContentText(HtmlParser.strip(status.content))
.setOngoing(!dismissable)
.setShowWhen(false)
@@ -280,7 +281,7 @@ public class AudioPlayerService extends Service{
if(playerReady){
boolean isPlaying=player.isPlaying();
- bldr.addAction(new Notification.Action.Builder(Icon.createWithResource(this, isPlaying ? R.drawable.ic_pause_24 : R.drawable.ic_play_24),
+ bldr.addAction(new Notification.Action.Builder(Icon.createWithResource(this, isPlaying ? R.drawable.ic_fluent_pause_24_filled : R.drawable.ic_fluent_play_24_filled),
getString(isPlaying ? R.string.pause : R.string.play),
PendingIntent.getBroadcast(this, 2, new Intent(ACTION_PLAY_PAUSE), PendingIntent.FLAG_IMMUTABLE))
.build());
diff --git a/mastodon/src/main/java/org/joinmastodon/android/ExitActivity.java b/mastodon/src/main/java/org/joinmastodon/android/ExitActivity.java
new file mode 100644
index 000000000..54a5ccbc4
--- /dev/null
+++ b/mastodon/src/main/java/org/joinmastodon/android/ExitActivity.java
@@ -0,0 +1,24 @@
+package org.joinmastodon.android;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class ExitActivity extends Activity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ finishAndRemoveTask();
+ }
+
+ public static void exit(Context context) {
+ Intent intent = new Intent(context, ExitActivity.class);
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
+ context.startActivity(intent);
+ }
+
+}
diff --git a/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java b/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java
index 218c16f62..382456256 100644
--- a/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java
+++ b/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java
@@ -3,21 +3,25 @@ package org.joinmastodon.android;
import android.app.Fragment;
import android.content.ClipData;
import android.content.Intent;
-import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
+import android.util.Pair;
import android.widget.Toast;
+import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.api.session.AccountSession;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.fragments.ComposeFragment;
-import org.joinmastodon.android.ui.M3AlertDialogBuilder;
+import org.joinmastodon.android.ui.AccountSwitcherSheet;
import org.joinmastodon.android.ui.utils.UiUtils;
+import org.jsoup.internal.StringUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
+import java.util.function.BiConsumer;
import androidx.annotation.Nullable;
import me.grishka.appkit.FragmentStackActivity;
@@ -28,21 +32,51 @@ public class ExternalShareActivity extends FragmentStackActivity{
UiUtils.setUserPreferredTheme(this);
super.onCreate(savedInstanceState);
if(savedInstanceState==null){
+ Optional text = Optional.ofNullable(getIntent().getStringExtra(Intent.EXTRA_TEXT));
+ Optional>> fediHandle = text.flatMap(UiUtils::parseFediverseHandle);
+ boolean isFediUrl = text.map(UiUtils::looksLikeFediverseUrl).orElse(false);
+ boolean isOpenable = isFediUrl || fediHandle.isPresent();
+
List sessions=AccountSessionManager.getInstance().getLoggedInAccounts();
- if(sessions.isEmpty()){
+ if (sessions.isEmpty()){
Toast.makeText(this, R.string.err_not_logged_in, Toast.LENGTH_SHORT).show();
finish();
- }else if(sessions.size()==1){
+ } else if (isOpenable || sessions.size() > 1) {
+ AccountSwitcherSheet sheet = new AccountSwitcherSheet(this, null, true, isOpenable);
+ sheet.setOnClick((accountId, open) -> {
+ if (open && text.isPresent()) {
+ BiConsumer, Bundle> callback = (clazz, args) -> {
+ if (clazz == null) {
+ Toast.makeText(this, R.string.sk_open_in_app_failed, Toast.LENGTH_SHORT).show();
+ // TODO: do something about the window getting leaked
+ sheet.dismiss();
+ finish();
+ return;
+ }
+ args.putString("fromExternalShare", clazz.getSimpleName());
+ Intent intent = new Intent(this, MainActivity.class);
+ intent.putExtras(args);
+ finish();
+ startActivity(intent);
+ };
+
+ fediHandle
+ .>map(handle ->
+ UiUtils.lookupAccountHandle(this, accountId, handle, callback))
+ .or(() ->
+ UiUtils.lookupURL(this, accountId, text.get(), callback))
+ .ifPresent(req ->
+ req.wrapProgress(this, R.string.loading, true, d -> {
+ UiUtils.transformDialogForLookup(this, accountId, isFediUrl ? text.get() : null, d);
+ d.setOnDismissListener((x) -> finish());
+ }));
+ } else {
+ openComposeFragment(accountId);
+ }
+ });
+ sheet.show();
+ } else if (sessions.size() == 1) {
openComposeFragment(sessions.get(0).getID());
- }else{
- getWindow().setBackgroundDrawable(new ColorDrawable(0xff000000));
- new M3AlertDialogBuilder(this)
- .setItems(sessions.stream().map(as->"@"+as.self.username+"@"+as.domain).toArray(String[]::new), (dialog, which)->{
- openComposeFragment(sessions.get(which).getID());
- })
- .setTitle(R.string.choose_account)
- .setOnCancelListener(dialog -> finish())
- .show();
}
}
}
@@ -51,7 +85,20 @@ public class ExternalShareActivity extends FragmentStackActivity{
getWindow().setBackgroundDrawable(null);
Intent intent=getIntent();
- String text=intent.getStringExtra(Intent.EXTRA_TEXT);
+ StringBuilder builder=new StringBuilder();
+ String subject = "";
+ if (intent.hasExtra(Intent.EXTRA_SUBJECT)) {
+ subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
+ if (!StringUtil.isBlank(subject)) builder.append(subject).append("\n\n");
+ }
+ if (intent.hasExtra(Intent.EXTRA_TEXT)) {
+ String extra = intent.getStringExtra(Intent.EXTRA_TEXT);
+ if (!StringUtil.isBlank(extra)) {
+ if (extra.startsWith(subject)) extra = extra.substring(subject.length()).trim();
+ builder.append(extra).append("\n\n");
+ }
+ }
+ String text=builder.toString();
List mediaUris;
if(Intent.ACTION_SEND.equals(intent.getAction())){
Uri singleUri=intent.getParcelableExtra(Intent.EXTRA_STREAM);
@@ -77,6 +124,7 @@ public class ExternalShareActivity extends FragmentStackActivity{
args.putString("account", accountID);
if(!TextUtils.isEmpty(text))
args.putString("prefilledText", text);
+ args.putInt("selectionStart", StringUtil.isBlank(subject) ? 0 : subject.length());
if(mediaUris!=null && !mediaUris.isEmpty())
args.putParcelableArrayList("mediaAttachments", toArrayList(mediaUris));
Fragment fragment=new ComposeFragment();
diff --git a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java
index 7be513f96..fd0f2420f 100644
--- a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java
+++ b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java
@@ -1,34 +1,182 @@
package org.joinmastodon.android;
+import static org.joinmastodon.android.api.MastodonAPIController.gson;
+import static org.joinmastodon.android.api.session.AccountLocalPreferences.ColorPreference.MATERIAL3;
+
import android.content.Context;
import android.content.SharedPreferences;
+import android.util.Log;
+
+import androidx.annotation.StringRes;
+import android.os.Build;
+
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.reflect.TypeToken;
+
+import org.joinmastodon.android.api.session.AccountLocalPreferences;
+import org.joinmastodon.android.api.session.AccountLocalPreferences.ColorPreference;
+import org.joinmastodon.android.api.session.AccountSession;
+import org.joinmastodon.android.api.session.AccountSessionManager;
+import org.joinmastodon.android.model.ContentType;
+import org.joinmastodon.android.model.Instance;
+import org.joinmastodon.android.model.TimelineDefinition;
+
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.model.Account;
public class GlobalUserPreferences{
+ private static final String TAG="GlobalUserPreferences";
+
public static boolean playGifs;
public static boolean useCustomTabs;
public static boolean altTextReminders, confirmUnfollow, confirmBoost, confirmDeletePost;
public static ThemePreference theme;
- private static SharedPreferences getPrefs(){
+ // MEGALODON
+ public static boolean trueBlackTheme;
+ public static boolean loadNewPosts;
+ public static boolean showNewPostsButton;
+ public static boolean toolbarMarquee;
+ public static boolean disableSwipe;
+ public static boolean enableDeleteNotifications;
+ public static boolean translateButtonOpenedOnly;
+ public static boolean uniformNotificationIcon;
+ public static boolean reduceMotion;
+ public static boolean showAltIndicator;
+ public static boolean showNoAltIndicator;
+ public static boolean enablePreReleases;
+ public static PrefixRepliesMode prefixReplies;
+ public static boolean collapseLongPosts;
+ public static boolean spectatorMode;
+ public static boolean autoHideFab;
+ public static boolean allowRemoteLoading;
+ public static boolean forwardReportDefault;
+ public static AutoRevealMode autoRevealEqualSpoilers;
+ public static boolean disableM3PillActiveIndicator;
+ public static boolean showNavigationLabels;
+ public static boolean displayPronounsInTimelines, displayPronounsInThreads, displayPronounsInUserListings;
+ public static boolean overlayMedia;
+ public static boolean showSuicideHelp;
+ public static boolean underlinedLinks;
+ public static ColorPreference color;
+ public static boolean likeIcon;
+
+ // MOSHIDON
+ public static boolean showDividers;
+ public static boolean relocatePublishButton;
+ public static boolean defaultToUnlistedReplies;
+ public static boolean doubleTapToSearch;
+ public static boolean doubleTapToSwipe;
+ public static boolean confirmBeforeReblog;
+ public static boolean hapticFeedback;
+ public static boolean replyLineAboveHeader;
+ public static boolean swapBookmarkWithBoostAction;
+ public static boolean loadRemoteAccountFollowers;
+ public static boolean mentionRebloggerAutomatically;
+ public static boolean showPostsWithoutAlt;
+ public static boolean showMediaPreview;
+
+ public static SharedPreferences getPrefs(){
return MastodonApp.context.getSharedPreferences("global", Context.MODE_PRIVATE);
}
- private static SharedPreferences getPreReplyPrefs(){
- return MastodonApp.context.getSharedPreferences("pre_reply_sheets", Context.MODE_PRIVATE);
+ public static T fromJson(String json, Type type, T orElse){
+ if(json==null) return orElse;
+ try{
+ T value=gson.fromJson(json, type);
+ return value==null ? orElse : value;
+ }catch(JsonSyntaxException ignored){
+ return orElse;
+ }
+ }
+
+ public static > T enumValue(Class enumType, String name) {
+ try { return Enum.valueOf(enumType, name); }
+ catch (NullPointerException npe) { return null; }
}
public static void load(){
SharedPreferences prefs=getPrefs();
+
playGifs=prefs.getBoolean("playGifs", true);
useCustomTabs=prefs.getBoolean("useCustomTabs", true);
- altTextReminders=prefs.getBoolean("altTextReminders", false);
- confirmUnfollow=prefs.getBoolean("confirmUnfollow", false);
+ theme=ThemePreference.values()[prefs.getInt("theme", 0)];
+ altTextReminders=prefs.getBoolean("altTextReminders", true);
+ confirmUnfollow=prefs.getBoolean("confirmUnfollow", true);
confirmBoost=prefs.getBoolean("confirmBoost", false);
confirmDeletePost=prefs.getBoolean("confirmDeletePost", true);
+
+ // MEGALODON
+ trueBlackTheme=prefs.getBoolean("trueBlackTheme", false);
+ loadNewPosts=prefs.getBoolean("loadNewPosts", true);
+ showNewPostsButton=prefs.getBoolean("showNewPostsButton", true);
+ toolbarMarquee=prefs.getBoolean("toolbarMarquee", true);
+ disableSwipe=prefs.getBoolean("disableSwipe", false);
+ enableDeleteNotifications=prefs.getBoolean("enableDeleteNotifications", false);
+ translateButtonOpenedOnly=prefs.getBoolean("translateButtonOpenedOnly", false);
+ uniformNotificationIcon=prefs.getBoolean("uniformNotificationIcon", false);
+ reduceMotion=prefs.getBoolean("reduceMotion", false);
+ showAltIndicator=prefs.getBoolean("showAltIndicator", true);
+ showNoAltIndicator=prefs.getBoolean("showNoAltIndicator", true);
+ enablePreReleases=prefs.getBoolean("enablePreReleases", false);
+ prefixReplies=PrefixRepliesMode.valueOf(prefs.getString("prefixReplies", PrefixRepliesMode.NEVER.name()));
+ collapseLongPosts=prefs.getBoolean("collapseLongPosts", true);
+ spectatorMode=prefs.getBoolean("spectatorMode", false);
+ autoHideFab=prefs.getBoolean("autoHideFab", true);
+ allowRemoteLoading=prefs.getBoolean("allowRemoteLoading", true);
+ autoRevealEqualSpoilers=AutoRevealMode.valueOf(prefs.getString("autoRevealEqualSpoilers", AutoRevealMode.THREADS.name()));
+ forwardReportDefault=prefs.getBoolean("forwardReportDefault", true);
+ disableM3PillActiveIndicator=prefs.getBoolean("disableM3PillActiveIndicator", false);
+ showNavigationLabels=prefs.getBoolean("showNavigationLabels", true);
+ displayPronounsInTimelines=prefs.getBoolean("displayPronounsInTimelines", true);
+ displayPronounsInThreads=prefs.getBoolean("displayPronounsInThreads", true);
+ displayPronounsInUserListings=prefs.getBoolean("displayPronounsInUserListings", true);
+ overlayMedia=prefs.getBoolean("overlayMedia", false);
+ showSuicideHelp=prefs.getBoolean("showSuicideHelp", true);
+ underlinedLinks=prefs.getBoolean("underlinedLinks", true);
+ color=ColorPreference.valueOf(prefs.getString("color", MATERIAL3.name()));
+ likeIcon=prefs.getBoolean("likeIcon", false);
+
+ // MOSHIDON
+ uniformNotificationIcon=prefs.getBoolean("uniformNotificationIcon", false);
+ showDividers =prefs.getBoolean("showDividers", false);
+ relocatePublishButton=prefs.getBoolean("relocatePublishButton", true);
+ defaultToUnlistedReplies=prefs.getBoolean("defaultToUnlistedReplies", false);
+ doubleTapToSearch =prefs.getBoolean("doubleTapToSearch", true);
+ doubleTapToSwipe =prefs.getBoolean("doubleTapToSwipe", true);
+ replyLineAboveHeader=prefs.getBoolean("replyLineAboveHeader", true);
+ confirmBeforeReblog=prefs.getBoolean("confirmBeforeReblog", false);
+ hapticFeedback=prefs.getBoolean("hapticFeedback", true);
+ swapBookmarkWithBoostAction=prefs.getBoolean("swapBookmarkWithBoostAction", false);
+ loadRemoteAccountFollowers=prefs.getBoolean("loadRemoteAccountFollowers", true);
+ mentionRebloggerAutomatically=prefs.getBoolean("mentionRebloggerAutomatically", false);
+ showPostsWithoutAlt=prefs.getBoolean("showPostsWithoutAlt", true);
+ showMediaPreview=prefs.getBoolean("showMediaPreview", true);
+
theme=ThemePreference.values()[prefs.getInt("theme", 0)];
+
+
+ if (prefs.contains("prefixRepliesWithRe")) {
+ prefixReplies = prefs.getBoolean("prefixRepliesWithRe", false)
+ ? PrefixRepliesMode.TO_OTHERS : PrefixRepliesMode.NEVER;
+ prefs.edit()
+ .putString("prefixReplies", prefixReplies.name())
+ .remove("prefixRepliesWithRe")
+ .apply();
+ }
+
+ int migrationLevel=prefs.getInt("migrationLevel", BuildConfig.VERSION_CODE);
+ if(migrationLevel < 61)
+ migrateToUpstreamVersion61();
+ if(migrationLevel < BuildConfig.VERSION_CODE)
+ prefs.edit().putInt("migrationLevel", BuildConfig.VERSION_CODE).apply();
}
public static void save(){
@@ -40,45 +188,126 @@ public class GlobalUserPreferences{
.putBoolean("confirmUnfollow", confirmUnfollow)
.putBoolean("confirmBoost", confirmBoost)
.putBoolean("confirmDeletePost", confirmDeletePost)
+
+ // MEGALODON
+ .putBoolean("loadNewPosts", loadNewPosts)
+ .putBoolean("showNewPostsButton", showNewPostsButton)
+ .putBoolean("trueBlackTheme", trueBlackTheme)
+ .putBoolean("toolbarMarquee", toolbarMarquee)
+ .putBoolean("disableSwipe", disableSwipe)
+ .putBoolean("enableDeleteNotifications", enableDeleteNotifications)
+ .putBoolean("translateButtonOpenedOnly", translateButtonOpenedOnly)
+ .putBoolean("uniformNotificationIcon", uniformNotificationIcon)
+ .putBoolean("reduceMotion", reduceMotion)
+ .putBoolean("showAltIndicator", showAltIndicator)
+ .putBoolean("showNoAltIndicator", showNoAltIndicator)
+ .putBoolean("enablePreReleases", enablePreReleases)
+ .putString("prefixReplies", prefixReplies.name())
+ .putBoolean("collapseLongPosts", collapseLongPosts)
+ .putBoolean("spectatorMode", spectatorMode)
+ .putBoolean("autoHideFab", autoHideFab)
+ .putBoolean("allowRemoteLoading", allowRemoteLoading)
+ .putString("autoRevealEqualSpoilers", autoRevealEqualSpoilers.name())
+ .putBoolean("forwardReportDefault", forwardReportDefault)
+ .putBoolean("disableM3PillActiveIndicator", disableM3PillActiveIndicator)
+ .putBoolean("showNavigationLabels", showNavigationLabels)
+ .putBoolean("displayPronounsInTimelines", displayPronounsInTimelines)
+ .putBoolean("displayPronounsInThreads", displayPronounsInThreads)
+ .putBoolean("displayPronounsInUserListings", displayPronounsInUserListings)
+ .putBoolean("overlayMedia", overlayMedia)
+ .putBoolean("showSuicideHelp", showSuicideHelp)
+ .putBoolean("underlinedLinks", underlinedLinks)
+ .putString("color", color.name())
+ .putBoolean("likeIcon", likeIcon)
+
+ // MOSHIDON
+ .putBoolean("defaultToUnlistedReplies", defaultToUnlistedReplies)
+ .putBoolean("doubleTapToSearch", doubleTapToSearch)
+ .putBoolean("doubleTapToSwipe", doubleTapToSwipe)
+ .putBoolean("replyLineAboveHeader", replyLineAboveHeader)
+ .putBoolean("confirmBeforeReblog", confirmBeforeReblog)
+ .putBoolean("swapBookmarkWithBoostAction", swapBookmarkWithBoostAction)
+ .putBoolean("loadRemoteAccountFollowers", loadRemoteAccountFollowers)
+ .putBoolean("hapticFeedback", hapticFeedback)
+ .putBoolean("mentionRebloggerAutomatically", mentionRebloggerAutomatically)
+ .putBoolean("showDividers", showDividers)
+ .putBoolean("relocatePublishButton", relocatePublishButton)
+ .putBoolean("enableDeleteNotifications", enableDeleteNotifications)
+ .putBoolean("showPostsWithoutAlt", showPostsWithoutAlt)
+ .putBoolean("showMediaPreview", showMediaPreview)
+
.apply();
}
- public static boolean isOptedOutOfPreReplySheet(PreReplySheetType type, Account account, String accountID){
- if(getPreReplyPrefs().getBoolean("opt_out_"+type, false))
- return true;
- if(account==null)
- return false;
- String accountKey=account.acct;
- if(!accountKey.contains("@"))
- accountKey+="@"+AccountSessionManager.get(accountID).domain;
- return getPreReplyPrefs().getBoolean("opt_out_"+type+"_"+accountKey.toLowerCase(), false);
- }
-
- public static void optOutOfPreReplySheet(PreReplySheetType type, Account account, String accountID){
- String key;
- if(account==null){
- key="opt_out_"+type;
- }else{
- String accountKey=account.acct;
- if(!accountKey.contains("@"))
- accountKey+="@"+AccountSessionManager.get(accountID).domain;
- key="opt_out_"+type+"_"+accountKey.toLowerCase();
- }
- getPreReplyPrefs().edit().putBoolean(key, true).apply();
- }
-
- public static void resetPreReplySheets(){
- getPreReplyPrefs().edit().clear().apply();
- }
-
public enum ThemePreference{
AUTO,
LIGHT,
DARK
}
- public enum PreReplySheetType{
- OLD_POST,
- NON_MUTUAL
+ public enum AutoRevealMode {
+ NEVER,
+ THREADS,
+ DISCUSSIONS
}
+
+ public enum PrefixRepliesMode {
+ NEVER,
+ ALWAYS,
+ TO_OTHERS
+ }
+
+
+ //region preferences migrations
+
+ private static void migrateToUpstreamVersion61(){
+ Log.d(TAG, "Migrating preferences to upstream version 61!!");
+
+ Type accountsDefaultContentTypesType = new TypeToken