Merged in follow_button (pull request #116)

Follow button
This commit is contained in:
tom79 2017-09-17 13:36:09 +00:00
commit f0156f7a66
10 changed files with 414 additions and 236 deletions

View File

@ -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 Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
import java.util.List;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveManyRelationshipsInterface;
/**
* Created by Thomas on 16/09/2017.
* Retrieves many relationship between the authenticated user and other accounts
*/
public class RetrieveManyRelationshipsAsyncTask extends AsyncTask<Void, Void, Void> {
private Context context;
private List<Account> accounts;
private OnRetrieveManyRelationshipsInterface listener;
private APIResponse apiResponse;
public RetrieveManyRelationshipsAsyncTask(Context context, List<Account> accounts, OnRetrieveManyRelationshipsInterface onRetrieveManyRelationshipsInterface){
this.context = context;
this.listener = onRetrieveManyRelationshipsInterface;
this.accounts = accounts;
}
@Override
protected Void doInBackground(Void... params) {
apiResponse = new API(context).getRelationship(accounts);
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveRelationship(apiResponse);
}
}

View File

@ -69,6 +69,7 @@ public class API {
private Attachment attachment;
private List<Account> accounts;
private List<Status> statuses;
private List<Relationship> relationships;
private List<Notification> notifications;
private int tootPerPage, accountPerPage, notificationPerPage;
private int actionCode;
@ -270,6 +271,43 @@ public class API {
return relationship;
}
/**
* Returns a relationship between the authenticated account and an account
* @param accounts ArrayList<Account> accounts fetched
* @return Relationship entity
*/
public APIResponse getRelationship(List<Account> accounts) {
relationship = new Relationship();
RequestParams params = new RequestParams();
if( accounts != null && accounts.size() > 0 ) {
for(Account account: accounts) {
params.add("id[]", account.getId());
}
}
get("/accounts/relationships", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Relationship relationship = parseRelationshipResponse(response);
relationships.add(relationship);
apiResponse.setSince_id(findSinceId(headers));
apiResponse.setMax_id(findMaxId(headers));
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
apiResponse.setSince_id(findSinceId(headers));
apiResponse.setMax_id(findMaxId(headers));
relationships = parseRelationshipResponse(response);
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response){
setError(statusCode, error);
}
});
apiResponse.setRelationships(relationships);
return apiResponse;
}
/**
* Retrieves status for the account *synchronously*
*
@ -1399,29 +1437,47 @@ public class API {
/**
* Parse json response for list of accounts which could contain the developer name
* @param jsonArray JSONArray
* @return List<Account>
* Parse json response an unique relationship
* @param resobj JSONObject
* @return Relationship
*/
private List<Account> parseDeveloperResponse(JSONArray jsonArray){
private Relationship parseRelationshipResponse(JSONObject resobj){
List<Account> accounts = new ArrayList<>();
Relationship relationship = new Relationship();
try {
int i = 0;
Account account = null;
while (i < jsonArray.length() ) {
JSONObject resobj = jsonArray.getJSONObject(i);
account = parseAccountResponse(context, resobj);
if( account.getAcct().contains(Helper.DEVELOPER_INSTANCE))
accounts.add(account);
i++;
}
if( accounts.size() == 0)
accounts.add(account);
relationship.setId(resobj.get("id").toString());
relationship.setFollowing(Boolean.valueOf(resobj.get("following").toString()));
relationship.setFollowed_by(Boolean.valueOf(resobj.get("followed_by").toString()));
relationship.setBlocking(Boolean.valueOf(resobj.get("blocking").toString()));
relationship.setMuting(Boolean.valueOf(resobj.get("muting").toString()));
relationship.setRequested(Boolean.valueOf(resobj.get("requested").toString()));
} catch (JSONException e) {
e.printStackTrace();
}
return accounts;
return relationship;
}
/**
* Parse json response for list of relationship
* @param jsonArray JSONArray
* @return List<Relationship>
*/
private List<Relationship> parseRelationshipResponse(JSONArray jsonArray){
List<Relationship> relationships = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ) {
JSONObject resobj = jsonArray.getJSONObject(i);
Relationship relationship = parseRelationshipResponse(resobj);
relationships.add(relationship);
i++;
}
} catch (JSONException e) {
e.printStackTrace();
}
return relationships;
}
/**
@ -1469,26 +1525,7 @@ public class API {
return attachment;
}
/**
* Parse json response an unique relationship
* @param resobj JSONObject
* @return Relationship
*/
private Relationship parseRelationshipResponse(JSONObject resobj){
Relationship relationship = new Relationship();
try {
relationship.setId(resobj.get("id").toString());
relationship.setFollowing(Boolean.valueOf(resobj.get("following").toString()));
relationship.setFollowed_by(Boolean.valueOf(resobj.get("followed_by").toString()));
relationship.setBlocking(Boolean.valueOf(resobj.get("blocking").toString()));
relationship.setMuting(Boolean.valueOf(resobj.get("muting").toString()));
relationship.setRequested(Boolean.valueOf(resobj.get("requested").toString()));
} catch (JSONException e) {
e.printStackTrace();
}
return relationship;
}
/**
* Parse json response an unique notification

View File

@ -29,6 +29,7 @@ public class APIResponse {
private List<Status> statuses = null;
private List<Context> contexts = null;
private List<Notification> notifications = null;
private List<Relationship> relationships = null;
private fr.gouv.etalab.mastodon.client.Entities.Error error = null;
private String since_id, max_id;
private Instance instance;
@ -96,4 +97,12 @@ public class APIResponse {
public void setInstance(Instance instance) {
this.instance = instance;
}
public List<Relationship> getRelationships() {
return relationships;
}
public void setRelationships(List<Relationship> relationships) {
this.relationships = relationships;
}
}

View File

@ -48,6 +48,34 @@ public class Account implements Parcelable {
private String instance;
private boolean isFollowing;
private boolean isRemote;
private followAction followType = followAction.NOTHING;
private boolean isMakingAction = false;
public followAction getFollowType() {
return followType;
}
public void setFollowType(followAction followType) {
this.followType = followType;
}
public boolean isMakingAction() {
return isMakingAction;
}
public void setMakingAction(boolean makingAction) {
isMakingAction = makingAction;
}
public enum followAction{
FOLLOW,
NOT_FOLLOW,
BLOCK,
MUTE,
REQUEST_SENT,
NOTHING
}
protected Account(Parcel in) {
id = in.readString();

View File

@ -14,9 +14,7 @@ package fr.gouv.etalab.mastodon.drawers;
* 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.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
@ -33,17 +31,14 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
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.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
@ -78,7 +73,15 @@ public class AccountsListAdapter extends BaseAdapter implements OnPostActionInte
this.targetedId = targetedId;
}
public enum action{
FOLLOW,
UNFOLLOW,
UNBLOCK,
NOTHING,
UNMUTE
}
private API.StatusAction doAction;
@Override
public int getCount() {
@ -126,9 +129,8 @@ public class AccountsListAdapter extends BaseAdapter implements OnPostActionInte
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_action_block = (FloatingActionButton) convertView.findViewById(R.id.account_action_block);
holder.account_action_mute = (FloatingActionButton) convertView.findViewById(R.id.account_action_mute);
holder.account_follow = (FloatingActionButton) convertView.findViewById(R.id.account_follow);
holder.account_follow_request = (TextView) convertView.findViewById(R.id.account_follow_request);
holder.account_container = (LinearLayout) convertView.findViewById(R.id.account_container);
convertView.setTag(holder);
} else {
@ -136,24 +138,40 @@ public class AccountsListAdapter extends BaseAdapter implements OnPostActionInte
}
if( action == RetrieveAccountsAsyncTask.Type.BLOCKED)
holder.account_action_block.setVisibility(View.VISIBLE);
account.setFollowType(Account.followAction.BLOCK);
else if( action == RetrieveAccountsAsyncTask.Type.MUTED)
holder.account_action_mute.setVisibility(View.VISIBLE);
account.setFollowType(Account.followAction.MUTE);
if (account.getFollowType() == Account.followAction.NOTHING){
holder.account_follow.setVisibility(View.GONE);
holder.account_follow_request.setVisibility(View.GONE);
doAction = null;
}else if( account.getFollowType() == Account.followAction.REQUEST_SENT){
holder.account_follow.setVisibility(View.GONE);
holder.account_follow_request.setVisibility(View.VISIBLE);
doAction = null;
}else if( account.getFollowType() == Account.followAction.FOLLOW){
holder.account_follow.setImageResource(R.drawable.ic_user_times);
doAction = API.StatusAction.UNFOLLOW;
holder.account_follow.setVisibility(View.VISIBLE);
holder.account_follow_request.setVisibility(View.GONE);
}else if( account.getFollowType() == Account.followAction.NOT_FOLLOW){
holder.account_follow.setImageResource(R.drawable.ic_user_plus);
doAction = API.StatusAction.FOLLOW;
holder.account_follow.setVisibility(View.VISIBLE);
holder.account_follow_request.setVisibility(View.GONE);
}else if( account.getFollowType() == Account.followAction.BLOCK){
holder.account_follow.setImageResource(R.drawable.ic_unlock_alt);
doAction = API.StatusAction.UNBLOCK;
holder.account_follow.setVisibility(View.VISIBLE);
holder.account_follow_request.setVisibility(View.GONE);
}else if( account.getFollowType() == Account.followAction.MUTE){
holder.account_follow.setImageResource(R.drawable.ic_mute_white);
doAction = API.StatusAction.UNMUTE;
holder.account_follow.setVisibility(View.VISIBLE);
holder.account_follow_request.setVisibility(View.GONE);
}
holder.account_action_mute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
moreOptionDialog(account);
}
});
holder.account_action_block.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
moreOptionDialog(account);
}
});
holder.account_container.setOnClickListener(new View.OnClickListener() {
@Override
@ -184,6 +202,22 @@ public class AccountsListAdapter extends BaseAdapter implements OnPostActionInte
imageLoader.displayImage(account.getAvatar(), holder.account_pp, options);
if( account.isMakingAction()){
holder.account_follow.setEnabled(false);
}else {
holder.account_follow.setEnabled(true);
}
//Follow button
holder.account_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( doAction != null) {
account.setMakingAction(true);
new PostActionAsyncTask(context, doAction, account.getId(), AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
});
holder.account_pp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -233,44 +267,9 @@ public class AccountsListAdapter extends BaseAdapter implements OnPostActionInte
TextView account_sc;
TextView account_fgc;
TextView account_frc;
FloatingActionButton account_follow;
TextView account_follow_request;
LinearLayout account_container;
FloatingActionButton account_action_block;
FloatingActionButton account_action_mute;
}
/**
* More option for acccounts (unmute / unblock)
* @param account Account current account
*/
private void moreOptionDialog(final Account account){
String[] stringArrayConf = context.getResources().getStringArray(R.array.more_action_confirm_account);
final API.StatusAction doAction;
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
if( action == RetrieveAccountsAsyncTask.Type.BLOCKED) {
dialog.setMessage(stringArrayConf[1]);
doAction = API.StatusAction.UNBLOCK;
}else {
dialog.setMessage(stringArrayConf[0]);
doAction = API.StatusAction.UNMUTE;
}
dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
new PostActionAsyncTask(context, doAction, account.getId(), AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
dialog.dismiss();
}
});
dialog.show();
}
}

View File

@ -34,9 +34,13 @@ import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveManyRelationshipsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveManyRelationshipsInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.drawers.AccountsListAdapter;
@ -47,7 +51,7 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsInterface;
* Created by Thomas on 27/04/2017.
* Fragment to display content related to accounts
*/
public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccountsInterface {
public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccountsInterface, OnRetrieveManyRelationshipsInterface {
private boolean flag_loading;
private Context context;
@ -59,7 +63,6 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
private int accountPerPage;
private String targetedId;
private boolean swiped;
private ListView lv_accounts;
@ -97,7 +100,6 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
accountPerPage = sharedpreferences.getInt(Helper.SET_ACCOUNTS_PER_PAGE, 40);
lv_accounts = (ListView) rootView.findViewById(R.id.lv_accounts);
mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader);
@ -166,7 +168,6 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC2,
R.color.mastodonC3);
@ -246,5 +247,46 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
if( type != RetrieveAccountsAsyncTask.Type.BLOCKED && type != RetrieveAccountsAsyncTask.Type.MUTED)
new RetrieveManyRelationshipsAsyncTask(context, accounts,DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void onRetrieveRelationship(APIResponse apiResponse) {
if( apiResponse.getError() != 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, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
return;
}
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
List<Relationship> relationships = apiResponse.getRelationships();
if( relationships != null && relationships.size() > 0 ){
for(Relationship relationship: relationships){
for(Account account: accounts){
if( account.getId().equals(userId)){
account.setFollowType(Account.followAction.NOTHING);
continue;
}
if( account.getId().equals(relationship.getId())){
if( relationship.isFollowing())
account.setFollowType(Account.followAction.FOLLOW);
else
account.setFollowType(Account.followAction.NOT_FOLLOW);
if(relationship.isBlocking())
account.setFollowType(Account.followAction.BLOCK);
else if(relationship.isMuting())
account.setFollowType(Account.followAction.MUTE);
else if(relationship.isRequested())
account.setFollowType(Account.followAction.REQUEST_SENT);
break;
}
}
}
accountsListAdapter.notifyDataSetChanged();
}
}
}

View File

@ -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 Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.interfaces;
import fr.gouv.etalab.mastodon.client.APIResponse;
/**
* Created by Thomas on 16/09/2017.
* Interface when relationships have been retrieved for several accounts
*/
public interface OnRetrieveManyRelationshipsInterface {
void onRetrieveRelationship(APIResponse apiResponse);
}

View File

@ -97,6 +97,9 @@ public class StreamingService extends Service {
}
/**
* Task in background starts here.
*/
@ -109,9 +112,12 @@ public class StreamingService extends Service {
boolean notif_ask = sharedpreferences.getBoolean(Helper.SET_NOTIF_ASK, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
boolean notif_home = sharedpreferences.getBoolean(Helper.SET_NOTIF_HOMETIMELINE, true);
//User disagree with all notifications
if( !notif_follow && !notif_add && !notif_ask && !notif_mention && !notif_share)
if( !notif_home && !notif_follow && !notif_add && !notif_ask && !notif_mention && !notif_share) {
return; //Nothing is done
}
//No account connected, the service is stopped
if(!Helper.isLoggedIn(getApplicationContext()))
return;
@ -122,37 +128,7 @@ public class StreamingService extends Service {
Thread readThread = new Thread(new Runnable() {
@Override
public void run() {
try {
boolean connectionAlive = false;
isConnectingHashMap = true;
if( httpsURLConnection != null) {
try {
connectionAlive = (httpsURLConnection.getResponseCode() == 200);
} catch (Exception e) {
connectionAlive = false;
}
}
if( connectionAlive) {
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
}
try {
URL url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user");
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + account.getToken());
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Keep-Alive", "header");
httpsURLConnection.setRequestProperty("Connection", "close");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
InputStream inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
readStream(inputStream, account);
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
forceRestart(account);
}
} catch (Exception ignored) {
}
longPolling(account);
}
});
readThread.start();
@ -160,89 +136,96 @@ public class StreamingService extends Service {
}
@SuppressWarnings("ConstantConditions")
private String readStream(InputStream inputStream, final Account account) {
BufferedReader reader = null;
try{
reader = new BufferedReader(new InputStreamReader(inputStream));
String event;
EventStreaming eventStreaming;
//noinspection InfiniteLoopStatement
while(true){
public void longPolling(Account account) {
//noinspection InfiniteLoopStatement
while (true) {
try {
if( httpsURLConnection != null)
httpsURLConnection.disconnect();
URL url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user");
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + account.getToken());
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Keep-Alive", "header");
httpsURLConnection.setRequestProperty("Connection", "close");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setConnectTimeout(70000);
httpsURLConnection.setReadTimeout(70000);
httpsURLConnection.connect();
InputStream inputStream = null;
try {
event = reader.readLine();
inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = null;
try{
reader = new BufferedReader(new InputStreamReader(inputStream));
String event;
EventStreaming eventStreaming;
//noinspection InfiniteLoopStatement
while((event = reader.readLine()) != null){
if (event !=null){
if( (lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) {
case "event: update":
lastEvent = EventStreaming.UPDATE;
break;
case "event: notification":
lastEvent = EventStreaming.NOTIFICATION;
break;
case "event: delete":
lastEvent = EventStreaming.DELETE;
break;
default:
lastEvent = EventStreaming.NONE;
}
}else{
if( !event.startsWith("data: ")){
lastEvent = EventStreaming.NONE;
continue;
}
event = event.substring(6);
if(lastEvent == EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE;
}else if(lastEvent == EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION;
}else if( lastEvent == EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE;
event = "{id:" + event + "}";
}else {
eventStreaming = EventStreaming.UPDATE;
}
lastEvent = EventStreaming.NONE;
try {
JSONObject eventJson = new JSONObject(event);
onRetrieveStreaming(eventStreaming, eventJson, account.getId());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}catch (Exception e){
e.printStackTrace();
forceRestart(account);
break;
}
if (event !=null){
if( (lastEvent == EventStreaming.NONE || lastEvent == null) && !event.startsWith("data: ")) {
switch (event.trim()) {
case "event: update":
lastEvent = EventStreaming.UPDATE;
break;
case "event: notification":
lastEvent = EventStreaming.NOTIFICATION;
break;
case "event: delete":
lastEvent = EventStreaming.DELETE;
break;
default:
lastEvent = EventStreaming.NONE;
}
}else{
if( !event.startsWith("data: ")){
lastEvent = EventStreaming.NONE;
continue;
}
event = event.substring(6);
if(lastEvent == EventStreaming.UPDATE) {
eventStreaming = EventStreaming.UPDATE;
}else if(lastEvent == EventStreaming.NOTIFICATION) {
eventStreaming = EventStreaming.NOTIFICATION;
}else if( lastEvent == EventStreaming.DELETE) {
eventStreaming = EventStreaming.DELETE;
event = "{id:" + event + "}";
}else {
eventStreaming = EventStreaming.UPDATE;
}
lastEvent = EventStreaming.NONE;
try {
JSONObject eventJson = new JSONObject(event);
onRetrieveStreaming(eventStreaming, eventJson, account.getId());
} catch (JSONException e) {
}finally {
if(reader != null){
try{
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
forceRestart(account);
}
return null;
}
private void forceRestart(Account account){
isConnectingHashMap = false;
SystemClock.sleep(1000);
Intent intent = new Intent(getApplicationContext(), StreamingService.class);
intent.putExtra("accountId", account.getId());
intent.putExtra("accountAcct", account.getAcct());
startService(intent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {

View File

@ -154,27 +154,27 @@
android:autoLink="web"
android:visibility="gone"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:layout_marginTop="20dp"
<TextView
android:id="@+id/account_follow_request"
android:textColor="?attr/colorAccent"
android:text="@string/request_sent"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_gravity="center"
android:gravity="center"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/account_follow"
android:visibility="gone"
android:id="@+id/account_action_block"
app:fabSize="mini"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="center"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/ic_block_white" />
<android.support.design.widget.FloatingActionButton
android:layout_marginTop="20dp"
android:visibility="gone"
android:id="@+id/account_action_mute"
app:fabSize="mini"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="center"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/ic_mute_white" />
/>
</LinearLayout>
</android.support.v7.widget.CardView>

View File

@ -132,27 +132,27 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:layout_marginTop="20dp"
<TextView
android:id="@+id/account_follow_request"
android:textColor="?attr/colorAccent"
android:text="@string/request_sent"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_gravity="center"
android:gravity="center"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/account_follow"
android:visibility="gone"
android:id="@+id/account_action_block"
app:fabSize="mini"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="center"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/ic_block_white" />
<android.support.design.widget.FloatingActionButton
android:layout_marginTop="20dp"
android:visibility="gone"
android:id="@+id/account_action_mute"
app:fabSize="mini"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="center"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/ic_mute_white" />
/>
</LinearLayout>
</android.support.v7.widget.CardView>