diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index 7001107e5..92d6689ca 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -39,6 +39,7 @@ import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -78,6 +79,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask; +import fr.gouv.etalab.mastodon.asynctasks.PostStatusAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAccountsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.UploadActionAsyncTask; import fr.gouv.etalab.mastodon.client.API; @@ -92,6 +94,7 @@ import fr.gouv.etalab.mastodon.drawers.AccountsSearchAdapter; import fr.gouv.etalab.mastodon.drawers.DraftsListAdapter; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; +import fr.gouv.etalab.mastodon.interfaces.OnPostStatusActionInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveSearcAccountshInterface; import fr.gouv.etalab.mastodon.jobs.ScheduledTootsSyncJob; @@ -100,6 +103,8 @@ import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; import mastodon.etalab.gouv.fr.mastodon.R; +import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT; +import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; import static fr.gouv.etalab.mastodon.helper.Helper.loadPPInActionBar; @@ -108,7 +113,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.loadPPInActionBar; * Toot activity class */ -public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostActionInterface { +public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface { private int charsInCw; @@ -238,11 +243,11 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); Account account = new AccountDAO(getApplicationContext(),db).getAccountByID(userId); - ImageLoader imageLoader; - DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false) - .cacheOnDisk(true).resetViewBeforeLoading(true).build(); - imageLoader = ImageLoader.getInstance(); - imageLoader.loadImage(account.getAvatar(), options, new SimpleImageLoadingListener(){ + String url = account.getAvatar(); + if( url.startsWith("/") ){ + url = "https://" + Helper.getLiveInstance(getApplicationContext()) + account.getAvatar(); + } + imageLoader.loadImage(url, options, new SimpleImageLoadingListener(){ @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { super.onLoadingComplete(imageUri, view, loadedImage); @@ -370,7 +375,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc toot.setContent(toot_content.getText().toString().trim()); if( tootReply != null) toot.setIn_reply_to_id(tootReply.getId()); - new PostActionAsyncTask(getApplicationContext(), API.StatusAction.CREATESTATUS, null, toot, null, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + new PostStatusAsyncTask(getApplicationContext(), toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }); @@ -818,40 +823,57 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc storeToot(true); } + @Override - public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) { - if( error != null){ + public void onPostStatusAction(APIResponse apiResponse) { + if( apiResponse.getError() != null){ final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true); if( show_error_messages) - Toast.makeText(getApplicationContext(), error.getError(),Toast.LENGTH_LONG).show(); + Toast.makeText(getApplicationContext(), apiResponse.getError().getError(),Toast.LENGTH_LONG).show(); return; } - if( statusCode == 200){ - //Clear the toot - toot_content.setText(""); - toot_cw_content.setText(""); - if( attachments != null) { - for (Attachment attachment : attachments) { - View namebar = findViewById(Integer.parseInt(attachment.getId())); - if (namebar != null && namebar.getParent() != null) - ((ViewGroup) namebar.getParent()).removeView(namebar); - } - List tmp_attachment = new ArrayList<>(); - tmp_attachment.addAll(attachments); - attachments.removeAll(tmp_attachment); - tmp_attachment.clear(); + //Clear the toot + toot_content.setText(""); + toot_cw_content.setText(""); + if( attachments != null) { + for (Attachment attachment : attachments) { + View namebar = findViewById(Integer.parseInt(attachment.getId())); + if (namebar != null && namebar.getParent() != null) + ((ViewGroup) namebar.getParent()).removeView(namebar); } - isSensitive = false; - toot_sensitive.setVisibility(View.GONE); - currentToId = -1; - Toast.makeText(TootActivity.this,R.string.toot_sent, Toast.LENGTH_LONG).show(); - }else { - Toast.makeText(TootActivity.this,R.string.toast_error, Toast.LENGTH_LONG).show(); + List tmp_attachment = new ArrayList<>(); + tmp_attachment.addAll(attachments); + attachments.removeAll(tmp_attachment); + tmp_attachment.clear(); } + isSensitive = false; + toot_sensitive.setVisibility(View.GONE); + currentToId = -1; + Toast.makeText(TootActivity.this,R.string.toot_sent, Toast.LENGTH_LONG).show(); toot_it.setEnabled(true); - } + //It's a reply, so the user will be redirect to its answer + if( tootReply != null){ + List statuses = apiResponse.getStatuses(); + if( statuses != null && statuses.size() > 0 ){ + Status status = statuses.get(0); + if( status != null ) { + Intent intent = new Intent(getApplicationContext(), ShowConversationActivity.class); + Bundle b = new Bundle(); + b.putString("statusId", status.getId()); + intent.putExtras(b); + startActivity(intent); + finish(); + } + } + }else { + Intent intent = new Intent(getApplicationContext(), MainActivity.class); + intent.putExtra(INTENT_ACTION, HOME_TIMELINE_INTENT); + startActivity(intent); + finish(); + } + } @Override public void onRetrieveSearchAccounts(APIResponse apiResponse) { @@ -1075,4 +1097,6 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc } } + + } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java index 631ba963d..536cb0c14 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java @@ -17,7 +17,6 @@ package fr.gouv.etalab.mastodon.asynctasks; import android.content.Context; import android.os.AsyncTask; import fr.gouv.etalab.mastodon.client.API; -import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostStatusAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostStatusAsyncTask.java new file mode 100644 index 000000000..301cf0a35 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostStatusAsyncTask.java @@ -0,0 +1,54 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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. + * + * Mastalab 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 Thomas Schneider; if not, + * see . */ +package fr.gouv.etalab.mastodon.asynctasks; + +import android.content.Context; +import android.os.AsyncTask; + +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.interfaces.OnPostStatusActionInterface; + + +/** + * Created by Thomas on 21/07/2017. + * Posts status (live version) - scheduled toots are sent via classic post feature in api + */ + +public class PostStatusAsyncTask extends AsyncTask { + + private Context context; + private OnPostStatusActionInterface listener; + private APIResponse apiResponse; + private fr.gouv.etalab.mastodon.client.Entities.Status status; + + public PostStatusAsyncTask(Context context, fr.gouv.etalab.mastodon.client.Entities.Status status, OnPostStatusActionInterface onPostStatusActionInterface){ + this.context = context; + this.listener = onPostStatusActionInterface; + this.status = status; + } + + @Override + protected Void doInBackground(Void... params) { + apiResponse = new API(context).postStatusAction(status); + return null; + } + + @Override + protected void onPostExecute(Void result) { + listener.onPostStatusAction(apiResponse); + } + +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index b4f31e87f..ed7d211ad 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -898,6 +898,58 @@ public class API { } + /** + * Posts a status + * @param status Status object related to the status + * @return APIResponse + */ + public APIResponse postStatusAction(Status status){ + + String action; + RequestParams params; + params = new RequestParams(); + action = "/statuses"; + params.put("status", status.getContent()); + if( status.getIn_reply_to_id() != null) + params.put("in_reply_to_id", status.getIn_reply_to_id()); + if( status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0 ) { + for(Attachment attachment: status.getMedia_attachments()) { + params.add("media_ids[]", attachment.getId()); + } + } + if( status.isSensitive()) + params.put("sensitive", Boolean.toString(status.isSensitive())); + if( status.getSpoiler_text() != null) + params.put("spoiler_text", status.getSpoiler_text()); + params.put("visibility", status.getVisibility()); + statuses = new ArrayList<>(); + post(action, 30000, params, new JsonHttpResponseHandler() { + + @Override + public void onSuccess(int statusCode, Header[] headers, JSONObject response) { + Status statusreturned = parseStatuses(response); + statuses.add(statusreturned); + apiResponse.setSince_id(findSinceId(headers)); + apiResponse.setMax_id(findMaxId(headers)); + } + @Override + public void onSuccess(int statusCode, Header[] headers, JSONArray response) { + statuses = parseStatuses(response); + apiResponse.setSince_id(findSinceId(headers)); + apiResponse.setMax_id(findMaxId(headers)); + } + @Override + public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response) { + actionCode = statusCode; + setError(statusCode, error); + error.printStackTrace(); + } + }); + apiResponse.setStatuses(statuses); + return apiResponse; + } + + /** * Retrieves notifications for the authenticated account since an id*synchronously* * @param since_id String since max 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 a8375cd80..918a36821 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 @@ -294,8 +294,8 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf public void onClick(View v) { Intent intent = new Intent(context, ShowConversationActivity.class); Bundle b = new Bundle(); - b.putString("statusId", status.getId()); //Your id - intent.putExtras(b); //Put your id to your next Intent + b.putString("statusId", status.getId()); + intent.putExtras(b); context.startActivity(intent); } }); @@ -335,7 +335,6 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf holder.status_account_username.setText(String.format("@%s",username)); } - holder.status_reply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -344,6 +343,13 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf b.putParcelable("tootReply", status); intent.putExtras(b); //Put your id to your next Intent context.startActivity(intent); + if( type == RetrieveFeedsAsyncTask.Type.CONTEXT ){ + try { + //Avoid to open multi activities when replying in a conversation + ((ShowConversationActivity)context).finish(); + }catch (Exception ignored){} + + } } }); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index ac61e45bc..f2413e377 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -745,7 +745,11 @@ public class Helper { imageLoader = ImageLoader.getInstance(); final ImageView imageView = new ImageView(activity); item.setIcon(R.drawable.ic_person); - imageLoader.displayImage(account.getAvatar(), imageView, options, new ImageLoadingListener() { + String url = account.getAvatar(); + if( url.startsWith("/") ){ + url = "https://" + Helper.getLiveInstance(activity) + account.getAvatar(); + } + imageLoader.displayImage(url, imageView, options, new ImageLoadingListener() { @Override public void onLoadingStarted(String s, View view) { } @@ -865,14 +869,7 @@ public class Helper { editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken()); editor.putString(Helper.PREF_KEY_ID, account.getId()); editor.apply(); - ImageLoader imageLoader; - DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false) - .cacheOnDisk(true).resetViewBeforeLoading(true).build(); - imageLoader = ImageLoader.getInstance(); - View headerLayout = navigationView.getHeaderView(0); - updateHeaderAccountInfo(activity, account, headerLayout, imageLoader, options); - //Adds the profile picture as an icon - loadPPInActionBar(activity, account.getAvatar()); + activity.recreate(); } @@ -886,6 +883,9 @@ public class Helper { DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false) .cacheOnDisk(true).resetViewBeforeLoading(true).build(); imageLoader = ImageLoader.getInstance(); + if( url.startsWith("/") ){ + url = "https://" + Helper.getLiveInstance(activity) + url; + } imageLoader.loadImage(url, options, new SimpleImageLoadingListener(){ @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { @@ -939,7 +939,11 @@ public class Helper { ownerFollowing.setText(String.valueOf(account.getFollowing_count())); username.setText(String.format("@%s",account.getUsername())); displayedName.setText(account.getDisplay_name()); - imageLoader.displayImage(account.getAvatar(), profilePicture, options); + String url = account.getAvatar(); + if( url.startsWith("/") ){ + url = "https://" + Helper.getLiveInstance(activity) + account.getAvatar(); + } + imageLoader.displayImage(url, profilePicture, options); } profilePicture.setOnClickListener(null); profilePicture.setOnClickListener(new View.OnClickListener() { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPostStatusActionInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPostStatusActionInterface.java new file mode 100644 index 000000000..b3ac3be71 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPostStatusActionInterface.java @@ -0,0 +1,26 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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. + * + * Mastalab 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 Thomas Schneider; if not, + * see . */ +package fr.gouv.etalab.mastodon.interfaces; + + +import fr.gouv.etalab.mastodon.client.APIResponse; + +/** + * Created by Thomas on 21/07/2017. + * Interface when posting a status + */ +public interface OnPostStatusActionInterface { + void onPostStatusAction(APIResponse apiResponse); +}