summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/activities/CustomSharingActivity.java
blob: ee3cf8b856f3f580a9733bb77752bde7a63f29a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package app.fedilab.android.mastodon.activities;
/* Copyright 2022 Thomas Schneider
 *
 * This file is a part of Fedilab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */

import static app.fedilab.android.BaseMainActivity.currentAccount;

import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.preference.PreferenceManager;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import app.fedilab.android.R;
import app.fedilab.android.databinding.ActivityCustomSharingBinding;
import app.fedilab.android.mastodon.client.entities.api.Attachment;
import app.fedilab.android.mastodon.client.entities.api.Emoji;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.api.Tag;
import app.fedilab.android.mastodon.client.entities.app.CachedBundle;
import app.fedilab.android.mastodon.helper.Helper;
import app.fedilab.android.mastodon.helper.customsharing.CustomSharingAsyncTask;
import app.fedilab.android.mastodon.helper.customsharing.CustomSharingResponse;
import app.fedilab.android.mastodon.helper.customsharing.OnCustomSharingInterface;
import es.dmoral.toasty.Toasty;


/**
 * Created by Curtis on 13/02/2019.
 * Share status metadata to remote content aggregators
 */

public class CustomSharingActivity extends BaseBarActivity implements OnCustomSharingInterface {

    private String title, keywords, custom_sharing_url, encodedCustomSharingURL;
    private String bundle_url;
    private String bundle_source;
    private String bundle_id;
    private String bundle_content;
    private String bundle_thumbnailurl;
    private String bundle_creator;
    private ActivityCustomSharingBinding binding;
    private Status status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        binding = ActivityCustomSharingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        Bundle args = getIntent().getExtras();
        status = null;
        if (args != null) {
            long bundleId = args.getLong(Helper.ARG_INTENT_ID, -1);
            new CachedBundle(CustomSharingActivity.this).getBundle(bundleId, currentAccount, this::initializeAfterBundle);
        } else {
            initializeAfterBundle(null);
        }

    }

    private void initializeAfterBundle(Bundle bundle) {

        if (bundle != null) {
            status = (Status) bundle.getSerializable(Helper.ARG_STATUS);
        }
        if (status == null) {
            finish();
            return;
        }
        SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(CustomSharingActivity.this);
        bundle_creator = status.account.acct;
        bundle_url = status.url;
        bundle_id = status.uri;
        bundle_source = status.account.url;
        String bundle_tags = getTagsString();
        bundle_content = formatedContent(status.content, status.emojis);
        if (status.card != null && status.card.image != null) {
            bundle_thumbnailurl = status.card.image;
        } else if (status.media_attachments != null && status.media_attachments.size() > 0) {
            List<Attachment> mediaAttachments = status.media_attachments;
            Attachment firstAttachment = mediaAttachments.get(0);
            bundle_thumbnailurl = firstAttachment.preview_url;
        } else {
            bundle_thumbnailurl = status.account.avatar;
        }
        if (!bundle_creator.contains("@")) {
            bundle_creator = bundle_creator + "@" + currentAccount.instance;
        }

        binding.setCustomSharingTitle.setEllipsize(TextUtils.TruncateAt.END);
        //set text on title, description, and keywords
        String[] lines = bundle_content.split("\n");
        //Remove tags in title
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            lines[0] = Html.fromHtml(lines[0], Html.FROM_HTML_MODE_LEGACY).toString();
        else
            lines[0] = Html.fromHtml(lines[0]).toString();
        String newTitle;
        if (lines[0].length() > 60) {
            newTitle = lines[0].substring(0, 60) + '…';
        } else {
            newTitle = lines[0];
        }
        binding.setCustomSharingTitle.setText(newTitle);
        String newDescription;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            newDescription = Html.fromHtml(bundle_content, Html.FROM_HTML_MODE_LEGACY).toString();
        else
            newDescription = Html.fromHtml(bundle_content).toString();

        binding.setCustomSharingDescription.setText(newDescription);
        binding.setCustomSharingKeywords.setText(bundle_tags);
        binding.setCustomSharingSave.setOnClickListener(v -> {
            // obtain title, description, keywords
            title = binding.setCustomSharingTitle.getText().toString();
            keywords = Objects.requireNonNull(binding.setCustomSharingKeywords.getText()).toString();
            CharSequence comma_only = ",";
            CharSequence space_only = " ";
            CharSequence double_space = "  ";
            keywords = keywords.replace(comma_only, space_only);
            keywords = keywords.replace(double_space, space_only);
            // Create encodedCustomSharingURL
            custom_sharing_url = sharedpreferences.getString(getString(R.string.SET_CUSTOM_SHARING_URL),
                    "http://example.net/add?token=YOUR_TOKEN&url=${url}&title=${title}" +
                            "&source=${source}&id=${id}&description=${description}&keywords=${keywords}&creator=${creator}&thumbnailurl=${thumbnailurl}");
            encodedCustomSharingURL = encodeCustomSharingURL();
            new CustomSharingAsyncTask(CustomSharingActivity.this, encodedCustomSharingURL, CustomSharingActivity.this);
        });
    }

    private String getTagsString() {
        //iterate through tags and create comma delimited string of tag names
        StringBuilder tag_names = new StringBuilder();
        for (Tag t : status.tags) {
            if (tag_names.toString().equals("")) {
                tag_names = new StringBuilder(t.name);
            } else {
                tag_names.append(", ").append(t.name);
            }
        }
        return tag_names.toString();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCustomSharing(CustomSharingResponse customSharingResponse) {
        binding.setCustomSharingSave.setEnabled(true);
        if (customSharingResponse.getError() != null) {
            Toasty.error(CustomSharingActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
            return;
        }
        String response = customSharingResponse.getResponse();
        Toasty.success(CustomSharingActivity.this, response, Toast.LENGTH_LONG).show();
        finish();
    }

    public String encodeCustomSharingURL() {
        Uri uri = Uri.parse(custom_sharing_url);
        String protocol = uri.getScheme();
        String server = uri.getAuthority();
        String path = uri.getPath();
        if (path != null) {
            path = path.replaceAll("/", "");
        }
        Uri.Builder builder = new Uri.Builder();
        builder.scheme(protocol)
                .authority(server)
                .appendPath(path);
        Set<String> args = uri.getQueryParameterNames();
        boolean paramFound;
        for (String param_name : args) {
            paramFound = false;
            String param_value = uri.getQueryParameter(param_name);
            if (param_value != null)
                switch (param_value) {
                    case "${url}" -> {
                        paramFound = true;
                        builder.appendQueryParameter(param_name, bundle_url);
                    }
                    case "${title}" -> {
                        paramFound = true;
                        builder.appendQueryParameter(param_name, title