| 1 | /* |
| 2 | * This is the source code of Telegram for Android v. 5.x.x. |
| 3 | * It is licensed under GNU GPL v. 2 or later. |
| 4 | * You should have received a copy of the license in this archive (see LICENSE). |
| 5 | * |
| 6 | * Copyright Nikolai Kudashov, 2013-2018. |
| 7 | */ |
| 8 | |
| 9 | package tw.nekomimi.nekogram; |
| 10 | |
| 11 | import android.content.Context; |
| 12 | import android.graphics.Canvas; |
| 13 | import android.graphics.PorterDuff; |
| 14 | import android.graphics.PorterDuffColorFilter; |
| 15 | import android.os.Build; |
| 16 | import android.text.InputType; |
| 17 | import android.text.TextUtils; |
| 18 | import android.util.TypedValue; |
| 19 | import android.view.Gravity; |
| 20 | import android.view.View; |
| 21 | import android.view.ViewGroup; |
| 22 | import android.view.inputmethod.EditorInfo; |
| 23 | import android.widget.FrameLayout; |
| 24 | import android.widget.ImageView; |
| 25 | import android.widget.LinearLayout; |
| 26 | import android.widget.ScrollView; |
| 27 | import android.widget.TextView; |
| 28 | |
| 29 | import org.telegram.messenger.AndroidUtilities; |
| 30 | import org.telegram.messenger.LocaleController; |
| 31 | import org.telegram.messenger.R; |
| 32 | import org.telegram.messenger.SharedConfig; |
| 33 | import org.telegram.ui.ActionBar.ActionBar; |
| 34 | import org.telegram.ui.ActionBar.ActionBarMenuItem; |
| 35 | import org.telegram.ui.ActionBar.BaseFragment; |
| 36 | import org.telegram.ui.ActionBar.Theme; |
| 37 | import org.telegram.ui.ActionBar.ThemeDescription; |
| 38 | import org.telegram.ui.Cells.TextCheckCell; |
| 39 | import org.telegram.ui.Cells.TextInfoPrivacyCell; |
| 40 | import org.telegram.ui.Components.EditTextBoldCursor; |
| 41 | import org.telegram.ui.Components.LayoutHelper; |
| 42 | |
| 43 | import java.util.ArrayList; |
| 44 | |
| 45 | import cn.hutool.core.util.StrUtil; |
| 46 | |
| 47 | public class WsSettingsActivity extends BaseFragment { |
| 48 | |
| 49 | private EditTextBoldCursor[] inputFields; |
| 50 | |
| 51 | private EditTextBoldCursor ipField; |
| 52 | private EditTextBoldCursor payloadField; |
| 53 | private TextCheckCell useTlsField; |
| 54 | private EditTextBoldCursor remarksField; |
| 55 | |
| 56 | private ScrollView scrollView; |
| 57 | private LinearLayout linearLayout2; |
| 58 | private LinearLayout inputFieldsContainer; |
| 59 | |
| 60 | private TextInfoPrivacyCell bottomCell; |
| 61 | private ActionBarMenuItem doneItem; |
| 62 | |
| 63 | private SharedConfig.WsProxy currentProxyInfo; |
| 64 | private WsLoader.Bean currentBean; |
| 65 | |
| 66 | private static final int done_button = 1; |
| 67 | |
| 68 | public class TypeCell extends FrameLayout { |
| 69 | |
| 70 | private TextView textView; |
| 71 | private ImageView checkImage; |
| 72 | private boolean needDivider; |
| 73 | |
| 74 | public TypeCell(Context context) { |
| 75 | super(context); |
| 76 | |
| 77 | setWillNotDraw(false); |
| 78 | |
| 79 | textView = new TextView(context); |
| 80 | textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 81 | textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 82 | textView.setLines(1); |
| 83 | textView.setMaxLines(1); |
| 84 | textView.setSingleLine(true); |
| 85 | textView.setEllipsize(TextUtils.TruncateAt.END); |
| 86 | textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL); |
| 87 | addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 23 + 48 : 21, 0, LocaleController.isRTL ? 21 |
| 88 | |
| 89 | checkImage = new ImageView(context); |
| 90 | checkImage.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_featuredStickers_addedIcon), PorterDuff.Mode.SRC_IN)); |
| 91 | checkImage.setImageResource(R.drawable.sticker_added); |
| 92 | addView(checkImage, LayoutHelper.createFrame(19, 14, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL, 21, 0, 21, 0)); |
| 93 | |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 98 | super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(50) + (needDivider ? 1 : 0), MeasureSpec.EXACTLY)); |
| 99 | } |
| 100 | |
| 101 | public void setValue(String name, boolean checked, boolean divider) { |
| 102 | textView.setText(name); |
| 103 | checkImage.setVisibility(checked ? VISIBLE : INVISIBLE); |
| 104 | needDivider = divider; |
| 105 | } |
| 106 | |
| 107 | public void setTypeChecked(boolean value) { |
| 108 | checkImage.setVisibility(value ? VISIBLE : INVISIBLE); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void onDraw(Canvas canvas) { |
| 113 | if (needDivider) { |
| 114 | canvas.drawLine(0, getMeasuredHeight() - 1, getMeasuredWidth(), getMeasuredHeight() - 1, Theme.dividerPaint); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public WsSettingsActivity() { |
| 120 | super(); |
| 121 | currentBean = new WsLoader.Bean(); |
| 122 | } |
| 123 | |
| 124 | public WsSettingsActivity(SharedConfig.WsProxy proxyInfo) { |
| 125 | super(); |
| 126 | currentProxyInfo = proxyInfo; |
| 127 | currentBean = proxyInfo.bean; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | @Override |
| 132 | public void onResume() { |
| 133 | super.onResume(); |
| 134 | AndroidUtilities.requestAdjustResize(getParentActivity(), classGuid); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public View createView(Context context) { |
| 139 | actionBar.setTitle(LocaleController.getString("ProxyDetails", R.string.ProxyDetails)); |
| 140 | actionBar.setBackButtonImage(R.drawable.ic_ab_back); |
| 141 | actionBar.setAllowOverlayTitle(false); |
| 142 | if (AndroidUtilities.isTablet()) { |
| 143 | actionBar.setOccupyStatusBar(false); |
| 144 | } |
| 145 | |
| 146 | actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() { |
| 147 | @Override |
| 148 | public void onItemClick(int id) { |
| 149 | if (id == -1) { |
| 150 | finishFragment(); |
| 151 | } else if (id == done_button) { |
| 152 | |
| 153 | if (getParentActivity() == null) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | if (StrUtil.isBlank(ipField.getText())) { |
| 158 | |
| 159 | ipField.requestFocus(); |
| 160 | AndroidUtilities.showKeyboard(ipField); |
| 161 | |
| 162 | return; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | if (StrUtil.isBlank(payloadField.getText())) { |
| 167 | |
| 168 | payloadField.requestFocus(); |
| 169 | AndroidUtilities.showKeyboard(payloadField); |
| 170 | |
| 171 | return; |
| 172 | |
| 173 | } |
| 174 | |
| 175 | |
| 176 | currentBean.setServer(ipField.getText().toString()); |
| 177 | currentBean.setPayloadStr(payloadField.getText().toString()); |
| 178 | currentBean.setTls(useTlsField.isChecked()); |
| 179 | currentBean.setRemarks(remarksField.getText().toString()); |
| 180 | |
| 181 | if (currentProxyInfo == null) { |
| 182 | currentProxyInfo = new SharedConfig.WsProxy(currentBean); |
| 183 | SharedConfig.addProxy(currentProxyInfo); |
| 184 | SharedConfig.setCurrentProxy(currentProxyInfo); |
| 185 | } else { |
| 186 | currentProxyInfo.proxyCheckPingId = 0; |
| 187 | currentProxyInfo.availableCheckTime = 0; |
| 188 | currentProxyInfo.ping = 0; |
| 189 | SharedConfig.saveProxyList(); |
| 190 | SharedConfig.setProxyEnable(false); |
| 191 | } |
| 192 | |
| 193 | finishFragment(); |
| 194 | |
| 195 | } |
| 196 | } |
| 197 | }); |
| 198 | |
| 199 | doneItem = actionBar.createMenu().addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56)); |
| 200 | doneItem.setContentDescription(LocaleController.getString("Done", R.string.Done)); |
| 201 | |
| 202 | fragmentView = new FrameLayout(context); |
| 203 | FrameLayout frameLayout = (FrameLayout) fragmentView; |
| 204 | fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray)); |
| 205 | |
| 206 | scrollView = new ScrollView(context); |
| 207 | scrollView.setFillViewport(true); |
| 208 | AndroidUtilities.setScrollViewEdgeEffectColor(scrollView, Theme.getColor(Theme.key_actionBarDefault)); |
| 209 | frameLayout.addView(scrollView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT)); |
| 210 | |
| 211 | linearLayout2 = new LinearLayout(context); |
| 212 | linearLayout2.setOrientation(LinearLayout.VERTICAL); |
| 213 | scrollView.addView(linearLayout2, new ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 214 | |
| 215 | inputFieldsContainer = new LinearLayout(context); |
| 216 | inputFieldsContainer.setOrientation(LinearLayout.VERTICAL); |
| 217 | inputFieldsContainer.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 218 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 219 | // bring to front for transitions |
| 220 | inputFieldsContainer.setElevation(AndroidUtilities.dp(1f)); |
| 221 | inputFieldsContainer.setOutlineProvider(null); |
| 222 | } |
| 223 | linearLayout2.addView(inputFieldsContainer, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT)); |
| 224 | |
| 225 | inputFields = new EditTextBoldCursor[5]; |
| 226 | |
| 227 | for (int a = 0; a < 5; a++) { |
| 228 | FrameLayout container = new FrameLayout(context); |
| 229 | EditTextBoldCursor cursor = mkCursor(); |
| 230 | inputFields[a] = cursor; |
| 231 | cursor.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI); |
| 232 | switch (a) { |
| 233 | case 0: |
| 234 | ipField = cursor; |
| 235 | cursor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); |
| 236 | cursor.setHintText(LocaleController.getString("UseProxyAddress", R.string.UseProxyAddress)); |
| 237 | cursor.setText(currentBean.getServer()); |
| 238 | break; |
| 239 | case 1: |
| 240 | payloadField = cursor; |
| 241 | cursor.setInputType(InputType.TYPE_CLASS_NUMBER); |
| 242 | cursor.setHintText(LocaleController.getString("WsPayload", R.string.WsPayload)); |
| 243 | cursor.setText(currentBean.getPayloadStr()); |
| 244 | break; |
| 245 | case 2: |
| 246 | remarksField = cursor; |
| 247 | cursor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); |
| 248 | cursor.setHintText(LocaleController.getString("ProxyRemarks", R.string.ProxyRemarks)); |
| 249 | cursor.setText(currentBean.getRemarks()); |
| 250 | break; |
| 251 | } |
| 252 | cursor.setSelection(cursor.length()); |
| 253 | |
| 254 | cursor.setPadding(0, 0, 0, 0); |
| 255 | container.addView(cursor, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.LEFT | Gravity.TOP, 17, a == 0 ? 12 : 0, 17, 0)); |
| 256 | |
| 257 | } |
| 258 | |
| 259 | inputFieldsContainer.addView((View) ipField.getParent(), LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 64)); |
| 260 | inputFieldsContainer.addView((View) payloadField.getParent(), LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 64)); |
| 261 | |
| 262 | FrameLayout container = new FrameLayout(context); |
| 263 | useTlsField = new TextCheckCell(context); |
| 264 | useTlsField.setBackground(Theme.getSelectorDrawable(false)); |
| 265 | useTlsField.setTextAndCheck(LocaleController.getString("VmessTls", R.string.VmessTls), currentBean.getTls(), false); |
| 266 | container.addView(useTlsField, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.LEFT | Gravity.TOP, 0, 0, 0, 0)); |
| 267 | |
| 268 | useTlsField.setOnClickListener((v) -> useTlsField.setChecked(!useTlsField.isChecked())); |
| 269 | |
| 270 | inputFieldsContainer.addView(container, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 64)); |
| 271 | |
| 272 | inputFieldsContainer.addView((View) remarksField.getParent(), LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 64)); |
| 273 | |
| 274 | bottomCell = new TextInfoPrivacyCell(context); |
| 275 | bottomCell.setBackground(Theme.getThemedDrawable(context, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow)); |
| 276 | bottomCell.setText(LocaleController.getString("ProxyInfoWS", R.string.ProxyInfoWS)); |
| 277 | linearLayout2.addView(bottomCell, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT)); |
| 278 | |
| 279 | return fragmentView; |
| 280 | |
| 281 | } |
| 282 | |
| 283 | EditTextBoldCursor mkCursor() { |
| 284 | |
| 285 | EditTextBoldCursor cursor = new EditTextBoldCursor(getParentActivity()); |
| 286 | cursor.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 287 | cursor.setHintColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText)); |
| 288 | cursor.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 289 | cursor.setBackground(null); |
| 290 | cursor.setCursorColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 291 | cursor.setCursorSize(AndroidUtilities.dp(20)); |
| 292 | cursor.setCursorWidth(1.5f); |
| 293 | cursor.setSingleLine(true); |
| 294 | cursor.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL); |
| 295 | cursor.setHeaderHintColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueHeader)); |
| 296 | cursor.setTransformHintToHeader(true); |
| 297 | cursor.setLineColors(Theme.getColor(Theme.key_windowBackgroundWhiteInputField), Theme.getColor(Theme.key_windowBackgroundWhiteInputFieldActivated), Theme.getColor(Theme.key_windowBackgroundWhiteRedText3)); |
| 298 | return cursor; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | protected void onTransitionAnimationEnd(boolean isOpen, boolean backward) { |
| 304 | if (isOpen && !backward && currentProxyInfo == null) { |
| 305 | ipField.requestFocus(); |
| 306 | AndroidUtilities.showKeyboard(ipField); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public ArrayList<ThemeDescription> getThemeDescriptions() { |
| 312 | final ThemeDescription.ThemeDescriptionDelegate delegate = () -> { |
| 313 | if (inputFields != null) { |
| 314 | for (int i = 0; i < inputFields.length; i++) { |
| 315 | inputFields[i].setLineColors(Theme.getColor(Theme.key_windowBackgroundWhiteInputField), |
| 316 | Theme.getColor(Theme.key_windowBackgroundWhiteInputFieldActivated), |
| 317 | Theme.getColor(Theme.key_windowBackgroundWhiteRedText3)); |
| 318 | } |
| 319 | } |
| 320 | }; |
| 321 | ArrayList<ThemeDescription> arrayList = new ArrayList<>(); |
| 322 | arrayList.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray)); |
| 323 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault)); |
| 324 | arrayList.add(new ThemeDescription(scrollView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault)); |
| 325 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon)); |
| 326 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle)); |
| 327 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector)); |
| 328 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCH, null, null, null, null, Theme.key_actionBarDefaultSearch)); |
| 329 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCHPLACEHOLDER, null, null, null, null, Theme.key_actionBarDefaultSearchPlaceholder)); |
| 330 | arrayList.add(new ThemeDescription(inputFieldsContainer, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundWhite)); |
| 331 | arrayList.add(new ThemeDescription(linearLayout2, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider)); |
| 332 | |
| 333 | arrayList.add(new ThemeDescription(null, 0, null, null, null, null, delegate, Theme.key_windowBackgroundWhiteBlueText4)); |
| 334 | arrayList.add(new ThemeDescription(null, 0, null, null, null, null, delegate, Theme.key_windowBackgroundWhiteGrayText2)); |
| 335 | |
| 336 | |
| 337 | if (inputFields != null) { |
| 338 | for (int a = 0; a < inputFields.length; a++) { |
| 339 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 340 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText)); |
| 341 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_HINTTEXTCOLOR | ThemeDescription.FLAG_PROGRESSBAR, null, null, null, null, Theme.key_windowBackgroundWhiteBlueHeader)); |
| 342 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_CURSORCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 343 | arrayList.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_windowBackgroundWhiteInputField)); |
| 344 | arrayList.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_windowBackgroundWhiteInputFieldActivated)); |
| 345 | arrayList.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_windowBackgroundWhiteRedText3)); |
| 346 | } |
| 347 | } else { |
| 348 | arrayList.add(new ThemeDescription(null, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 349 | arrayList.add(new ThemeDescription(null, ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText)); |
| 350 | } |
| 351 | |
| 352 | arrayList.add(new ThemeDescription(bottomCell, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{TextInfoPrivacyCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow)); |
| 353 | arrayList.add(new ThemeDescription(bottomCell, 0, new Class[]{TextInfoPrivacyCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText4)); |
| 354 | arrayList.add(new ThemeDescription(bottomCell, ThemeDescription.FLAG_LINKCOLOR, new Class[]{TextInfoPrivacyCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteLinkText)); |
| 355 | |
| 356 | return arrayList; |
| 357 | } |
| 358 | } |
| 359 | |