| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.annotation.SuppressLint; |
| 4 | import android.content.Context; |
| 5 | import android.content.SharedPreferences; |
| 6 | import android.view.Gravity; |
| 7 | import android.view.View; |
| 8 | import android.view.ViewGroup; |
| 9 | import android.widget.FrameLayout; |
| 10 | |
| 11 | import androidx.recyclerview.widget.LinearLayoutManager; |
| 12 | import androidx.recyclerview.widget.RecyclerView; |
| 13 | |
| 14 | import org.telegram.messenger.AndroidUtilities; |
| 15 | import org.telegram.messenger.ApplicationLoader; |
| 16 | import org.telegram.messenger.BuildVars; |
| 17 | import org.telegram.messenger.FileLog; |
| 18 | import org.telegram.messenger.LocaleController; |
| 19 | import org.telegram.messenger.MessagesController; |
| 20 | import org.telegram.messenger.R; |
| 21 | import org.telegram.messenger.SharedConfig; |
| 22 | import org.telegram.messenger.UserConfig; |
| 23 | import org.telegram.tgnet.ConnectionsManager; |
| 24 | import org.telegram.ui.ActionBar.ActionBar; |
| 25 | import org.telegram.ui.ActionBar.AlertDialog; |
| 26 | import org.telegram.ui.ActionBar.BaseFragment; |
| 27 | import org.telegram.ui.ActionBar.Theme; |
| 28 | import org.telegram.ui.ActionBar.ThemeDescription; |
| 29 | import org.telegram.ui.Cells.EmptyCell; |
| 30 | import org.telegram.ui.Cells.HeaderCell; |
| 31 | import org.telegram.ui.Cells.NotificationsCheckCell; |
| 32 | import org.telegram.ui.Cells.ShadowSectionCell; |
| 33 | import org.telegram.ui.Cells.TextCheckCell; |
| 34 | import org.telegram.ui.Cells.TextDetailSettingsCell; |
| 35 | import org.telegram.ui.Cells.TextSettingsCell; |
| 36 | import org.telegram.ui.Components.LayoutHelper; |
| 37 | import org.telegram.ui.Components.RecyclerListView; |
| 38 | |
| 39 | public class NekoSettingsActivity extends BaseFragment { |
| 40 | |
| 41 | private RecyclerListView listView; |
| 42 | private ListAdapter listAdapter; |
| 43 | |
| 44 | private int rowCount; |
| 45 | |
| 46 | private int connectionRow; |
| 47 | private int ipv6Row; |
| 48 | private int connection2Row; |
| 49 | |
| 50 | private int emojiRow; |
| 51 | private int useSystemEmojiRow; |
| 52 | private int singleBigEmojiRow; |
| 53 | private int emoji2Row; |
| 54 | |
| 55 | private int settingsRow; |
| 56 | private int hidePhoneRow; |
| 57 | private int inappCameraRow; |
| 58 | private int ignoreBlockedRow; |
| 59 | private int nameOrderRow; |
| 60 | private int forceTabletRow; |
| 61 | private int settings2Row; |
| 62 | |
| 63 | @Override |
| 64 | public boolean onFragmentCreate() { |
| 65 | super.onFragmentCreate(); |
| 66 | |
| 67 | updateRows(false); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | @SuppressLint("NewApi") |
| 73 | @Override |
| 74 | public View createView(Context context) { |
| 75 | actionBar.setBackButtonImage(R.drawable.ic_ab_back); |
| 76 | actionBar.setTitle(LocaleController.getString("NekoSettings", R.string.NekoSettings)); |
| 77 | |
| 78 | if (AndroidUtilities.isTablet()) { |
| 79 | actionBar.setOccupyStatusBar(false); |
| 80 | } |
| 81 | actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() { |
| 82 | @Override |
| 83 | public void onItemClick(int id) { |
| 84 | if (id == -1) { |
| 85 | finishFragment(); |
| 86 | } |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | listAdapter = new ListAdapter(context); |
| 91 | |
| 92 | fragmentView = new FrameLayout(context); |
| 93 | fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray)); |
| 94 | FrameLayout frameLayout = (FrameLayout) fragmentView; |
| 95 | |
| 96 | listView = new RecyclerListView(context); |
| 97 | listView.setVerticalScrollBarEnabled(false); |
| 98 | listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) { |
| 99 | @Override |
| 100 | public boolean supportsPredictiveItemAnimations() { |
| 101 | return false; |
| 102 | } |
| 103 | }); |
| 104 | listView.setGlowColor(Theme.getColor(Theme.key_avatar_backgroundActionBarBlue)); |
| 105 | listView.setAdapter(listAdapter); |
| 106 | listView.setItemAnimator(null); |
| 107 | listView.setLayoutAnimation(null); |
| 108 | frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT)); |
| 109 | listView.setOnItemClickListener((view, position, x, y) -> { |
| 110 | if (position == ipv6Row) { |
| 111 | NekoConfig.toggleIPv6(); |
| 112 | if (view instanceof TextCheckCell) { |
| 113 | ((TextCheckCell) view).setChecked(NekoConfig.useIPv6); |
| 114 | } |
| 115 | for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { |
| 116 | if (UserConfig.getInstance(a).isClientActivated()) { |
| 117 | ConnectionsManager.native_setUseIpv6(a, NekoConfig.useIPv6); |
| 118 | } |
| 119 | } |
| 120 | } else if (position == hidePhoneRow) { |
| 121 | NekoConfig.toggleHidePhone(); |
| 122 | if (view instanceof TextCheckCell) { |
| 123 | ((TextCheckCell) view).setChecked(NekoConfig.hidePhone); |
| 124 | } |
| 125 | } else if (position == inappCameraRow) { |
| 126 | SharedConfig.toggleInappCamera(); |
| 127 | if (view instanceof TextCheckCell) { |
| 128 | ((TextCheckCell) view).setChecked(SharedConfig.inappCamera); |
| 129 | } |
| 130 | } else if (position == forceTabletRow) { |
| 131 | NekoConfig.toggleForceTablet(); |
| 132 | if (view instanceof TextCheckCell) { |
| 133 | ((TextCheckCell) view).setChecked(NekoConfig.forceTablet); |
| 134 | } |
| 135 | } else if (position == ignoreBlockedRow) { |
| 136 | NekoConfig.toggleIgnoreBlocked(); |
| 137 | if (view instanceof TextCheckCell) { |
| 138 | ((TextCheckCell) view).setChecked(NekoConfig.ignoreBlocked); |
| 139 | } |
| 140 | } else if (position == useSystemEmojiRow) { |
| 141 | SharedConfig.useSystemEmoji = !SharedConfig.useSystemEmoji; |
| 142 | SharedPreferences.Editor editor = MessagesController.getGlobalMainSettings().edit(); |
| 143 | editor.putBoolean("useSystemEmoji", SharedConfig.useSystemEmoji); |
| 144 | editor.commit(); |
| 145 | if (view instanceof TextCheckCell) { |
| 146 | ((TextCheckCell) view).setChecked(SharedConfig.useSystemEmoji); |
| 147 | } |
| 148 | } else if (position == singleBigEmojiRow) { |
| 149 | SharedConfig.allowBigEmoji = !SharedConfig.allowBigEmoji; |
| 150 | SharedPreferences.Editor editor = MessagesController.getGlobalMainSettings().edit(); |
| 151 | editor.putBoolean("allowBigEmoji", SharedConfig.allowBigEmoji); |
| 152 | editor.commit(); |
| 153 | if (view instanceof TextCheckCell) { |
| 154 | ((TextCheckCell) view).setChecked(SharedConfig.allowBigEmoji); |
| 155 | } |
| 156 | } else if (position == nameOrderRow) { |
| 157 | AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity()); |
| 158 | builder.setTitle(LocaleController.getString("NameOrder", R.string.NameOrder)); |
| 159 | CharSequence[] items = new CharSequence[]{ |
| 160 | LocaleController.getString("FirstLast", R.string.FirstLast), |
| 161 | LocaleController.getString("LastFirst", R.string.LastFirst), |
| 162 | }; |
| 163 | builder.setItems(items, (dialog, which) -> { |
| 164 | NekoConfig.setNameOrder(which + 1); |
| 165 | listAdapter.notifyItemChanged(nameOrderRow); |
| 166 | }); |
| 167 | showDialog(builder.create()); |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | return fragmentView; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void onResume() { |
| 176 | super.onResume(); |
| 177 | if (listAdapter != null) { |
| 178 | listAdapter.notifyDataSetChanged(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private void updateRows(boolean notify) { |
| 183 | rowCount = 0; |
| 184 | connectionRow = rowCount++; |
| 185 | ipv6Row = rowCount++; |
| 186 | connection2Row = rowCount++; |
| 187 | emojiRow = rowCount++; |
| 188 | useSystemEmojiRow = rowCount++; |
| 189 | singleBigEmojiRow = rowCount++; |
| 190 | emoji2Row = rowCount++; |
| 191 | settingsRow = rowCount++; |
| 192 | hidePhoneRow = rowCount++; |
| 193 | inappCameraRow = rowCount++; |
| 194 | ignoreBlockedRow = rowCount++; |
| 195 | forceTabletRow = rowCount++; |
| 196 | nameOrderRow = rowCount++; |
| 197 | settings2Row = rowCount++; |
| 198 | if (notify && listAdapter != null) { |
| 199 | listAdapter.notifyDataSetChanged(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public ThemeDescription[] getThemeDescriptions() { |
| 205 | return new ThemeDescription[]{ |
| 206 | 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), |
| 207 | new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray), |
| 208 | |
| 209 | new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue), |
| 210 | new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue), |
| 211 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_avatar_actionBarIconBlue), |
| 212 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle), |
| 213 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_avatar_actionBarSelectorBlue), |
| 214 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUBACKGROUND, null, null, null, null, Theme.key_actionBarDefaultSubmenuBackground), |
| 215 | new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUITEM, null, null, null, null, Theme.key_actionBarDefaultSubmenuItem), |
| 216 | |
| 217 | new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector), |
| 218 | |
| 219 | new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider), |
| 220 | |
| 221 | new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{ShadowSectionCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow), |
| 222 | |
| 223 | new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText), |
| 224 | new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteValueText), |
| 225 | |
| 226 | new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText), |
| 227 | new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2), |
| 228 | new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack), |
| 229 | new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked), |
| 230 | |
| 231 | new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText), |
| 232 | new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2), |
| 233 | new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack), |
| 234 | new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked), |
| 235 | |
| 236 | new ThemeDescription(listView, 0, new Class[]{HeaderCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlueHeader), |
| 237 | |
| 238 | new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText), |
| 239 | new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2), |
| 240 | }; |
| 241 | } |
| 242 | |
| 243 | private class ListAdapter extends RecyclerListView.SelectionAdapter { |
| 244 | |
| 245 | private Context mContext; |
| 246 | |
| 247 | public ListAdapter(Context context) { |
| 248 | mContext = context; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public int getItemCount() { |
| 253 | return rowCount; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 258 | switch (holder.getItemViewType()) { |
| 259 | case 1: { |
| 260 | break; |
| 261 | } |
| 262 | case 2: { |
| 263 | TextSettingsCell textCell = (TextSettingsCell) holder.itemView; |
| 264 | if (position == nameOrderRow) { |
| 265 | String value; |
| 266 | switch (NekoConfig.nameOrder) { |
| 267 | case 2: |
| 268 | value = LocaleController.getString("LastFirst", R.string.LastFirst); |
| 269 | break; |
| 270 | case 1: |
| 271 | default: |
| 272 | value = LocaleController.getString("FirstLast", R.string.FirstLast); |
| 273 | break; |
| 274 | } |
| 275 | textCell.setTextAndValue(LocaleController.getString("NameOrder", R.string.NameOrder), value, false); |
| 276 | } |
| 277 | break; |
| 278 | } |
| 279 | case 3: { |
| 280 | TextCheckCell textCell = (TextCheckCell) holder.itemView; |
| 281 | if (position == ipv6Row) { |
| 282 | textCell.setTextAndCheck(LocaleController.getString("IPv6", R.string.IPv6), NekoConfig.useIPv6, false); |
| 283 | } else if (position == hidePhoneRow) { |
| 284 | textCell.setTextAndCheck(LocaleController.getString("HidePhone", R.string.HidePhone), NekoConfig.hidePhone, true); |
| 285 | } else if (position == inappCameraRow) { |
| 286 | textCell.setTextAndCheck(LocaleController.getString("DebugMenuEnableCamera", R.string.DebugMenuEnableCamera), SharedConfig.inappCamera, true); |
| 287 | } else if (position == useSystemEmojiRow) { |
| 288 | textCell.setTextAndCheck(LocaleController.getString("EmojiUseDefault", R.string.EmojiUseDefault), SharedConfig.useSystemEmoji, true); |
| 289 | } else if (position == singleBigEmojiRow) { |
| 290 | textCell.setTextAndCheck(LocaleController.getString("EmojiBigSize", R.string.EmojiBigSize), SharedConfig.allowBigEmoji, false); |
| 291 | } else if (position == ignoreBlockedRow) { |
| 292 | textCell.setTextAndCheck(LocaleController.getString("IgnoreBlocked", R.string.IgnoreBlocked), NekoConfig.ignoreBlocked, true); |
| 293 | } else if (position == forceTabletRow) { |
| 294 | textCell.setTextAndCheck(LocaleController.getString("ForceTabletMode", R.string.ForceTabletMode), NekoConfig.forceTablet, true); |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | case 4: { |
| 299 | HeaderCell headerCell = (HeaderCell) holder.itemView; |
| 300 | if (position == settingsRow) { |
| 301 | headerCell.setText(LocaleController.getString("Settings", R.string.Settings)); |
| 302 | } else if (position == emojiRow) { |
| 303 | headerCell.setText(LocaleController.getString("Emoji", R.string.Emoji)); |
| 304 | } else if (position == connectionRow) { |
| 305 | headerCell.setText(LocaleController.getString("Connection", R.string.Connection)); |
| 306 | } |
| 307 | break; |
| 308 | } |
| 309 | case 5: { |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public boolean isEnabled(RecyclerView.ViewHolder holder) { |
| 317 | int position = holder.getAdapterPosition(); |
| 318 | return position == hidePhoneRow || position == inappCameraRow || position == ignoreBlockedRow || |
| 319 | position == useSystemEmojiRow || position == singleBigEmojiRow || position == ipv6Row || |
| 320 | position == nameOrderRow || position == forceTabletRow; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 325 | View view = null; |
| 326 | switch (viewType) { |
| 327 | case 1: |
| 328 | view = new ShadowSectionCell(mContext); |
| 329 | break; |
| 330 | case 2: |
| 331 | view = new TextSettingsCell(mContext); |
| 332 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 333 | break; |
| 334 | case 3: |
| 335 | view = new TextCheckCell(mContext); |
| 336 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 337 | break; |
| 338 | case 4: |
| 339 | view = new HeaderCell(mContext); |
| 340 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 341 | break; |
| 342 | case 5: |
| 343 | view = new NotificationsCheckCell(mContext); |
| 344 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 345 | break; |
| 346 | case 6: |
| 347 | view = new TextDetailSettingsCell(mContext); |
| 348 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 349 | break; |
| 350 | } |
| 351 | view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); |
| 352 | return new RecyclerListView.Holder(view); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public int getItemViewType(int position) { |
| 357 | if (position == settings2Row || position == emoji2Row || position == connection2Row) { |
| 358 | return 1; |
| 359 | } else if (position == nameOrderRow) { |
| 360 | return 2; |
| 361 | } else if (position == ipv6Row || position == hidePhoneRow || position == inappCameraRow || |
| 362 | position == ignoreBlockedRow || position == useSystemEmojiRow || position == singleBigEmojiRow || |
| 363 | position == forceTabletRow) { |
| 364 | return 3; |
| 365 | } else if (position == settingsRow || position == connectionRow || position == emojiRow) { |
| 366 | return 4; |
| 367 | } |
| 368 | return 6; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |