| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.graphics.Canvas; |
| 5 | import android.os.SystemClock; |
| 6 | import android.text.TextUtils; |
| 7 | import android.util.TypedValue; |
| 8 | import android.view.Gravity; |
| 9 | import android.view.View; |
| 10 | import android.view.ViewGroup; |
| 11 | import android.widget.FrameLayout; |
| 12 | import android.widget.TextView; |
| 13 | |
| 14 | import androidx.annotation.NonNull; |
| 15 | import androidx.recyclerview.widget.RecyclerView; |
| 16 | |
| 17 | import org.telegram.messenger.AndroidUtilities; |
| 18 | import org.telegram.messenger.DocumentObject; |
| 19 | import org.telegram.messenger.ImageLocation; |
| 20 | import org.telegram.messenger.LocaleController; |
| 21 | import org.telegram.messenger.MediaDataController; |
| 22 | import org.telegram.messenger.NotificationCenter; |
| 23 | import org.telegram.messenger.R; |
| 24 | import org.telegram.messenger.SvgHelper; |
| 25 | import org.telegram.tgnet.ConnectionsManager; |
| 26 | import org.telegram.tgnet.TLRPC; |
| 27 | import org.telegram.ui.ActionBar.Theme; |
| 28 | import org.telegram.ui.Cells.HeaderCell; |
| 29 | import org.telegram.ui.Components.BackupImageView; |
| 30 | import org.telegram.ui.Components.LayoutHelper; |
| 31 | import org.telegram.ui.Components.LinkSpanDrawable; |
| 32 | import org.telegram.ui.Components.RecyclerListView; |
| 33 | |
| 34 | import java.util.Locale; |
| 35 | |
| 36 | import tw.nekomimi.nekogram.helpers.MessageHelper; |
| 37 | import tw.nekomimi.nekogram.settings.BaseNekoSettingsActivity; |
| 38 | |
| 39 | public class DatacenterActivity extends BaseNekoSettingsActivity implements NotificationCenter.NotificationCenterDelegate { |
| 40 | |
| 41 | private final int dcToHighlight; |
| 42 | |
| 43 | private int headerRow; |
| 44 | private int header2Row; |
| 45 | |
| 46 | private int datacentersRow; |
| 47 | private int datacenters2Row; |
| 48 | |
| 49 | public DatacenterActivity(int dcToHighlight) { |
| 50 | super(); |
| 51 | this.dcToHighlight = dcToHighlight; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public View createView(Context context) { |
| 56 | var view = super.createView(context); |
| 57 | if (dcToHighlight != 0) { |
| 58 | listView.highlightRow(() -> { |
| 59 | layoutManager.scrollToPositionWithOffset(datacentersRow + dcToHighlight, AndroidUtilities.dp(60)); |
| 60 | return datacentersRow + dcToHighlight; |
| 61 | }); |
| 62 | } |
| 63 | return view; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean onFragmentCreate() { |
| 68 | NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.proxyCheckDone); |
| 69 | return super.onFragmentCreate(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onFragmentDestroy() { |
| 74 | super.onFragmentDestroy(); |
| 75 | NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.proxyCheckDone); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void onItemClick(View view, int position, float x, float y) { |
| 80 | if (position > datacentersRow && position < datacenters2Row) { |
| 81 | var datacenterInfo = NekoConfig.datacenterInfos.get(position - datacentersRow - 1); |
| 82 | if (datacenterInfo.checking) { |
| 83 | return; |
| 84 | } |
| 85 | checkDatacenter(datacenterInfo, true); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected BaseListAdapter createAdapter(Context context) { |
| 91 | return new ListAdapter(context); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void updateRows() { |
| 96 | super.updateRows(); |
| 97 | |
| 98 | headerRow = rowCount++; |
| 99 | header2Row = rowCount++; |
| 100 | |
| 101 | datacentersRow = rowCount++; |
| 102 | rowCount += 5; |
| 103 | datacenters2Row = rowCount++; |
| 104 | |
| 105 | for (var datacenterInfo : NekoConfig.datacenterInfos) { |
| 106 | checkDatacenter(datacenterInfo, false); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private void checkDatacenter(NekoConfig.DatacenterInfo datacenterInfo, boolean force) { |
| 111 | if (datacenterInfo.checking) { |
| 112 | return; |
| 113 | } |
| 114 | if (!force && SystemClock.elapsedRealtime() - datacenterInfo.availableCheckTime < 2 * 60 * 1000) { |
| 115 | return; |
| 116 | } |
| 117 | datacenterInfo.checking = true; |
| 118 | int position = datacentersRow + NekoConfig.datacenterInfos.indexOf(datacenterInfo) + 1; |
| 119 | if (force) { |
| 120 | listAdapter.notifyItemChanged(position); |
| 121 | } |
| 122 | datacenterInfo.pingId = ConnectionsManager.getInstance(currentAccount).checkProxy("ping.neko", datacenterInfo.id, null, null, null, time -> AndroidUtilities.runOnUIThread(() -> { |
| 123 | datacenterInfo.availableCheckTime = SystemClock.elapsedRealtime(); |
| 124 | datacenterInfo.checking = false; |
| 125 | if (time == -1) { |
| 126 | datacenterInfo.available = false; |
| 127 | datacenterInfo.ping = 0; |
| 128 | } else { |
| 129 | datacenterInfo.ping = time; |
| 130 | datacenterInfo.available = true; |
| 131 | } |
| 132 | NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.proxyCheckDone, null, datacenterInfo.id); |
| 133 | })); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected String getActionBarTitle() { |
| 138 | return LocaleController.getString("DatacenterStatus", R.string.DatacenterStatus); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | protected boolean hasWhiteActionBar() { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void didReceivedNotification(int id, int account, Object... args) { |
| 148 | if (id == NotificationCenter.proxyCheckDone) { |
| 149 | if (listAdapter != null && args.length > 1) { |
| 150 | listAdapter.notifyItemChanged(datacentersRow + ((int) args[1])); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private class ListAdapter extends BaseListAdapter { |
| 156 | |
| 157 | public ListAdapter(Context context) { |
| 158 | super(context); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 163 | switch (holder.getItemViewType()) { |
| 164 | case 1: { |
| 165 | if (position == datacenters2Row) { |
| 166 | holder.itemView.setBackground(Theme.getThemedDrawable(mContext, R.drawable.greydivider_bottom, getThemedColor(Theme.key_windowBackgroundGrayShadow))); |
| 167 | } else { |
| 168 | holder.itemView.setBackground(Theme.getThemedDrawable(mContext, R.drawable.greydivider, getThemedColor(Theme.key_windowBackgroundGrayShadow))); |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | case 4: { |
| 173 | HeaderCell cell = (HeaderCell) holder.itemView; |
| 174 | cell.setText(LocaleController.getString("DatacenterStatus", R.string.DatacenterStatus)); |
| 175 | break; |
| 176 | } |
| 177 | case Integer.MAX_VALUE: { |
| 178 | DatacenterCell cell = (DatacenterCell) holder.itemView; |
| 179 | cell.setDC(NekoConfig.datacenterInfos.get(position - datacentersRow - 1), position + 1 != datacenters2Row); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @NonNull |
| 186 | @Override |
| 187 | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 188 | if (viewType == Integer.MAX_VALUE - 1) { |
| 189 | var headerCell = new DatacenterHeaderCell(mContext); |
| 190 | headerCell.setBackgroundColor(getThemedColor(Theme.key_windowBackgroundWhite)); |
| 191 | headerCell.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); |
| 192 | return new RecyclerListView.Holder(headerCell); |
| 193 | } else if (viewType == Integer.MAX_VALUE) { |
| 194 | var dcCell = new DatacenterCell(mContext); |
| 195 | dcCell.setBackgroundColor(getThemedColor(Theme.key_windowBackgroundWhite)); |
| 196 | dcCell.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); |
| 197 | return new RecyclerListView.Holder(dcCell); |
| 198 | } else { |
| 199 | return super.onCreateViewHolder(parent, viewType); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public boolean isEnabled(RecyclerView.ViewHolder holder) { |
| 205 | var position = holder.getAdapterPosition(); |
| 206 | if (position > datacentersRow && position < datacenters2Row) { |
| 207 | var datacenterInfo = NekoConfig.datacenterInfos.get(position - datacentersRow - 1); |
| 208 | return !datacenterInfo.checking; |
| 209 | } else { |
| 210 | return super.isEnabled(holder); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public int getItemViewType(int position) { |
| 216 | if (position == header2Row || position == datacenters2Row) { |
| 217 | return 1; |
| 218 | } else if (position == datacentersRow) { |
| 219 | return 4; |
| 220 | } else if (position == headerRow) { |
| 221 | return Integer.MAX_VALUE - 1; |
| 222 | } |
| 223 | return Integer.MAX_VALUE; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private class DatacenterHeaderCell extends FrameLayout implements NotificationCenter.NotificationCenterDelegate { |
| 228 | |
| 229 | BackupImageView imageView; |
| 230 | TextView textView; |
| 231 | |
| 232 | public DatacenterHeaderCell(@NonNull Context context) { |
| 233 | super(context); |
| 234 | imageView = new BackupImageView(context); |
| 235 | addView(imageView, LayoutHelper.createFrame(120, 120, Gravity.CENTER_HORIZONTAL, 0, 16, 0, 0)); |
| 236 | |
| 237 | imageView.setOnClickListener(view -> { |
| 238 | if (imageView.getImageReceiver().getLottieAnimation() != null && !imageView.getImageReceiver().getLottieAnimation().isRunning()) { |
| 239 | imageView.getImageReceiver().getLottieAnimation().setCurrentFrame(0, false); |
| 240 | imageView.getImageReceiver().getLottieAnimation().restart(); |
| 241 | } |
| 242 | }); |
| 243 | imageView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); |
| 244 | |
| 245 | textView = new LinkSpanDrawable.LinksTextView(context, resourcesProvider); |
| 246 | addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 0, 36, 152, 36, 0)); |
| 247 | textView.setGravity(Gravity.CENTER_HORIZONTAL); |
| 248 | textView.setTextColor(getThemedColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 249 | textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); |
| 250 | textView.setLinkTextColor(getThemedColor(Theme.key_windowBackgroundWhiteLinkText)); |
| 251 | textView.setHighlightColor(getThemedColor(Theme.key_windowBackgroundWhiteLinkSelection)); |
| 252 | textView.setMovementMethod(new AndroidUtilities.LinkMovementMethodMy()); |
| 253 | textView.setText(getSpannedString(R.string.DatacenterStatusAbout, "https://core.telegram.org/api/datacenter")); |
| 254 | |
| 255 | setSticker(); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 260 | super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(196), MeasureSpec.EXACTLY)); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | protected void onAttachedToWindow() { |
| 265 | super.onAttachedToWindow(); |
| 266 | setSticker(); |
| 267 | NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.diceStickersDidLoad); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | protected void onDetachedFromWindow() { |
| 272 | super.onDetachedFromWindow(); |
| 273 | NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.diceStickersDidLoad); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void didReceivedNotification(int id, int account, Object... args) { |
| 278 | if (id == NotificationCenter.diceStickersDidLoad) { |
| 279 | String name = (String) args[0]; |
| 280 | if (AndroidUtilities.STICKERS_PLACEHOLDER_PACK_NAME.equals(name)) { |
| 281 | setSticker(); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | private void setSticker() { |
| 287 | TLRPC.Document document = null; |
| 288 | TLRPC.TL_messages_stickerSet set; |
| 289 | |
| 290 | set = MediaDataController.getInstance(currentAccount).getStickerSetByName(AndroidUtilities.STICKERS_PLACEHOLDER_PACK_NAME); |
| 291 | if (set == null) { |
| 292 | set = MediaDataController.getInstance(currentAccount).getStickerSetByEmojiOrName(AndroidUtilities.STICKERS_PLACEHOLDER_PACK_NAME); |
| 293 | } |
| 294 | if (set != null && set.documents.size() >= 3) { |
| 295 | document = set.documents.get(2); |
| 296 | } |
| 297 | |
| 298 | SvgHelper.SvgDrawable svgThumb = null; |
| 299 | if (document != null) { |
| 300 | svgThumb = DocumentObject.getSvgThumb(document.thumbs, Theme.key_emptyListPlaceholder, 0.2f); |
| 301 | } |
| 302 | if (svgThumb != null) { |
| 303 | svgThumb.overrideWidthAndHeight(512, 512); |
| 304 | } |
| 305 | |
| 306 | if (document != null) { |
| 307 | ImageLocation imageLocation = ImageLocation.getForDocument(document); |
| 308 | imageView.setImage(imageLocation, "130_130", "tgs", svgThumb, set); |
| 309 | imageView.getImageReceiver().setAutoRepeat(2); |
| 310 | } else { |
| 311 | MediaDataController.getInstance(currentAccount).loadStickersByEmojiOrName(AndroidUtilities.STICKERS_PLACEHOLDER_PACK_NAME, false, set == null); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | public class DatacenterCell extends FrameLayout { |
| 317 | |
| 318 | private final TextView textView; |
| 319 | private final TextView valueTextView; |
| 320 | private NekoConfig.DatacenterInfo currentInfo; |
| 321 | |
| 322 | private boolean needDivider; |
| 323 | |
| 324 | public DatacenterCell(Context context) { |
| 325 | super(context); |
| 326 | |
| 327 | textView = new TextView(context); |
| 328 | textView.setTextColor(getThemedColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 329 | textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 330 | textView.setLines(1); |
| 331 | textView.setMaxLines(1); |
| 332 | textView.setSingleLine(true); |
| 333 | textView.setEllipsize(TextUtils.TruncateAt.END); |
| 334 | textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL); |
| 335 | addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, (LocaleController.isRTL ? 56 : 21), 10, (LocaleController.isRTL ? 21 : 56), |
| 336 | |
| 337 | valueTextView = new TextView(context); |
| 338 | valueTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13); |
| 339 | valueTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT); |
| 340 | valueTextView.setLines(1); |
| 341 | valueTextView.setMaxLines(1); |
| 342 | valueTextView.setSingleLine(true); |
| 343 | valueTextView.setCompoundDrawablePadding(AndroidUtilities.dp(6)); |
| 344 | valueTextView.setEllipsize(TextUtils.TruncateAt.END); |
| 345 | valueTextView.setPadding(0, 0, 0, 0); |
| 346 | addView(valueTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, (LocaleController.isRTL ? 56 : 21), 35, (LocaleController.isRTL ? 21 : 56), |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 351 | super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(64) + (needDivider ? 1 : 0), MeasureSpec.EXACTLY)); |
| 352 | } |
| 353 | |
| 354 | public void setDC(NekoConfig.DatacenterInfo info, boolean divider) { |
| 355 | textView.setText(String.format(Locale.US, "DC%d %s, %s", info.id, MessageHelper.getDCName(info.id), MessageHelper.getDCLocation(info.id))); |
| 356 | currentInfo = info; |
| 357 | needDivider = divider; |
| 358 | setWillNotDraw(!needDivider); |
| 359 | updateStatus(); |
| 360 | } |
| 361 | |
| 362 | public void updateStatus() { |
| 363 | int colorKey; |
| 364 | if (currentInfo.checking) { |
| 365 | valueTextView.setText(LocaleController.getString("Checking", R.string.Checking)); |
| 366 | colorKey = Theme.key_windowBackgroundWhiteGrayText2; |
| 367 | } else if (currentInfo.available) { |
| 368 | if (currentInfo.ping >= 1000) { |
| 369 | valueTextView.setText(String.format("%s, %s", LocaleController.getString("SpeedSlow", R.string.SpeedSlow), LocaleController.formatString("Ping", R.string.Ping, currentInfo.ping))); |
| 370 | colorKey = Theme.key_windowBackgroundWhiteRedText4; |
| 371 | } else if (currentInfo.ping != 0) { |
| 372 | valueTextView.setText(String.format("%s, %s", LocaleController.getString("Available", R.string.Available), LocaleController.formatString("Ping", R.string.Ping, currentInfo.ping))); |
| 373 | colorKey = Theme.key_windowBackgroundWhiteGreenText; |
| 374 | } else { |
| 375 | valueTextView.setText(LocaleController.getString("Available", R.string.Available)); |
| 376 | colorKey = Theme.key_windowBackgroundWhiteGreenText; |
| 377 | } |
| 378 | } else { |
| 379 | valueTextView.setText(LocaleController.getString("Unavailable", R.string.Unavailable)); |
| 380 | colorKey = Theme.key_windowBackgroundWhiteRedText4; |
| 381 | } |
| 382 | valueTextView.setTag(colorKey); |
| 383 | valueTextView.setTextColor(getThemedColor(colorKey)); |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | protected void onDraw(Canvas canvas) { |
| 388 | if (needDivider) { |
| 389 | canvas.drawLine(LocaleController.isRTL ? 0 : AndroidUtilities.dp(20), getMeasuredHeight() - 1, getMeasuredWidth() - (LocaleController.isRTL ? AndroidUtilities.dp(20) : |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | } |
| 395 | |