package tw.nekomimi.nekogram; import android.content.Context; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import org.telegram.messenger.AndroidUtilities; import org.telegram.messenger.ApplicationLoader; import org.telegram.messenger.LocaleController; import org.telegram.messenger.R; import org.telegram.ui.ActionBar.ActionBar; import org.telegram.ui.ActionBar.AlertDialog; import org.telegram.ui.ActionBar.BaseFragment; import org.telegram.ui.ActionBar.Theme; import org.telegram.ui.ActionBar.ThemeDescription; import org.telegram.ui.Cells.EmptyCell; import org.telegram.ui.Cells.HeaderCell; import org.telegram.ui.Cells.NotificationsCheckCell; import org.telegram.ui.Cells.ShadowSectionCell; import org.telegram.ui.Cells.TextCheckCell; import org.telegram.ui.Cells.TextDetailSettingsCell; import org.telegram.ui.Cells.TextSettingsCell; import org.telegram.ui.Components.LayoutHelper; import org.telegram.ui.Components.RecyclerListView; import java.io.File; import java.util.ArrayList; import tw.nekomimi.nekogram.parts.UpdateChecksKt; import tw.nekomimi.nekogram.utils.AlertUtil; import tw.nekomimi.nekogram.utils.FileUtil; import tw.nekomimi.nekogram.utils.LocaleUtil; import tw.nekomimi.nekogram.utils.ShareUtil; import tw.nekomimi.nekogram.utils.UIUtil; import tw.nekomimi.nekogram.utils.ZipUtil; public class NekoXSettingActivity extends BaseFragment { private RecyclerListView listView; private ListAdapter listAdapter; private int rowCount; private int developerSettingsRow; private int enableRow; private int disableFlagSecureRow; private int disableScreenshotDetectionRow; private int disableStatusUpdateRow; private int fetchAndExportLangRow; @Override public boolean onFragmentCreate() { super.onFragmentCreate(); updateRows(); return true; } @Override public View createView(Context context) { actionBar.setBackButtonImage(R.drawable.ic_ab_back); actionBar.setTitle(LocaleController.getString("NekoSettings", R.string.NekoSettings)); if (AndroidUtilities.isTablet()) { actionBar.setOccupyStatusBar(false); } actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() { @Override public void onItemClick(int id) { if (id == -1) { finishFragment(); } } }); listAdapter = new ListAdapter(context); fragmentView = new FrameLayout(context); fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray)); FrameLayout frameLayout = (FrameLayout) fragmentView; listView = new RecyclerListView(context); listView.setVerticalScrollBarEnabled(false); listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) { @Override public boolean supportsPredictiveItemAnimations() { return false; } }); listView.setGlowColor(Theme.getColor(Theme.key_avatar_backgroundActionBarBlue)); listView.setAdapter(listAdapter); listView.setItemAnimator(null); listView.setLayoutAnimation(null); frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT)); listView.setOnItemClickListener((view, position, x, y) -> { if (position == fetchAndExportLangRow) { fetchAndExportLang(); } if (position == enableRow) { NekoXConfig.toggleDeveloperMode(); updateRows(); } else if (position == disableFlagSecureRow) { NekoXConfig.toggleDisableFlagSecure(); if (view instanceof TextCheckCell) { ((TextCheckCell) view).setChecked(NekoXConfig.disableFlagSecure); } } else if (position == disableScreenshotDetectionRow) { NekoXConfig.toggleDisableScreenshotDetection(); if (view instanceof TextCheckCell) { ((TextCheckCell) view).setChecked(NekoXConfig.disableScreenshotDetection); } } else if (position == disableStatusUpdateRow) { NekoXConfig.toggleDisableStatusUpdate(); if (view instanceof TextCheckCell) { ((TextCheckCell) view).setChecked(NekoXConfig.disableStatusUpdate); } } }); return fragmentView; } @Override public void onResume() { super.onResume(); if (listAdapter != null) { listAdapter.notifyDataSetChanged(); } } private void updateRows() { rowCount = 0; developerSettingsRow = rowCount++; enableRow = rowCount++; disableFlagSecureRow = rowCount++; disableScreenshotDetectionRow = rowCount++; disableStatusUpdateRow = rowCount++; fetchAndExportLangRow = rowCount++; if (listAdapter != null) { listAdapter.notifyDataSetChanged(); } } public void fetchAndExportLang() { AlertDialog pro = new AlertDialog(getParentActivity(), 3); pro.show(); UIUtil.runOnIoDispatcher(() -> { LocaleUtil.fetchAndExportLang(); File zipFile = new File(ApplicationLoader.applicationContext.getCacheDir(), "languages.zip"); FileUtil.delete(zipFile); File[] files = LocaleUtil.cacheDir.listFiles(); if (files != null) { try { ZipUtil.makeZip(zipFile, LocaleUtil.cacheDir); AndroidUtilities.runOnUIThread(() -> { pro.dismiss(); ShareUtil.shareFile(getParentActivity(), zipFile); }); } catch (Exception e) { AlertUtil.showToast(e); } } else { AlertUtil.showToast("No files"); AndroidUtilities.runOnUIThread(pro::dismiss); } }); } @Override public ArrayList getThemeDescriptions() { ArrayList themeDescriptions = new ArrayList<>(); 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)); themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray)); themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue)); themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue)); themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_avatar_actionBarIconBlue)); themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle)); themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_avatar_actionBarSelectorBlue)); themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUBACKGROUND, null, null, null, null, Theme.key_actionBarDefaultSubmenuBackground)); themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUITEM, null, null, null, null, Theme.key_actionBarDefaultSubmenuItem)); themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider)); themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{ShadowSectionCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteValueText)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{HeaderCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlueHeader)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); return themeDescriptions; } private class ListAdapter extends RecyclerListView.SelectionAdapter { private Context mContext; public ListAdapter(Context context) { mContext = context; } @Override public int getItemCount() { return rowCount; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { // init switch (holder.getItemViewType()) { case 4: { HeaderCell headerCell = (HeaderCell) holder.itemView; if (position == developerSettingsRow) { headerCell.setText(LocaleController.getString("DeveloperSettings", R.string.DeveloperSettings)); } break; } case 3: { TextCheckCell textCell = (TextCheckCell) holder.itemView; textCell.setEnabled(true, null); if (position == enableRow) { textCell.setTextAndCheck("Enable", NekoXConfig.developerMode, true); } else { if (!NekoXConfig.developerMode) { textCell.setEnabled(false); } if (position == disableFlagSecureRow) { textCell.setTextAndCheck("Disable Flag Secure", NekoXConfig.disableFlagSecure, true); } else if (position == disableScreenshotDetectionRow) { textCell.setTextAndCheck("Disable Screenshot Detection", NekoXConfig.disableScreenshotDetection, false); } else if (position == disableStatusUpdateRow) { textCell.setTextAndCheck("Disable Status Update", NekoXConfig.disableStatusUpdate, false); } } break; } case 2: { TextSettingsCell textCell = (TextSettingsCell) holder.itemView; if (!NekoXConfig.developerMode) { textCell.setEnabled(false); } if (position == fetchAndExportLangRow) { textCell.setText("Export Builtin Languages", true); } } } } @Override public boolean isEnabled(RecyclerView.ViewHolder holder) { int type = holder.getItemViewType(); return (type == 2 || type == 3) && holder.itemView.isEnabled(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = null; switch (viewType) { case 1: view = new ShadowSectionCell(mContext); break; case 2: view = new TextSettingsCell(mContext); view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); break; case 3: view = new TextCheckCell(mContext); view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); break; case 4: view = new HeaderCell(mContext); view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); break; case 5: view = new NotificationsCheckCell(mContext); view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); break; case 6: view = new TextDetailSettingsCell(mContext); view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); break; } view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); return new RecyclerListView.Holder(view); } @Override public int getItemViewType(int position) { if (position == developerSettingsRow) { return 4; } else if (position == fetchAndExportLangRow) { return 2; } return 3; } } }