fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java

442 lines
20 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
2017-05-05 16:36:04 +02:00
*
* 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.
*
2017-07-10 10:33:24 +02:00
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* 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 <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
2017-05-20 19:40:46 +02:00
import android.content.BroadcastReceiver;
2017-05-05 16:36:04 +02:00
import android.content.Context;
2017-05-20 19:40:46 +02:00
import android.content.Intent;
import android.content.IntentFilter;
2017-05-05 16:36:04 +02:00
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
2017-05-26 17:20:36 +02:00
import android.os.Handler;
2017-07-31 19:29:14 +02:00
import android.support.design.widget.FloatingActionButton;
2017-05-05 16:36:04 +02:00
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
2017-05-20 19:40:46 +02:00
import android.support.v4.content.LocalBroadcastManager;
2017-05-05 16:36:04 +02:00
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
2017-07-31 19:29:14 +02:00
import android.support.v7.app.ActionBar;
2017-05-05 16:36:04 +02:00
import android.support.v7.app.AppCompatActivity;
2017-07-31 19:29:14 +02:00
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
2017-05-05 16:36:04 +02:00
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
2017-05-05 16:36:04 +02:00
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
2017-07-31 19:29:14 +02:00
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
2017-06-11 14:28:29 +02:00
2017-05-05 16:36:04 +02:00
import java.io.File;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRelationshipAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.fragments.DisplayAccountsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsAccountInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRelationshipInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
/**
* Created by Thomas on 01/05/2017.
* Show account activity class
*/
public class ShowAccountActivity extends AppCompatActivity implements OnPostActionInterface, OnRetrieveAccountInterface, OnRetrieveFeedsAccountInterface, OnRetrieveRelationshipInterface {
private ImageLoader imageLoader;
private DisplayImageOptions options;
private List<Status> statuses;
private StatusListAdapter statusListAdapter;
2017-07-31 19:29:14 +02:00
private FloatingActionButton account_follow;
2017-05-05 16:36:04 +02:00
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private String accountId;
private TabLayout tabLayout;
2017-05-20 19:40:46 +02:00
private BroadcastReceiver hide_header;
2017-07-31 19:29:14 +02:00
private TextView account_note, account_follow_request;
2017-05-20 19:40:46 +02:00
private String userId;
private boolean isHiddingShowing = false;
private static int instanceValue = 0;
2017-07-31 20:18:14 +02:00
private Relationship relationship;
2017-05-05 16:36:04 +02:00
public enum action{
FOLLOW,
UNFOLLOW,
UNBLOCK,
NOTHING
}
private action doAction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-06-30 17:09:07 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2017-06-30 17:09:07 +02:00
if( theme == Helper.THEME_LIGHT){
setTheme(R.style.AppTheme);
}else {
setTheme(R.style.AppThemeDark);
}
2017-07-31 19:29:14 +02:00
setTitle("");
2017-05-05 16:36:04 +02:00
setContentView(R.layout.activity_show_account);
instanceValue += 1;
Bundle b = getIntent().getExtras();
2017-07-31 19:29:14 +02:00
account_follow = (FloatingActionButton) findViewById(R.id.account_follow);
account_follow_request = (TextView) findViewById(R.id.account_follow_request);
2017-05-05 16:36:04 +02:00
account_follow.setEnabled(false);
if(b != null){
accountId = b.getString("accountId");
new RetrieveRelationshipAsyncTask(getApplicationContext(), accountId,ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new RetrieveAccountAsyncTask(getApplicationContext(),accountId, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-05-20 19:40:46 +02:00
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2017-05-05 16:36:04 +02:00
if( accountId != null && accountId.equals(userId)){
account_follow.setVisibility(View.GONE);
}
}else{
Toast.makeText(this,R.string.toast_error_loading_account,Toast.LENGTH_LONG).show();
}
imageLoader = ImageLoader.getInstance();
File cacheDir = new File(getCacheDir(), getString(R.string.app_name));
ImageLoaderConfiguration configImg = new ImageLoaderConfiguration.Builder(this)
.imageDownloader(new PatchBaseImageDownloader(getApplicationContext()))
.threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
.diskCache(new UnlimitedDiskCache(cacheDir))
.build();
imageLoader.init(configImg);
statuses = new ArrayList<>();
boolean isOnWifi = Helper.isOnWIFI(getApplicationContext());
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
statusListAdapter = new StatusListAdapter(getApplicationContext(), RetrieveFeedsAsyncTask.Type.USER, accountId, isOnWifi, behaviorWithAttachments, this.statuses);
2017-07-31 19:29:14 +02:00
options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(80)).cacheInMemory(false)
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
2017-05-05 16:36:04 +02:00
if( getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tabLayout = (TabLayout) findViewById(R.id.account_tabLayout);
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.toots)));
2017-05-05 16:36:04 +02:00
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.following)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.followers)));
mPager = (ViewPager) findViewById(R.id.account_viewpager);
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
TabLayout.Tab tab = tabLayout.getTabAt(position);
if( tab != null)
tab.select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
2017-05-20 19:40:46 +02:00
account_note = (TextView) findViewById(R.id.account_note);
//Register LocalBroadcast to receive selected accounts after search
hide_header = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
2017-05-26 17:20:36 +02:00
if( !isHiddingShowing ){
isHiddingShowing = true;
ImageView account_pp = (ImageView) findViewById(R.id.account_pp);
boolean hide = intent.getBooleanExtra("hide", false);
if( hide){
2017-05-20 19:40:46 +02:00
account_follow.setVisibility(View.GONE);
2017-05-26 17:20:36 +02:00
account_note.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE);
account_pp.getLayoutParams().width = (int) Helper.convertDpToPixel(50, context);
account_pp.getLayoutParams().height = (int) Helper.convertDpToPixel(50, context);
}else {
2017-07-31 20:18:14 +02:00
manageButtonVisibility();
2017-05-26 17:20:36 +02:00
if( accountId != null && accountId.equals(userId)){
account_follow.setVisibility(View.GONE);
}
account_pp.getLayoutParams().width = (int) Helper.convertDpToPixel(80, context);
account_pp.getLayoutParams().height = (int) Helper.convertDpToPixel(80, context);
tabLayout.setVisibility(View.VISIBLE);
account_note.setVisibility(View.VISIBLE);
2017-05-20 19:40:46 +02:00
}
2017-05-26 17:20:36 +02:00
account_pp.requestLayout();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
isHiddingShowing = false;
}
}, 500);
2017-05-20 19:40:46 +02:00
}
2017-05-26 17:20:36 +02:00
2017-05-20 19:40:46 +02:00
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(hide_header, new IntentFilter(Helper.HEADER_ACCOUNT+String.valueOf(instanceValue)));
2017-05-20 19:40:46 +02:00
2017-05-05 16:36:04 +02:00
//Follow button
account_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2017-06-07 13:58:21 +02:00
if( doAction == action.NOTHING){
Toast.makeText(getApplicationContext(), R.string.nothing_to_do, Toast.LENGTH_LONG).show();
}else if( doAction == action.FOLLOW){
2017-05-05 16:36:04 +02:00
account_follow.setEnabled(false);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.FOLLOW, accountId, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else if( doAction == action.UNFOLLOW){
account_follow.setEnabled(false);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.UNFOLLOW, accountId, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else if( doAction == action.UNBLOCK){
account_follow.setEnabled(false);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.UNBLOCK, accountId, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
});
}
@Override
public void onPostAction(int statusCode,API.StatusAction statusAction, String targetedId, Error error) {
if( error != 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();
return;
}
2017-05-05 16:36:04 +02:00
Helper.manageMessageStatusCode(getApplicationContext(), statusCode, statusAction);
new RetrieveRelationshipAsyncTask(getApplicationContext(), accountId,ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onRetrieveAccount(Account account, Error error) {
if( error != 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();
return;
}
2017-05-05 16:36:04 +02:00
ImageView account_pp = (ImageView) findViewById(R.id.account_pp);
TextView account_dn = (TextView) findViewById(R.id.account_dn);
TextView account_un = (TextView) findViewById(R.id.account_un);
2017-05-20 19:40:46 +02:00
2017-07-31 19:29:14 +02:00
ActionBar actionBar = getSupportActionBar();
LayoutInflater mInflater = LayoutInflater.from(ShowAccountActivity.this);
if( actionBar != null){
View show_account_actionbar = mInflater.inflate(R.layout.showaccount_actionbar, null);
TextView actionbar_title = (TextView) show_account_actionbar.findViewById(R.id.show_account_title);
if( account != null && account.getAcct() != null)
actionbar_title.setText(account.getAcct());
actionBar.setCustomView(show_account_actionbar);
actionBar.setDisplayShowCustomEnabled(true);
}else {
if( account != null && account.getAcct() != null)
setTitle(account.getAcct());
}
2017-05-05 16:36:04 +02:00
if( account != null){
account_dn.setText(Helper.shortnameToUnicode(account.getDisplay_name(), true));
account_un.setText(String.format("@%s", account.getUsername()));
2017-07-31 19:29:14 +02:00
SpannableString spannableString = Helper.clickableElementsDescription(ShowAccountActivity.this, account.getNote());
account_note.setText(spannableString, TextView.BufferType.SPANNABLE);
account_note.setMovementMethod(LinkMovementMethod.getInstance());
tabLayout.getTabAt(0).setText(getString(R.string.status_cnt, account.getStatuses_count()));
tabLayout.getTabAt(1).setText(getString(R.string.following_cnt, account.getFollowing_count()));
tabLayout.getTabAt(2).setText(getString(R.string.followers_cnt, account.getFollowers_count()));
2017-05-05 16:36:04 +02:00
imageLoader.displayImage(account.getAvatar(), account_pp, options);
}
}
@Override
public void onRetrieveFeedsAccount(List<Status> statuses) {
if( statuses != null) {
for(Status tmpStatus: statuses){
this.statuses.add(tmpStatus);
}
statusListAdapter.notifyDataSetChanged();
}
}
2017-05-20 19:40:46 +02:00
@Override
public void onDestroy(){
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(hide_header);
}
2017-05-05 16:36:04 +02:00
@Override
public void onRetrieveRelationship(Relationship relationship, Error error) {
if( error != 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();
return;
}
2017-07-31 20:18:14 +02:00
this.relationship = relationship;
manageButtonVisibility();
//The authenticated account is followed by the account
if( relationship.isFollowed_by()){
TextView account_followed_by = (TextView) findViewById(R.id.account_followed_by);
account_followed_by.setVisibility(View.VISIBLE);
}
}
//Manages the visibility of the button
private void manageButtonVisibility(){
if( relationship == null)
return;
2017-06-07 13:58:21 +02:00
account_follow.setEnabled(true);
2017-05-05 16:36:04 +02:00
if( relationship.isBlocking()){
2017-07-31 19:29:14 +02:00
account_follow.setImageResource(R.drawable.ic_unlock_alt);
2017-05-05 16:36:04 +02:00
doAction = action.UNBLOCK;
2017-07-31 20:18:14 +02:00
account_follow.setVisibility(View.VISIBLE);
2017-05-05 16:36:04 +02:00
}else if( relationship.isRequested()){
2017-07-31 19:29:14 +02:00
account_follow_request.setVisibility(View.VISIBLE);
account_follow.setVisibility(View.GONE);
2017-05-05 16:36:04 +02:00
doAction = action.NOTHING;
}else if( relationship.isFollowing()){
2017-07-31 19:29:14 +02:00
account_follow.setImageResource(R.drawable.ic_user_times);
2017-05-05 16:36:04 +02:00
doAction = action.UNFOLLOW;
2017-07-31 20:18:14 +02:00
account_follow.setVisibility(View.VISIBLE);
2017-05-05 16:36:04 +02:00
}else if( !relationship.isFollowing()){
2017-07-31 19:29:14 +02:00
account_follow.setImageResource(R.drawable.ic_user_plus);
2017-05-05 16:36:04 +02:00
doAction = action.FOLLOW;
2017-07-31 20:18:14 +02:00
account_follow.setVisibility(View.VISIBLE);
2017-05-05 16:36:04 +02:00
}else{
2017-07-31 19:29:14 +02:00
account_follow.setVisibility(View.GONE);
2017-05-05 16:36:04 +02:00
doAction = action.NOTHING;
}
}
/**
* Pager adapter for the 3 fragments
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
switch (position){
case 0:
DisplayStatusFragment displayStatusFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER);
bundle.putString("targetedId", accountId);
2017-05-20 19:40:46 +02:00
bundle.putBoolean("hideHeader",true);
bundle.putString("hideHeaderValue",String.valueOf(instanceValue));
2017-05-05 16:36:04 +02:00
displayStatusFragment.setArguments(bundle);
return displayStatusFragment;
case 1:
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWING);
bundle.putString("targetedId", accountId);
2017-05-20 19:40:46 +02:00
bundle.putBoolean("hideHeader",true);
bundle.putString("hideHeaderValue",String.valueOf(instanceValue));
2017-05-05 16:36:04 +02:00
displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment;
case 2:
displayAccountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWERS);
bundle.putString("targetedId", accountId);
2017-05-20 19:40:46 +02:00
bundle.putBoolean("hideHeader",true);
bundle.putString("hideHeaderValue",String.valueOf(instanceValue));
2017-05-05 16:36:04 +02:00
displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment;
}
return null;
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}