| 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.os.Build; |
| 13 | import android.text.InputType; |
| 14 | import android.util.TypedValue; |
| 15 | import android.view.Gravity; |
| 16 | import android.view.View; |
| 17 | import android.view.ViewGroup; |
| 18 | import android.widget.EditText; |
| 19 | import android.widget.FrameLayout; |
| 20 | import android.widget.LinearLayout; |
| 21 | import android.widget.ScrollView; |
| 22 | |
| 23 | import org.telegram.messenger.AndroidUtilities; |
| 24 | import org.telegram.messenger.LocaleController; |
| 25 | import org.telegram.messenger.R; |
| 26 | import org.telegram.messenger.SharedConfig; |
| 27 | import org.telegram.ui.ActionBar.ActionBar; |
| 28 | import org.telegram.ui.ActionBar.ActionBarMenu; |
| 29 | import org.telegram.ui.ActionBar.AlertDialog; |
| 30 | import org.telegram.ui.ActionBar.BaseFragment; |
| 31 | import org.telegram.ui.ActionBar.Theme; |
| 32 | import org.telegram.ui.ActionBar.ThemeDescription; |
| 33 | import org.telegram.ui.Components.EditTextBoldCursor; |
| 34 | import org.telegram.ui.Components.LayoutHelper; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.concurrent.atomic.AtomicBoolean; |
| 39 | |
| 40 | import cn.hutool.core.util.StrUtil; |
| 41 | import kotlin.collections.ArraysKt; |
| 42 | import kotlin.collections.CollectionsKt; |
| 43 | import tw.nekomimi.nekogram.sub.SubInfo; |
| 44 | import tw.nekomimi.nekogram.sub.SubManager; |
| 45 | import tw.nekomimi.nekogram.utils.AlertUtil; |
| 46 | import tw.nekomimi.nekogram.utils.UIUtil; |
| 47 | |
| 48 | public class SubSettingsActivity extends BaseFragment { |
| 49 | |
| 50 | private EditText[] inputFields; |
| 51 | |
| 52 | private EditText urlsField; |
| 53 | private EditTextBoldCursor remarksField; |
| 54 | |
| 55 | private ScrollView scrollView; |
| 56 | private LinearLayout linearLayout2; |
| 57 | private LinearLayout inputFieldsContainer; |
| 58 | |
| 59 | private SubInfo subInfo; |
| 60 | |
| 61 | private boolean ignoreOnTextChange; |
| 62 | |
| 63 | private static int done_button = 1; |
| 64 | private static int menu_delete = 2; |
| 65 | |
| 66 | public SubSettingsActivity() { |
| 67 | super(); |
| 68 | subInfo = new SubInfo(); |
| 69 | subInfo.id = 0L; |
| 70 | } |
| 71 | |
| 72 | public SubSettingsActivity(SubInfo subInfo) { |
| 73 | super(); |
| 74 | this.subInfo = subInfo; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onResume() { |
| 79 | super.onResume(); |
| 80 | AndroidUtilities.requestAdjustResize(getParentActivity(), classGuid); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public View createView(Context context) { |
| 85 | actionBar.setTitle(LocaleController.getString("ProxySubDetails", R.string.ProxySubDetails)); |
| 86 | actionBar.setBackButtonImage(R.drawable.ic_ab_back); |
| 87 | actionBar.setAllowOverlayTitle(false); |
| 88 | if (AndroidUtilities.isTablet()) { |
| 89 | actionBar.setOccupyStatusBar(false); |
| 90 | } |
| 91 | |
| 92 | actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() { |
| 93 | @Override |
| 94 | public void onItemClick(int id) { |
| 95 | if (id == -1) { |
| 96 | finishFragment(); |
| 97 | } else if (id == done_button) { |
| 98 | |
| 99 | if (getParentActivity() == null) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (StrUtil.isBlank(urlsField.getText())) { |
| 104 | |
| 105 | urlsField.requestFocus(); |
| 106 | AndroidUtilities.showKeyboard(urlsField); |
| 107 | |
| 108 | return; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | if (StrUtil.isBlank(remarksField.getText())) { |
| 113 | |
| 114 | remarksField.requestFocus(); |
| 115 | AndroidUtilities.showKeyboard(remarksField); |
| 116 | |
| 117 | return; |
| 118 | |
| 119 | } |
| 120 | |
| 121 | subInfo.urls = ArraysKt.toList(urlsField.getText().toString().split("\n")); |
| 122 | subInfo.name = remarksField.getText().toString(); |
| 123 | |
| 124 | doGetProxies(); |
| 125 | |
| 126 | } else if (id == menu_delete) { |
| 127 | |
| 128 | AlertUtil.showConfirm(getParentActivity(), |
| 129 | LocaleController.getString("SubscriptionDelete", R.string.SubscriptionDelete), |
| 130 | R.drawable.baseline_delete_24, LocaleController.getString("Delete", R.string.Delete), |
| 131 | true, () -> { |
| 132 | |
| 133 | AlertDialog pro = AlertUtil.showProgress(getParentActivity()); |
| 134 | |
| 135 | pro.show(); |
| 136 | |
| 137 | UIUtil.runOnIoDispatcher(() -> { |
| 138 | |
| 139 | SubManager.getSubList().remove(subInfo); |
| 140 | |
| 141 | SharedConfig.reloadProxyList(); |
| 142 | |
| 143 | UIUtil.runOnUIThread(() -> { |
| 144 | |
| 145 | pro.dismiss(); |
| 146 | |
| 147 | finishFragment(); |
| 148 | |
| 149 | }); |
| 150 | |
| 151 | }); |
| 152 | |
| 153 | }); |
| 154 | |
| 155 | |
| 156 | } |
| 157 | } |
| 158 | }); |
| 159 | |
| 160 | ActionBarMenu menu = actionBar.createMenu(); |
| 161 | |
| 162 | if (subInfo.id != 0) { |
| 163 | |
| 164 | menu.addItem(menu_delete, R.drawable.baseline_delete_24, AndroidUtilities.dp(56)).setContentDescription(LocaleController.getString("Delete", R.string.Delete)); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56)).setContentDescription(LocaleController.getString("Done", R.string.Done)); |
| 169 | |
| 170 | fragmentView = new FrameLayout(context); |
| 171 | FrameLayout frameLayout = (FrameLayout) fragmentView; |
| 172 | fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray)); |
| 173 | |
| 174 | scrollView = new ScrollView(context); |
| 175 | scrollView.setFillViewport(true); |
| 176 | AndroidUtilities.setScrollViewEdgeEffectColor(scrollView, Theme.getColor(Theme.key_actionBarDefault)); |
| 177 | frameLayout.addView(scrollView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT)); |
| 178 | |
| 179 | linearLayout2 = new LinearLayout(context); |
| 180 | linearLayout2.setOrientation(LinearLayout.VERTICAL); |
| 181 | scrollView.addView(linearLayout2, new ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 182 | |
| 183 | inputFieldsContainer = new LinearLayout(context); |
| 184 | inputFieldsContainer.setOrientation(LinearLayout.VERTICAL); |
| 185 | inputFieldsContainer.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 186 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 187 | // bring to front for transitions |
| 188 | inputFieldsContainer.setElevation(AndroidUtilities.dp(1f)); |
| 189 | inputFieldsContainer.setOutlineProvider(null); |
| 190 | } |
| 191 | linearLayout2.addView(inputFieldsContainer, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT)); |
| 192 | |
| 193 | inputFields = new EditText[2]; |
| 194 | |
| 195 | urlsField = new EditText(context); |
| 196 | inputFields[0] = urlsField; |
| 197 | urlsField.setImeOptions(InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); |
| 198 | urlsField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE); |
| 199 | |
| 200 | urlsField.setHint(LocaleController.getString("SubscriptionUrls", R.string.SubscriptionUrls)); |
| 201 | urlsField.setText(CollectionsKt.joinToString(subInfo.urls, "\n", "", "", -1, "", null)); |
| 202 | urlsField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 203 | urlsField.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText)); |
| 204 | urlsField.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 205 | urlsField.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP); |
| 206 | urlsField.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueHeader)); |
| 207 | urlsField.setSingleLine(false); |
| 208 | urlsField.setMinLines(6); |
| 209 | |
| 210 | inputFieldsContainer.addView(urlsField, LayoutHelper.createLinear(-1, -2, 17, 0, 17, 0)); |
| 211 | |
| 212 | FrameLayout container = new FrameLayout(context); |
| 213 | |
| 214 | remarksField = mkCursor(); |
| 215 | inputFields[1] = remarksField; |
| 216 | remarksField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); |
| 217 | remarksField.setHintText(LocaleController.getString("ProxyRemarks", R.string.ProxyRemarks)); |
| 218 | remarksField.setText(subInfo.name); |
| 219 | remarksField.setSelection(remarksField.length()); |
| 220 | |
| 221 | container.addView(remarksField, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.LEFT | Gravity.TOP, 17, 0, 17, 0)); |
| 222 | |
| 223 | inputFieldsContainer.addView((View) remarksField.getParent(), LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 64)); |
| 224 | |
| 225 | return fragmentView; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | EditTextBoldCursor mkCursor() { |
| 230 | |
| 231 | EditTextBoldCursor cursor = new EditTextBoldCursor(getParentActivity()); |
| 232 | cursor.setSingleLine(true); |
| 233 | cursor.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); |
| 234 | cursor.setHintColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText)); |
| 235 | cursor.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 236 | cursor.setBackground(null); |
| 237 | cursor.setCursorColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 238 | cursor.setCursorSize(AndroidUtilities.dp(20)); |
| 239 | cursor.setCursorWidth(1.5f); |
| 240 | cursor.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL); |
| 241 | cursor.setHeaderHintColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueHeader)); |
| 242 | cursor.setTransformHintToHeader(true); |
| 243 | cursor.setLineColors(Theme.getColor(Theme.key_windowBackgroundWhiteInputField), Theme.getColor(Theme.key_windowBackgroundWhiteInputFieldActivated), Theme.getColor(Theme.key_windowBackgroundWhiteRedText3)); |
| 244 | return cursor; |
| 245 | |
| 246 | } |
| 247 | |
| 248 | public void doGetProxies() { |
| 249 | |
| 250 | AlertDialog pro = AlertUtil.showProgress(getParentActivity(), LocaleController.getString("SubscriptionUpdating", R.string.SubscriptionUpdating)); |
| 251 | |
| 252 | AtomicBoolean canceled = new AtomicBoolean(); |
| 253 | |
| 254 | pro.setOnCancelListener((it) -> { |
| 255 | |
| 256 | canceled.set(true); |
| 257 | |
| 258 | }); |
| 259 | |
| 260 | pro.show(); |
| 261 | |
| 262 | UIUtil.runOnIoDispatcher(() -> { |
| 263 | |
| 264 | try { |
| 265 | |
| 266 | subInfo.proxies = subInfo.reloadProxies(); |
| 267 | subInfo.lastFetch = System.currentTimeMillis(); |
| 268 | |
| 269 | } catch (IOException allTriesFailed) { |
| 270 | |
| 271 | if (canceled.get()) return; |
| 272 | |
| 273 | UIUtil.runOnUIThread(pro::dismiss); |
| 274 | |
| 275 | AlertUtil.showSimpleAlert(getParentActivity(), "tries failed: " + allTriesFailed.toString().trim()); |
| 276 | |
| 277 | return; |
| 278 | |
| 279 | } |
| 280 | |
| 281 | if (subInfo.id == 0) subInfo.id = SubManager.getCount() + 10; |
| 282 | |
| 283 | do { |
| 284 | |
| 285 | try { |
| 286 | SubManager.getSubList().update(subInfo, true); |
| 287 | break; |
| 288 | } catch (Exception ignored) { |
| 289 | } |
| 290 | subInfo.id ++; |
| 291 | |
| 292 | } while (true); |
| 293 | |
| 294 | SharedConfig.reloadProxyList(); |
| 295 | |
| 296 | UIUtil.runOnUIThread(() -> { |
| 297 | |
| 298 | pro.dismiss(); |
| 299 | |
| 300 | finishFragment(); |
| 301 | |
| 302 | }); |
| 303 | |
| 304 | }); |
| 305 | |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public ArrayList<ThemeDescription> getThemeDescriptions() { |
| 310 | final ThemeDescription.ThemeDescriptionDelegate delegate = () -> { |
| 311 | if (inputFields != null) { |
| 312 | for (int i = 0; i < inputFields.length; i++) { |
| 313 | inputFields[i].setText(Theme.getColor(Theme.key_windowBackgroundWhiteInputField)); |
| 314 | } |
| 315 | } |
| 316 | }; |
| 317 | ArrayList<ThemeDescription> arrayList = new ArrayList<>(); |
| 318 | arrayList.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray)); |
| 319 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault)); |
| 320 | arrayList.add(new ThemeDescription(scrollView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault)); |
| 321 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon)); |
| 322 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle)); |
| 323 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector)); |
| 324 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCH, null, null, null, null, Theme.key_actionBarDefaultSearch)); |
| 325 | arrayList.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCHPLACEHOLDER, null, null, null, null, Theme.key_actionBarDefaultSearchPlaceholder)); |
| 326 | arrayList.add(new ThemeDescription(inputFieldsContainer, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundWhite)); |
| 327 | arrayList.add(new ThemeDescription(linearLayout2, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider)); |
| 328 | |
| 329 | arrayList.add(new ThemeDescription(null, 0, null, null, null, null, delegate, Theme.key_windowBackgroundWhiteBlueText4)); |
| 330 | arrayList.add(new ThemeDescription(null, 0, null, null, null, null, delegate, Theme.key_windowBackgroundWhiteGrayText2)); |
| 331 | |
| 332 | if (inputFields != null) { |
| 333 | for (int a = 0; a < inputFields.length; a++) { |
| 334 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 335 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText)); |
| 336 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_HINTTEXTCOLOR | ThemeDescription.FLAG_PROGRESSBAR, null, null, null, null, Theme.key_windowBackgroundWhiteBlueHeader)); |
| 337 | arrayList.add(new ThemeDescription(inputFields[a], ThemeDescription.FLAG_CURSORCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 338 | arrayList.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_windowBackgroundWhiteInputField)); |
| 339 | arrayList.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_windowBackgroundWhiteInputFieldActivated)); |
| 340 | arrayList.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_windowBackgroundWhiteRedText3)); |
| 341 | } |
| 342 | } else { |
| 343 | arrayList.add(new ThemeDescription(null, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 344 | arrayList.add(new ThemeDescription(null, ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText)); |
| 345 | } |
| 346 | |
| 347 | return arrayList; |
| 348 | } |
| 349 | } |
| 350 | |