Search in sources :

Example 1 with SessionCell

use of org.telegram.ui.Cells.SessionCell in project Telegram-FOSS by Telegram-FOSS-Team.

the class SessionsActivity method createView.

@Override
public View createView(Context context) {
    globalFlickerLoadingView = new FlickerLoadingView(context);
    globalFlickerLoadingView.setIsSingleCell(true);
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);
    if (currentType == 0) {
        actionBar.setTitle(LocaleController.getString("Devices", R.string.Devices));
    } else {
        actionBar.setTitle(LocaleController.getString("WebSessionsTitle", R.string.WebSessionsTitle));
    }
    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {

        @Override
        public void onItemClick(int id) {
            if (id == -1) {
                finishFragment();
            }
        }
    });
    listAdapter = new ListAdapter(context);
    fragmentView = new FrameLayout(context);
    FrameLayout frameLayout = (FrameLayout) fragmentView;
    frameLayout.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
    emptyLayout = new LinearLayout(context);
    emptyLayout.setOrientation(LinearLayout.VERTICAL);
    emptyLayout.setGravity(Gravity.CENTER);
    emptyLayout.setBackgroundDrawable(Theme.getThemedDrawable(context, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow));
    emptyLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AndroidUtilities.displaySize.y - ActionBar.getCurrentActionBarHeight()));
    imageView = new ImageView(context);
    if (currentType == 0) {
        imageView.setImageResource(R.drawable.devices);
    } else {
        imageView.setImageResource(R.drawable.no_apps);
    }
    imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_sessions_devicesImage), PorterDuff.Mode.MULTIPLY));
    emptyLayout.addView(imageView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT));
    textView1 = new TextView(context);
    textView1.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText2));
    textView1.setGravity(Gravity.CENTER);
    textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
    textView1.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    if (currentType == 0) {
        textView1.setText(LocaleController.getString("NoOtherSessions", R.string.NoOtherSessions));
    } else {
        textView1.setText(LocaleController.getString("NoOtherWebSessions", R.string.NoOtherWebSessions));
    }
    emptyLayout.addView(textView1, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 16, 0, 0));
    textView2 = new TextView(context);
    textView2.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText2));
    textView2.setGravity(Gravity.CENTER);
    textView2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
    textView2.setPadding(AndroidUtilities.dp(20), 0, AndroidUtilities.dp(20), 0);
    if (currentType == 0) {
        textView2.setText(LocaleController.getString("NoOtherSessionsInfo", R.string.NoOtherSessionsInfo));
    } else {
        textView2.setText(LocaleController.getString("NoOtherWebSessionsInfo", R.string.NoOtherWebSessionsInfo));
    }
    emptyLayout.addView(textView2, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 14, 0, 0));
    emptyView = new EmptyTextProgressView(context);
    emptyView.showProgress();
    frameLayout.addView(emptyView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.CENTER));
    listView = new RecyclerListView(context);
    listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
    listView.setVerticalScrollBarEnabled(false);
    listView.setEmptyView(emptyView);
    listView.setAnimateEmptyView(true, 0);
    frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener((view, position) -> {
        if (position == ttlRow) {
            if (getParentActivity() == null) {
                return;
            }
            int selected;
            if (ttlDays <= 7) {
                selected = 0;
            } else if (ttlDays <= 93) {
                selected = 1;
            } else if (ttlDays <= 183) {
                selected = 2;
            } else {
                selected = 3;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            builder.setTitle(LocaleController.getString("SessionsSelfDestruct", R.string.SessionsSelfDestruct));
            String[] items = new String[] { LocaleController.formatPluralString("Weeks", 1), LocaleController.formatPluralString("Months", 3), LocaleController.formatPluralString("Months", 6), LocaleController.formatPluralString("Years", 1) };
            final LinearLayout linearLayout = new LinearLayout(getParentActivity());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            builder.setView(linearLayout);
            for (int a = 0; a < items.length; a++) {
                RadioColorCell cell = new RadioColorCell(getParentActivity());
                cell.setPadding(AndroidUtilities.dp(4), 0, AndroidUtilities.dp(4), 0);
                cell.setTag(a);
                cell.setCheckColor(Theme.getColor(Theme.key_radioBackground), Theme.getColor(Theme.key_dialogRadioBackgroundChecked));
                cell.setTextAndValue(items[a], selected == a);
                linearLayout.addView(cell);
                cell.setOnClickListener(v -> {
                    builder.getDismissRunnable().run();
                    Integer which = (Integer) v.getTag();
                    int value = 0;
                    if (which == 0) {
                        value = 7;
                    } else if (which == 1) {
                        value = 90;
                    } else if (which == 2) {
                        value = 183;
                    } else if (which == 3) {
                        value = 365;
                    }
                    final TLRPC.TL_account_setAuthorizationTTL req = new TLRPC.TL_account_setAuthorizationTTL();
                    req.authorization_ttl_days = value;
                    ttlDays = value;
                    if (listAdapter != null) {
                        listAdapter.notifyDataSetChanged();
                    }
                    getConnectionsManager().sendRequest(req, (response, error) -> {
                    });
                });
            }
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            showDialog(builder.create());
        } else if (position == terminateAllSessionsRow) {
            if (getParentActivity() == null) {
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            String buttonText;
            if (currentType == 0) {
                builder.setMessage(LocaleController.getString("AreYouSureSessions", R.string.AreYouSureSessions));
                builder.setTitle(LocaleController.getString("AreYouSureSessionsTitle", R.string.AreYouSureSessionsTitle));
                buttonText = LocaleController.getString("Terminate", R.string.Terminate);
            } else {
                builder.setMessage(LocaleController.getString("AreYouSureWebSessions", R.string.AreYouSureWebSessions));
                builder.setTitle(LocaleController.getString("TerminateWebSessionsTitle", R.string.TerminateWebSessionsTitle));
                buttonText = LocaleController.getString("Disconnect", R.string.Disconnect);
            }
            builder.setPositiveButton(buttonText, (dialogInterface, i) -> {
                if (currentType == 0) {
                    TLRPC.TL_auth_resetAuthorizations req = new TLRPC.TL_auth_resetAuthorizations();
                    ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> {
                        AndroidUtilities.runOnUIThread(() -> {
                            if (getParentActivity() == null) {
                                return;
                            }
                            if (error == null && response instanceof TLRPC.TL_boolTrue) {
                                Toast toast = Toast.makeText(getParentActivity(), LocaleController.getString("TerminateAllSessions", R.string.TerminateAllSessions), Toast.LENGTH_SHORT);
                                toast.show();
                                finishFragment();
                            }
                        });
                        for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
                            UserConfig userConfig = UserConfig.getInstance(a);
                            if (!userConfig.isClientActivated()) {
                                continue;
                            }
                            userConfig.registeredForPush = false;
                            userConfig.saveConfig(false);
                            MessagesController.getInstance(a).registerForPush(SharedConfig.pushString);
                            ConnectionsManager.getInstance(a).setUserId(userConfig.getClientUserId());
                        }
                    });
                } else {
                    TLRPC.TL_account_resetWebAuthorizations req = new TLRPC.TL_account_resetWebAuthorizations();
                    ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                        if (getParentActivity() == null) {
                            return;
                        }
                        if (error == null && response instanceof TLRPC.TL_boolTrue) {
                            Toast toast = Toast.makeText(getParentActivity(), LocaleController.getString("TerminateAllWebSessions", R.string.TerminateAllWebSessions), Toast.LENGTH_SHORT);
                            toast.show();
                        } else {
                            Toast toast = Toast.makeText(getParentActivity(), LocaleController.getString("UnknownError", R.string.UnknownError), Toast.LENGTH_SHORT);
                            toast.show();
                        }
                        finishFragment();
                    }));
                }
            });
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            AlertDialog alertDialog = builder.create();
            showDialog(alertDialog);
            TextView button = (TextView) alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            if (button != null) {
                button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
            }
        } else if (position >= otherSessionsStartRow && position < otherSessionsEndRow || position >= passwordSessionsStartRow && position < passwordSessionsEndRow || position == currentSessionRow) {
            if (getParentActivity() == null) {
                return;
            }
            if (currentType == 0) {
                final TLRPC.TL_authorization authorization;
                boolean isCurrentSession = false;
                if (position == currentSessionRow) {
                    authorization = currentSession;
                    isCurrentSession = true;
                } else if (position >= otherSessionsStartRow && position < otherSessionsEndRow) {
                    authorization = (TLRPC.TL_authorization) sessions.get(position - otherSessionsStartRow);
                } else {
                    authorization = (TLRPC.TL_authorization) passwordSessions.get(position - passwordSessionsStartRow);
                }
                showSessionBottomSheet(authorization, isCurrentSession);
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            final boolean[] param = new boolean[1];
            String buttonText;
            if (currentType == 0) {
                builder.setMessage(LocaleController.getString("TerminateSessionText", R.string.TerminateSessionText));
                builder.setTitle(LocaleController.getString("AreYouSureSessionTitle", R.string.AreYouSureSessionTitle));
                buttonText = LocaleController.getString("Terminate", R.string.Terminate);
            } else {
                final TLRPC.TL_webAuthorization authorization = (TLRPC.TL_webAuthorization) sessions.get(position - otherSessionsStartRow);
                builder.setMessage(LocaleController.formatString("TerminateWebSessionText", R.string.TerminateWebSessionText, authorization.domain));
                builder.setTitle(LocaleController.getString("TerminateWebSessionTitle", R.string.TerminateWebSessionTitle));
                buttonText = LocaleController.getString("Disconnect", R.string.Disconnect);
                FrameLayout frameLayout1 = new FrameLayout(getParentActivity());
                TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(authorization.bot_id);
                String name;
                if (user != null) {
                    name = UserObject.getFirstName(user);
                } else {
                    name = "";
                }
                CheckBoxCell cell = new CheckBoxCell(getParentActivity(), 1);
                cell.setBackgroundDrawable(Theme.getSelectorDrawable(false));
                cell.setText(LocaleController.formatString("TerminateWebSessionStop", R.string.TerminateWebSessionStop, name), "", false, false);
                cell.setPadding(LocaleController.isRTL ? AndroidUtilities.dp(16) : AndroidUtilities.dp(8), 0, LocaleController.isRTL ? AndroidUtilities.dp(8) : AndroidUtilities.dp(16), 0);
                frameLayout1.addView(cell, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.TOP | Gravity.LEFT, 0, 0, 0, 0));
                cell.setOnClickListener(v -> {
                    if (!v.isEnabled()) {
                        return;
                    }
                    CheckBoxCell cell1 = (CheckBoxCell) v;
                    param[0] = !param[0];
                    cell1.setChecked(param[0], true);
                });
                builder.setCustomViewOffset(16);
                builder.setView(frameLayout1);
            }
            builder.setPositiveButton(buttonText, (dialogInterface, option) -> {
                if (getParentActivity() == null) {
                    return;
                }
                final AlertDialog progressDialog = new AlertDialog(getParentActivity(), 3);
                progressDialog.setCanCacnel(false);
                progressDialog.show();
                if (currentType == 0) {
                    final TLRPC.TL_authorization authorization;
                    if (position >= otherSessionsStartRow && position < otherSessionsEndRow) {
                        authorization = (TLRPC.TL_authorization) sessions.get(position - otherSessionsStartRow);
                    } else {
                        authorization = (TLRPC.TL_authorization) passwordSessions.get(position - passwordSessionsStartRow);
                    }
                    TLRPC.TL_account_resetAuthorization req = new TLRPC.TL_account_resetAuthorization();
                    req.hash = authorization.hash;
                    ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                        try {
                            progressDialog.dismiss();
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                        if (error == null) {
                            sessions.remove(authorization);
                            passwordSessions.remove(authorization);
                            updateRows();
                            if (listAdapter != null) {
                                listAdapter.notifyDataSetChanged();
                            }
                        }
                    }));
                } else {
                    final TLRPC.TL_webAuthorization authorization = (TLRPC.TL_webAuthorization) sessions.get(position - otherSessionsStartRow);
                    TLRPC.TL_account_resetWebAuthorization req = new TLRPC.TL_account_resetWebAuthorization();
                    req.hash = authorization.hash;
                    ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                        try {
                            progressDialog.dismiss();
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                        if (error == null) {
                            sessions.remove(authorization);
                            updateRows();
                            if (listAdapter != null) {
                                listAdapter.notifyDataSetChanged();
                            }
                        }
                    }));
                    if (param[0]) {
                        MessagesController.getInstance(currentAccount).blockPeer(authorization.bot_id);
                    }
                }
            });
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            AlertDialog alertDialog = builder.create();
            showDialog(alertDialog);
            TextView button = (TextView) alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            if (button != null) {
                button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
            }
        }
    });
    if (currentType == 0) {
        undoView = new UndoView(context) {

            @Override
            public void hide(boolean apply, int animated) {
                if (!apply) {
                    TLRPC.TL_authorization authorization = (TLRPC.TL_authorization) getCurrentInfoObject();
                    TLRPC.TL_account_resetAuthorization req = new TLRPC.TL_account_resetAuthorization();
                    req.hash = authorization.hash;
                    ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                        if (error == null) {
                            sessions.remove(authorization);
                            passwordSessions.remove(authorization);
                            updateRows();
                            if (listAdapter != null) {
                                listAdapter.notifyDataSetChanged();
                            }
                            loadSessions(true);
                        }
                    }));
                }
                super.hide(apply, animated);
            }
        };
        frameLayout.addView(undoView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.BOTTOM | Gravity.LEFT, 8, 0, 8, 8));
    }
    itemsEnterAnimator = new RecyclerItemsEnterAnimator(listView, true) {

        @Override
        public View getProgressView() {
            View progressView = null;
            for (int i = 0; i < listView.getChildCount(); i++) {
                View child = listView.getChildAt(i);
                if (listView.getChildAdapterPosition(child) >= 0 && child instanceof SessionCell && ((SessionCell) child).isStub()) {
                    progressView = child;
                }
            }
            return progressView;
        }
    };
    itemsEnterAnimator.animateAlphaProgressView = false;
    updateRows();
    return fragmentView;
}
Also used : AlertDialog(org.telegram.ui.ActionBar.AlertDialog) ThemeDescription(org.telegram.ui.ActionBar.ThemeDescription) LinearLayout(android.widget.LinearLayout) SvgHelper(org.telegram.messenger.SvgHelper) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) FrameLayout(android.widget.FrameLayout) ImageView(android.widget.ImageView) AndroidUtilities(org.telegram.messenger.AndroidUtilities) CheckBoxCell(org.telegram.ui.Cells.CheckBoxCell) Manifest(android.Manifest) ApplicationLoader(org.telegram.messenger.ApplicationLoader) SessionCell(org.telegram.ui.Cells.SessionCell) TextCell(org.telegram.ui.Cells.TextCell) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) ContextCompat(androidx.core.content.ContextCompat) DocumentObject(org.telegram.messenger.DocumentObject) SlideChooseView(org.telegram.ui.Components.SlideChooseView) EmptyTextProgressView(org.telegram.ui.Components.EmptyTextProgressView) RLottieImageView(org.telegram.ui.Components.RLottieImageView) ImageLocation(org.telegram.messenger.ImageLocation) UndoView(org.telegram.ui.Components.UndoView) BaseFragment(org.telegram.ui.ActionBar.BaseFragment) PorterDuff(android.graphics.PorterDuff) ConnectionsManager(org.telegram.tgnet.ConnectionsManager) ViewGroup(android.view.ViewGroup) NotificationCenter(org.telegram.messenger.NotificationCenter) RecyclerItemsEnterAnimator(org.telegram.ui.Components.RecyclerItemsEnterAnimator) UserConfig(org.telegram.messenger.UserConfig) TextView(android.widget.TextView) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Context(android.content.Context) Spanned(android.text.Spanned) ImageSpan(android.text.style.ImageSpan) ColoredImageSpan(org.telegram.ui.Components.ColoredImageSpan) Theme(org.telegram.ui.ActionBar.Theme) Intent(android.content.Intent) LocaleController(org.telegram.messenger.LocaleController) HeaderCell(org.telegram.ui.Cells.HeaderCell) URLSpanNoUnderline(org.telegram.ui.Components.URLSpanNoUnderline) AlertsCreator(org.telegram.ui.Components.AlertsCreator) TextInfoPrivacyCell(org.telegram.ui.Cells.TextInfoPrivacyCell) ArrayList(java.util.ArrayList) SpannableStringBuilder(android.text.SpannableStringBuilder) TLRPC(org.telegram.tgnet.TLRPC) Toast(android.widget.Toast) ActionBar(org.telegram.ui.ActionBar.ActionBar) TLObject(org.telegram.tgnet.TLObject) SharedConfig(org.telegram.messenger.SharedConfig) MediaDataController(org.telegram.messenger.MediaDataController) Build(android.os.Build) DialogInterface(android.content.DialogInterface) TextSettingsCell(org.telegram.ui.Cells.TextSettingsCell) R(org.telegram.messenger.R) ReplaceableIconDrawable(org.telegram.ui.Components.ReplaceableIconDrawable) BackupImageView(org.telegram.ui.Components.BackupImageView) AbsListView(android.widget.AbsListView) LayoutHelper(org.telegram.ui.Components.LayoutHelper) FileLog(org.telegram.messenger.FileLog) FlickerLoadingView(org.telegram.ui.Components.FlickerLoadingView) MessagesController(org.telegram.messenger.MessagesController) Gravity(android.view.Gravity) UserObject(org.telegram.messenger.UserObject) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) Base64(android.util.Base64) RadioColorCell(org.telegram.ui.Cells.RadioColorCell) AlertDialog(org.telegram.ui.ActionBar.AlertDialog) RecyclerListView(org.telegram.ui.Components.RecyclerListView) SpannableStringBuilder(android.text.SpannableStringBuilder) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TLRPC(org.telegram.tgnet.TLRPC) TextView(android.widget.TextView) ImageView(android.widget.ImageView) RLottieImageView(org.telegram.ui.Components.RLottieImageView) BackupImageView(org.telegram.ui.Components.BackupImageView) ActionBar(org.telegram.ui.ActionBar.ActionBar) AbsListView(android.widget.AbsListView) UserConfig(org.telegram.messenger.UserConfig) CheckBoxCell(org.telegram.ui.Cells.CheckBoxCell) RecyclerItemsEnterAnimator(org.telegram.ui.Components.RecyclerItemsEnterAnimator) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) RecyclerListView(org.telegram.ui.Components.RecyclerListView) UndoView(org.telegram.ui.Components.UndoView) Toast(android.widget.Toast) FlickerLoadingView(org.telegram.ui.Components.FlickerLoadingView) EmptyTextProgressView(org.telegram.ui.Components.EmptyTextProgressView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SlideChooseView(org.telegram.ui.Components.SlideChooseView) EmptyTextProgressView(org.telegram.ui.Components.EmptyTextProgressView) RLottieImageView(org.telegram.ui.Components.RLottieImageView) UndoView(org.telegram.ui.Components.UndoView) TextView(android.widget.TextView) BackupImageView(org.telegram.ui.Components.BackupImageView) AbsListView(android.widget.AbsListView) FlickerLoadingView(org.telegram.ui.Components.FlickerLoadingView) RecyclerListView(org.telegram.ui.Components.RecyclerListView) SessionCell(org.telegram.ui.Cells.SessionCell) FrameLayout(android.widget.FrameLayout) RadioColorCell(org.telegram.ui.Cells.RadioColorCell) LinearLayout(android.widget.LinearLayout)

Aggregations

Manifest (android.Manifest)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageManager (android.content.pm.PackageManager)1 PorterDuff (android.graphics.PorterDuff)1 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)1 Uri (android.net.Uri)1 Build (android.os.Build)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 ImageSpan (android.text.style.ImageSpan)1 Base64 (android.util.Base64)1 TypedValue (android.util.TypedValue)1 Gravity (android.view.Gravity)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 AbsListView (android.widget.AbsListView)1 FrameLayout (android.widget.FrameLayout)1