2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.os.Bundle;
|
2013-12-20 19:25:49 +00:00
|
|
|
import android.support.v7.app.ActionBar;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.view.LayoutInflater;
|
2013-12-20 19:25:49 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.telegram.PhoneFormat.PhoneFormat;
|
2014-03-22 22:31:55 +00:00
|
|
|
import org.telegram.messenger.LocaleController;
|
2014-02-28 22:28:25 +00:00
|
|
|
import org.telegram.messenger.TLObject;
|
|
|
|
import org.telegram.messenger.TLRPC;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.messenger.ConnectionsManager;
|
|
|
|
import org.telegram.messenger.MessagesController;
|
|
|
|
import org.telegram.messenger.NotificationCenter;
|
|
|
|
import org.telegram.messenger.R;
|
|
|
|
import org.telegram.messenger.RPCRequest;
|
|
|
|
import org.telegram.messenger.Utilities;
|
2014-02-04 18:36:55 +00:00
|
|
|
import org.telegram.ui.Cells.ChatOrUserCell;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.ui.Views.BaseFragment;
|
|
|
|
import org.telegram.ui.Views.OnSwipeTouchListener;
|
2014-03-22 22:31:55 +00:00
|
|
|
import org.w3c.dom.Text;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
public class SettingsBlockedUsers extends BaseFragment implements NotificationCenter.NotificationCenterDelegate, ContactsActivity.ContactsActivityDelegate {
|
|
|
|
private ListView listView;
|
|
|
|
private ListAdapter listViewAdapter;
|
|
|
|
private boolean loading;
|
|
|
|
private View progressView;
|
2014-03-22 22:31:55 +00:00
|
|
|
private TextView emptyView;
|
2013-10-25 15:19:00 +00:00
|
|
|
private ArrayList<TLRPC.TL_contactBlocked> blockedContacts = new ArrayList<TLRPC.TL_contactBlocked>();
|
|
|
|
private HashMap<Integer, TLRPC.TL_contactBlocked> blockedContactsDict = new HashMap<Integer, TLRPC.TL_contactBlocked>();
|
|
|
|
private int selectedUserId;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onFragmentCreate() {
|
|
|
|
super.onFragmentCreate();
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().addObserver(this, MessagesController.updateInterfaces);
|
2013-10-25 15:19:00 +00:00
|
|
|
loadBlockedContacts(0, 200);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFragmentDestroy() {
|
|
|
|
super.onFragmentDestroy();
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().removeObserver(this, MessagesController.updateInterfaces);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
if (fragmentView == null) {
|
|
|
|
fragmentView = inflater.inflate(R.layout.settings_blocked_users_layout, container, false);
|
|
|
|
listViewAdapter = new ListAdapter(parentActivity);
|
|
|
|
listView = (ListView)fragmentView.findViewById(R.id.listView);
|
|
|
|
progressView = fragmentView.findViewById(R.id.progressLayout);
|
2014-03-22 22:31:55 +00:00
|
|
|
emptyView = (TextView)fragmentView.findViewById(R.id.searchEmptyView);
|
|
|
|
emptyView.setText(LocaleController.getString("NoBlocked", R.string.NoBlocked));
|
2013-10-25 15:19:00 +00:00
|
|
|
if (loading) {
|
|
|
|
progressView.setVisibility(View.VISIBLE);
|
|
|
|
emptyView.setVisibility(View.GONE);
|
|
|
|
listView.setEmptyView(null);
|
|
|
|
} else {
|
|
|
|
progressView.setVisibility(View.GONE);
|
|
|
|
listView.setEmptyView(emptyView);
|
|
|
|
}
|
|
|
|
listView.setAdapter(listViewAdapter);
|
|
|
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
|
|
if (i < blockedContacts.size()) {
|
|
|
|
UserProfileActivity fragment = new UserProfileActivity();
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putInt("user_id", blockedContacts.get(i).user_id);
|
|
|
|
fragment.setArguments(args);
|
2014-03-04 20:04:57 +00:00
|
|
|
((LaunchActivity)parentActivity).presentFragment(fragment, "user_" + blockedContacts.get(i).user_id, false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
|
|
if (i >= blockedContacts.size()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
selectedUserId = blockedContacts.get(i).user_id;
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
CharSequence[] items = new CharSequence[] {LocaleController.getString("Unblock", R.string.Unblock)};
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
builder.setItems(items, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
if (i == 0) {
|
|
|
|
TLRPC.TL_contacts_unblock req = new TLRPC.TL_contacts_unblock();
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.User user = MessagesController.getInstance().users.get(selectedUserId);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (user == null) {
|
|
|
|
return;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
req.id = MessagesController.getInputUser(user);
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_contactBlocked blocked = blockedContactsDict.get(selectedUserId);
|
|
|
|
blockedContactsDict.remove(selectedUserId);
|
|
|
|
blockedContacts.remove(blocked);
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.show().setCanceledOnTouchOutside(true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
listView.setOnTouchListener(new OnSwipeTouchListener() {
|
|
|
|
public void onSwipeRight() {
|
|
|
|
finishFragment(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
emptyView.setOnTouchListener(new OnSwipeTouchListener() {
|
|
|
|
public void onSwipeRight() {
|
|
|
|
finishFragment(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ViewGroup parent = (ViewGroup)fragmentView.getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
parent.removeView(fragmentView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fragmentView;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadBlockedContacts(int offset, int count) {
|
|
|
|
if (loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loading = true;
|
|
|
|
TLRPC.TL_contacts_getBlocked req = new TLRPC.TL_contacts_getBlocked();
|
|
|
|
req.offset = offset;
|
|
|
|
req.limit = count;
|
2014-03-22 22:31:55 +00:00
|
|
|
long requestId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
loading = false;
|
|
|
|
if (progressView != null) {
|
|
|
|
progressView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
if (listView != null) {
|
|
|
|
if (listView.getEmptyView() == null) {
|
|
|
|
listView.setEmptyView(emptyView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (listViewAdapter != null) {
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
final TLRPC.contacts_Blocked res = (TLRPC.contacts_Blocked)response;
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
loading = false;
|
|
|
|
for (TLRPC.User user : res.users) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesController.getInstance().users.put(user.id, user);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
for (TLRPC.TL_contactBlocked blocked : res.blocked) {
|
|
|
|
if (!blockedContactsDict.containsKey(blocked.user_id)) {
|
|
|
|
blockedContacts.add(blocked);
|
|
|
|
blockedContactsDict.put(blocked.user_id, blocked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (progressView != null) {
|
|
|
|
progressView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
if (listView != null) {
|
|
|
|
if (listView.getEmptyView() == null) {
|
|
|
|
listView.setEmptyView(emptyView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (listViewAdapter != null) {
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(requestId, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, Object... args) {
|
|
|
|
if (id == MessagesController.updateInterfaces) {
|
2014-02-04 18:36:55 +00:00
|
|
|
int mask = (Integer)args[0];
|
|
|
|
if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (mask & MessagesController.UPDATE_MASK_NAME) != 0) {
|
|
|
|
updateVisibleRows(mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateVisibleRows(int mask) {
|
|
|
|
if (listView == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int count = listView.getChildCount();
|
|
|
|
for (int a = 0; a < count; a++) {
|
|
|
|
View child = listView.getChildAt(a);
|
|
|
|
if (child instanceof ChatOrUserCell) {
|
|
|
|
((ChatOrUserCell) child).update(mask);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void applySelfActionBar() {
|
|
|
|
if (parentActivity == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ActionBar actionBar = parentActivity.getSupportActionBar();
|
|
|
|
actionBar.setDisplayShowTitleEnabled(true);
|
|
|
|
actionBar.setDisplayShowHomeEnabled(false);
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setDisplayUseLogoEnabled(false);
|
|
|
|
actionBar.setDisplayShowCustomEnabled(false);
|
|
|
|
actionBar.setSubtitle(null);
|
|
|
|
actionBar.setCustomView(null);
|
2014-03-22 22:31:55 +00:00
|
|
|
actionBar.setTitle(LocaleController.getString("BlockedUsers", R.string.BlockedUsers));
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
TextView title = (TextView)parentActivity.findViewById(R.id.action_bar_title);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (title == null) {
|
|
|
|
final int subtitleId = parentActivity.getResources().getIdentifier("action_bar_title", "id", "android");
|
|
|
|
title = (TextView)parentActivity.findViewById(subtitleId);
|
|
|
|
}
|
|
|
|
if (title != null) {
|
|
|
|
title.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
|
|
|
title.setCompoundDrawablePadding(0);
|
|
|
|
}
|
2014-03-04 20:04:57 +00:00
|
|
|
((LaunchActivity)parentActivity).fixBackButton();
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (isFinish) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (getActivity() == null) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!firstStart && listViewAdapter != null) {
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
firstStart = false;
|
2014-03-04 20:04:57 +00:00
|
|
|
((LaunchActivity)parentActivity).showActionBar();
|
|
|
|
((LaunchActivity)parentActivity).updateActionBar();
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
2013-10-25 15:19:00 +00:00
|
|
|
inflater.inflate(R.menu.settings_block_users_bar_menu, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int itemId = item.getItemId();
|
|
|
|
switch (itemId) {
|
|
|
|
case android.R.id.home:
|
|
|
|
finishFragment();
|
|
|
|
break;
|
|
|
|
case R.id.block_user:
|
|
|
|
ContactsActivity fragment = new ContactsActivity();
|
|
|
|
fragment.animationType = 1;
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putBoolean("onlyUsers", true);
|
|
|
|
bundle.putBoolean("destroyAfterSelect", true);
|
|
|
|
bundle.putBoolean("usersAsSections", true);
|
|
|
|
bundle.putBoolean("returnAsResult", true);
|
|
|
|
fragment.delegate = this;
|
|
|
|
fragment.setArguments(bundle);
|
2014-03-04 20:04:57 +00:00
|
|
|
((LaunchActivity)parentActivity).presentFragment(fragment, "contacts_block", false);
|
2013-10-25 15:19:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 18:36:55 +00:00
|
|
|
public void didSelectContact(TLRPC.User user) {
|
|
|
|
if (user == null || blockedContactsDict.containsKey(user.id)) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.TL_contacts_block req = new TLRPC.TL_contacts_block();
|
2014-02-04 18:36:55 +00:00
|
|
|
req.id = MessagesController.getInputUser(user);
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_contactBlocked blocked = new TLRPC.TL_contactBlocked();
|
2014-02-04 18:36:55 +00:00
|
|
|
blocked.user_id = user.id;
|
2013-10-25 15:19:00 +00:00
|
|
|
blocked.date = (int)(System.currentTimeMillis() / 1000);
|
|
|
|
blockedContactsDict.put(blocked.user_id, blocked);
|
|
|
|
blockedContacts.add(blocked);
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ListAdapter extends BaseAdapter {
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
public ListAdapter(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areAllItemsEnabled() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled(int i) {
|
|
|
|
return i != blockedContacts.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
if (blockedContacts.isEmpty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return blockedContacts.size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object getItem(int i) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int i) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasStableIds() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int i, View view, ViewGroup viewGroup) {
|
|
|
|
int type = getItemViewType(i);
|
|
|
|
if (type == 0) {
|
|
|
|
if (view == null) {
|
2014-02-04 18:36:55 +00:00
|
|
|
view = new ChatOrUserCell(mContext);
|
|
|
|
((ChatOrUserCell)view).usePadding = false;
|
|
|
|
((ChatOrUserCell)view).useSeparator = true;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.User user = MessagesController.getInstance().users.get(blockedContacts.get(i).user_id);
|
|
|
|
((ChatOrUserCell)view).setData(user, null, null, null, user.phone != null && user.phone.length() != 0 ? PhoneFormat.getInstance().format("+" + user.phone) : "Unknown");
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (type == 1) {
|
|
|
|
if (view == null) {
|
|
|
|
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
view = li.inflate(R.layout.settings_unblock_info_row_layout, viewGroup, false);
|
2014-03-22 22:31:55 +00:00
|
|
|
TextView textView = (TextView)view.findViewById(R.id.info_text_view);
|
|
|
|
textView.setText(LocaleController.getString("UnblockText", R.string.UnblockText));
|
2013-10-25 15:19:00 +00:00
|
|
|
registerForContextMenu(view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int i) {
|
|
|
|
if(i == blockedContacts.size()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getViewTypeCount() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return blockedContacts.isEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|