diff options
Diffstat (limited to 'app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java')
-rw-r--r-- | app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java | 275 |
1 files changed, 226 insertions, 49 deletions
diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java index 51b4cb3b1..bc77d85c2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java @@ -32,6 +32,8 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; +import es.dmoral.toasty.Toasty; +import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.activities.BaseActivity; import fr.gouv.etalab.mastodon.activities.ShowAccountActivity; import fr.gouv.etalab.mastodon.activities.ShowConversationActivity; @@ -44,10 +46,11 @@ import fr.gouv.etalab.mastodon.client.Entities.Mention; import fr.gouv.etalab.mastodon.client.Entities.Results; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.drawers.AccountsSearchAdapter; +import fr.gouv.etalab.mastodon.drawers.StatusListAdapter; import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; -import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO; /** * Will handle cross actions between accounts boost/favourites/pin and replies @@ -55,6 +58,7 @@ import fr.gouv.etalab.mastodon.R; public class CrossActions { + private static int style; /** * Returns the list of connected accounts when cross actions are allowed otherwise, returns the current account @@ -116,6 +120,15 @@ public class CrossActions { boolean undoAction = (doAction == API.StatusAction.UNPIN || doAction == API.StatusAction.UNREBLOG || doAction == API.StatusAction.UNFAVOURITE); //Undo actions won't ask for choosing a user + int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); + if (theme == Helper.THEME_DARK) { + style = R.style.DialogDark; + } else if (theme == Helper.THEME_BLACK){ + style = R.style.DialogBlack; + }else { + style = R.style.Dialog; + } + if(type != null && type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && limitedToOwner){ String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); @@ -141,7 +154,7 @@ public class CrossActions { pinAction(context, status, baseAdapter, onPostActionInterface); } } else { - AlertDialog.Builder builderSingle = new AlertDialog.Builder(context); + AlertDialog.Builder builderSingle = new AlertDialog.Builder(context, style); builderSingle.setTitle(context.getString(R.string.choose_accounts)); final AccountsSearchAdapter accountsSearchAdapter = new AccountsSearchAdapter(context, accounts, true); final Account[] accountArray = new Account[accounts.size()]; @@ -197,6 +210,37 @@ public class CrossActions { } + public static void followPeertubeChannel(final Context context, Account remoteAccount, OnPostActionInterface onPostActionInterface){ + new AsyncTask<Void, Void, Void>() { + private WeakReference<Context> contextReference = new WeakReference<>(context); + Results response; + + @Override + protected void onPreExecute() { + Toasty.info(contextReference.get(), contextReference.get().getString(R.string.retrieve_remote_account), Toast.LENGTH_SHORT).show(); + } + + @Override + protected Void doInBackground(Void... voids) { + API api = new API(contextReference.get()); + String url; + url = "https://" + remoteAccount.getHost() + "/video-channels/" + remoteAccount.getAcct().split("@")[0]; + response = api.search(url); + return null; + } + @Override + protected void onPostExecute(Void result) { + if( response == null){ + return; + } + List<Account> remoteAccounts = response.getAccounts(); + if( remoteAccounts != null && remoteAccounts.size() > 0) { + new PostActionAsyncTask(context, null, remoteAccounts.get(0), API.StatusAction.FOLLOW, onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR ); + } + public static void doCrossProfile(final Context context, Account remoteAccount){ SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); @@ -209,25 +253,32 @@ public class CrossActions { @Override protected void onPreExecute() { - Toast.makeText(contextReference.get(), R.string.retrieve_remote_account, Toast.LENGTH_SHORT).show(); + Toasty.info(contextReference.get(), contextReference.get().getString(R.string.retrieve_remote_account), Toast.LENGTH_SHORT).show(); } @Override protected Void doInBackground(Void... voids) { API api = new API(contextReference.get(), account.getInstance(), account.getToken()); - String url = "https://" + remoteAccount.getInstance() + "/@" + remoteAccount.getAcct(); + String url; + if( remoteAccount.getHost() != null && remoteAccount.getAcct().split("@").length > 1) //Peertube compatibility + url = "https://" + remoteAccount.getHost() + "/accounts/" + remoteAccount.getAcct().split("@")[0]; + else + url = "https://" + remoteAccount.getInstance() + "/@" + remoteAccount.getAcct(); response = api.search(url); return null; } @Override protected void onPostExecute(Void result) { - List<Account> remoteAccounts = response.getAccounts(); if( response == null){ return; } + List<Account> remoteAccounts = response.getAccounts(); if( remoteAccounts != null && remoteAccounts.size() > 0) { Intent intent = new Intent(context, ShowAccountActivity.class); Bundle b = new Bundle(); + //Flag it has a peertube account + if( remoteAccount.getHost() != null && remoteAccount.getAcct().split("@").length > 1) + b.putBoolean("peertubeAccount", true); b.putString("accountId", remoteAccounts.get(0).getId()); intent.putExtras(b); context.startActivity(intent); @@ -236,6 +287,126 @@ public class CrossActions { }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR ); } + public static void doCrossConversation(final Context context, Status remoteStatus){ + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + Account account = new AccountDAO(context, db).getAccountByID(userId); + + new AsyncTask<Void, Void, Void>() { + private WeakReference<Context> contextReference = new WeakReference<>(context); + Results response; + + @Override + protected void onPreExecute() { + Toasty.info(contextReference.get(), contextReference.get().getString(R.string.retrieve_remote_conversation), Toast.LENGTH_SHORT).show(); + } + + @Override + protected Void doInBackground(Void... voids) { + API api = new API(contextReference.get(), account.getInstance(), account.getToken()); + response = api.search(remoteStatus.getUrl()); + return null; + } + @Override + protected void onPostExecute(Void result) { + if( response == null){ + return; + } + List<fr.gouv.etalab.mastodon.client.Entities.Status> statuses = response.getStatuses(); + if( statuses != null && statuses.size() > 0) { + Intent intent = new Intent(context, ShowConversationActivity.class); + Bundle b = new Bundle(); + b.putParcelable("status", statuses.get(0)); + intent.putExtras(b); + context.startActivity(intent); + } + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR ); + } + + + public static void doCrossBookmark(final Context context, final Status status, StatusListAdapter statusListAdapter ){ + List<Account> accounts = connectedAccounts(context, status, false); + + if( accounts.size() == 1) { + status.setBookmarked(!status.isBookmarked()); + try { + SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + if (status.isBookmarked()) { + new StatusCacheDAO(context, db).insertStatus(StatusCacheDAO.BOOKMARK_CACHE, status); + Toasty.success(context, context.getString(R.string.status_bookmarked), Toast.LENGTH_LONG).show(); + } else { + new StatusCacheDAO(context, db).remove(StatusCacheDAO.BOOKMARK_CACHE, status); + Toasty.success(context, context.getString(R.string.status_unbookmarked), Toast.LENGTH_LONG).show(); + } + statusListAdapter.notifyStatusChanged(status); + }catch (Exception e){ + e.printStackTrace(); + Toasty.error(context, context.getString(R.string.toast_error),Toast.LENGTH_LONG).show(); + } + }else { + AlertDialog.Builder builderSingle = new AlertDialog.Builder(context, style); + builderSingle.setTitle(context.getString(R.string.choose_accounts)); + final AccountsSearchAdapter accountsSearchAdapter = new AccountsSearchAdapter(context, accounts, true); + final Account[] accountArray = new Account[accounts.size()]; + int i = 0; + for(Account account: accounts){ + accountArray[i] = account; + i++; + } + builderSingle.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + builderSingle.setAdapter(accountsSearchAdapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(final DialogInterface dialog, int which) { + final Account account = accountArray[which]; + new AsyncTask<Void, Void, Void>() { + private WeakReference<Context> contextReference = new WeakReference<>(context); + Results response; + + @Override + protected void onPreExecute() { + Toasty.info(contextReference.get(), contextReference.get().getString(R.string.retrieve_remote_status), Toast.LENGTH_SHORT).show(); + } + + @Override + protected Void doInBackground(Void... voids) { + API api = new API(contextReference.get(), account.getInstance(), account.getToken()); + response = api.search(status.getUrl()); + return null; + } + @Override + protected void onPostExecute(Void result) { + if( response == null){ + Toasty.error(contextReference.get(),context.getString(R.string.toast_error),Toast.LENGTH_LONG).show(); + return; + } + List<fr.gouv.etalab.mastodon.client.Entities.Status> statuses = response.getStatuses(); + if( statuses != null && statuses.size() > 0) { + final SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + fr.gouv.etalab.mastodon.client.Entities.Status statusBookmarked = new StatusCacheDAO(contextReference.get(), db).getStatus(StatusCacheDAO.BOOKMARK_CACHE, statuses.get(0).getId(), account.getId(), account.getInstance()); + if (statusBookmarked == null) { + new StatusCacheDAO(contextReference.get(), db).insertStatus(StatusCacheDAO.BOOKMARK_CACHE, statuses.get(0), account.getId(), account.getInstance()); + Toasty.success(contextReference.get(), contextReference.get().getString(R.string.status_bookmarked), Toast.LENGTH_LONG).show(); + } else { + new StatusCacheDAO(contextReference.get(), db).remove(StatusCacheDAO.BOOKMARK_CACHE, statuses.get(0), account.getId(), account.getInstance()); + Toasty.success(contextReference.get(), contextReference.get().getString(R.string.status_unbookmarked), Toast.LENGTH_LONG).show(); + } + statusListAdapter.notifyStatusChanged(statuses.get(0)); + } + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR ); + + } + }); + builderSingle.show(); + } + } @@ -245,7 +416,7 @@ public class CrossActions { if( accounts.size() == 1 && type != RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE) { Intent intent = new Intent(context, TootActivity.class); Bundle b = new Bundle(); - if( status.getReblog() != null ) + if( status != null && status.getReblog() != null ) b.putParcelable("tootReply", status.getReblog()); else b.putParcelable("tootReply", status); @@ -260,7 +431,7 @@ public class CrossActions { } }else { if( type != RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE){ - AlertDialog.Builder builderSingle = new AlertDialog.Builder(context); + AlertDialog.Builder builderSingle = new AlertDialog.Builder(context, style); builderSingle.setTitle(context.getString(R.string.choose_accounts)); final AccountsSearchAdapter accountsSearchAdapter = new AccountsSearchAdapter(context, accounts, true); final Account[] accountArray = new Account[accounts.size()]; @@ -279,14 +450,15 @@ public class CrossActions { @Override public void onClick(final DialogInterface dialog, int which) { final Account account = accountArray[which]; - new AsyncTask<Void, Void, Void>() { - private List<fr.gouv.etalab.mastodon.client.Entities.Status> remoteStatuses; - private WeakReference<Context> contextReference = new WeakReference<>(context); + if(status != null) { + new AsyncTask<Void, Void, Void>() { + private List<fr.gouv.etalab.mastodon.client.Entities.Status> remoteStatuses; + private WeakReference<Context> contextReference = new WeakReference<>(context); + + @Override + protected Void doInBackground(Void... voids) { - @Override - protected Void doInBackground(Void... voids) { - if(status != null) { API api = new API(contextReference.get(), account.getInstance(), account.getToken()); String uri; if (status.getReblog() != null) { @@ -304,41 +476,47 @@ public class CrossActions { if (search != null) { remoteStatuses = search.getStatuses(); } + return null; } - return null; - } - @Override - protected void onPostExecute(Void result) { - Intent intent = new Intent(contextReference.get(), TootActivity.class); - Bundle b = new Bundle(); - if( remoteStatuses == null || remoteStatuses.size() == 0){ - dialog.dismiss(); - b.putParcelable("accountReply", account); + @Override + protected void onPostExecute(Void result) { + Intent intent = new Intent(contextReference.get(), TootActivity.class); + Bundle b = new Bundle(); + if (remoteStatuses == null || remoteStatuses.size() == 0) { + dialog.dismiss(); + intent.putExtras(b); //Put your id to your next Intent + contextReference.get().startActivity(intent); + return; + } + if (remoteStatuses.get(0).getReblog() != null) { + b.putParcelable("tootReply", remoteStatuses.get(0).getReblog()); + b.putParcelable("idRedirect", status.getReblog()); + } else { + b.putParcelable("tootReply", remoteStatuses.get(0)); + b.putParcelable("idRedirect", status); + } + b.putString("accountReplyToken", account.getToken()); intent.putExtras(b); //Put your id to your next Intent contextReference.get().startActivity(intent); - return; - } - if( remoteStatuses.get(0).getReblog() != null ) { - b.putParcelable("tootReply", remoteStatuses.get(0).getReblog()); - b.putString("idRedirect", status.getReblog().getId()); - }else { - b.putParcelable("tootReply", remoteStatuses.get(0)); - b.putString("idRedirect", status.getId()); - } - b.putParcelable("accountReply", account); - intent.putExtras(b); //Put your id to your next Intent - contextReference.get().startActivity(intent); - if( type == RetrieveFeedsAsyncTask.Type.CONTEXT ){ - try { - //Avoid to open multi activities when replying in a conversation - ((ShowConversationActivity)contextReference.get()).finish(); - }catch (Exception ignored){} + if (type == RetrieveFeedsAsyncTask.Type.CONTEXT) { + try { + //Avoid to open multi activities when replying in a conversation + ((ShowConversationActivity) contextReference.get()).finish(); + } catch (Exception ignored) { + } + } + dialog.dismiss(); } - dialog.dismiss(); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR ); + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + }else{ + Intent intent = new Intent(context, TootActivity.class); + Bundle b = new Bundle(); + b.putString("accountReplyToken", account.getToken()); + intent.putExtras(b); //Put your id to your next Intent + context.startActivity(intent); + } } }); @@ -385,12 +563,12 @@ public class CrossActions { } if( remoteStatuses.get(0).getReblog() != null ) { b.putParcelable("tootReply", remoteStatuses.get(0).getReblog()); - b.putString("idRedirect", remoteStatuses.get(0).getReblog().getId()); + b.putParcelable("idRedirect", remoteStatuses.get(0).getReblog()); }else { b.putParcelable("tootReply", remoteStatuses.get(0)); - b.putString("idRedirect", remoteStatuses.get(0).getId()); + b.putParcelable("idRedirect", remoteStatuses.get(0)); } - b.putParcelable("accountReply", account); + b.putString("accountReplyToken", account.getToken()); intent.putExtras(b); //Put your id to your next Intent contextReference.get().startActivity(intent); } @@ -409,7 +587,7 @@ public class CrossActions { context.startActivity(intentToot); ((BaseActivity)context).finish(); }else { - AlertDialog.Builder builderSingle = new AlertDialog.Builder(context); + AlertDialog.Builder builderSingle = new AlertDialog.Builder(context, style); builderSingle.setTitle(context.getString(R.string.choose_accounts)); final AccountsSearchAdapter accountsSearchAdapter = new AccountsSearchAdapter(context, accounts, true); final Account[] accountArray = new Account[accounts.size()]; @@ -428,9 +606,8 @@ public class CrossActions { @Override public void onClick(final DialogInterface dialog, int which) { final Account account = accountArray[which]; - Intent intentToot = new Intent(context, TootActivity.class); - bundle.putParcelable("accountReply", account); + bundle.putString("accountReplyToken", account.getToken()); intentToot.putExtras(bundle); context.startActivity(intentToot); ((BaseActivity)context).finish(); @@ -463,7 +640,7 @@ public class CrossActions { title = context.getString(R.string.pin_remove); } - AlertDialog.Builder builder = new AlertDialog.Builder(context); + AlertDialog.Builder builder = new AlertDialog.Builder(context, style); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) builder.setMessage(Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY)); |