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