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