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