Created
February 16, 2021 17:14
-
-
Save forthegithub/1abce7b867b5daf8e54b60169f330162 to your computer and use it in GitHub Desktop.
java reflection example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.telegram.ui.Custom; | |
import java.lang.reflect.Field; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.Callable; | |
import java.util.function.BooleanSupplier; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.util.TypedValue; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import org.telegram.messenger.ApplicationLoader; | |
import org.telegram.messenger.MessagesController; | |
import org.telegram.messenger.SharedConfig; | |
import org.telegram.ui.ActionBar.Theme; | |
import org.telegram.ui.ActionBar.AlertDialog; | |
//import org.telegram.ui.Cells.CheckBoxCell; | |
import org.telegram.ui.Cells.TextCheckCell; | |
import org.telegram.ui.Components.LayoutHelper; | |
public class AdditionalSettingsHelper { | |
static { | |
initAllFields(); | |
} | |
private static SharedPreferences preferences_ = null; | |
private static Field[] fields_ = null; | |
private static HashMap<String, Field> fieldHashMap_ = new HashMap<String, Field>(); | |
private static void initAllFields() { | |
fields_= AdditionalSettingsHelper.class.getFields(); | |
for(int i = 0, l = fields_.length; i < l; i++) { | |
fieldHashMap_.put(fields_[i].getName(), fields_[i]); | |
} | |
} | |
private static String configKey = "additionalSettings"; | |
private static boolean configLoaded; | |
private static final Object sync = new Object(); | |
// region DontSendTyping | |
public static boolean bDontSendTyping; | |
// region DontSendTextTyping | |
public static boolean bDontSendTextTyping; | |
public static boolean bDontSendTextTypingInRegularChats; | |
public static boolean bDontSendTextTypingInSecretChats; | |
public static boolean bDontSendTextTypingInSupergroupChats; | |
// endregion DontSendTextTyping | |
// endregion DontSendTyping | |
// region DontSendTyping | |
public static boolean bNoFlagSecureInSecretChats; | |
public static boolean bNoFlagSecureInSecretChatsPhotoViewer; | |
public static boolean bNoFlagSecureInDisposablePhotoViewer; | |
public static boolean bNoSecretMessageRead; | |
public static boolean bNoSecretMessageReadInEncryptedChats; | |
public static boolean bDontUpdateDestroyTime; | |
public static boolean bIgnoreDestroyTimer; | |
public static boolean bIgnoreCreateTaskForSecretChat; | |
public static boolean bIgnoreCreateTaskForMid; | |
public static boolean bIgnoreMarkMessageContentAsRead; | |
public static boolean bIgnoreMarkDialogAsRead; | |
// region | |
private static String g_szDontSendTextTyping = "Don't send typing"; | |
private static String g_szDontSendTextTyping_Description = "Don't send typing on text message writing"; | |
private static String g_szDontSendTextTypingInRegularChats = "In regular chats"; | |
private static String g_szDontSendTextTypingInRegularChats_Description = "Don't send 'typing...' in regular chats"; | |
private static String g_szDontSendTextTypingInSecretChats = "In secret chats"; | |
private static String g_szDontSendTextTypingInSecretChats_Description = "Don't send 'typing...' in secret chats"; | |
private static String g_szDontSendTextTypingInSupergroupChats = "In super group chats"; | |
private static String g_szDontSendTextTypingInSupergroupChats_Description = "Don't send 'typing...' in super group chats"; | |
// endregion | |
// endregion DontSendTyping | |
private static TextCheckCell addNewTextCellWithValue(Context context, String text, String value, boolean checked, boolean divider) { | |
final TextCheckCell pCell = new TextCheckCell(context); | |
pCell.setTextAndValueAndCheck(text, value, checked, true, divider); | |
pCell.setOnClickListener(v -> pCell.setChecked(fn.getAsBoolean())); | |
return pCell; | |
} | |
public static void showSettings(final Context context) { | |
LinearLayout ll = new LinearLayout(context); | |
ll.setOrientation(LinearLayout.VERTICAL); | |
// region | |
TextView warning = new TextView(context); | |
warning.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); | |
warning.setText("Please only change these settings if you know exactly what they do."); | |
warning.setTextColor(Theme.getColor(Theme.key_dialogTextBlack)); | |
ll.addView(warning, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 16, 8, 16, 8)); | |
// endregion | |
TextCheckCell pCell = null; | |
// region DontSendTextTyping2 | |
pCell = addNewTextCellWithValue(context, g_szDontSendTextTyping, g_szDontSendTextTyping_Description, bDontSendTyping,false, "bDontSendTyping"); | |
ll.addView(pCell); | |
pCell = addNewTextCellWithValue(context, g_szDontSendTextTypingInRegularChats, g_szDontSendTextTypingInRegularChats_Description, bDontSendTyping,false, AdditionalSettingsHelper::bDontSendTyping); | |
ll.addView(pCell); | |
pCell = addNewTextCellWithValue(context, g_szDontSendTextTypingInSecretChats, g_szDontSendTextTypingInSecretChats_Description, bDontSendTyping,false, AdditionalSettingsHelper::bDontSendTyping); | |
ll.addView(pCell); | |
pCell = addNewTextCellWithValue(context, g_szDontSendTextTypingInSupergroupChats, g_szDontSendTextTypingInSupergroupChats_Description, bDontSendTyping,false, AdditionalSettingsHelper::bDontSendTyping); | |
ll.addView(pCell); | |
// endregion DontSendTextTyping | |
pCell = addNewTextCellWithValue(context, g_szDontSendTextTypingInSupergroupChats, g_szDontSendTextTypingInSupergroupChats_Description, bDontSendTyping,false, AdditionalSettingsHelper::bDontSendTyping); | |
ll.addView(pCell); | |
new AlertDialog.Builder(context) | |
.setTitle("Title") | |
.setView(ll) | |
.show(); | |
} | |
public static void loadConfig() throws IllegalAccessException { | |
synchronized (sync) { | |
if (configLoaded || ApplicationLoader.applicationContext == null) { | |
return; | |
} | |
preferences_ = ApplicationLoader.applicationContext.getSharedPreferences(configKey, Context.MODE_PRIVATE); | |
Field tempField = null; | |
boolean bTempValue = false; | |
for(int i = 0, l = fieldHashMap_.size(); i < l; i++) { | |
tempField = fields_[i]; | |
tempField.setBoolean(null, preferences_.getBoolean(tempField.getName(), true)); | |
} | |
configLoaded = true; | |
} | |
} | |
public static boolean bToggleAndReturnValue(String name) throws IllegalAccessException { | |
Field tempField = fieldHashMap_.get(name); | |
if(tempField == null) return false; | |
SharedPreferences.Editor editor = preferences_.edit(); | |
boolean value = tempField.getBoolean(null); | |
value = !value; | |
tempField.setBoolean(null, value); | |
editor.putBoolean(name, value); | |
editor.commit(); | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment