| 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.drawable.Drawable; |
| 9 | import android.os.Bundle; |
| 10 | import android.text.TextUtils; |
| 11 | import android.util.Base64; |
| 12 | import android.util.SparseArray; |
| 13 | import android.util.TypedValue; |
| 14 | import android.view.Gravity; |
| 15 | import android.view.View; |
| 16 | import android.view.WindowManager; |
| 17 | import android.widget.FrameLayout; |
| 18 | import android.widget.ImageView; |
| 19 | import android.widget.TextView; |
| 20 | |
| 21 | import org.telegram.messenger.AndroidUtilities; |
| 22 | import org.telegram.messenger.ApplicationLoader; |
| 23 | import org.telegram.messenger.ImageLocation; |
| 24 | import org.telegram.messenger.LocaleController; |
| 25 | import org.telegram.messenger.MessagesController; |
| 26 | import org.telegram.messenger.R; |
| 27 | import org.telegram.messenger.UserObject; |
| 28 | import org.telegram.messenger.Utilities; |
| 29 | import org.telegram.tgnet.SerializedData; |
| 30 | import org.telegram.tgnet.TLRPC; |
| 31 | import org.telegram.ui.ActionBar.ActionBarPopupWindow; |
| 32 | import org.telegram.ui.ActionBar.AlertDialog; |
| 33 | import org.telegram.ui.ActionBar.BaseFragment; |
| 34 | import org.telegram.ui.ActionBar.Theme; |
| 35 | import org.telegram.ui.ChatActivity; |
| 36 | import org.telegram.ui.Components.AvatarDrawable; |
| 37 | import org.telegram.ui.Components.BackupImageView; |
| 38 | import org.telegram.ui.Components.LayoutHelper; |
| 39 | import org.telegram.ui.ProfileActivity; |
| 40 | import org.telegram.ui.TopicsFragment; |
| 41 | |
| 42 | import java.util.LinkedList; |
| 43 | import java.util.List; |
| 44 | import java.util.concurrent.atomic.AtomicReference; |
| 45 | |
| 46 | public class BackButtonMenuRecent { |
| 47 | |
| 48 | private static final int MAX_RECENT_DIALOGS = 25; |
| 49 | private static final int MAX_DISK_RECENT_DIALOGS = 1000; |
| 50 | |
| 51 | private static final SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nekorecentdialogs", Context.MODE_PRIVATE); |
| 52 | private static final SparseArray<LinkedList<Long>> recentDialogs = new SparseArray<>(); |
| 53 | |
| 54 | public static void show(int currentAccount, BaseFragment fragment, View backButton) { |
| 55 | if (fragment == null) { |
| 56 | return; |
| 57 | } |
| 58 | final Context context = fragment.getParentActivity(); |
| 59 | final View fragmentView = fragment.getFragmentView(); |
| 60 | if (context == null || fragmentView == null) { |
| 61 | return; |
| 62 | } |
| 63 | List<Long> dialogs = getRecentDialogs(fragment.getCurrentAccount()); |
| 64 | if (dialogs.isEmpty()) { |
| 65 | return; |
| 66 | } |
| 67 | if (dialogs.size() > MAX_RECENT_DIALOGS) { |
| 68 | dialogs = dialogs.subList(0, MAX_RECENT_DIALOGS); |
| 69 | } |
| 70 | |
| 71 | ActionBarPopupWindow.ActionBarPopupWindowLayout layout = new ActionBarPopupWindow.ActionBarPopupWindowLayout(context) { |
| 72 | final Path path = new Path(); |
| 73 | |
| 74 | @Override |
| 75 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 76 | canvas.save(); |
| 77 | path.rewind(); |
| 78 | AndroidUtilities.rectTmp.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom()); |
| 79 | path.addRoundRect(AndroidUtilities.rectTmp, AndroidUtilities.dp(6), AndroidUtilities.dp(6), Path.Direction.CW); |
| 80 | canvas.clipPath(path); |
| 81 | boolean draw = super.drawChild(canvas, child, drawingTime); |
| 82 | canvas.restore(); |
| 83 | return draw; |
| 84 | } |
| 85 | }; |
| 86 | Rect backgroundPaddings = new Rect(); |
| 87 | Drawable shadowDrawable = fragment.getParentActivity().getResources().getDrawable(R.drawable.popup_fixed_alert2).mutate(); |
| 88 | shadowDrawable.getPadding(backgroundPaddings); |
| 89 | layout.setBackgroundColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuBackground)); |
| 90 | |
| 91 | AtomicReference<ActionBarPopupWindow> scrimPopupWindowRef = new AtomicReference<>(); |
| 92 | |
| 93 | FrameLayout headerView = new FrameLayout(context); |
| 94 | headerView.setBackgroundColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuBackground)); |
| 95 | |
| 96 | TextView titleTextView = new TextView(context); |
| 97 | titleTextView.setTextColor(Theme.getColor(Theme.key_dialogTextBlue)); |
| 98 | titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 99 | titleTextView.setText(LocaleController.getString(R.string.RecentChats)); |
| 100 | titleTextView.setTypeface(AndroidUtilities.getTypeface(AndroidUtilities.TYPEFACE_ROBOTO_MEDIUM)); |
| 101 | headerView.addView(titleTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, 24, Gravity.LEFT)); |
| 102 | |
| 103 | ImageView clearImageView = new ImageView(context); |
| 104 | clearImageView.setScaleType(ImageView.ScaleType.CENTER); |
| 105 | clearImageView.setColorFilter(Theme.getColor(Theme.key_actionBarDefaultSubmenuItemIcon)); |
| 106 | clearImageView.setImageResource(R.drawable.msg_close); |
| 107 | clearImageView.setBackground(Theme.createSelectorDrawable(Theme.getColor(Theme.key_listSelector))); |
| 108 | clearImageView.setOnClickListener(e2 -> { |
| 109 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 110 | builder.setTitle(LocaleController.getString(R.string.ClearRecentChats)); |
| 111 | builder.setMessage(LocaleController.getString(R.string.ClearRecentChatAlert)); |
| 112 | builder.setPositiveButton(LocaleController.getString(R.string.ClearButton).toUpperCase(), (dialogInterface, i) -> { |
| 113 | if (scrimPopupWindowRef.get() != null) { |
| 114 | scrimPopupWindowRef.getAndSet(null).dismiss(); |
| 115 | } |
| 116 | clearRecentDialogs(currentAccount); |
| 117 | }); |
| 118 | builder.setNegativeButton(LocaleController.getString(R.string.Cancel), null); |
| 119 | fragment.showDialog(builder.create()); |
| 120 | }); |
| 121 | headerView.addView(clearImageView, LayoutHelper.createFrame(24, 24, Gravity.RIGHT | Gravity.CENTER_VERTICAL)); |
| 122 | |
| 123 | headerView.setPadding(AndroidUtilities.dp(9), AndroidUtilities.dp(8), AndroidUtilities.dp(8), AndroidUtilities.dp(8)); |
| 124 | layout.addView(headerView, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 4, 4, 4, 4)); |
| 125 | |
| 126 | for (Long dialogId : dialogs) { |
| 127 | final TLRPC.Chat chat; |
| 128 | final TLRPC.User user; |
| 129 | if (dialogId < 0) { |
| 130 | chat = MessagesController.getInstance(currentAccount).getChat(-dialogId); |
| 131 | user = null; |
| 132 | } else { |
| 133 | chat = null; |
| 134 | user = MessagesController.getInstance(currentAccount).getUser(dialogId); |
| 135 | } |
| 136 | if (chat == null && user == null) { |
| 137 | continue; |
| 138 | } |
| 139 | FrameLayout cell = new FrameLayout(context); |
| 140 | cell.setMinimumWidth(AndroidUtilities.dp(200)); |
| 141 | |
| 142 | BackupImageView imageView = new BackupImageView(context); |
| 143 | imageView.setRoundRadius(chat != null && chat.forum ? AndroidUtilities.dp(8) : AndroidUtilities.dp(16)); |
| 144 | cell.addView(imageView, LayoutHelper.createFrameRelatively(32, 32, Gravity.START | Gravity.CENTER_VERTICAL, 13, 0, 0, 0)); |
| 145 | |
| 146 | TextView titleView = new TextView(context); |
| 147 | titleView.setLines(1); |
| 148 | titleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 149 | titleView.setTextColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuItem)); |
| 150 | titleView.setEllipsize(TextUtils.TruncateAt.END); |
| 151 | cell.addView(titleView, LayoutHelper.createFrameRelatively(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.START | Gravity.CENTER_VERTICAL, 59, 0, 12, 0)); |
| 152 | |
| 153 | AvatarDrawable avatarDrawable = new AvatarDrawable(); |
| 154 | avatarDrawable.setScaleSize(.8f); |
| 155 | Drawable thumb = avatarDrawable; |
| 156 | |
| 157 | if (chat != null) { |
| 158 | avatarDrawable.setInfo(chat); |
| 159 | if (chat.photo != null && chat.photo.strippedBitmap != null) { |
| 160 | thumb = chat.photo.strippedBitmap; |
| 161 | } |
| 162 | imageView.setImage(ImageLocation.getForChat(chat, ImageLocation.TYPE_SMALL), "50_50", thumb, chat); |
| 163 | titleView.setText(chat.title); |
| 164 | } else { |
| 165 | String name; |
| 166 | if (user.photo != null && user.photo.strippedBitmap != null) { |
| 167 | thumb = user.photo.strippedBitmap; |
| 168 | } |
| 169 | if (UserObject.isReplyUser(user)) { |
| 170 | name = LocaleController.getString(R.string.RepliesTitle); |
| 171 | avatarDrawable.setAvatarType(AvatarDrawable.AVATAR_TYPE_REPLIES); |
| 172 | imageView.setImageDrawable(avatarDrawable); |
| 173 | } else if (UserObject.isDeleted(user)) { |
| 174 | name = LocaleController.getString(R.string.HiddenName); |
| 175 | avatarDrawable.setInfo(user); |
| 176 | imageView.setImage(ImageLocation.getForUser(user, ImageLocation.TYPE_SMALL), "50_50", avatarDrawable, user); |
| 177 | } else { |
| 178 | name = UserObject.getUserName(user); |
| 179 | avatarDrawable.setInfo(user); |
| 180 | imageView.setImage(ImageLocation.getForUser(user, ImageLocation.TYPE_SMALL), "50_50", thumb, user); |
| 181 | } |
| 182 | titleView.setText(name); |
| 183 | } |
| 184 | |
| 185 | cell.setBackground(Theme.getSelectorDrawable(Theme.getColor(Theme.key_listSelector), false)); |
| 186 | cell.setOnClickListener(e2 -> { |
| 187 | if (scrimPopupWindowRef.get() != null) { |
| 188 | scrimPopupWindowRef.getAndSet(null).dismiss(); |
| 189 | } |
| 190 | Bundle bundle = new Bundle(); |
| 191 | if (dialogId < 0) { |
| 192 | bundle.putLong("chat_id", -dialogId); |
| 193 | if (MessagesController.getInstance(currentAccount).isForum(dialogId)) { |
| 194 | fragment.presentFragment(new TopicsFragment(bundle)); |
| 195 | } else { |
| 196 | fragment.presentFragment(new ChatActivity(bundle)); |
| 197 | } |
| 198 | } else { |
| 199 | bundle.putLong("user_id", dialogId); |
| 200 | fragment.presentFragment(new ChatActivity(bundle)); |
| 201 | } |
| 202 | }); |
| 203 | cell.setOnLongClickListener(e2 -> { |
| 204 | if (scrimPopupWindowRef.get() != null) { |
| 205 | scrimPopupWindowRef.getAndSet(null).dismiss(); |
| 206 | } |
| 207 | Bundle bundle = new Bundle(); |
| 208 | if (dialogId < 0) { |
| 209 | bundle.putLong("chat_id", -dialogId); |
| 210 | } else { |
| 211 | bundle.putLong("user_id", dialogId); |
| 212 | } |
| 213 | fragment.presentFragment(new ProfileActivity(bundle)); |
| 214 | return true; |
| 215 | }); |
| 216 | layout.addView(cell, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 48)); |
| 217 | } |
| 218 | |
| 219 | ActionBarPopupWindow scrimPopupWindow = new ActionBarPopupWindow(layout, LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT); |
| 220 | scrimPopupWindowRef.set(scrimPopupWindow); |
| 221 | scrimPopupWindow.setPauseNotifications(true); |
| 222 | scrimPopupWindow.setDismissAnimationDuration(220); |
| 223 | scrimPopupWindow.setOutsideTouchable(true); |
| 224 | scrimPopupWindow.setClippingEnabled(true); |
| 225 | scrimPopupWindow.setAnimationStyle(R.style.PopupContextAnimation); |
| 226 | scrimPopupWindow.setFocusable(true); |
| 227 | layout.measure(View.MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), View.MeasureSpec.AT_MOST)); |
| 228 | scrimPopupWindow.setInputMethodMode(ActionBarPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 229 | scrimPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED); |
| 230 | scrimPopupWindow.getContentView().setFocusableInTouchMode(true); |
| 231 | layout.setFitItems(true); |
| 232 | |
| 233 | int popupX = AndroidUtilities.dp(8) - backgroundPaddings.left; |
| 234 | if (AndroidUtilities.isTablet()) { |
| 235 | int[] location = new int[2]; |
| 236 | fragmentView.getLocationInWindow(location); |
| 237 | popupX += location[0]; |
| 238 | } |
| 239 | int popupY = backButton.getBottom() - backgroundPaddings.top - AndroidUtilities.dp(8); |
| 240 | scrimPopupWindow.showAtLocation(fragmentView, Gravity.LEFT | Gravity.TOP, popupX, popupY); |
| 241 | scrimPopupWindow.dimBehind(); |
| 242 | } |
| 243 | |
| 244 | public static LinkedList<Long> getRecentDialogs(int currentAccount) { |
| 245 | LinkedList<Long> recentDialog = recentDialogs.get(currentAccount); |
| 246 | if (recentDialog == null) { |
| 247 | recentDialog = new LinkedList<>(); |
| 248 | String list = preferences.getString("recents_" + currentAccount, null); |
| 249 | if (!TextUtils.isEmpty(list)) { |
| 250 | byte[] bytes = Base64.decode(list, Base64.NO_WRAP | Base64.NO_PADDING); |
| 251 | SerializedData data = new SerializedData(bytes); |
| 252 | int count = data.readInt32(false); |
| 253 | for (int a = 0; a < count; a++) { |
| 254 | recentDialog.add(data.readInt64(false)); |
| 255 | } |
| 256 | data.cleanup(); |
| 257 | } |
| 258 | recentDialogs.put(currentAccount, recentDialog); |
| 259 | } |
| 260 | return recentDialog; |
| 261 | } |
| 262 | |
| 263 | public static void addToRecentDialogs(int currentAccount, long dialogId) { |
| 264 | LinkedList<Long> recentDialog = getRecentDialogs(currentAccount); |
| 265 | for (int i = 0; i < recentDialog.size(); i++) { |
| 266 | if (recentDialog.get(i) == dialogId) { |
| 267 | recentDialog.remove(i); |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (recentDialog.size() >= MAX_DISK_RECENT_DIALOGS) { |
| 273 | recentDialog.removeLast(); |
| 274 | } |
| 275 | recentDialog.addFirst(dialogId); |
| 276 | LinkedList<Long> finalRecentDialog = new LinkedList<>(recentDialog); |
| 277 | Utilities.globalQueue.postRunnable(() -> saveRecentDialogs(currentAccount, finalRecentDialog)); |
| 278 | } |
| 279 | |
| 280 | public static void deleteFromRecentDialogs(int currentAccount, long dialogId) { |
| 281 | LinkedList<Long> recentDialog = getRecentDialogs(currentAccount); |
| 282 | recentDialog.remove(dialogId); |
| 283 | Utilities.globalQueue.postRunnable(() -> saveRecentDialogs(currentAccount, recentDialog)); |
| 284 | } |
| 285 | |
| 286 | public static void saveRecentDialogs(int currentAccount, LinkedList<Long> recentDialog) { |
| 287 | SerializedData serializedData = new SerializedData(); |
| 288 | int count = recentDialog.size(); |
| 289 | serializedData.writeInt32(count); |
| 290 | for (Long dialog : recentDialog) { |
| 291 | serializedData.writeInt64(dialog); |
| 292 | } |
| 293 | preferences.edit().putString("recents_" + currentAccount, Base64.encodeToString(serializedData.toByteArray(), Base64.NO_WRAP | Base64.NO_PADDING)).apply(); |
| 294 | serializedData.cleanup(); |
| 295 | } |
| 296 | |
| 297 | public static void clearRecentDialogs(int currentAccount) { |
| 298 | getRecentDialogs(currentAccount).clear(); |
| 299 | preferences.edit().putString("recents_" + currentAccount, "").apply(); |
| 300 | } |
| 301 | } |
| 302 | |