| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.content.SharedPreferences; |
| 5 | import android.graphics.Canvas; |
| 6 | import android.graphics.Path; |
| 7 | import android.graphics.Rect; |
| 8 | import android.graphics.RectF; |
| 9 | import android.graphics.drawable.Drawable; |
| 10 | import android.os.Bundle; |
| 11 | import android.text.TextUtils; |
| 12 | import android.util.Base64; |
| 13 | import android.util.SparseArray; |
| 14 | import android.util.TypedValue; |
| 15 | import android.view.Gravity; |
| 16 | import android.view.View; |
| 17 | import android.view.WindowManager; |
| 18 | import android.widget.FrameLayout; |
| 19 | import android.widget.ImageView; |
| 20 | import android.widget.TextView; |
| 21 | |
| 22 | import androidx.annotation.NonNull; |
| 23 | import androidx.annotation.Nullable; |
| 24 | |
| 25 | import org.telegram.messenger.AndroidUtilities; |
| 26 | import org.telegram.messenger.ApplicationLoader; |
| 27 | import org.telegram.messenger.ImageLocation; |
| 28 | import org.telegram.messenger.LocaleController; |
| 29 | import org.telegram.messenger.MessagesController; |
| 30 | import org.telegram.messenger.R; |
| 31 | import org.telegram.messenger.UserObject; |
| 32 | import org.telegram.messenger.Utilities; |
| 33 | import org.telegram.tgnet.SerializedData; |
| 34 | import org.telegram.tgnet.TLRPC; |
| 35 | import org.telegram.ui.ActionBar.ActionBar; |
| 36 | import org.telegram.ui.ActionBar.ActionBarMenuItem; |
| 37 | import org.telegram.ui.ActionBar.ActionBarPopupWindow; |
| 38 | import org.telegram.ui.ActionBar.AlertDialog; |
| 39 | import org.telegram.ui.ActionBar.BaseFragment; |
| 40 | import org.telegram.ui.ActionBar.MenuOverlayHost; |
| 41 | import org.telegram.ui.ActionBar.SimpleActionBarMenuOverlay; |
| 42 | import org.telegram.ui.ActionBar.Theme; |
| 43 | import org.telegram.ui.ChatActivity; |
| 44 | import org.telegram.ui.Components.AvatarDrawable; |
| 45 | import org.telegram.ui.Components.BackupImageView; |
| 46 | import org.telegram.ui.Components.LayoutHelper; |
| 47 | import org.telegram.ui.Components.blur3.BlurredBackgroundDrawableViewFactory; |
| 48 | import org.telegram.ui.Components.blur3.drawable.BlurredBackgroundDrawable; |
| 49 | import org.telegram.ui.Components.blur3.drawable.color.BlurredBackgroundColorProvider; |
| 50 | import org.telegram.ui.ProfileActivity; |
| 51 | import org.telegram.ui.TopicsFragment; |
| 52 | import org.telegram.utils.glass.source.GlassSourceCrossfade; |
| 53 | |
| 54 | import java.util.LinkedList; |
| 55 | import java.util.List; |
| 56 | import java.util.concurrent.atomic.AtomicReference; |
| 57 | |
| 58 | public class BackButtonMenuRecent { |
| 59 | |
| 60 | private static final int MAX_RECENT_DIALOGS = 25; |
| 61 | private static final int MAX_DISK_RECENT_DIALOGS = 1000; |
| 62 | |
| 63 | private static final SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nekorecentdialogs", Context.MODE_PRIVATE); |
| 64 | private static final SparseArray<LinkedList<Long>> recentDialogs = new SparseArray<>(); |
| 65 | |
| 66 | /** |
| 67 | * The recent chats in the menu overlay of the screen, growing out of the glass of the back button as the menus of |
| 68 | * the header's buttons. Null when the screen has no overlay or there are no recent chats to show. |
| 69 | * |
| 70 | * @param glassFactory the glass of the menu, as the header's menus; null without glass |
| 71 | * @param glassSource the glass going over to the frosted one as the menu opens; null without a crossfade |
| 72 | */ |
| 73 | @Nullable |
| 74 | public static SimpleActionBarMenuOverlay showInOverlay(int currentAccount, BaseFragment fragment, View backButton, |
| 75 | @Nullable BlurredBackgroundDrawableViewFactory glassFactory, |
| 76 | @Nullable GlassSourceCrossfade glassSource, |
| 77 | @Nullable BlurredBackgroundColorProvider glassColors, |
| 78 | @Nullable Runnable onClosed) { |
| 79 | if (fragment == null || backButton == null) { |
| 80 | return null; |
| 81 | } |
| 82 | final Context context = fragment.getParentActivity(); |
| 83 | final ActionBar actionBar = fragment.getActionBar(); |
| 84 | if (context == null || actionBar == null) { |
| 85 | return null; |
| 86 | } |
| 87 | final List<Long> dialogs = getDialogsToShow(fragment.getCurrentAccount()); |
| 88 | if (dialogs.isEmpty()) { |
| 89 | return null; |
| 90 | } |
| 91 | final SimpleActionBarMenuOverlay overlay = fragment.getSimpleMenuOverlay(); |
| 92 | if (overlay == null) { |
| 93 | return null; |
| 94 | } |
| 95 | final RecentChatsMenuHost host = new RecentChatsMenuHost(actionBar, backButton, glassFactory, glassSource, glassColors, fragment.getResourceProvider(), overlay); |
| 96 | final ActionBarPopupWindow.ActionBarPopupWindowLayout layout = buildLayout(context, fragment, currentAccount, dialogs, |
| 97 | () -> overlay.close(true)); |
| 98 | overlay.open(host, layout); |
| 99 | overlay.setOnClosed(onClosed); |
| 100 | return overlay; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * The back button as the host of the menu: the menu grows out of its glass, the overlay draws the button |
| 105 | * inside the growing menu. |
| 106 | */ |
| 107 | private static class RecentChatsMenuHost implements MenuOverlayHost { |
| 108 | private final ActionBar actionBar; |
| 109 | private final View backButton; |
| 110 | @Nullable |
| 111 | private final BlurredBackgroundDrawableViewFactory glassFactory; |
| 112 | @Nullable |
| 113 | private final GlassSourceCrossfade glassSource; |
| 114 | @Nullable |
| 115 | private final BlurredBackgroundColorProvider glassColors; |
| 116 | @Nullable |
| 117 | private final Theme.ResourcesProvider resourcesProvider; |
| 118 | private final SimpleActionBarMenuOverlay overlay; |
| 119 | |
| 120 | private boolean shown; |
| 121 | private float buttonAlpha = 1f; |
| 122 | |
| 123 | RecentChatsMenuHost(ActionBar actionBar, View backButton, |
| 124 | @Nullable BlurredBackgroundDrawableViewFactory glassFactory, |
| 125 | @Nullable GlassSourceCrossfade glassSource, |
| 126 | @Nullable BlurredBackgroundColorProvider glassColors, |
| 127 | @Nullable Theme.ResourcesProvider resourcesProvider, |
| 128 | SimpleActionBarMenuOverlay overlay) { |
| 129 | this.actionBar = actionBar; |
| 130 | this.backButton = backButton; |
| 131 | this.glassFactory = glassFactory; |
| 132 | this.glassSource = glassSource; |
| 133 | this.glassColors = glassColors; |
| 134 | this.resourcesProvider = resourcesProvider; |
| 135 | this.overlay = overlay; |
| 136 | } |
| 137 | |
| 138 | @Nullable |
| 139 | @Override |
| 140 | public BlurredBackgroundDrawable createMenuGlass(View owner) { |
| 141 | if (glassFactory == null) { |
| 142 | return null; |
| 143 | } |
| 144 | return glassFactory.create(owner).setColorProvider(glassColors).setRadius(AndroidUtilities.dp(23)).setPadding(AndroidUtilities.dp(6)); |
| 145 | } |
| 146 | |
| 147 | @Nullable |
| 148 | @Override |
| 149 | public BlurredBackgroundColorProvider getMenuGlassColorProvider() { |
| 150 | return glassColors; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int getButtonsGlassBackgroundColor() { |
| 155 | return glassColors != null ? glassColors.getBackgroundColor() : 0; |
| 156 | } |
| 157 | |
| 158 | @Nullable |
| 159 | @Override |
| 160 | public BlurredBackgroundColorProvider getButtonsGlassColorProvider() { |
| 161 | return glassColors; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public boolean hasMenuGlassStroke() { |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void setMenuGlassFrostedProgress(float progress) { |
| 171 | if (glassSource != null) { |
| 172 | glassSource.setProgress(progress); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * The glass of the back button as it is drawn by the bar: its pill has a padding for the shadow, so the visible |
| 178 | * shape is {@code s} inside it, round by its half. The menu starts from exactly that, not a pixel bigger. |
| 179 | */ |
| 180 | @Override |
| 181 | public boolean getMenuItemGlassRect(ActionBarMenuItem item, RectF out) { |
| 182 | final int s = AndroidUtilities.dp(46); |
| 183 | final int p = AndroidUtilities.dp(6); |
| 184 | final int top = actionBar.getHeight() - actionBar.getExtraHeight() - (ActionBar.getCurrentActionBarHeight() + s) / 2; |
| 185 | out.set(p, top, p + s, top + s); |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public View getMenuView() { |
| 191 | return backButton; |
| 192 | } |
| 193 | |
| 194 | @NonNull |
| 195 | @Override |
| 196 | public View getMenuHostView() { |
| 197 | return actionBar; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void setMenuOverlay(@Nullable SimpleActionBarMenuOverlay overlay, @Nullable ActionBarMenuItem item) { |
| 202 | final boolean shown = overlay != null; |
| 203 | if (this.shown == shown) { |
| 204 | return; |
| 205 | } |
| 206 | this.shown = shown; |
| 207 | // the overlay draws the button inside the growing menu, the bar keeps its place meanwhile |
| 208 | if (shown) { |
| 209 | buttonAlpha = backButton.getAlpha(); |
| 210 | backButton.setAlpha(0f); |
| 211 | } else { |
| 212 | backButton.setAlpha(buttonAlpha); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Nullable |
| 217 | @Override |
| 218 | public SimpleActionBarMenuOverlay getOpenMenuOverlay() { |
| 219 | return shown ? overlay : null; |
| 220 | } |
| 221 | |
| 222 | @Nullable |
| 223 | @Override |
| 224 | public Theme.ResourcesProvider getMenuResourcesProvider() { |
| 225 | return resourcesProvider; |
| 226 | } |
| 227 | |
| 228 | /** As the menus of the menu items: the menu starts a little above the button. */ |
| 229 | @Override |
| 230 | public int getMenuRise() { |
| 231 | return actionBar.getMenuRise(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public static ActionBarPopupWindow show(int currentAccount, BaseFragment fragment, View backButton) { |
| 236 | if (fragment == null) { |
| 237 | return null; |
| 238 | } |
| 239 | final Context context = fragment.getParentActivity(); |
| 240 | final View fragmentView = fragment.getFragmentView(); |
| 241 | if (context == null || fragmentView == null) { |
| 242 | return null; |
| 243 | } |
| 244 | final List<Long> dialogs = getDialogsToShow(fragment.getCurrentAccount()); |
| 245 | if (dialogs.isEmpty()) { |
| 246 | return null; |
| 247 | } |
| 248 | |
| 249 | final AtomicReference<ActionBarPopupWindow> scrimPopupWindowRef = new AtomicReference<>(); |
| 250 | final ActionBarPopupWindow.ActionBarPopupWindowLayout layout = buildLayout(context, fragment, currentAccount, dialogs, () -> { |
| 251 | final ActionBarPopupWindow window = scrimPopupWindowRef.getAndSet(null); |
| 252 | if (window != null) { |
| 253 | window.dismiss(); |
| 254 | } |
| 255 | }); |
| 256 | |
| 257 | Rect backgroundPaddings = new Rect(); |
| 258 | Drawable shadowDrawable = context.getResources().getDrawable(R.drawable.popup_fixed_alert2).mutate(); |
| 259 | shadowDrawable.getPadding(backgroundPaddings); |
| 260 | |
| 261 | ActionBarPopupWindow scrimPopupWindow = new ActionBarPopupWindow(layout, LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT); |
| 262 | scrimPopupWindowRef.set(scrimPopupWindow); |
| 263 | scrimPopupWindow.setPauseNotifications(true); |
| 264 | scrimPopupWindow.setDismissAnimationDuration(220); |
| 265 | scrimPopupWindow.setOutsideTouchable(true); |
| 266 | scrimPopupWindow.setClippingEnabled(true); |
| 267 | scrimPopupWindow.setAnimationStyle(R.style.PopupContextAnimation); |
| 268 | scrimPopupWindow.setFocusable(true); |
| 269 | layout.measure(View.MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), View.MeasureSpec.AT_MOST)); |
| 270 | scrimPopupWindow.setInputMethodMode(ActionBarPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 271 | scrimPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED); |
| 272 | scrimPopupWindow.getContentView().setFocusableInTouchMode(true); |
| 273 | |
| 274 | int popupX = AndroidUtilities.dp(8) - backgroundPaddings.left; |
| 275 | if (AndroidUtilities.isTablet()) { |
| 276 | int[] location = new int[2]; |
| 277 | fragmentView.getLocationInWindow(location); |
| 278 | popupX += location[0]; |
| 279 | } |
| 280 | int popupY = backButton.getBottom() - backgroundPaddings.top - AndroidUtilities.dp(8); |
| 281 | scrimPopupWindow.showAtLocation(fragmentView, Gravity.LEFT | Gravity.BOTTOM, popupX, popupY); |
| 282 | scrimPopupWindow.dimBehind(); |
| 283 | return scrimPopupWindow; |
| 284 | } |
| 285 | |
| 286 | /** The rows of the recent chats in a menu layout; {@code closeMenu} closes the menu they are shown in. */ |
| 287 | private static ActionBarPopupWindow.ActionBarPopupWindowLayout buildLayout(Context context, BaseFragment fragment, int currentAccount, List<Long> dialogs, Runnable closeMenu) { |
| 288 | final Theme.ResourcesProvider resourcesProvider = fragment.getResourceProvider(); |
| 289 | ActionBarPopupWindow.ActionBarPopupWindowLayout layout = new ActionBarPopupWindow.ActionBarPopupWindowLayout(context) { |
| 290 | final Path path = new Path(); |
| 291 | |
| 292 | @Override |
| 293 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 294 | canvas.save(); |
| 295 | path.rewind(); |
| 296 | AndroidUtilities.rectTmp.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom()); |
| 297 | path.addRoundRect(AndroidUtilities.rectTmp, AndroidUtilities.dp(6), AndroidUtilities.dp(6), Path.Direction.CW); |
| 298 | canvas.clipPath(path); |
| 299 | boolean draw = super.drawChild(canvas, child, drawingTime); |
| 300 | canvas.restore(); |
| 301 | return draw; |
| 302 | } |
| 303 | }; |
| 304 | layout.setBackgroundColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuBackground, resourcesProvider)); |
| 305 | layout.setFitItems(true); |
| 306 | |
| 307 | FrameLayout headerView = new FrameLayout(context); |
| 308 | |
| 309 | TextView titleTextView = new TextView(context); |
| 310 | titleTextView.setTextColor(Theme.getColor(Theme.key_dialogTextBlue, resourcesProvider)); |
| 311 | titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 312 | titleTextView.setText(LocaleController.getString(R.string.RecentChats)); |
| 313 | titleTextView.setTypeface(AndroidUtilities.getTypeface(AndroidUtilities.TYPEFACE_ROBOTO_MEDIUM)); |
| 314 | headerView.addView(titleTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, 24, Gravity.LEFT)); |
| 315 | |
| 316 | ImageView clearImageView = new ImageView(context); |
| 317 | clearImageView.setScaleType(ImageView.ScaleType.CENTER); |
| 318 | clearImageView.setColorFilter(Theme.getColor(Theme.key_actionBarDefaultSubmenuItemIcon, resourcesProvider)); |
| 319 | clearImageView.setImageResource(R.drawable.msg_close); |
| 320 | clearImageView.setBackground(Theme.createSelectorDrawable(Theme.getColor(Theme.key_listSelector, resourcesProvider))); |
| 321 | clearImageView.setOnClickListener(e2 -> { |
| 322 | AlertDialog.Builder builder = new AlertDialog.Builder(context, resourcesProvider); |
| 323 | builder.setTitle(LocaleController.getString(R.string.ClearRecentChats)); |
| 324 | builder.setMessage(LocaleController.getString(R.string.ClearRecentChatAlert)); |
| 325 | builder.setPositiveButton(LocaleController.getString(R.string.ClearButton).toUpperCase(), (dialogInterface, i) -> { |
| 326 | closeMenu.run(); |
| 327 | clearRecentDialogs(currentAccount); |
| 328 | }); |
| 329 | builder.setNegativeButton(LocaleController.getString(R.string.Cancel), null); |
| 330 | fragment.showDialog(builder.create()); |
| 331 | }); |
| 332 | headerView.addView(clearImageView, LayoutHelper.createFrame(24, 24, Gravity.RIGHT | Gravity.CENTER_VERTICAL)); |
| 333 | |
| 334 | headerView.setPadding(AndroidUtilities.dp(9), AndroidUtilities.dp(8), AndroidUtilities.dp(8), AndroidUtilities.dp(8)); |
| 335 | layout.addView(headerView, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 4, 4, 4, 4)); |
| 336 | |
| 337 | for (Long dialogId : dialogs) { |
| 338 | final TLRPC.Chat chat; |
| 339 | final TLRPC.User user; |
| 340 | if (dialogId < 0) { |
| 341 | chat = MessagesController.getInstance(currentAccount).getChat(-dialogId); |
| 342 | user = null; |
| 343 | } else { |
| 344 | chat = null; |
| 345 | user = MessagesController.getInstance(currentAccount).getUser(dialogId); |
| 346 | } |
| 347 | if (chat == null && user == null) { |
| 348 | continue; |
| 349 | } |
| 350 | FrameLayout cell = new FrameLayout(context); |
| 351 | cell.setMinimumWidth(AndroidUtilities.dp(200)); |
| 352 | |
| 353 | BackupImageView imageView = new BackupImageView(context); |
| 354 | imageView.setRoundRadius(chat != null && chat.forum ? AndroidUtilities.dp(8) : AndroidUtilities.dp(16)); |
| 355 | cell.addView(imageView, LayoutHelper.createFrameRelatively(32, 32, Gravity.START | Gravity.CENTER_VERTICAL, 13, 0, 0, 0)); |
| 356 | |
| 357 | TextView titleView = new TextView(context); |
| 358 | titleView.setLines(1); |
| 359 | titleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 360 | titleView.setTextColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuItem, resourcesProvider)); |
| 361 | titleView.setEllipsize(TextUtils.TruncateAt.END); |
| 362 | cell.addView(titleView, LayoutHelper.createFrameRelatively(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.START | Gravity.CENTER_VERTICAL, 59, 0, 12, 0)); |
| 363 | |
| 364 | AvatarDrawable avatarDrawable = new AvatarDrawable(); |
| 365 | avatarDrawable.setScaleSize(.8f); |
| 366 | Drawable thumb = avatarDrawable; |
| 367 | |
| 368 | if (chat != null) { |
| 369 | avatarDrawable.setInfo(chat); |
| 370 | if (chat.photo != null && chat.photo.strippedBitmap != null) { |
| 371 | thumb = chat.photo.strippedBitmap; |
| 372 | } |
| 373 | imageView.setImage(ImageLocation.getForChat(chat, ImageLocation.TYPE_SMALL), "50_50", thumb, chat); |
| 374 | titleView.setText(chat.title); |
| 375 | } else { |
| 376 | String name; |
| 377 | if (user.photo != null && user.photo.strippedBitmap != null) { |
| 378 | thumb = user.photo.strippedBitmap; |
| 379 | } |
| 380 | if (UserObject.isReplyUser(user)) { |
| 381 | name = LocaleController.getString(R.string.RepliesTitle); |
| 382 | avatarDrawable.setAvatarType(AvatarDrawable.AVATAR_TYPE_REPLIES); |
| 383 | imageView.setImageDrawable(avatarDrawable); |
| 384 | } else if (UserObject.isDeleted(user)) { |
| 385 | name = LocaleController.getString(R.string.HiddenName); |
| 386 | avatarDrawable.setInfo(user); |
| 387 | imageView.setImage(ImageLocation.getForUser(user, ImageLocation.TYPE_SMALL), "50_50", avatarDrawable, user); |
| 388 | } else { |
| 389 | name = UserObject.getUserName(user); |
| 390 | avatarDrawable.setInfo(user); |
| 391 | imageView.setImage(ImageLocation.getForUser(user, ImageLocation.TYPE_SMALL), "50_50", thumb, user); |
| 392 | } |
| 393 | titleView.setText(name); |
| 394 | } |
| 395 | |
| 396 | cell.setBackground(Theme.getSelectorDrawable(Theme.getColor(Theme.key_listSelector, resourcesProvider), false)); |
| 397 | cell.setOnClickListener(e2 -> { |
| 398 | closeMenu.run(); |
| 399 | Bundle bundle = new Bundle(); |
| 400 | if (dialogId < 0) { |
| 401 | bundle.putLong("chat_id", -dialogId); |
| 402 | if (MessagesController.getInstance(currentAccount).isForum(dialogId)) { |
| 403 | fragment.presentFragment(new TopicsFragment(bundle)); |
| 404 | } else { |
| 405 | fragment.presentFragment(new ChatActivity(bundle)); |
| 406 | } |
| 407 | } else { |
| 408 | bundle.putLong("user_id", dialogId); |
| 409 | fragment.presentFragment(new ChatActivity(bundle)); |
| 410 | } |
| 411 | }); |
| 412 | cell.setOnLongClickListener(e2 -> { |
| 413 | closeMenu.run(); |
| 414 | Bundle bundle = new Bundle(); |
| 415 | if (dialogId < 0) { |
| 416 | bundle.putLong("chat_id", -dialogId); |
| 417 | } else { |
| 418 | bundle.putLong("user_id", dialogId); |
| 419 | } |
| 420 | fragment.presentFragment(new ProfileActivity(bundle)); |
| 421 | return true; |
| 422 | }); |
| 423 | layout.addView(cell, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 48)); |
| 424 | } |
| 425 | return layout; |
| 426 | } |
| 427 | |
| 428 | private static List<Long> getDialogsToShow(int currentAccount) { |
| 429 | List<Long> dialogs = getRecentDialogs(currentAccount); |
| 430 | if (dialogs.size() > MAX_RECENT_DIALOGS) { |
| 431 | dialogs = dialogs.subList(0, MAX_RECENT_DIALOGS); |
| 432 | } |
| 433 | return dialogs; |
| 434 | } |
| 435 | |
| 436 | public static LinkedList<Long> getRecentDialogs(int currentAccount) { |
| 437 | LinkedList<Long> recentDialog = recentDialogs.get(currentAccount); |
| 438 | if (recentDialog == null) { |
| 439 | recentDialog = new LinkedList<>(); |
| 440 | String list = preferences.getString("recents_" + currentAccount, null); |
| 441 | if (!TextUtils.isEmpty(list)) { |
| 442 | byte[] bytes = Base64.decode(list, Base64.NO_WRAP | Base64.NO_PADDING); |
| 443 | SerializedData data = new SerializedData(bytes); |
| 444 | int count = data.readInt32(false); |
| 445 | for (int a = 0; a < count; a++) { |
| 446 | recentDialog.add(data.readInt64(false)); |
| 447 | } |
| 448 | data.cleanup(); |
| 449 | } |
| 450 | recentDialogs.put(currentAccount, recentDialog); |
| 451 | } |
| 452 | return recentDialog; |
| 453 | } |
| 454 | |
| 455 | public static void addToRecentDialogs(int currentAccount, long dialogId) { |
| 456 | LinkedList<Long> recentDialog = getRecentDialogs(currentAccount); |
| 457 | for (int i = 0; i < recentDialog.size(); i++) { |
| 458 | if (recentDialog.get(i) == dialogId) { |
| 459 | recentDialog.remove(i); |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (recentDialog.size() >= MAX_DISK_RECENT_DIALOGS) { |
| 465 | recentDialog.removeLast(); |
| 466 | } |
| 467 | recentDialog.addFirst(dialogId); |
| 468 | LinkedList<Long> finalRecentDialog = new LinkedList<>(recentDialog); |
| 469 | Utilities.globalQueue.postRunnable(() -> saveRecentDialogs(currentAccount, finalRecentDialog)); |
| 470 | } |
| 471 | |
| 472 | public static void deleteFromRecentDialogs(int currentAccount, long dialogId) { |
| 473 | LinkedList<Long> recentDialog = getRecentDialogs(currentAccount); |
| 474 | recentDialog.remove(dialogId); |
| 475 | Utilities.globalQueue.postRunnable(() -> saveRecentDialogs(currentAccount, recentDialog)); |
| 476 | } |
| 477 | |
| 478 | public static void saveRecentDialogs(int currentAccount, LinkedList<Long> recentDialog) { |
| 479 | SerializedData serializedData = new SerializedData(); |
| 480 | int count = recentDialog.size(); |
| 481 | serializedData.writeInt32(count); |
| 482 | for (Long dialog : recentDialog) { |
| 483 | serializedData.writeInt64(dialog); |
| 484 | } |
| 485 | preferences.edit().putString("recents_" + currentAccount, Base64.encodeToString(serializedData.toByteArray(), Base64.NO_WRAP | Base64.NO_PADDING)).apply(); |
| 486 | serializedData.cleanup(); |
| 487 | } |
| 488 | |
| 489 | public static void clearRecentDialogs(int currentAccount) { |
| 490 | getRecentDialogs(currentAccount).clear(); |
| 491 | preferences.edit().putString("recents_" + currentAccount, "").apply(); |
| 492 | } |
| 493 | } |
| 494 | |