Nagram/TMessagesProj/src/main/java/org/telegram/ui/SettingsBlockedUsersActivity.java

281 lines
11 KiB
Java
Raw Normal View History

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;
import android.view.LayoutInflater;
2014-10-07 20:14:27 +00:00
import android.view.MotionEvent;
2013-10-25 15:19:00 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import org.telegram.PhoneFormat.PhoneFormat;
import org.telegram.android.LocaleController;
import org.telegram.messenger.TLRPC;
import org.telegram.android.MessagesController;
import org.telegram.android.NotificationCenter;
2013-10-25 15:19:00 +00:00
import org.telegram.messenger.R;
import org.telegram.ui.Adapters.BaseFragmentAdapter;
2014-11-10 11:05:22 +00:00
import org.telegram.ui.Cells.UserCell;
import org.telegram.ui.Views.ActionBar.ActionBarLayer;
import org.telegram.ui.Views.ActionBar.ActionBarMenu;
import org.telegram.ui.Views.ActionBar.BaseFragment;
2013-10-25 15:19:00 +00:00
2014-07-19 23:31:49 +00:00
public class SettingsBlockedUsersActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate, ContactsActivity.ContactsActivityDelegate {
2013-10-25 15:19:00 +00:00
private ListView listView;
private ListAdapter listViewAdapter;
private View progressView;
private TextView emptyView;
2013-10-25 15:19:00 +00:00
private int selectedUserId;
private final static int block_user = 1;
2013-10-25 15:19:00 +00:00
@Override
public boolean onFragmentCreate() {
super.onFragmentCreate();
NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
NotificationCenter.getInstance().addObserver(this, NotificationCenter.blockedUsersDidLoaded);
2013-10-25 15:19:00 +00:00
return true;
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.updateInterfaces);
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.blockedUsersDidLoaded);
MessagesController.getInstance().getBlockedUsers(false);
2013-10-25 15:19:00 +00:00
}
@Override
public View createView(LayoutInflater inflater, ViewGroup container) {
2013-10-25 15:19:00 +00:00
if (fragmentView == null) {
actionBarLayer.setBackButtonImage(R.drawable.ic_ab_back);
actionBarLayer.setBackOverlay(R.layout.updating_state_layout);
actionBarLayer.setTitle(LocaleController.getString("BlockedUsers", R.string.BlockedUsers));
actionBarLayer.setActionBarMenuOnItemClick(new ActionBarLayer.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
} else if (id == block_user) {
Bundle args = new Bundle();
args.putBoolean("onlyUsers", true);
args.putBoolean("destroyAfterSelect", true);
args.putBoolean("returnAsResult", true);
ContactsActivity fragment = new ContactsActivity(args);
2014-07-19 23:31:49 +00:00
fragment.setDelegate(SettingsBlockedUsersActivity.this);
presentFragment(fragment);
}
}
});
ActionBarMenu menu = actionBarLayer.createMenu();
menu.addItem(block_user, R.drawable.plus);
2013-10-25 15:19:00 +00:00
fragmentView = inflater.inflate(R.layout.settings_blocked_users_layout, container, false);
listViewAdapter = new ListAdapter(getParentActivity());
2013-10-25 15:19:00 +00:00
listView = (ListView)fragmentView.findViewById(R.id.listView);
progressView = fragmentView.findViewById(R.id.progressLayout);
emptyView = (TextView)fragmentView.findViewById(R.id.searchEmptyView);
2014-10-07 20:14:27 +00:00
emptyView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
emptyView.setText(LocaleController.getString("NoBlocked", R.string.NoBlocked));
if (MessagesController.getInstance().loadingBlockedUsers) {
2013-10-25 15:19:00 +00:00
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 < MessagesController.getInstance().blockedUsers.size()) {
2013-10-25 15:19:00 +00:00
Bundle args = new Bundle();
args.putInt("user_id", MessagesController.getInstance().blockedUsers.get(i));
2014-11-10 11:05:22 +00:00
presentFragment(new ProfileActivity(args));
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 < 0 || i >= MessagesController.getInstance().blockedUsers.size() || getParentActivity() == null) {
2013-10-25 15:19:00 +00:00
return true;
}
selectedUserId = MessagesController.getInstance().blockedUsers.get(i);
2013-10-25 15:19:00 +00:00
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
2013-10-25 15:19:00 +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) {
MessagesController.getInstance().unblockUser(selectedUserId);
2013-10-25 15:19:00 +00:00
}
}
});
showAlertDialog(builder);
2013-10-25 15:19:00 +00:00
return true;
}
});
} else {
ViewGroup parent = (ViewGroup)fragmentView.getParent();
if (parent != null) {
parent.removeView(fragmentView);
}
}
return fragmentView;
}
@Override
public void didReceivedNotification(int id, Object... args) {
if (id == NotificationCenter.updateInterfaces) {
int mask = (Integer)args[0];
if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (mask & MessagesController.UPDATE_MASK_NAME) != 0) {
updateVisibleRows(mask);
}
} else if (id == NotificationCenter.blockedUsersDidLoaded) {
if (progressView != null) {
progressView.setVisibility(View.GONE);
}
if (listView != null && listView.getEmptyView() == null) {
listView.setEmptyView(emptyView);
}
if (listViewAdapter != null) {
listViewAdapter.notifyDataSetChanged();
}
}
}
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);
2014-11-10 11:05:22 +00:00
if (child instanceof UserCell) {
((UserCell) child).update(mask);
2013-10-25 15:19:00 +00:00
}
}
}
@Override
public void onResume() {
super.onResume();
if (listViewAdapter != null) {
2013-10-25 15:19:00 +00:00
listViewAdapter.notifyDataSetChanged();
}
}
@Override
public void didSelectContact(final TLRPC.User user, String param) {
if (user == null) {
2013-10-25 15:19:00 +00:00
return;
}
MessagesController.getInstance().blockUser(user.id);
2013-10-25 15:19:00 +00:00
}
private class ListAdapter extends BaseFragmentAdapter {
2013-10-25 15:19:00 +00:00
private Context mContext;
public ListAdapter(Context context) {
mContext = context;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int i) {
return i != MessagesController.getInstance().blockedUsers.size();
2013-10-25 15:19:00 +00:00
}
@Override
public int getCount() {
if (MessagesController.getInstance().blockedUsers.isEmpty()) {
2013-10-25 15:19:00 +00:00
return 0;
}
return MessagesController.getInstance().blockedUsers.size() + 1;
2013-10-25 15:19:00 +00:00
}
@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-11-10 11:05:22 +00:00
view = new UserCell(mContext);
((UserCell)view).useSeparator = true;
2013-10-25 15:19:00 +00:00
}
TLRPC.User user = MessagesController.getInstance().getUser(MessagesController.getInstance().blockedUsers.get(i));
2014-11-10 11:05:22 +00:00
((UserCell)view).setData(user, null, user.phone != null && user.phone.length() != 0 ? PhoneFormat.getInstance().format("+" + user.phone) : LocaleController.getString("NumberUnknown", R.string.NumberUnknown));
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);
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
}
}
return view;
}
@Override
public int getItemViewType(int i) {
if(i == MessagesController.getInstance().blockedUsers.size()) {
2013-10-25 15:19:00 +00:00
return 1;
}
return 0;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public boolean isEmpty() {
return MessagesController.getInstance().blockedUsers.isEmpty();
2013-10-25 15:19:00 +00:00
}
}
}