diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/CustomSharingActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/CustomSharingActivity.java index ea35d405f..537c351c8 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/CustomSharingActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/CustomSharingActivity.java @@ -15,68 +15,34 @@ package fr.gouv.etalab.mastodon.activities; -import android.Manifest; import android.annotation.SuppressLint; -import android.app.Activity; -import android.content.DialogInterface; -import android.content.Intent; import android.content.SharedPreferences; -import android.content.pm.PackageManager; import android.database.sqlite.SQLiteDatabase; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; -import android.media.ThumbnailUtils; import android.net.Uri; import android.os.AsyncTask; -import android.os.Build; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.app.ActivityCompat; -import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; -import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; -import android.text.Editable; -import android.text.Html; -import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; -import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; +import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; -import com.bumptech.glide.Glide; - -import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; +import java.util.Set; import es.dmoral.toasty.Toasty; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.asynctasks.CustomSharingAsyncTask; -import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountInfoAsyncTask; -import fr.gouv.etalab.mastodon.asynctasks.UpdateCredentialAsyncTask; -import fr.gouv.etalab.mastodon.client.API; -import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.CustomSharingResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; -import fr.gouv.etalab.mastodon.client.Entities.Error; -import fr.gouv.etalab.mastodon.client.Entities.Version; -import fr.gouv.etalab.mastodon.client.Glide.GlideApp; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnCustomSharingInterface; -import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountInterface; -import fr.gouv.etalab.mastodon.interfaces.OnUpdateCredentialInterface; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -84,8 +50,8 @@ import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT; /** - * Created by Thomas on 27/08/2017. - * Edit profile activity + * Created by Curtis on 13/02/2019. + * Share status metadata to remote content aggregators */ public class CustomSharingActivity extends BaseActivity implements OnCustomSharingInterface { @@ -93,7 +59,7 @@ public class CustomSharingActivity extends BaseActivity implements OnCustomShari private EditText set_custom_sharing_title, set_custom_sharing_description, set_custom_sharing_keywords; private Button set_custom_sharing_save; private ImageView pp_actionBar; - private String title, description, keywords, encodedCustomSharingURL; + private String title, description, keywords, custom_sharing_url, encodedCustomSharingURL; private String bundle_url, bundle_source, bundle_id, bundle_tags, bundle_content; @Override protected void onCreate(Bundle savedInstanceState) { @@ -154,19 +120,49 @@ public class CustomSharingActivity extends BaseActivity implements OnCustomShari Helper.loadGiF(getApplicationContext(), url, pp_actionBar); Bundle b = getIntent().getExtras(); if(b != null) { - bundle_url = b.getParcelable("url"); - bundle_id = b.getParcelable("id"); - bundle_source = b.getParcelable("source"); - bundle_tags = b.getParcelable("tags"); - bundle_content = b.getParcelable("content"); + bundle_url = b.getString("url"); + bundle_id = b.getString("id"); + bundle_source = b.getString("source"); + bundle_tags = b.getString("tags"); + bundle_content = b.getString("content"); } set_custom_sharing_title = findViewById(R.id.set_custom_sharing_title); set_custom_sharing_description = findViewById(R.id.set_custom_sharing_description); set_custom_sharing_keywords = findViewById(R.id.set_custom_sharing_keywords); + + //set text on title, description, and keywords + System.out.println("Content: " + bundle_content); + String[] lines = bundle_content.split("\n"); + set_custom_sharing_title.setText(lines[0]); + if (lines.length >= 2) { + String newDescription = ""; + int i = 0; + for (String line: lines) { + i += 1; + if (i > 1) { + newDescription = newDescription + line; + } + } + set_custom_sharing_description.setText(newDescription); + } + set_custom_sharing_keywords.setText(bundle_tags); set_custom_sharing_save = findViewById(R.id.set_custom_sharing_save); set_custom_sharing_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + // obtain title, description, keywords + title = set_custom_sharing_title.getText().toString(); + description = set_custom_sharing_description.getText().toString(); + keywords = set_custom_sharing_keywords.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(Helper.SET_CUSTOM_SHARING_URL, + "http://my.site/add?user=fedilab&url=${url}&title=${title}&source=${source}&id=${id}&description=${description}&keywords=${keywords}"); + encodedCustomSharingURL = encodeCustomSharingURL(custom_sharing_url, bundle_url, bundle_id, bundle_source, title, description, keywords); new CustomSharingAsyncTask(getApplicationContext(), encodedCustomSharingURL, CustomSharingActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }); @@ -190,8 +186,78 @@ public class CustomSharingActivity extends BaseActivity implements OnCustomShari Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show(); return; } - Toasty.success(getApplicationContext(), getString(R.string.toast_update_credential_ok), Toast.LENGTH_LONG).show(); + String response = customSharingResponse.getResponse(); + Toasty.success(getApplicationContext(), response, Toast.LENGTH_LONG).show(); finish(); } + public String encodeCustomSharingURL(String custom_sharing_url, String bundle_url, String bundle_id, String bundle_source, String title, String description, String keywords) { + String url_user = ""; + String url_param_url = ""; + String url_param_title = ""; + String url_param_source = ""; + String url_param_id = ""; + String url_param_description = ""; + String url_param_keywords = ""; + 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("/", ""); + } + Set args = uri.getQueryParameterNames(); + for (String param_name : args) { + if (param_name.equals("user")) { + url_user = uri.getQueryParameter("user"); + } + String param_value = uri.getQueryParameter(param_name); + switch(param_value) { + case "${url}": + url_param_url = param_name; + break; + case "${title}": + url_param_title = param_name; + break; + case "${source}": + url_param_source = param_name; + break; + case "${id}": + url_param_id = param_name; + break; + case "${description}": + url_param_description = param_name; + break; + case "${keywords}": + url_param_keywords = param_name; + break; + } + } + Uri.Builder builder = new Uri.Builder(); + builder.scheme(protocol) + .authority(server) + .appendPath(path); + if (!url_user.equals("")) { + builder.appendQueryParameter("user", url_user); + } + if (!url_param_url.equals("")) { + builder.appendQueryParameter(url_param_url, bundle_url); + } + if (!url_param_title.equals("")) { + builder.appendQueryParameter(url_param_title, title); + } + if (!url_param_source.equals("")) { + builder.appendQueryParameter(url_param_source, bundle_source); + } + if (!url_param_id.equals("")) { + builder.appendQueryParameter(url_param_id, bundle_id); + } + if (!url_param_description.equals("")) { + builder.appendQueryParameter(url_param_description, description); + } + if (!url_param_keywords.equals("")) { + builder.appendQueryParameter(url_param_keywords, keywords); + } + return builder.build().toString(); + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/CustomSharingAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/CustomSharingAsyncTask.java index bf977a9b3..1c77c549a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/CustomSharingAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/CustomSharingAsyncTask.java @@ -18,16 +18,10 @@ package fr.gouv.etalab.mastodon.asynctasks; import android.content.Context; import android.os.AsyncTask; -import java.io.ByteArrayInputStream; import java.lang.ref.WeakReference; -import java.util.HashMap; - -import fr.gouv.etalab.mastodon.client.API; -import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.CustomSharing; import fr.gouv.etalab.mastodon.client.CustomSharingResponse; import fr.gouv.etalab.mastodon.interfaces.OnCustomSharingInterface; -import fr.gouv.etalab.mastodon.interfaces.OnUpdateCredentialInterface; /** * Created by Curtis on 13/02/2019. diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharing.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharing.java index 2fcb96c3c..ef7370e3d 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharing.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharing.java @@ -1,5 +1,5 @@ package fr.gouv.etalab.mastodon.client; -/* Copyright 2017 Thomas Schneider +/* Copyright 2019 Curtis Rock * * This file is a part of Mastalab * @@ -15,61 +15,14 @@ package fr.gouv.etalab.mastodon.client; * see . */ import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.database.sqlite.SQLiteDatabase; -import android.os.Bundle; -import android.support.v4.content.LocalBroadcastManager; - -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import fr.gouv.etalab.mastodon.R; -import fr.gouv.etalab.mastodon.activities.MainActivity; -import fr.gouv.etalab.mastodon.asynctasks.RetrieveOpenCollectiveAsyncTask; -import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask; -import fr.gouv.etalab.mastodon.client.Entities.Account; -import fr.gouv.etalab.mastodon.client.Entities.Application; -import fr.gouv.etalab.mastodon.client.Entities.Attachment; -import fr.gouv.etalab.mastodon.client.Entities.Card; -import fr.gouv.etalab.mastodon.client.Entities.Conversation; -import fr.gouv.etalab.mastodon.client.Entities.Emojis; import fr.gouv.etalab.mastodon.client.Entities.Error; -import fr.gouv.etalab.mastodon.client.Entities.Filters; -import fr.gouv.etalab.mastodon.client.Entities.HowToVideo; -import fr.gouv.etalab.mastodon.client.Entities.Instance; -import fr.gouv.etalab.mastodon.client.Entities.InstanceNodeInfo; -import fr.gouv.etalab.mastodon.client.Entities.InstanceSocial; -import fr.gouv.etalab.mastodon.client.Entities.Mention; -import fr.gouv.etalab.mastodon.client.Entities.NodeInfo; -import fr.gouv.etalab.mastodon.client.Entities.Notification; -import fr.gouv.etalab.mastodon.client.Entities.Peertube; -import fr.gouv.etalab.mastodon.client.Entities.Relationship; import fr.gouv.etalab.mastodon.client.Entities.Results; -import fr.gouv.etalab.mastodon.client.Entities.Schedule; -import fr.gouv.etalab.mastodon.client.Entities.Status; -import fr.gouv.etalab.mastodon.client.Entities.StoredStatus; -import fr.gouv.etalab.mastodon.client.Entities.Tag; -import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment; -import fr.gouv.etalab.mastodon.helper.Helper; -import fr.gouv.etalab.mastodon.sqlite.AccountDAO; -import fr.gouv.etalab.mastodon.sqlite.Sqlite; /** @@ -112,6 +65,7 @@ public class CustomSharing { } catch (KeyManagementException e) { e.printStackTrace(); } + customSharingResponse.setResponse(HTTPResponse); return customSharingResponse; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharingResponse.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharingResponse.java index 1874abd64..06e77ee0f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharingResponse.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/CustomSharingResponse.java @@ -1,5 +1,5 @@ package fr.gouv.etalab.mastodon.client; -/* Copyright 2017 Thomas Schneider +/* Copyright 2019 Curtis Rock * * This file is a part of Mastalab * @@ -14,189 +14,31 @@ package fr.gouv.etalab.mastodon.client; * You should have received a copy of the GNU General Public License along with Mastalab; if not, * see . */ -import android.content.Context; - -import java.util.List; - -import fr.gouv.etalab.mastodon.client.Entities.Account; -import fr.gouv.etalab.mastodon.client.Entities.Conversation; -import fr.gouv.etalab.mastodon.client.Entities.Emojis; import fr.gouv.etalab.mastodon.client.Entities.Error; -import fr.gouv.etalab.mastodon.client.Entities.Filters; -import fr.gouv.etalab.mastodon.client.Entities.HowToVideo; -import fr.gouv.etalab.mastodon.client.Entities.Instance; -import fr.gouv.etalab.mastodon.client.Entities.Notification; -import fr.gouv.etalab.mastodon.client.Entities.Peertube; -import fr.gouv.etalab.mastodon.client.Entities.PeertubeNotification; -import fr.gouv.etalab.mastodon.client.Entities.Relationship; -import fr.gouv.etalab.mastodon.client.Entities.Status; -import fr.gouv.etalab.mastodon.client.Entities.StoredStatus; /** - * Created by Thomas on 03/06/2017. - * Hydrate response from the API + * Created by Curtis on 13/02/2019. + * Hydrate response from the remote content aggregator for Custom Sharing */ public class CustomSharingResponse { - private List accounts = null; - private List statuses = null; - private List contexts = null; - private List conversations = null; - private List notifications = null; - private List relationships = null; - private List howToVideos = null; - private List peertubes = null; - private List peertubeNotifications = null; - private List filters = null; - private List domains = null; - private List lists = null; - private List emojis = null; private Error error = null; - private String since_id, max_id; - private Instance instance; - private List storedStatuses; - public List getAccounts() { - return accounts; - } - - public void setAccounts(List accounts) { - this.accounts = accounts; - } - - public List getStatuses() { - return statuses; - } - - public void setStatuses(List statuses) { - this.statuses = statuses; - } - - public List getContexts() { - return contexts; - } - - public void setContexts(List contexts) { - this.contexts = contexts; - } - - public List getNotifications() { - return notifications; - } - - public void setNotifications(List notifications) { - this.notifications = notifications; - } + private String response; public Error getError() { return error; } + public String getResponse() { + return response; + } + public void setError(Error error) { this.error = error; } - public String getMax_id() { - return max_id; - } - - public void setMax_id(String max_id) { - this.max_id = max_id; - } - - public String getSince_id() { - return since_id; - } - - public void setSince_id(String since_id) { - this.since_id = since_id; - } - - public Instance getInstance() { - return instance; - } - - public void setInstance(Instance instance) { - this.instance = instance; - } - - public List getRelationships() { - return relationships; - } - - public void setRelationships(List relationships) { - this.relationships = relationships; - } - - public List getEmojis() { - return emojis; - } - - public void setEmojis(List emojis) { - this.emojis = emojis; - } - - public List getLists() { - return lists; - } - - public void setLists(List lists) { - this.lists = lists; - } - - public List getFilters() { - return filters; - } - - public void setFilters(List filters) { - this.filters = filters; - } - - public List getDomains() { - return domains; - } - - public void setDomains(List domains) { - this.domains = domains; - } - - public List getHowToVideos() { - return howToVideos; - } - - public void setHowToVideos(List howToVideos) { - this.howToVideos = howToVideos; - } - - public List getPeertubes() { - return peertubes; - } - - public void setPeertubes(List peertubes) { - this.peertubes = peertubes; - } - - public List getConversations() { - return conversations; - } - - public void setConversations(List conversations) { - this.conversations = conversations; - } - - public List getStoredStatuses() { - return storedStatuses; - } - - public void setStoredStatuses(List storedStatuses) { - this.storedStatuses = storedStatuses; - } - - public List getPeertubeNotifications() { - return peertubeNotifications; - } - - public void setPeertubeNotifications(List peertubeNotifications) { - this.peertubeNotifications = peertubeNotifications; + public void setResponse(String response) { + this.response = response; } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index a455dacab..9793e517f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -2102,13 +2102,13 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct bCustomSharing.putString("source", status.getReblog().getAccount().getDisplay_name()); bCustomSharing.putString("id", status.getReblog().getId()); bCustomSharing.putString("tags", status.getReblog().getTagsString()); - bCustomSharing.putString("content", status.getReblog().getContent()); + bCustomSharing.putString("content", status.getReblog().getContentSpan().toString()); } else { bCustomSharing.putString("url", status.getUrl()); bCustomSharing.putString("source", status.getAccount().getDisplay_name()); bCustomSharing.putString("id", status.getId()); bCustomSharing.putString("tags", status.getTagsString()); - bCustomSharing.putString("content", status.getContent()); + bCustomSharing.putString("content", status.getContentSpan().toString()); } intentCustomSharing.putExtras(bCustomSharing); context.startActivity(intentCustomSharing); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java index 6e1aa3a4e..ac8f5cddb 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java @@ -482,14 +482,33 @@ public class SettingsFragment extends Fragment { } }); - String custom_sharing_url = sharedpreferences.getString(Helper.SET_CUSTOM_SHARING_URL, - "http://my.site/add?user=fedilab&url=${url}&title=${title}&source=${source}&id=${id}&descripition=${description}&keywords=${keywords}"); + String custom_sharing_url = sharedpreferences.getString(Helper.SET_CUSTOM_SHARING_URL,""); final EditText edit_custom_sharing_url = rootView.findViewById(R.id.custom_sharing_url); if (custom_sharing_url.equals("")) { - custom_sharing_url = "http://my.site/add?user=fedilab&url=${url}&title=${title}&source=${source}&id=${id}&descripition=${description}&keywords=${keywords}"; + custom_sharing_url = "http://my.site/add?user=fedilab&url=${url}&title=${title}&source=${source}&id=${id}&description=${description}&keywords=${keywords}"; } edit_custom_sharing_url.setText(custom_sharing_url); + + edit_custom_sharing_url.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.SET_CUSTOM_SHARING_URL, s.toString().trim()); + editor.apply(); + } + }); + boolean display_direct = sharedpreferences.getBoolean(Helper.SET_DISPLAY_DIRECT, true); final CheckBox set_display_direct = rootView.findViewById(R.id.set_display_direct); set_display_direct.setChecked(display_direct);