| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.view.Gravity; |
| 5 | import android.view.View; |
| 6 | import android.view.ViewGroup; |
| 7 | import android.widget.FrameLayout; |
| 8 | |
| 9 | import androidx.recyclerview.widget.LinearLayoutManager; |
| 10 | import androidx.recyclerview.widget.RecyclerView; |
| 11 | |
| 12 | import org.telegram.messenger.AndroidUtilities; |
| 13 | import org.telegram.messenger.ApplicationLoader; |
| 14 | import org.telegram.messenger.LocaleController; |
| 15 | import org.telegram.messenger.R; |
| 16 | import org.telegram.ui.ActionBar.ActionBar; |
| 17 | import org.telegram.ui.ActionBar.AlertDialog; |
| 18 | import org.telegram.ui.ActionBar.BaseFragment; |
| 19 | import org.telegram.ui.ActionBar.Theme; |
| 20 | import org.telegram.ui.ActionBar.ThemeDescription; |
| 21 | import org.telegram.ui.Cells.EmptyCell; |
| 22 | import org.telegram.ui.Cells.HeaderCell; |
| 23 | import org.telegram.ui.Cells.NotificationsCheckCell; |
| 24 | import org.telegram.ui.Cells.ShadowSectionCell; |
| 25 | import org.telegram.ui.Cells.TextCheckCell; |
| 26 | import org.telegram.ui.Cells.TextDetailSettingsCell; |
| 27 | import org.telegram.ui.Cells.TextSettingsCell; |
| 28 | import org.telegram.ui.Components.LayoutHelper; |
| 29 | import org.telegram.ui.Components.RecyclerListView; |
| 30 | |
| 31 | import java.io.File; |
| 32 | import java.util.ArrayList; |
| 33 | |
| 34 | import tw.nekomimi.nekogram.parts.UpdateChecksKt; |
| 35 | import tw.nekomimi.nekogram.utils.AlertUtil; |
| 36 | import tw.nekomimi.nekogram.utils.FileUtil; |
| 37 | import tw.nekomimi.nekogram.utils.LocaleUtil; |
| 38 | import tw.nekomimi.nekogram.utils.ShareUtil; |
| 39 | import tw.nekomimi.nekogram.utils.UIUtil; |
| 40 | import tw.nekomimi.nekogram.utils.ZipUtil; |
| 41 | |
| 42 | public class NekoXSettingActivity extends BaseFragment { |
| 43 | |
| 44 | private RecyclerListView listView; |
| 45 | private ListAdapter listAdapter; |
| 46 | |
| 47 | private int rowCount; |
| 48 | |
| 49 | private int developerSettingsRow; |
| 50 | |
| 51 | private int enableRow; |
| 52 | private int disableFlagSecureRow; |
| 53 | private int disableScreenshotDetectionRow; |
| 54 | private int disableStatusUpdateRow; |
| 55 | |
| 56 | private int fetchAndExportLangRow; |
| 57 | private int checkUpdateRepoForceRow; |
| 58 | |
| 59 | @Override |
| 60 | public boolean onFragmentCreate() { |
| 61 | super.onFragmentCreate(); |
| 62 | updateRows(); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public View createView(Context context) { |
| 68 | actionBar.setBackButtonImage(R.drawable.ic_ab_back); |
| 69 | actionBar.setTitle(LocaleController.getString("NekoSettings", R.string.NekoSettings)); |
| 70 | |
| 71 | if (AndroidUtilities.isTablet()) { |
| 72 | actionBar.setOccupyStatusBar(false); |
| 73 | } |
| 74 | actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() { |
| 75 | @Override |
| 76 | public void onItemClick(int id) { |
| 77 | if (id == -1) { |
| 78 | finishFragment(); |
| 79 | } |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | listAdapter = new ListAdapter(context); |
| 84 | |
| 85 | fragmentView = new FrameLayout(context); |
| 86 | fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray)); |
| 87 | FrameLayout frameLayout = (FrameLayout) fragmentView; |
| 88 | |
| 89 | listView = new RecyclerListView(context); |
| 90 | listView.setVerticalScrollBarEnabled(false); |
| 91 | listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) { |
| 92 | @Override |
| 93 | public boolean supportsPredictiveItemAnimations() { |
| 94 | return false; |
| 95 | } |
| 96 | }); |
| 97 | listView.setGlowColor(Theme.getColor(Theme.key_avatar_backgroundActionBarBlue)); |
| 98 | listView.setAdapter(listAdapter); |
| 99 | listView.setItemAnimator(null); |
| 100 | listView.setLayoutAnimation(null); |
| 101 | frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT)); |
| 102 | listView.setOnItemClickListener((view, position, x, y) -> { |
| 103 | |
| 104 | if (position == fetchAndExportLangRow) { |
| 105 | fetchAndExportLang(); |
| 106 | } else if (position == checkUpdateRepoForceRow) { |
| 107 | UpdateChecksKt.checkUpdate(getParentActivity(), true); |
| 108 | } |
| 109 | |
| 110 | if (position == enableRow) { |
| 111 | NekoXConfig.toggleDeveloperMode(); |
| 112 | updateRows(); |
| 113 | } else if (position == disableFlagSecureRow) { |
| 114 | NekoXConfig.toggleDisableFlagSecure(); |
| 115 | if (view instanceof TextCheckCell) { |
| 116 | ((TextCheckCell) view).setChecked(NekoXConfig.disableFlagSecure); |
| 117 | } |
| 118 | } else if (position == disableScreenshotDetectionRow) { |
| 119 | NekoXConfig.toggleDisableScreenshotDetection(); |
| 120 | if (view instanceof TextCheckCell) { |
| 121 | ((TextCheckCell) view).setChecked(NekoXConfig.disableScreenshotDetection); |
| 122 | } |
| 123 | } else if (position == disableStatusUpdateRow) { |
| 124 | NekoXConfig.toggleDisableStatusUpdate(); |
| 125 | if (view instanceof TextCheckCell) { |
| 126 | ((TextCheckCell) view).setChecked(NekoXConfig.disableStatusUpdate); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | }); |
| 131 | |
| 132 | return fragmentView; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void onResume() { |
| 137 | super.onResume(); |
| 138 | if (listAdapter != null) { |
| 139 | listAdapter.notifyDataSetChanged(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private void updateRows() { |
| 144 | rowCount = 0; |
| 145 | |
| 146 | developerSettingsRow = rowCount++; |
| 147 | |
| 148 | enableRow = rowCount++; |
| 149 | |
| 150 | disableFlagSecureRow = rowCount++; |
| 151 | disableScreenshotDetectionRow = rowCount++; |
| 152 | disableStatusUpdateRow = rowCount++; |
| 153 | |
| 154 | fetchAndExportLangRow = rowCount++; |
| 155 | checkUpdateRepoForceRow = rowCount++; |
| 156 | |
| 157 | if (listAdapter != null) { |
| 158 | listAdapter.notifyDataSetChanged(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public void fetchAndExportLang() { |
| 163 | |
| 164 | AlertDialog pro = new AlertDialog(getParentActivity(), 3); |
| 165 | |
| 166 | pro.show(); |
| 167 | |
| 168 | UIUtil.runOnIoDispatcher(() -> { |
| 169 | |
| 170 | LocaleUtil.fetchAndExportLang(); |
| 171 | |
| 172 | File zipFile = new File(ApplicationLoader.applicationContext.getCacheDir(), "languages.zip"); |
| 173 | |
| 174 | FileUtil.delete(zipFile); |
| 175 | |
| 176 | File[] files = LocaleUtil.cacheDir.listFiles(); |
| 177 | |
| 178 | if (files != null) { |
| 179 | |
| 180 | try { |
| 181 | |
| 182 | ZipUtil.makeZip(zipFile, LocaleUtil.cacheDir); |
| 183 | |
| 184 | AndroidUtilities.runOnUIThread(() -> { |
| 185 | |
| 186 | pro.dismiss(); |
| 187 | |
| 188 | ShareUtil.shareFile(getParentActivity(), zipFile); |
| 189 | |
| 190 | }); |
| 191 | |
| 192 | } catch (Exception e) { |
| 193 | |
| 194 | AlertUtil.showToast(e); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | } else { |
| 199 | |
| 200 | AlertUtil.showToast("No files"); |
| 201 | |
| 202 | AndroidUtilities.runOnUIThread(pro::dismiss); |
| 203 | |
| 204 | } |
| 205 | |
| 206 | }); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public ArrayList<ThemeDescription> getThemeDescriptions() { |
| 212 | ArrayList<ThemeDescription> themeDescriptions = new ArrayList<>(); |
| 213 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CELLBACKGROUNDCOLOR, new Class[]{EmptyCell.class, TextSettingsCell.class, TextCheckCell.class, HeaderCell.class, TextDetailSettingsCell.class, NotificationsCheckCell.class}, null, null, null, Theme.key_windowBackgroundWhite)); |
| 214 | themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray)); |
| 215 | |
| 216 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue)); |
| 217 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue)); |
| 218 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_avatar_actionBarIconBlue)); |
| 219 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle)); |
| 220 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_avatar_actionBarSelectorBlue)); |
| 221 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUBACKGROUND, null, null, null, null, Theme.key_actionBarDefaultSubmenuBackground)); |
| 222 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUITEM, null, null, null, null, Theme.key_actionBarDefaultSubmenuItem)); |
| 223 | |
| 224 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector)); |
| 225 | |
| 226 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider)); |
| 227 | |
| 228 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{ShadowSectionCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow)); |
| 229 | |
| 230 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 231 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteValueText)); |
| 232 | |
| 233 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 234 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); |
| 235 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack)); |
| 236 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked)); |
| 237 | |
| 238 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 239 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); |
| 240 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack)); |
| 241 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked)); |
| 242 | |
| 243 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{HeaderCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlueHeader)); |
| 244 | |
| 245 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 246 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); |
| 247 | |
| 248 | return themeDescriptions; |
| 249 | } |
| 250 | |
| 251 | private class ListAdapter extends RecyclerListView.SelectionAdapter { |
| 252 | |
| 253 | private Context mContext; |
| 254 | |
| 255 | public ListAdapter(Context context) { |
| 256 | mContext = context; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public int getItemCount() { |
| 261 | return rowCount; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 266 | |
| 267 | // init |
| 268 | |
| 269 | switch (holder.getItemViewType()) { |
| 270 | |
| 271 | case 4: { |
| 272 | HeaderCell headerCell = (HeaderCell) holder.itemView; |
| 273 | if (position == developerSettingsRow) { |
| 274 | headerCell.setText(LocaleController.getString("DeveloperSettings", R.string.DeveloperSettings)); |
| 275 | } |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | case 3: { |
| 280 | TextCheckCell textCell = (TextCheckCell) holder.itemView; |
| 281 | textCell.setEnabled(true, null); |
| 282 | if (position == enableRow) { |
| 283 | textCell.setTextAndCheck("Enable", NekoXConfig.developerMode, true); |
| 284 | } else { |
| 285 | if (!NekoXConfig.developerMode) { |
| 286 | textCell.setEnabled(false); |
| 287 | } |
| 288 | if (position == disableFlagSecureRow) { |
| 289 | textCell.setTextAndCheck("Disable Flag Secure", NekoXConfig.disableFlagSecure, true); |
| 290 | } else if (position == disableScreenshotDetectionRow) { |
| 291 | textCell.setTextAndCheck("Disable Screenshot Detection", NekoXConfig.disableScreenshotDetection, false); |
| 292 | } else if (position == disableStatusUpdateRow) { |
| 293 | textCell.setTextAndCheck("Disable Status Update", NekoXConfig.disableStatusUpdate, false); |
| 294 | |
| 295 | } |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | case 2: { |
| 301 | TextSettingsCell textCell = (TextSettingsCell) holder.itemView; |
| 302 | if (!NekoXConfig.developerMode) { |
| 303 | textCell.setEnabled(false); |
| 304 | } |
| 305 | if (position == fetchAndExportLangRow) { |
| 306 | textCell.setText("Export Builtin Languages", true); |
| 307 | } else if (position == checkUpdateRepoForceRow) { |
| 308 | textCell.setText("Force Update (Repo)", false); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public boolean isEnabled(RecyclerView.ViewHolder holder) { |
| 318 | int type = holder.getItemViewType(); |
| 319 | return (type == 2 || type == 3) && holder.itemView.isEnabled(); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 324 | View view = null; |
| 325 | switch (viewType) { |
| 326 | case 1: |
| 327 | view = new ShadowSectionCell(mContext); |
| 328 | break; |
| 329 | case 2: |
| 330 | view = new TextSettingsCell(mContext); |
| 331 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 332 | break; |
| 333 | case 3: |
| 334 | view = new TextCheckCell(mContext); |
| 335 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 336 | break; |
| 337 | case 4: |
| 338 | view = new HeaderCell(mContext); |
| 339 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 340 | break; |
| 341 | case 5: |
| 342 | view = new NotificationsCheckCell(mContext); |
| 343 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 344 | break; |
| 345 | case 6: |
| 346 | view = new TextDetailSettingsCell(mContext); |
| 347 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 348 | break; |
| 349 | } |
| 350 | view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); |
| 351 | return new RecyclerListView.Holder(view); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public int getItemViewType(int position) { |
| 356 | if (position == developerSettingsRow) { |
| 357 | return 4; |
| 358 | } else if (position == fetchAndExportLangRow || position == checkUpdateRepoForceRow) { |
| 359 | return 2; |
| 360 | } |
| 361 | return 3; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |