Improves remote follow

This commit is contained in:
tom79 2017-10-07 18:18:26 +02:00
parent 9adeddb4e4
commit c6637eda44
8 changed files with 67 additions and 280 deletions

View File

@ -7,8 +7,8 @@ android {
applicationId "fr.gouv.etalab.mastodon"
minSdkVersion 15
targetSdkVersion 26
versionCode 58
versionName "1.4.9-rc-3"
versionCode 59
versionName "1.4.9-rc-4"
}
buildTypes {
release {

View File

@ -23,10 +23,12 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
@ -34,9 +36,11 @@ import java.util.List;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveDeveloperAccountsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRelationshipAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRemoteAccountsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.drawers.AccountSearchDevAdapter;
import fr.gouv.etalab.mastodon.helper.ExpandableHeightListView;
import fr.gouv.etalab.mastodon.helper.Helper;
@ -158,40 +162,36 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveRemote
@Override
public void onRetrieveRemoteAccount(boolean error, String name, String username, String instance_name, boolean locked, String avatar, String bio, String statusCount, String followingCount, String followersCount) {
if( error){
public void onRetrieveRemoteAccount(Results results) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
if( results == null){
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
if( show_error_messages)
Toast.makeText(getApplicationContext(), R.string.toast_error,Toast.LENGTH_LONG).show();
return;
}
Account account = new Account();
account.setInstance(instance_name);
account.setAcct(username + "@" + instance_name);
account.setAvatar(avatar);
account.setDisplay_name(username);
account.setStatuses_count_str(statusCount);
account.setFollowers_count_str(followersCount);
account.setFollowing_count_str(followingCount);
account.setUsername(name);
account.setLocked(locked);
account.setNote(bio);
account.setFollowing(false);
account.setRemote(true);
if( username.equals("@tschneider")) {
developers.add(account);
accountSearchWebAdapterDeveloper.notifyDataSetChanged();
}else {
contributors.add(account);
accountSearchWebAdapterContributors.notifyDataSetChanged();
List<Account> accounts = results.getAccounts();
Account account;
if( accounts != null && accounts.size() > 0){
account = accounts.get(0);
account.setFollowing(false);
account.setRemote(true);
if( account.getUsername().equals("@tschneider")) {
developers.add(account);
accountSearchWebAdapterDeveloper.notifyDataSetChanged();
}else {
contributors.add(account);
accountSearchWebAdapterContributors.notifyDataSetChanged();
}
}
}
@Override
public void onRetrieveSearchDevelopersAccounts(ArrayList<Account> accounts) {
if( accounts == null || accounts.size() == 0) {
new RetrieveRemoteAccountsAsyncTask("tschneider", "mastodon.etalab.gouv.fr", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask("PhotonQyv", "mastodon.xyz", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask("angrytux", "social.tchncs.de", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask(getApplicationContext(), "tschneider", "mastodon.etalab.gouv.fr", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask(getApplicationContext(),"PhotonQyv", "mastodon.xyz", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask(getApplicationContext(),"angrytux", "social.tchncs.de", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return;
}
boolean tschneider = false;
@ -225,11 +225,11 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveRemote
}
}
if( !tschneider)
new RetrieveRemoteAccountsAsyncTask("tschneider", "mastodon.etalab.gouv.fr", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask(getApplicationContext(), "tschneider", "mastodon.etalab.gouv.fr", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if( !PhotonQyv)
new RetrieveRemoteAccountsAsyncTask("PhotonQyv", "mastodon.xyz", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask(getApplicationContext(), "PhotonQyv", "mastodon.xyz", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if( !angrytux)
new RetrieveRemoteAccountsAsyncTask("angrytux", "social.tchncs.de", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask(getApplicationContext(), "angrytux", "social.tchncs.de", AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override

View File

@ -63,11 +63,13 @@ import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRemoteAccountsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.KinrarClient;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.drawers.AccountSearchWebAdapter;
import fr.gouv.etalab.mastodon.drawers.AccountsListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRemoteAccountInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
@ -241,7 +243,7 @@ public class RemoteFollowActivity extends AppCompatActivity implements OnRetriev
rf_no_result.setVisibility(View.GONE);
if( screen_name.startsWith("@"))
screen_name = screen_name.substring(1);
new RetrieveRemoteAccountsAsyncTask(screen_name, instance_name, RemoteFollowActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveRemoteAccountsAsyncTask(getApplicationContext(), screen_name, instance_name, RemoteFollowActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(rf_search.getWindowToken(), 0);
}
@ -261,30 +263,26 @@ public class RemoteFollowActivity extends AppCompatActivity implements OnRetriev
@Override
public void onRetrieveRemoteAccount(boolean error, String name, String username, String instance_name, boolean locked, String avatar, String bio, String statusCount, String followingCount, String followersCount) {
public void onRetrieveRemoteAccount(Results results) {
loader.setVisibility(View.GONE);
rf_search.setEnabled(true);
if( error){
rf_no_result.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), R.string.toast_error,Toast.LENGTH_LONG).show();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
if( results == null){
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
if( show_error_messages)
Toast.makeText(getApplicationContext(), R.string.toast_error,Toast.LENGTH_LONG).show();
return;
}
Account account = new Account();
account.setInstance(instance_name);
account.setAcct(screen_name + "@" + instance_name);
account.setAvatar(avatar);
account.setDisplay_name(username);
account.setStatuses_count_str(statusCount);
account.setFollowers_count_str(followersCount);
account.setFollowing_count_str(followingCount);
account.setUsername(name);
account.setLocked(locked);
account.setNote(bio);
List<Account> accounts = results.getAccounts();
Account account;
List<Account> selectedAccount = new ArrayList<>();
selectedAccount.add(account);
AccountSearchWebAdapter accountSearchWebAdapter = new AccountSearchWebAdapter(RemoteFollowActivity.this, selectedAccount);
lv_account.setAdapter(accountSearchWebAdapter);
lv_account.setVisibility(View.VISIBLE);
if( accounts != null && accounts.size() > 0){
account = accounts.get(0);
selectedAccount.add(account);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
AccountsListAdapter accountSearchWebAdapter = new AccountsListAdapter(RemoteFollowActivity.this, RetrieveAccountsAsyncTask.Type.FOLLOWERS, userId, selectedAccount);
lv_account.setAdapter(accountSearchWebAdapter);
lv_account.setVisibility(View.VISIBLE);
}
}
}

View File

@ -14,12 +14,11 @@
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRemoteAccountInterface;
@ -32,49 +31,27 @@ public class RetrieveRemoteAccountsAsyncTask extends AsyncTask<Void, Void, Void>
private OnRetrieveRemoteAccountInterface listener;
private String url;
private String avatar, name, username, bio;
private String statusCount, followingCount, followersCount;
private boolean islocked;
private boolean error = false;
private String instance;
private Context context;
private Results results;
public RetrieveRemoteAccountsAsyncTask(String username, String instance, OnRetrieveRemoteAccountInterface onRetrieveRemoteAccountInterface){
public RetrieveRemoteAccountsAsyncTask(Context context, String username, String instance, OnRetrieveRemoteAccountInterface onRetrieveRemoteAccountInterface){
this.url = "https://" + instance + "/@" + username;
this.listener = onRetrieveRemoteAccountInterface;
this.instance = instance;
this.context = context;
}
@Override
protected Void doInBackground(Void... params) {
String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
try {
Document document = Jsoup.connect(url).userAgent(userAgent).get();
Elements avatarElement = document.getElementsByClass("avatar");
avatar = avatarElement.get(0).getElementsByClass("u-photo").get(0).attr("src");
avatar = "https://" + instance + avatar;
Elements nameElement = document.getElementsByClass("name");
name = nameElement.get(0).getElementsByClass("p-name").get(0).html();
username = nameElement.get(0).getElementsByTag("span").get(1).html();
islocked = (nameElement.get(0).getElementsByClass("fa-lock") != null && nameElement.get(0).getElementsByClass("fa-lock").size() > 0);
Elements bioElement = document.getElementsByClass("bio");
bio = bioElement.get(0).html();
Elements countElement = document.getElementsByClass("counter-number");
statusCount = countElement.get(0).html();
followingCount = countElement.get(1).html();
followersCount = countElement.get(2).html();
} catch (Exception e) {
error = true;
e.printStackTrace();
}
API api = new API(context);
results = api.search(this.url);
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveRemoteAccount(error, name, username, instance, islocked, avatar, bio, statusCount, followingCount, followersCount);
listener.onRetrieveRemoteAccount(results);
}
}

View File

@ -547,7 +547,6 @@ public class API {
params.put("since_id", since_id);
if (0 > limit || limit > 80)
limit = 80;
params.put("limit",String.valueOf(limit));
statuses = new ArrayList<>();
get("/timelines/home", params, new JsonHttpResponseHandler() {

View File

@ -1,191 +0,0 @@
package fr.gouv.etalab.mastodon.drawers;
/* 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 Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.io.File;
import java.util.List;
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
/**
* Created by Thomas on 22/08/2017.
* Adapter for accounts from web
*/
public class AccountSearchWebAdapter extends BaseAdapter implements OnPostActionInterface {
private List<Account> accounts;
private LayoutInflater layoutInflater;
private Context context;
private ViewHolder holder;
public AccountSearchWebAdapter(Context context, List<Account> accounts){
this.context = context;
this.accounts = accounts;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return accounts.size();
}
@Override
public Object getItem(int position) {
return accounts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ImageLoader imageLoader = ImageLoader.getInstance();
File cacheDir = new File(context.getCacheDir(), context.getString(R.string.app_name));
ImageLoaderConfiguration configImg = new ImageLoaderConfiguration.Builder(context)
.imageDownloader(new PatchBaseImageDownloader(context))
.threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
.diskCache(new UnlimitedDiskCache(cacheDir))
.build();
if( !imageLoader.isInited())
imageLoader.init(configImg);
DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false)
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
final Account account = accounts.get(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_account_search_html, parent, false);
holder = new ViewHolder();
holder.account_pp = (ImageView) convertView.findViewById(R.id.account_pp);
holder.account_dn = (TextView) convertView.findViewById(R.id.account_dn);
holder.account_un = (TextView) convertView.findViewById(R.id.account_un);
holder.account_ds = (TextView) convertView.findViewById(R.id.account_ds);
holder.account_sc = (TextView) convertView.findViewById(R.id.account_sc);
holder.account_fgc = (TextView) convertView.findViewById(R.id.account_fgc);
holder.account_frc = (TextView) convertView.findViewById(R.id.account_frc);
holder.account_follow = (FloatingActionButton) convertView.findViewById(R.id.account_follow);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Redraws icon for locked accounts
final float scale = context.getResources().getDisplayMetrics().density;
if( account != null && account.isLocked()){
Drawable img = ContextCompat.getDrawable(context, R.drawable.ic_action_lock_closed);
img.setBounds(0,0,(int) (20 * scale + 0.5f),(int) (20 * scale + 0.5f));
holder.account_dn.setCompoundDrawables( null, null, img, null);
}else{
holder.account_dn.setCompoundDrawables( null, null, null, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.account_ds.setText(Html.fromHtml(account.getNote(), Html.FROM_HTML_MODE_LEGACY));
holder.account_dn.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getDisplay_name(), true), Html.FROM_HTML_MODE_LEGACY));
holder.account_un.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getUsername(), true), Html.FROM_HTML_MODE_LEGACY));
}else {
//noinspection deprecation
holder.account_ds.setText(Html.fromHtml(account.getNote()));
holder.account_dn.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getDisplay_name(), true)));
holder.account_un.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getUsername(), true)));
}
changeDrawableColor(context, R.drawable.ic_action_lock_closed,R.color.mastodonC4);
holder.account_ds.setAutoLinkMask(Linkify.WEB_URLS);
holder.account_sc.setText(String.valueOf(account.getStatuses_count_str()));
holder.account_fgc.setText(String.valueOf(account.getFollowing_count_str()));
holder.account_frc.setText(String.valueOf(account.getFollowers_count_str()));
//Profile picture
imageLoader.displayImage(account.getAvatar(), holder.account_pp, options);
holder.account_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.account_follow.setEnabled(false);
new PostActionAsyncTask(context, API.StatusAction.REMOTE_FOLLOW, account.getAcct(), AccountSearchWebAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
return convertView;
}
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
if( error != null){
final SharedPreferences sharedpreferences = context.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(context, error.getError(),Toast.LENGTH_LONG).show();
holder.account_follow.setEnabled(true);
return;
}
holder.account_follow.setVisibility(View.GONE);
Toast.makeText(context, R.string.toast_follow, Toast.LENGTH_LONG).show();
}
private class ViewHolder {
ImageView account_pp;
TextView account_dn;
TextView account_un;
TextView account_ds;
TextView account_sc;
TextView account_fgc;
TextView account_frc;
FloatingActionButton account_follow;
}
}

View File

@ -231,6 +231,8 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
final String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
//Display a preview for accounts that have replied *if enabled and only for home timeline*
if( type == RetrieveFeedsAsyncTask.Type.HOME ) {
boolean showPreview = sharedpreferences.getBoolean(Helper.SET_PREVIEW_REPLIES, false);

View File

@ -15,10 +15,12 @@
package fr.gouv.etalab.mastodon.interfaces;
import fr.gouv.etalab.mastodon.client.Entities.Results;
/**
* Created by Thomas on 22/08/2017.
* Interface for retrieving a remote account
*/
public interface OnRetrieveRemoteAccountInterface {
void onRetrieveRemoteAccount(boolean error, String name, String username, String instance, boolean locked, String avatar, String bio, String statusCount, String followingCount, String followersCount);
void onRetrieveRemoteAccount(Results results);
}