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

395 lines
17 KiB
Java
Raw Normal View History

2014-11-14 15:40:15 +00:00
/*
2015-10-29 17:10:07 +00:00
* This is the source code of Telegram for Android v. 3.x.x.
2014-11-14 15:40:15 +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).
*
2015-10-29 17:10:07 +00:00
* Copyright Nikolai Kudashov, 2013-2015.
2014-11-14 15:40:15 +00:00
*/
package org.telegram.ui;
2014-11-17 02:44:57 +00:00
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
2015-09-24 20:52:02 +00:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ContactsController;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.NotificationCenter;
2014-11-17 02:44:57 +00:00
import org.telegram.messenger.FileLog;
import org.telegram.messenger.R;
2015-09-24 20:52:02 +00:00
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.RequestDelegate;
import org.telegram.tgnet.TLObject;
import org.telegram.tgnet.TLRPC;
2014-11-17 02:44:57 +00:00
import org.telegram.messenger.UserConfig;
import org.telegram.ui.ActionBar.ActionBar;
2014-11-14 15:40:15 +00:00
import org.telegram.ui.ActionBar.BaseFragment;
2014-11-17 02:44:57 +00:00
import org.telegram.ui.Adapters.BaseFragmentAdapter;
import org.telegram.ui.Cells.HeaderCell;
import org.telegram.ui.Cells.TextInfoPrivacyCell;
import org.telegram.ui.Cells.TextSettingsCell;
import org.telegram.ui.Components.LayoutHelper;
2014-11-17 02:44:57 +00:00
import java.util.ArrayList;
public class PrivacySettingsActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
private ListAdapter listAdapter;
private int privacySectionRow;
private int blockedRow;
private int lastSeenRow;
private int lastSeenDetailRow;
private int securitySectionRow;
2015-04-09 18:00:14 +00:00
private int sessionsRow;
2015-01-02 22:15:07 +00:00
private int passwordRow;
private int passcodeRow;
2015-04-09 18:00:14 +00:00
private int sessionsDetailRow;
2014-11-17 02:44:57 +00:00
private int deleteAccountSectionRow;
private int deleteAccountRow;
private int deleteAccountDetailRow;
private int rowCount;
@Override
public boolean onFragmentCreate() {
super.onFragmentCreate();
ContactsController.getInstance().loadPrivacySettings();
rowCount = 0;
privacySectionRow = rowCount++;
blockedRow = rowCount++;
lastSeenRow = rowCount++;
lastSeenDetailRow = rowCount++;
securitySectionRow = rowCount++;
passcodeRow = rowCount++;
2015-04-09 18:00:14 +00:00
passwordRow = rowCount++;
sessionsRow = rowCount++;
sessionsDetailRow = rowCount++;
2014-11-17 02:44:57 +00:00
deleteAccountSectionRow = rowCount++;
deleteAccountRow = rowCount++;
deleteAccountDetailRow = rowCount++;
NotificationCenter.getInstance().addObserver(this, NotificationCenter.privacyRulesUpdated);
return true;
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.privacyRulesUpdated);
}
@Override
2015-07-22 18:56:37 +00:00
public View createView(Context context) {
2015-04-09 18:00:14 +00:00
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setAllowOverlayTitle(true);
actionBar.setTitle(LocaleController.getString("PrivacySettings", R.string.PrivacySettings));
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
2014-11-17 02:44:57 +00:00
}
2015-04-09 18:00:14 +00:00
}
});
listAdapter = new ListAdapter(context);
fragmentView = new FrameLayout(context);
FrameLayout frameLayout = (FrameLayout) fragmentView;
frameLayout.setBackgroundColor(0xfff0f0f0);
ListView listView = new ListView(context);
listView.setDivider(null);
listView.setDividerHeight(0);
listView.setVerticalScrollBarEnabled(false);
listView.setDrawSelectorOnTop(true);
2015-05-21 21:27:27 +00:00
frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
2015-04-09 18:00:14 +00:00
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
if (i == blockedRow) {
presentFragment(new BlockedUsersActivity());
} else if (i == sessionsRow) {
presentFragment(new SessionsActivity());
} else if (i == deleteAccountRow) {
if (getParentActivity() == null) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setTitle(LocaleController.getString("DeleteAccountTitle", R.string.DeleteAccountTitle));
builder.setItems(new CharSequence[]{
LocaleController.formatPluralString("Months", 1),
LocaleController.formatPluralString("Months", 3),
LocaleController.formatPluralString("Months", 6),
LocaleController.formatPluralString("Years", 1)
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int value = 0;
if (which == 0) {
value = 30;
} else if (which == 1) {
value = 90;
} else if (which == 2) {
value = 182;
} else if (which == 3) {
value = 365;
2014-11-17 02:44:57 +00:00
}
2015-04-09 18:00:14 +00:00
final ProgressDialog progressDialog = new ProgressDialog(getParentActivity());
progressDialog.setMessage(LocaleController.getString("Loading", R.string.Loading));
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();
final TLRPC.TL_account_setAccountTTL req = new TLRPC.TL_account_setAccountTTL();
req.ttl = new TLRPC.TL_accountDaysTTL();
req.ttl.days = value;
2015-09-24 20:52:02 +00:00
ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
2015-04-09 18:00:14 +00:00
@Override
public void run(final TLObject response, final TLRPC.TL_error error) {
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
try {
progressDialog.dismiss();
} catch (Exception e) {
FileLog.e("tmessages", e);
2014-11-17 02:44:57 +00:00
}
2015-04-09 18:00:14 +00:00
if (response instanceof TLRPC.TL_boolTrue) {
ContactsController.getInstance().setDeleteAccountTTL(req.ttl.days);
listAdapter.notifyDataSetChanged();
}
}
});
}
});
}
2015-04-09 18:00:14 +00:00
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
2015-05-21 21:27:27 +00:00
showDialog(builder.create());
2015-04-09 18:00:14 +00:00
} else if (i == lastSeenRow) {
presentFragment(new LastSeenActivity());
} else if (i == passwordRow) {
presentFragment(new TwoStepVerificationActivity(0));
} else if (i == passcodeRow) {
if (UserConfig.passcodeHash.length() > 0) {
presentFragment(new PasscodeActivity(2));
} else {
presentFragment(new PasscodeActivity(0));
2014-11-17 02:44:57 +00:00
}
}
}
2015-04-09 18:00:14 +00:00
});
2014-11-17 02:44:57 +00:00
return fragmentView;
}
@Override
public void didReceivedNotification(int id, Object... args) {
if (id == NotificationCenter.privacyRulesUpdated) {
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
}
}
private String formatRulesString() {
ArrayList<TLRPC.PrivacyRule> privacyRules = ContactsController.getInstance().getPrivacyRules();
if (privacyRules.size() == 0) {
return LocaleController.getString("LastSeenNobody", R.string.LastSeenNobody);
}
int type = -1;
int plus = 0;
int minus = 0;
for (TLRPC.PrivacyRule rule : privacyRules) {
if (rule instanceof TLRPC.TL_privacyValueAllowUsers) {
plus += rule.users.size();
} else if (rule instanceof TLRPC.TL_privacyValueDisallowUsers) {
minus += rule.users.size();
} else if (rule instanceof TLRPC.TL_privacyValueAllowAll) {
type = 0;
} else if (rule instanceof TLRPC.TL_privacyValueDisallowAll) {
type = 1;
} else {
type = 2;
}
}
if (type == 0 || type == -1 && minus > 0) {
if (minus == 0) {
return LocaleController.getString("LastSeenEverybody", R.string.LastSeenEverybody);
} else {
return LocaleController.formatString("LastSeenEverybodyMinus", R.string.LastSeenEverybodyMinus, minus);
}
} else if (type == 2 || type == -1 && minus > 0 && plus > 0) {
if (plus == 0 && minus == 0) {
return LocaleController.getString("LastSeenContacts", R.string.LastSeenContacts);
} else {
if (plus != 0 && minus != 0) {
return LocaleController.formatString("LastSeenContactsMinusPlus", R.string.LastSeenContactsMinusPlus, minus, plus);
} else if (minus != 0) {
return LocaleController.formatString("LastSeenContactsMinus", R.string.LastSeenContactsMinus, minus);
2015-09-24 20:52:02 +00:00
} else {
2014-11-17 02:44:57 +00:00
return LocaleController.formatString("LastSeenContactsPlus", R.string.LastSeenContactsPlus, plus);
}
}
2015-09-24 20:52:02 +00:00
} else if (type == 1 || plus > 0) {
2014-11-17 02:44:57 +00:00
if (plus == 0) {
return LocaleController.getString("LastSeenNobody", R.string.LastSeenNobody);
} else {
return LocaleController.formatString("LastSeenNobodyPlus", R.string.LastSeenNobodyPlus, plus);
}
}
return "unknown";
}
@Override
public void onResume() {
super.onResume();
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
}
private class ListAdapter extends BaseFragmentAdapter {
private Context mContext;
public ListAdapter(Context context) {
mContext = context;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int i) {
2015-04-09 18:00:14 +00:00
return i == passcodeRow || i == passwordRow || i == blockedRow || i == sessionsRow || i == lastSeenRow && !ContactsController.getInstance().getLoadingLastSeenInfo() || i == deleteAccountRow && !ContactsController.getInstance().getLoadingDeleteInfo();
2014-11-17 02:44:57 +00:00
}
@Override
public int getCount() {
return rowCount;
}
@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) {
view = new TextSettingsCell(mContext);
view.setBackgroundColor(0xffffffff);
}
TextSettingsCell textCell = (TextSettingsCell) view;
if (i == blockedRow) {
textCell.setText(LocaleController.getString("BlockedUsers", R.string.BlockedUsers), true);
2015-04-09 18:00:14 +00:00
} else if (i == sessionsRow) {
textCell.setText(LocaleController.getString("SessionsTitle", R.string.SessionsTitle), false);
2015-01-02 22:15:07 +00:00
} else if (i == passwordRow) {
2015-04-09 18:00:14 +00:00
textCell.setText(LocaleController.getString("TwoStepVerification", R.string.TwoStepVerification), true);
} else if (i == passcodeRow) {
textCell.setText(LocaleController.getString("Passcode", R.string.Passcode), true);
2015-01-02 22:15:07 +00:00
} else if (i == lastSeenRow) {
2014-11-17 02:44:57 +00:00
String value;
if (ContactsController.getInstance().getLoadingLastSeenInfo()) {
value = LocaleController.getString("Loading", R.string.Loading);
} else {
value = formatRulesString();
}
textCell.setTextAndValue(LocaleController.getString("PrivacyLastSeen", R.string.PrivacyLastSeen), value, false);
2015-11-26 21:04:02 +00:00
} else if (i == deleteAccountRow) {
2014-11-17 02:44:57 +00:00
String value;
if (ContactsController.getInstance().getLoadingDeleteInfo()) {
value = LocaleController.getString("Loading", R.string.Loading);
} else {
int ttl = ContactsController.getInstance().getDeleteAccountTTL();
2014-11-17 22:04:31 +00:00
if (ttl <= 182) {
2014-11-17 02:44:57 +00:00
value = LocaleController.formatPluralString("Months", ttl / 30);
} else if (ttl == 365) {
value = LocaleController.formatPluralString("Years", ttl / 365);
} else {
value = LocaleController.formatPluralString("Days", ttl);
}
}
textCell.setTextAndValue(LocaleController.getString("DeleteAccountIfAwayFor", R.string.DeleteAccountIfAwayFor), value, false);
}
} else if (type == 1) {
if (view == null) {
view = new TextInfoPrivacyCell(mContext);
}
if (i == deleteAccountDetailRow) {
((TextInfoPrivacyCell) view).setText(LocaleController.getString("DeleteAccountHelp", R.string.DeleteAccountHelp));
view.setBackgroundResource(R.drawable.greydivider_bottom);
} else if (i == lastSeenDetailRow) {
((TextInfoPrivacyCell) view).setText(LocaleController.getString("LastSeenHelp", R.string.LastSeenHelp));
view.setBackgroundResource(R.drawable.greydivider);
2015-04-09 18:00:14 +00:00
} else if (i == sessionsDetailRow) {
((TextInfoPrivacyCell) view).setText(LocaleController.getString("SessionsInfo", R.string.SessionsInfo));
2014-11-17 02:44:57 +00:00
view.setBackgroundResource(R.drawable.greydivider);
}
} else if (type == 2) {
if (view == null) {
view = new HeaderCell(mContext);
view.setBackgroundColor(0xffffffff);
}
if (i == privacySectionRow) {
((HeaderCell) view).setText(LocaleController.getString("PrivacyTitle", R.string.PrivacyTitle));
} else if (i == securitySectionRow) {
((HeaderCell) view).setText(LocaleController.getString("SecurityTitle", R.string.SecurityTitle));
} else if (i == deleteAccountSectionRow) {
((HeaderCell) view).setText(LocaleController.getString("DeleteAccountTitle", R.string.DeleteAccountTitle));
}
}
return view;
}
@Override
public int getItemViewType(int i) {
2015-04-09 18:00:14 +00:00
if (i == lastSeenRow || i == blockedRow || i == deleteAccountRow || i == sessionsRow || i == passwordRow || i == passcodeRow) {
2014-11-17 02:44:57 +00:00
return 0;
2015-04-09 18:00:14 +00:00
} else if (i == deleteAccountDetailRow || i == lastSeenDetailRow || i == sessionsDetailRow) {
2014-11-17 02:44:57 +00:00
return 1;
} else if (i == securitySectionRow || i == deleteAccountSectionRow || i == privacySectionRow) {
return 2;
}
return 0;
}
@Override
public int getViewTypeCount() {
return 3;
}
2014-11-14 15:40:15 +00:00
2014-11-17 02:44:57 +00:00
@Override
public boolean isEmpty() {
return false;
}
}
2014-11-14 15:40:15 +00:00
}