| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.annotation.SuppressLint; |
| 4 | import android.content.Context; |
| 5 | import android.content.Intent; |
| 6 | import android.net.Uri; |
| 7 | import android.os.Build; |
| 8 | import android.os.Bundle; |
| 9 | import android.text.TextUtils; |
| 10 | import android.view.Gravity; |
| 11 | import android.view.View; |
| 12 | import android.view.ViewGroup; |
| 13 | import android.widget.FrameLayout; |
| 14 | import android.widget.Toast; |
| 15 | |
| 16 | import androidx.core.content.FileProvider; |
| 17 | import androidx.recyclerview.widget.LinearLayoutManager; |
| 18 | import androidx.recyclerview.widget.RecyclerView; |
| 19 | |
| 20 | import com.google.gson.Gson; |
| 21 | |
| 22 | import org.telegram.messenger.AndroidUtilities; |
| 23 | import org.telegram.messenger.BuildConfig; |
| 24 | import org.telegram.messenger.ContactsController; |
| 25 | import org.telegram.messenger.FileLoader; |
| 26 | import org.telegram.messenger.FileLog; |
| 27 | import org.telegram.messenger.LocaleController; |
| 28 | import org.telegram.messenger.MessageObject; |
| 29 | import org.telegram.messenger.R; |
| 30 | import org.telegram.tgnet.TLRPC; |
| 31 | import org.telegram.ui.ActionBar.ActionBar; |
| 32 | import org.telegram.ui.ActionBar.BaseFragment; |
| 33 | import org.telegram.ui.ActionBar.Theme; |
| 34 | import org.telegram.ui.ActionBar.ThemeDescription; |
| 35 | import org.telegram.ui.Cells.EmptyCell; |
| 36 | import org.telegram.ui.Cells.HeaderCell; |
| 37 | import org.telegram.ui.Cells.NotificationsCheckCell; |
| 38 | import org.telegram.ui.Cells.ShadowSectionCell; |
| 39 | import org.telegram.ui.Cells.TextCheckCell; |
| 40 | import org.telegram.ui.Cells.TextDetailSettingsCell; |
| 41 | import org.telegram.ui.Cells.TextSettingsCell; |
| 42 | import org.telegram.ui.Components.LayoutHelper; |
| 43 | import org.telegram.ui.Components.RecyclerListView; |
| 44 | import org.telegram.ui.ProfileActivity; |
| 45 | |
| 46 | import java.io.File; |
| 47 | import java.util.Date; |
| 48 | |
| 49 | public class MessageDetailsActivity extends BaseFragment { |
| 50 | |
| 51 | private RecyclerListView listView; |
| 52 | private ListAdapter listAdapter; |
| 53 | |
| 54 | private MessageObject messageObject; |
| 55 | private TLRPC.Chat fromChat; |
| 56 | private TLRPC.User fromUser; |
| 57 | private String filePath; |
| 58 | private String fileName; |
| 59 | |
| 60 | private int rowCount; |
| 61 | |
| 62 | private int idRow; |
| 63 | private int messageRow; |
| 64 | private int captionRow; |
| 65 | private int groupRow; |
| 66 | private int channelRow; |
| 67 | private int fromRow; |
| 68 | private int botRow; |
| 69 | private int dateRow; |
| 70 | private int editedRow; |
| 71 | private int forwardRow; |
| 72 | private int fileNameRow; |
| 73 | private int filePathRow; |
| 74 | private int fileSizeRow; |
| 75 | private int emptyRow; |
| 76 | private int exportRow; |
| 77 | private int endRow; |
| 78 | |
| 79 | public MessageDetailsActivity(MessageObject messageObject) { |
| 80 | this.messageObject = messageObject; |
| 81 | if (messageObject.messageOwner.to_id != null && messageObject.messageOwner.to_id.channel_id != 0) { |
| 82 | fromChat = getMessagesController().getChat(messageObject.messageOwner.to_id.channel_id); |
| 83 | } else if (messageObject.messageOwner.to_id != null && messageObject.messageOwner.to_id.chat_id != 0) { |
| 84 | fromChat = getMessagesController().getChat(messageObject.messageOwner.to_id.chat_id); |
| 85 | } |
| 86 | if (messageObject.messageOwner.from_id != 0) { |
| 87 | fromUser = getMessagesController().getUser(messageObject.messageOwner.from_id); |
| 88 | } |
| 89 | filePath = messageObject.messageOwner.attachPath; |
| 90 | if (filePath != null && filePath.length() > 0) { |
| 91 | File temp = new File(filePath); |
| 92 | if (!temp.exists()) { |
| 93 | filePath = null; |
| 94 | } |
| 95 | } |
| 96 | if (filePath == null || filePath.length() == 0) { |
| 97 | filePath = FileLoader.getPathToMessage(messageObject.messageOwner).toString(); |
| 98 | } |
| 99 | if (messageObject.messageOwner.media != null && messageObject.messageOwner.media.document != null) { |
| 100 | if (TextUtils.isEmpty(messageObject.messageOwner.media.document.file_name)) { |
| 101 | for (int a = 0; a < messageObject.messageOwner.media.document.attributes.size(); a++) { |
| 102 | if (messageObject.messageOwner.media.document.attributes.get(a) instanceof TLRPC.TL_documentAttributeFilename) { |
| 103 | fileName = messageObject.messageOwner.media.document.attributes.get(a).file_name; |
| 104 | } |
| 105 | } |
| 106 | } else { |
| 107 | fileName = messageObject.messageOwner.media.document.file_name; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean onFragmentCreate() { |
| 115 | super.onFragmentCreate(); |
| 116 | |
| 117 | updateRows(false); |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | @SuppressLint("NewApi") |
| 123 | @Override |
| 124 | public View createView(Context context) { |
| 125 | actionBar.setBackButtonImage(R.drawable.ic_ab_back); |
| 126 | actionBar.setTitle(LocaleController.getString("MessageDetails", R.string.MessageDetails)); |
| 127 | |
| 128 | if (AndroidUtilities.isTablet()) { |
| 129 | actionBar.setOccupyStatusBar(false); |
| 130 | } |
| 131 | actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() { |
| 132 | @Override |
| 133 | public void onItemClick(int id) { |
| 134 | if (id == -1) { |
| 135 | finishFragment(); |
| 136 | } |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | listAdapter = new ListAdapter(context); |
| 141 | |
| 142 | fragmentView = new FrameLayout(context); |
| 143 | fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray)); |
| 144 | FrameLayout frameLayout = (FrameLayout) fragmentView; |
| 145 | |
| 146 | listView = new RecyclerListView(context); |
| 147 | listView.setVerticalScrollBarEnabled(false); |
| 148 | listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) { |
| 149 | @Override |
| 150 | public boolean supportsPredictiveItemAnimations() { |
| 151 | return false; |
| 152 | } |
| 153 | }); |
| 154 | listView.setGlowColor(Theme.getColor(Theme.key_avatar_backgroundActionBarBlue)); |
| 155 | listView.setAdapter(listAdapter); |
| 156 | listView.setItemAnimator(null); |
| 157 | listView.setLayoutAnimation(null); |
| 158 | frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT)); |
| 159 | listView.setOnItemClickListener((view, position, x, y) -> { |
| 160 | if (position == exportRow) { |
| 161 | Gson gson = new Gson(); |
| 162 | try { |
| 163 | AndroidUtilities.addToClipboard(gson.toJson(messageObject)); |
| 164 | Toast.makeText(getParentActivity(), LocaleController.getString("TextCopied", R.string.TextCopied), Toast.LENGTH_SHORT).show(); |
| 165 | } catch (Exception e) { |
| 166 | FileLog.e(e); |
| 167 | } |
| 168 | } else if (position != endRow && position != emptyRow) { |
| 169 | TextDetailSettingsCell textCell = (TextDetailSettingsCell) view; |
| 170 | try { |
| 171 | AndroidUtilities.addToClipboard(textCell.getValueTextView().getText()); |
| 172 | Toast.makeText(getParentActivity(), LocaleController.getString("TextCopied", R.string.TextCopied), Toast.LENGTH_SHORT).show(); |
| 173 | } catch (Exception e) { |
| 174 | FileLog.e(e); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | }); |
| 179 | listView.setOnItemLongClickListener((view, position) -> { |
| 180 | if (position == filePathRow) { |
| 181 | AndroidUtilities.runOnUIThread(() -> { |
| 182 | Intent intent = new Intent(Intent.ACTION_SEND); |
| 183 | intent.setType("application/octet-stream"); |
| 184 | if (Build.VERSION.SDK_INT >= 24) { |
| 185 | try { |
| 186 | intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", new File(filePath))); |
| 187 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 188 | } catch (Exception ignore) { |
| 189 | intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath))); |
| 190 | } |
| 191 | } else { |
| 192 | intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath))); |
| 193 | } |
| 194 | startActivityForResult(Intent.createChooser(intent, LocaleController.getString("ShareFile", R.string.ShareFile)), 500); |
| 195 | }); |
| 196 | } else if (position == channelRow || position == groupRow) { |
| 197 | if (fromChat != null) { |
| 198 | Bundle args = new Bundle(); |
| 199 | args.putInt("chat_id", fromChat.id); |
| 200 | ProfileActivity fragment = new ProfileActivity(args); |
| 201 | presentFragment(fragment); |
| 202 | } |
| 203 | } else if (position == fromRow) { |
| 204 | if (fromUser != null) { |
| 205 | Bundle args = new Bundle(); |
| 206 | args.putInt("user_id", fromUser.id); |
| 207 | ProfileActivity fragment = new ProfileActivity(args); |
| 208 | presentFragment(fragment); |
| 209 | } |
| 210 | } else { |
| 211 | return false; |
| 212 | } |
| 213 | return true; |
| 214 | }); |
| 215 | return fragmentView; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void onResume() { |
| 220 | super.onResume(); |
| 221 | if (listAdapter != null) { |
| 222 | listAdapter.notifyDataSetChanged(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private void updateRows(boolean notify) { |
| 227 | rowCount = 0; |
| 228 | idRow = rowCount++; |
| 229 | messageRow = TextUtils.isEmpty(messageObject.messageText) ? -1 : rowCount++; |
| 230 | captionRow = TextUtils.isEmpty(messageObject.caption) ? -1 : rowCount++; |
| 231 | groupRow = fromChat != null && !fromChat.broadcast ? rowCount++ : -1; |
| 232 | channelRow = fromChat != null && fromChat.broadcast ? rowCount++ : -1; |
| 233 | fromRow = fromUser != null || messageObject.messageOwner.post_author != null ? rowCount++ : -1; |
| 234 | botRow = fromUser != null && fromUser.bot ? rowCount++ : -1; |
| 235 | dateRow = messageObject.messageOwner.date != 0 ? rowCount++ : -1; |
| 236 | editedRow = messageObject.messageOwner.edit_date != 0 ? rowCount++ : -1; |
| 237 | forwardRow = messageObject.isForwarded() ? rowCount++ : -1; |
| 238 | fileNameRow = TextUtils.isEmpty(fileName) ? -1 : rowCount++; |
| 239 | filePathRow = TextUtils.isEmpty(filePath) ? -1 : rowCount++; |
| 240 | fileSizeRow = messageObject.getSize() != 0 ? rowCount++ : -1; |
| 241 | emptyRow = rowCount++; |
| 242 | exportRow = rowCount++; |
| 243 | endRow = rowCount++; |
| 244 | if (notify && listAdapter != null) { |
| 245 | listAdapter.notifyDataSetChanged(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public ThemeDescription[] getThemeDescriptions() { |
| 251 | return new ThemeDescription[]{ |
| 252 | new ThemeDescription(listView, ThemeDescription.FLAG_CELLBACKGROUNDCOLOR, new Class[]{EmptyCell.class, TextSettingsCell.class, TextCheckCell.class, HeaderCell.class, TextDetailSettingsCell.class, NotificationsCheckCell.class}, null, null, null, Theme.key_windowBackgroundWhite), |
| 253 | new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray), |
| 254 | |
| 255 | new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue), |
| 256 | new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue), |
| 257 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_avatar_actionBarIconBlue), |
| 258 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle), |
| 259 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_avatar_actionBarSelectorBlue), |
| 260 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUBACKGROUND, null, null, null, null, Theme.key_actionBarDefaultSubmenuBackground), |
| 261 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUITEM, null, null, null, null, Theme.key_actionBarDefaultSubmenuItem), |
| 262 | |
| 263 | new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector), |
| 264 | |
| 265 | new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider), |
| 266 | |
| 267 | new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{ShadowSectionCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow), |
| 268 | |
| 269 | new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText), |
| 270 | new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteValueText), |
| 271 | |
| 272 | new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText), |
| 273 | new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2), |
| 274 | }; |
| 275 | } |
| 276 | |
| 277 | private class ListAdapter extends RecyclerListView.SelectionAdapter { |
| 278 | |
| 279 | private Context mContext; |
| 280 | |
| 281 | public ListAdapter(Context context) { |
| 282 | mContext = context; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public int getItemCount() { |
| 287 | return rowCount; |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 292 | switch (holder.getItemViewType()) { |
| 293 | case 1: { |
| 294 | break; |
| 295 | } |
| 296 | case 2: { |
| 297 | TextDetailSettingsCell textCell = (TextDetailSettingsCell) holder.itemView; |
| 298 | textCell.setMultilineDetail(true); |
| 299 | boolean divider = position + 1 != emptyRow; |
| 300 | if (position == idRow) { |
| 301 | textCell.setTextAndValue("ID", String.valueOf(messageObject.messageOwner.id), divider); |
| 302 | } else if (position == messageRow) { |
| 303 | textCell.setTextWithEmojiAnd21Value("Message", messageObject.messageText, divider); |
| 304 | } else if (position == captionRow) { |
| 305 | textCell.setTextWithEmojiAnd21Value("Caption", messageObject.caption, divider); |
| 306 | } else if (position == channelRow || position == groupRow) { |
| 307 | StringBuilder builder = new StringBuilder(); |
| 308 | builder.append(fromChat.title); |
| 309 | builder.append("\n"); |
| 310 | if (!TextUtils.isEmpty(fromChat.username)) { |
| 311 | builder.append("@"); |
| 312 | builder.append(fromChat.username); |
| 313 | builder.append("\n"); |
| 314 | } |
| 315 | builder.append(fromChat.id); |
| 316 | textCell.setTextWithEmojiAnd21Value(position == channelRow ? "Channel" : "Group", builder.toString(), divider); |
| 317 | } else if (position == fromRow) { |
| 318 | StringBuilder builder = new StringBuilder(); |
| 319 | if (fromUser != null) { |
| 320 | builder.append(ContactsController.formatName(fromUser.first_name, fromUser.last_name)); |
| 321 | builder.append("\n"); |
| 322 | if (!TextUtils.isEmpty(fromUser.username)) { |
| 323 | builder.append("@"); |
| 324 | builder.append(fromUser.username); |
| 325 | builder.append("\n"); |
| 326 | } |
| 327 | builder.append(fromUser.id); |
| 328 | } else { |
| 329 | builder.append(messageObject.messageOwner.post_author); |
| 330 | } |
| 331 | textCell.setTextWithEmojiAnd21Value("From", builder.toString(), divider); |
| 332 | } else if (position == botRow) { |
| 333 | textCell.setTextWithEmojiAnd21Value("Bot", "Yes", divider); |
| 334 | } else if (position == dateRow) { |
| 335 | long date = (long) messageObject.messageOwner.date * 1000; |
| 336 | textCell.setTextWithEmojiAnd21Value("Date", LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, LocaleController.getInstance().formatterYear.format(new Date(date)), LocaleController.getInstance().formatterDay.format(new Date(date))), divider); |
| 337 | } else if (position == editedRow) { |
| 338 | long date = (long) messageObject.messageOwner.date * 1000; |
| 339 | textCell.setTextWithEmojiAnd21Value("Edited", LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, LocaleController.getInstance().formatterYear.format(new Date(date)), LocaleController.getInstance().formatterDay.format(new Date(date))), divider); |
| 340 | } else if (position == forwardRow) { |
| 341 | StringBuilder builder = new StringBuilder(); |
| 342 | if (messageObject.messageOwner.fwd_from.channel_id != 0) { |
| 343 | TLRPC.Chat chat = getMessagesController().getChat(messageObject.messageOwner.fwd_from.channel_id); |
| 344 | builder.append(chat.title); |
| 345 | builder.append("\n"); |
| 346 | if (!TextUtils.isEmpty(chat.username)) { |
| 347 | builder.append("@"); |
| 348 | builder.append(chat.username); |
| 349 | builder.append("\n"); |
| 350 | } |
| 351 | builder.append(chat.id); |
| 352 | } else if (messageObject.messageOwner.fwd_from.from_id != 0) { |
| 353 | TLRPC.User user = getMessagesController().getUser(messageObject.messageOwner.fwd_from.from_id); |
| 354 | builder.append(ContactsController.formatName(user.first_name, user.last_name)); |
| 355 | builder.append("\n"); |
| 356 | if (!TextUtils.isEmpty(user.username)) { |
| 357 | builder.append("@"); |
| 358 | builder.append(user.username); |
| 359 | builder.append("\n"); |
| 360 | } |
| 361 | builder.append(user.id); |
| 362 | } else if (!TextUtils.isEmpty(messageObject.messageOwner.fwd_from.from_name)) { |
| 363 | builder.append(messageObject.messageOwner.fwd_from.from_name); |
| 364 | } |
| 365 | textCell.setTextWithEmojiAnd21Value("Forward from", builder.toString(), divider); |
| 366 | } else if (position == fileNameRow) { |
| 367 | textCell.setTextWithEmojiAnd21Value("File name", fileName, divider); |
| 368 | } else if (position == filePathRow) { |
| 369 | textCell.setTextWithEmojiAnd21Value("File path", filePath, divider); |
| 370 | } else if (position == fileSizeRow) { |
| 371 | textCell.setTextWithEmojiAnd21Value("File size", AndroidUtilities.formatFileSize(messageObject.getSize()), divider); |
| 372 | } |
| 373 | break; |
| 374 | } |
| 375 | case 3: { |
| 376 | TextSettingsCell textCell = (TextSettingsCell) holder.itemView; |
| 377 | if (position == exportRow) { |
| 378 | textCell.setText(LocaleController.getString("ExportAsJson", R.string.ExportAsJson), false); |
| 379 | textCell.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueText)); |
| 380 | } |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public boolean isEnabled(RecyclerView.ViewHolder holder) { |
| 388 | int position = holder.getAdapterPosition(); |
| 389 | return position != endRow && position != emptyRow; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 394 | View view = null; |
| 395 | switch (viewType) { |
| 396 | case 1: |
| 397 | view = new ShadowSectionCell(mContext); |
| 398 | break; |
| 399 | case 2: |
| 400 | view = new TextDetailSettingsCell(mContext); |
| 401 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 402 | break; |
| 403 | case 3: |
| 404 | view = new TextSettingsCell(mContext); |
| 405 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 406 | break; |
| 407 | } |
| 408 | view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); |
| 409 | return new RecyclerListView.Holder(view); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public int getItemViewType(int position) { |
| 414 | if (position == endRow || position == emptyRow) { |
| 415 | return 1; |
| 416 | } else if (position == exportRow) { |
| 417 | return 3; |
| 418 | } else { |
| 419 | return 2; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |