Search in sources :

Example 1 with WallpaperUpdater

use of org.telegram.ui.Components.WallpaperUpdater in project Telegram-FOSS by Telegram-FOSS-Team.

the class WallpapersListActivity method createView.

@Override
public View createView(Context context) {
    colorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    colorFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    colorFramePaint.setStrokeWidth(AndroidUtilities.dp(1));
    colorFramePaint.setStyle(Paint.Style.STROKE);
    colorFramePaint.setColor(0x33000000);
    updater = new WallpaperUpdater(getParentActivity(), this, new WallpaperUpdater.WallpaperUpdaterDelegate() {

        @Override
        public void didSelectWallpaper(File file, Bitmap bitmap, boolean gallery) {
            presentFragment(new ThemePreviewActivity(new FileWallpaper("", file, file), bitmap), gallery);
        }

        @Override
        public void needOpenColorPicker() {
        }
    });
    hasOwnBackground = true;
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);
    if (currentType == TYPE_ALL) {
        actionBar.setTitle(LocaleController.getString("ChatBackground", R.string.ChatBackground));
    } else if (currentType == TYPE_COLOR) {
        actionBar.setTitle(LocaleController.getString("SelectColorTitle", R.string.SelectColorTitle));
    }
    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {

        @Override
        public void onItemClick(int id) {
            if (id == -1) {
                if (actionBar.isActionModeShowed()) {
                    selectedWallPapers.clear();
                    actionBar.hideActionMode();
                    updateRowsSelection();
                } else {
                    finishFragment();
                }
            } else if (id == delete) {
                if (getParentActivity() == null) {
                    return;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                builder.setTitle(LocaleController.formatPluralString("DeleteBackground", selectedWallPapers.size()));
                builder.setMessage(LocaleController.formatString("DeleteChatBackgroundsAlert", R.string.DeleteChatBackgroundsAlert));
                builder.setPositiveButton(LocaleController.getString("Delete", R.string.Delete), (dialogInterface, i) -> {
                    progressDialog = new AlertDialog(getParentActivity(), 3);
                    progressDialog.setCanCacnel(false);
                    progressDialog.show();
                    ArrayList<Integer> ids = new ArrayList<>();
                    int[] deleteCount = new int[] { 0 };
                    for (int b = 0; b < selectedWallPapers.size(); b++) {
                        Object object = selectedWallPapers.valueAt(b);
                        if (object instanceof ColorWallpaper) {
                            ColorWallpaper colorWallpaper = (ColorWallpaper) object;
                            if (colorWallpaper.parentWallpaper != null && colorWallpaper.parentWallpaper.id < 0) {
                                getMessagesStorage().deleteWallpaper(colorWallpaper.parentWallpaper.id);
                                localWallPapers.remove(colorWallpaper);
                                localDict.remove(colorWallpaper.getHash());
                            } else {
                                object = colorWallpaper.parentWallpaper;
                            }
                        }
                        if (!(object instanceof TLRPC.WallPaper)) {
                            continue;
                        }
                        deleteCount[0]++;
                        TLRPC.WallPaper wallPaper = (TLRPC.WallPaper) object;
                        TLRPC.TL_account_saveWallPaper req = new TLRPC.TL_account_saveWallPaper();
                        req.settings = new TLRPC.TL_wallPaperSettings();
                        req.unsave = true;
                        if (object instanceof TLRPC.TL_wallPaperNoFile) {
                            TLRPC.TL_inputWallPaperNoFile inputWallPaper = new TLRPC.TL_inputWallPaperNoFile();
                            inputWallPaper.id = wallPaper.id;
                            req.wallpaper = inputWallPaper;
                        } else {
                            TLRPC.TL_inputWallPaper inputWallPaper = new TLRPC.TL_inputWallPaper();
                            inputWallPaper.id = wallPaper.id;
                            inputWallPaper.access_hash = wallPaper.access_hash;
                            req.wallpaper = inputWallPaper;
                        }
                        if (wallPaper.slug != null && wallPaper.slug.equals(selectedBackgroundSlug)) {
                            selectedBackgroundSlug = Theme.hasWallpaperFromTheme() ? Theme.THEME_BACKGROUND_SLUG : Theme.DEFAULT_BACKGROUND_SLUG;
                            Theme.getActiveTheme().setOverrideWallpaper(null);
                            Theme.reloadWallpaper();
                        }
                        ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                            deleteCount[0]--;
                            if (deleteCount[0] == 0) {
                                loadWallpapers(true);
                            }
                        }));
                    }
                    if (deleteCount[0] == 0) {
                        loadWallpapers(true);
                    }
                    selectedWallPapers.clear();
                    actionBar.hideActionMode();
                    actionBar.closeSearchField();
                });
                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 (id == forward) {
                Bundle args = new Bundle();
                args.putBoolean("onlySelect", true);
                args.putInt("dialogsType", 3);
                DialogsActivity fragment = new DialogsActivity(args);
                fragment.setDelegate((fragment1, dids, message, param) -> {
                    StringBuilder fmessage = new StringBuilder();
                    for (int b = 0; b < selectedWallPapers.size(); b++) {
                        Object object = selectedWallPapers.valueAt(b);
                        String link;
                        if (object instanceof TLRPC.TL_wallPaper) {
                            link = AndroidUtilities.getWallPaperUrl(object);
                        } else if (object instanceof ColorWallpaper) {
                            link = ((ColorWallpaper) object).getUrl();
                        } else {
                            continue;
                        }
                        if (!TextUtils.isEmpty(link)) {
                            if (fmessage.length() > 0) {
                                fmessage.append('\n');
                            }
                            fmessage.append(link);
                        }
                    }
                    selectedWallPapers.clear();
                    actionBar.hideActionMode();
                    actionBar.closeSearchField();
                    if (dids.size() > 1 || dids.get(0) == UserConfig.getInstance(currentAccount).getClientUserId() || message != null) {
                        updateRowsSelection();
                        for (int a = 0; a < dids.size(); a++) {
                            long did = dids.get(a);
                            if (message != null) {
                                SendMessagesHelper.getInstance(currentAccount).sendMessage(message.toString(), did, null, null, null, true, null, null, null, true, 0, null);
                            }
                            if (!TextUtils.isEmpty(fmessage)) {
                                SendMessagesHelper.getInstance(currentAccount).sendMessage(fmessage.toString(), did, null, null, null, true, null, null, null, true, 0, null);
                            }
                        }
                        fragment1.finishFragment();
                    } else {
                        long did = dids.get(0);
                        Bundle args1 = new Bundle();
                        args1.putBoolean("scrollToTopOnResume", true);
                        if (DialogObject.isEncryptedDialog(did)) {
                            args1.putInt("enc_id", DialogObject.getEncryptedChatId(did));
                        } else {
                            if (DialogObject.isUserDialog(did)) {
                                args1.putLong("user_id", did);
                            } else if (DialogObject.isChatDialog(did)) {
                                args1.putLong("chat_id", -did);
                            }
                            if (!MessagesController.getInstance(currentAccount).checkCanOpenChat(args1, fragment1)) {
                                return;
                            }
                        }
                        NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.closeChats);
                        ChatActivity chatActivity = new ChatActivity(args1);
                        presentFragment(chatActivity, true);
                        SendMessagesHelper.getInstance(currentAccount).sendMessage(fmessage.toString(), did, null, null, null, true, null, null, null, true, 0, null);
                    }
                });
                presentFragment(fragment);
            }
        }
    });
    if (currentType == TYPE_ALL) {
        ActionBarMenu menu = actionBar.createMenu();
        searchItem = menu.addItem(0, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {

            @Override
            public void onSearchExpand() {
                listView.setAdapter(searchAdapter);
                listView.invalidate();
            }

            @Override
            public void onSearchCollapse() {
                listView.setAdapter(listAdapter);
                listView.invalidate();
                searchAdapter.processSearch(null, true);
                searchItem.setSearchFieldCaption(null);
                onCaptionCleared();
            }

            @Override
            public void onTextChanged(EditText editText) {
                searchAdapter.processSearch(editText.getText().toString(), false);
            }

            @Override
            public void onCaptionCleared() {
                searchAdapter.clearColor();
                searchItem.setSearchFieldHint(LocaleController.getString("SearchBackgrounds", R.string.SearchBackgrounds));
            }
        });
        searchItem.setSearchFieldHint(LocaleController.getString("SearchBackgrounds", R.string.SearchBackgrounds));
        final ActionBarMenu actionMode = actionBar.createActionMode(false, null);
        actionMode.setBackgroundColor(Theme.getColor(Theme.key_actionBarDefault));
        actionBar.setItemsColor(Theme.getColor(Theme.key_actionBarDefaultIcon), true);
        actionBar.setItemsBackgroundColor(Theme.getColor(Theme.key_actionBarDefaultSelector), true);
        selectedMessagesCountTextView = new NumberTextView(actionMode.getContext());
        selectedMessagesCountTextView.setTextSize(18);
        selectedMessagesCountTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
        selectedMessagesCountTextView.setTextColor(Theme.getColor(Theme.key_actionBarDefaultIcon));
        selectedMessagesCountTextView.setOnTouchListener((v, event) -> true);
        actionMode.addView(selectedMessagesCountTextView, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT, 1.0f, 65, 0, 0, 0));
        actionModeViews.add(actionMode.addItemWithWidth(forward, R.drawable.msg_forward, AndroidUtilities.dp(54), LocaleController.getString("Forward", R.string.Forward)));
        actionModeViews.add(actionMode.addItemWithWidth(delete, R.drawable.msg_delete, AndroidUtilities.dp(54), LocaleController.getString("Delete", R.string.Delete)));
        selectedWallPapers.clear();
    }
    fragmentView = new FrameLayout(context);
    FrameLayout frameLayout = (FrameLayout) fragmentView;
    listView = new RecyclerListView(context) {

        private Paint paint = new Paint();

        @Override
        public boolean hasOverlappingRendering() {
            return false;
        }

        @Override
        public void onDraw(Canvas c) {
            ViewHolder holder;
            if (getAdapter() == listAdapter && resetInfoRow != -1) {
                holder = findViewHolderForAdapterPosition(resetInfoRow);
            } else {
                holder = null;
            }
            int bottom;
            int height = getMeasuredHeight();
            if (holder != null) {
                bottom = holder.itemView.getBottom();
                if (holder.itemView.getBottom() >= height) {
                    bottom = height;
                }
            } else {
                bottom = height;
            }
            paint.setColor(Theme.getColor(Theme.key_windowBackgroundWhite));
            c.drawRect(0, 0, getMeasuredWidth(), bottom, paint);
            if (bottom != height) {
                paint.setColor(Theme.getColor(Theme.key_windowBackgroundGray));
                c.drawRect(0, bottom, getMeasuredWidth(), height, paint);
            }
        }
    };
    listView.setClipToPadding(false);
    listView.setHorizontalScrollBarEnabled(false);
    listView.setVerticalScrollBarEnabled(false);
    listView.setItemAnimator(null);
    listView.setLayoutAnimation(null);
    listView.setLayoutManager(layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) {

        @Override
        public boolean supportsPredictiveItemAnimations() {
            return false;
        }
    });
    frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.LEFT | Gravity.TOP));
    listView.setAdapter(listAdapter = new ListAdapter(context));
    searchAdapter = new SearchAdapter(context);
    listView.setGlowColor(Theme.getColor(Theme.key_avatar_backgroundActionBarBlue));
    listView.setOnItemClickListener((view, position) -> {
        if (getParentActivity() == null || listView.getAdapter() == searchAdapter) {
            return;
        }
        if (position == uploadImageRow) {
            updater.openGallery();
        } else if (position == setColorRow) {
            WallpapersListActivity activity = new WallpapersListActivity(TYPE_COLOR);
            activity.patterns = patterns;
            presentFragment(activity);
        } else if (position == resetRow) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            builder.setTitle(LocaleController.getString("ResetChatBackgroundsAlertTitle", R.string.ResetChatBackgroundsAlertTitle));
            builder.setMessage(LocaleController.getString("ResetChatBackgroundsAlert", R.string.ResetChatBackgroundsAlert));
            builder.setPositiveButton(LocaleController.getString("Reset", R.string.Reset), (dialogInterface, i) -> {
                if (actionBar.isActionModeShowed()) {
                    selectedWallPapers.clear();
                    actionBar.hideActionMode();
                    updateRowsSelection();
                }
                progressDialog = new AlertDialog(getParentActivity(), 3);
                progressDialog.setCanCacnel(false);
                progressDialog.show();
                TLRPC.TL_account_resetWallPapers req = new TLRPC.TL_account_resetWallPapers();
                ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> loadWallpapers(false)));
            });
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            AlertDialog dialog = builder.create();
            showDialog(dialog);
            TextView button = (TextView) dialog.getButton(DialogInterface.BUTTON_POSITIVE);
            if (button != null) {
                button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
            }
        }
    });
    listView.setOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
            }
            scrolling = newState != RecyclerView.SCROLL_STATE_IDLE;
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (listView.getAdapter() == searchAdapter) {
                int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
                int visibleItemCount = firstVisibleItem == RecyclerView.NO_POSITION ? 0 : Math.abs(layoutManager.findLastVisibleItemPosition() - firstVisibleItem) + 1;
                if (visibleItemCount > 0) {
                    int totalItemCount = layoutManager.getItemCount();
                    if (visibleItemCount != 0 && firstVisibleItem + visibleItemCount > totalItemCount - 2) {
                        searchAdapter.loadMoreResults();
                    }
                }
            }
        }
    });
    searchEmptyView = new EmptyTextProgressView(context);
    searchEmptyView.setVisibility(View.GONE);
    searchEmptyView.setShowAtCenter(true);
    searchEmptyView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
    searchEmptyView.setText(LocaleController.getString("NoResult", R.string.NoResult));
    listView.setEmptyView(searchEmptyView);
    frameLayout.addView(searchEmptyView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    updateRows();
    return fragmentView;
}
Also used : AlertDialog(org.telegram.ui.ActionBar.AlertDialog) ThemeDescription(org.telegram.ui.ActionBar.ThemeDescription) ActionBarMenuItem(org.telegram.ui.ActionBar.ActionBarMenuItem) Bundle(android.os.Bundle) Spannable(android.text.Spannable) WindowManager(android.view.WindowManager) FrameLayout(android.widget.FrameLayout) AndroidUtilities(org.telegram.messenger.AndroidUtilities) ColorDrawable(android.graphics.drawable.ColorDrawable) Animator(android.animation.Animator) Drawable(android.graphics.drawable.Drawable) ShadowSectionCell(org.telegram.ui.Cells.ShadowSectionCell) ApplicationLoader(org.telegram.messenger.ApplicationLoader) TextCell(org.telegram.ui.Cells.TextCell) MediaController(org.telegram.messenger.MediaController) View(android.view.View) Canvas(android.graphics.Canvas) RecyclerView(androidx.recyclerview.widget.RecyclerView) ForegroundColorSpan(android.text.style.ForegroundColorSpan) CombinedDrawable(org.telegram.ui.Components.CombinedDrawable) Utilities(org.telegram.messenger.Utilities) EmptyTextProgressView(org.telegram.ui.Components.EmptyTextProgressView) Surface(android.view.Surface) ObjectAnimator(android.animation.ObjectAnimator) BaseFragment(org.telegram.ui.ActionBar.BaseFragment) SendMessagesHelper(org.telegram.messenger.SendMessagesHelper) ConnectionsManager(org.telegram.tgnet.ConnectionsManager) ViewGroup(android.view.ViewGroup) NotificationCenter(org.telegram.messenger.NotificationCenter) UserConfig(org.telegram.messenger.UserConfig) TextView(android.widget.TextView) Paint(android.graphics.Paint) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) LongSparseArray(android.util.LongSparseArray) FileLoader(org.telegram.messenger.FileLoader) Context(android.content.Context) Spanned(android.text.Spanned) Theme(org.telegram.ui.ActionBar.Theme) Intent(android.content.Intent) LocaleController(org.telegram.messenger.LocaleController) HashMap(java.util.HashMap) WallpaperUpdater(org.telegram.ui.Components.WallpaperUpdater) TextInfoPrivacyCell(org.telegram.ui.Cells.TextInfoPrivacyCell) ArrayList(java.util.ArrayList) MotionEvent(android.view.MotionEvent) TLRPC(org.telegram.tgnet.TLRPC) ActionBar(org.telegram.ui.ActionBar.ActionBar) TLObject(org.telegram.tgnet.TLObject) AnimatorSet(android.animation.AnimatorSet) ColorPicker(org.telegram.ui.Components.ColorPicker) MediaDataController(org.telegram.messenger.MediaDataController) DialogInterface(android.content.DialogInterface) DialogObject(org.telegram.messenger.DialogObject) ActionBarMenu(org.telegram.ui.ActionBar.ActionBarMenu) SpannableString(android.text.SpannableString) R(org.telegram.messenger.R) TextUtils(android.text.TextUtils) LayoutHelper(org.telegram.ui.Components.LayoutHelper) FileLog(org.telegram.messenger.FileLog) File(java.io.File) MessagesController(org.telegram.messenger.MessagesController) Color(android.graphics.Color) Gravity(android.view.Gravity) WallpaperCell(org.telegram.ui.Cells.WallpaperCell) NumberTextView(org.telegram.ui.Components.NumberTextView) SharedPreferences(android.content.SharedPreferences) GraySectionCell(org.telegram.ui.Cells.GraySectionCell) Bitmap(android.graphics.Bitmap) ViewTreeObserver(android.view.ViewTreeObserver) Activity(android.app.Activity) AlertDialog(org.telegram.ui.ActionBar.AlertDialog) Collections(java.util.Collections) EditText(android.widget.EditText) RecyclerListView(org.telegram.ui.Components.RecyclerListView) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TLRPC(org.telegram.tgnet.TLRPC) WallpaperUpdater(org.telegram.ui.Components.WallpaperUpdater) TextView(android.widget.TextView) NumberTextView(org.telegram.ui.Components.NumberTextView) ActionBar(org.telegram.ui.ActionBar.ActionBar) EditText(android.widget.EditText) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) ActionBarMenu(org.telegram.ui.ActionBar.ActionBarMenu) TLObject(org.telegram.tgnet.TLObject) DialogObject(org.telegram.messenger.DialogObject) File(java.io.File) RecyclerListView(org.telegram.ui.Components.RecyclerListView) Bitmap(android.graphics.Bitmap) NumberTextView(org.telegram.ui.Components.NumberTextView) Bundle(android.os.Bundle) EmptyTextProgressView(org.telegram.ui.Components.EmptyTextProgressView) Paint(android.graphics.Paint) FrameLayout(android.widget.FrameLayout) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Aggregations

Animator (android.animation.Animator)1 AnimatorSet (android.animation.AnimatorSet)1 ObjectAnimator (android.animation.ObjectAnimator)1 Activity (android.app.Activity)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 Color (android.graphics.Color)1 Paint (android.graphics.Paint)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 Spanned (android.text.Spanned)1 TextUtils (android.text.TextUtils)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1